c30bd7cc30f022ee7854a23f82ddcc75766ac8f8
[occt.git] / src / TDataStd / TDataStd_BooleanArray.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <Standard_GUID.hxx>
18 #include <Standard_Type.hxx>
19 #include <TDataStd_BooleanArray.hxx>
20 #include <TDF_Attribute.hxx>
21 #include <TDF_Label.hxx>
22 #include <TDF_RelocationTable.hxx>
23
24 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_BooleanArray,TDF_Attribute)
25
26 static Standard_Integer DegreeOf2(const Standard_Integer degree)
27 {
28   switch (degree)
29   {
30     case 0:
31       return 1;
32     case 1:
33       return 2;
34     case 2:
35       return 4;
36     case 3:
37       return 8;
38     case 4:
39       return 16;
40     case 5:
41       return 32;
42     case 6:
43       return 64;
44     case 7:
45       return 128;
46     case 8:
47       return 256;
48   }
49   return -1;
50 }
51
52 //=======================================================================
53 //function : GetID
54 //purpose  : 
55 //=======================================================================
56 const Standard_GUID& TDataStd_BooleanArray::GetID() 
57
58   static Standard_GUID TDataStd_BooleanArrayID ("C7E98E54-B5EA-4aa9-AC99-9164EBD07F10");
59   return TDataStd_BooleanArrayID; 
60 }
61
62 //=======================================================================
63 //function : SetAttr
64 //purpose  : Implements Set functionality
65 //=======================================================================
66 static Handle(TDataStd_BooleanArray) SetAttr(const TDF_Label&       label,
67                                              const Standard_Integer lower,
68                                              const Standard_Integer upper,
69                                              const Standard_GUID&   theGuid) 
70 {
71   Handle(TDataStd_BooleanArray) A;
72   if (!label.FindAttribute (theGuid, A)) 
73   {
74     A = new TDataStd_BooleanArray;
75     A->SetID(theGuid);
76     A->Init (lower, upper); 
77     label.AddAttribute(A);
78   }
79   else if (lower != A->Lower() || upper != A->Upper())
80   {
81     A->Init(lower, upper);
82   }
83   return A;
84 }
85
86 //=======================================================================
87 //function : TDataStd_BooleanArray
88 //purpose  : Empty Constructor
89 //=======================================================================
90 TDataStd_BooleanArray::TDataStd_BooleanArray() : myID(GetID())
91 {}
92
93 //=======================================================================
94 //function : Init
95 //purpose  : 
96 //=======================================================================
97 void TDataStd_BooleanArray::Init(const Standard_Integer lower,
98                                  const Standard_Integer upper)
99 {
100   Standard_RangeError_Raise_if(upper < lower,"TDataStd_BooleanArray::Init");
101   Backup();
102   myLower = lower;
103   myUpper = upper;
104   myValues = new TColStd_HArray1OfByte(0, Length() >> 3, 0/*initialize to FALSE*/);
105 }
106
107 //=======================================================================
108 //function : Set
109 //purpose  : 
110 //=======================================================================
111 Handle(TDataStd_BooleanArray) TDataStd_BooleanArray::Set(const TDF_Label&       label,
112                                                          const Standard_Integer lower,
113                                                          const Standard_Integer upper) 
114 {
115   return SetAttr(label, lower, upper, GetID());
116 }
117
118
119 //=======================================================================
120 //function : Set
121 //purpose  : Set user defined attribute with specific ID
122 //=======================================================================
123 Handle(TDataStd_BooleanArray) TDataStd_BooleanArray::Set(const TDF_Label&       label,
124                                                          const Standard_GUID&   theGuid,
125                                                          const Standard_Integer lower,
126                                                          const Standard_Integer upper)
127 {
128   return SetAttr(label, lower, upper, theGuid);
129 }
130
131 //=======================================================================
132 //function : SetValue
133 //purpose  : 
134 //=======================================================================
135 void TDataStd_BooleanArray::SetValue (const Standard_Integer index,
136                                       const Standard_Boolean value) 
137 {
138
139   if (myValues.IsNull()) 
140     return; 
141   Standard_Integer byte_index = (index - myLower) >> 3;
142   Standard_Integer degree = index - (byte_index << 3) - myLower;
143   Standard_Integer byte_value = DegreeOf2(degree);
144
145   if ((value != 0) == ((myValues->Value(byte_index) & byte_value) > 0))
146     return;
147
148   Backup();
149
150   if (value)
151   {
152     myValues->ChangeValue(byte_index) |= byte_value;
153   }
154   else
155   {
156     myValues->ChangeValue(byte_index) ^= byte_value;
157   }
158 }
159
160 //=======================================================================
161 //function : Value
162 //purpose  : 
163 //=======================================================================
164 Standard_Boolean TDataStd_BooleanArray::Value (const Standard_Integer index) const 
165 {
166   if (myValues.IsNull())
167     return Standard_False;
168   if (index < myLower || index > myUpper)
169     return Standard_False;
170
171   Standard_Integer byte_index = (index - myLower) >> 3;
172   Standard_Integer degree = index - (byte_index << 3) - myLower;
173   Standard_Integer byte_value = DegreeOf2(degree);
174
175   return (myValues->Value(byte_index) & byte_value) > 0;
176 }
177
178 //=======================================================================
179 //function : Lower
180 //purpose  : 
181 //=======================================================================
182 Standard_Integer TDataStd_BooleanArray::Lower (void) const 
183
184   return myLower;
185 }
186
187 //=======================================================================
188 //function : Upper
189 //purpose  : 
190 //=======================================================================
191 Standard_Integer TDataStd_BooleanArray::Upper (void) const 
192
193   return myUpper;
194 }
195
196 //=======================================================================
197 //function : Length
198 //purpose  : 
199 //=======================================================================
200 Standard_Integer TDataStd_BooleanArray::Length (void) const 
201 {
202   return myUpper - myLower + 1;
203 }
204
205 //=======================================================================
206 //function : InternalArray
207 //purpose  : 
208 //=======================================================================
209 const Handle(TColStd_HArray1OfByte)& TDataStd_BooleanArray::InternalArray () const 
210 {
211   return myValues;
212 }
213
214 //=======================================================================
215 //function : SetInternalArray
216 //purpose  : 
217 //=======================================================================
218 void TDataStd_BooleanArray::SetInternalArray (const Handle(TColStd_HArray1OfByte)& values)
219 {
220   myValues = values;
221 }
222
223 //=======================================================================
224 //function : ID
225 //purpose  : 
226 //=======================================================================
227 const Standard_GUID& TDataStd_BooleanArray::ID () const 
228
229   return myID; 
230 }
231
232 //=======================================================================
233 //function : SetID
234 //purpose  :
235 //=======================================================================
236
237 void TDataStd_BooleanArray::SetID( const Standard_GUID&  theGuid)
238 {  
239   if(myID == theGuid) return;
240   Backup();
241   myID = theGuid;
242 }
243
244 //=======================================================================
245 //function : SetID
246 //purpose  : sets default ID
247 //=======================================================================
248 void TDataStd_BooleanArray::SetID()
249 {
250   Backup();
251   myID = GetID();
252 }
253 //=======================================================================
254 //function : NewEmpty
255 //purpose  : 
256 //=======================================================================
257 Handle(TDF_Attribute) TDataStd_BooleanArray::NewEmpty () const
258 {  
259   return new TDataStd_BooleanArray(); 
260 }
261
262 //=======================================================================
263 //function : Restore
264 //purpose  : 
265 //=======================================================================
266 void TDataStd_BooleanArray::Restore(const Handle(TDF_Attribute)& With) 
267 {
268   Handle(TDataStd_BooleanArray) anArray = Handle(TDataStd_BooleanArray)::DownCast(With);
269   if (!anArray->myValues.IsNull()) 
270   {
271     myLower = anArray->Lower();
272     myUpper = anArray->Upper();
273     Standard_Integer byte_upper = Length() >> 3;
274     myValues = new TColStd_HArray1OfByte(0, byte_upper, 0/*initialize to FALSE*/);
275     const TColStd_Array1OfByte& with_array = anArray->myValues->Array1();
276     for (Standard_Integer i = 0; i <= byte_upper; i++)
277     {
278       myValues->SetValue(i, with_array.Value(i));
279     }
280     myID = anArray->ID();
281   }
282   else
283   {
284     myValues.Nullify();
285   }
286 }
287
288 //=======================================================================
289 //function : Paste
290 //purpose  : 
291 //=======================================================================
292 void TDataStd_BooleanArray::Paste (const Handle(TDF_Attribute)& Into,
293                                    const Handle(TDF_RelocationTable)& ) const
294 {
295   if (!myValues.IsNull()) 
296   {
297     Handle(TDataStd_BooleanArray) anArray = Handle(TDataStd_BooleanArray)::DownCast(Into);
298     if (!anArray.IsNull())
299     {
300       anArray->Init(myLower, myUpper);
301       for (Standard_Integer i = myLower; i <= myUpper; i++)
302       {
303         anArray->SetValue(i, Value(i));
304       }
305     }
306     anArray->SetID(myID);
307   }
308 }
309
310 //=======================================================================
311 //function : Dump
312 //purpose  : 
313 //=======================================================================
314 Standard_OStream& TDataStd_BooleanArray::Dump (Standard_OStream& anOS) const
315 {  
316   anOS << "\nBooleanArray: ";
317   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
318   myID.ToCString(sguid);
319   anOS << sguid;
320   anOS <<std::endl;
321   return anOS;
322 }