49ca2e1533d796f565edd7a60626e1f2c7103a95
[occt.git] / src / TDataStd / TDataStd_ByteArray.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_ByteArray.hxx>
20 #include <TDataStd_DeltaOnModificationOfByteArray.hxx>
21 #include <TDF_Attribute.hxx>
22 #include <TDF_DefaultDeltaOnModification.hxx>
23 #include <TDF_DeltaOnModification.hxx>
24 #include <TDF_Label.hxx>
25 #include <TDF_RelocationTable.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ByteArray,TDF_Attribute)
28
29 //=======================================================================
30 //function : GetID
31 //purpose  : 
32 //=======================================================================
33 const Standard_GUID& TDataStd_ByteArray::GetID() 
34
35   static Standard_GUID TDataStd_ByteArrayID ("FD9B918F-2980-4c66-85E0-D71965475290");
36   return TDataStd_ByteArrayID; 
37 }
38
39 //=======================================================================
40 //function : TDataStd_ByteArray
41 //purpose  : Empty Constructor
42 //=======================================================================
43 TDataStd_ByteArray::TDataStd_ByteArray() : myIsDelta(Standard_False),
44   myID(GetID())
45 {}
46
47 //=======================================================================
48 //function : SetAttr
49 //purpose  : Implements Set functionality
50 //=======================================================================
51 static Handle(TDataStd_ByteArray) SetAttr(const TDF_Label&       label,
52                                           const Standard_Integer lower,
53                                           const Standard_Integer upper,
54                                           const Standard_Boolean isDelta,
55                                           const Standard_GUID&   theGuid) 
56 {
57   Handle(TDataStd_ByteArray) A;
58   if (!label.FindAttribute (theGuid, A)) 
59   {
60     A = new TDataStd_ByteArray;
61     A->Init (lower, upper);
62     A->SetDelta(isDelta); 
63     A->SetID(theGuid);
64     label.AddAttribute(A);
65   }
66   else if (lower != A->Lower() || upper != A->Upper())
67   {
68     A->Init(lower, upper);
69   }
70   return A;
71 }
72 //=======================================================================
73 //function : Init
74 //purpose  : 
75 //=======================================================================
76 void TDataStd_ByteArray::Init(const Standard_Integer lower,
77                               const Standard_Integer upper)
78 {
79   Standard_RangeError_Raise_if(upper < lower,"TDataStd_ByteArray::Init");
80   Backup();
81   myValue = new TColStd_HArray1OfByte(lower, upper, 0x00);
82 }
83
84 //=======================================================================
85 //function : Set
86 //purpose  : 
87 //=======================================================================
88 Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label&       label,
89                                                    const Standard_Integer lower,
90                                                    const Standard_Integer upper,
91                                                    const Standard_Boolean isDelta) 
92 {
93   return SetAttr(label, lower, upper, isDelta, GetID());
94 }
95
96 //=======================================================================
97 //function : Set
98 //purpose  : Set user defined attribute with specific ID
99 //=======================================================================
100 Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label&       label,
101                                                    const Standard_GUID&   theGuid,
102                                                    const Standard_Integer lower,
103                                                    const Standard_Integer upper,
104                                                    const Standard_Boolean isDelta) 
105 {
106   return SetAttr(label, lower, upper, isDelta, theGuid);
107 }
108
109 //=======================================================================
110 //function : SetValue
111 //purpose  : 
112 //=======================================================================
113 void TDataStd_ByteArray::SetValue (const Standard_Integer index,
114                                    const Standard_Byte value) 
115 {
116   if (myValue.IsNull()) 
117     return;
118   if (value == myValue->Value(index))
119     return;
120   Backup();
121
122   myValue->SetValue(index, value);
123 }
124
125 //=======================================================================
126 //function : Value
127 //purpose  : 
128 //=======================================================================
129 Standard_Byte TDataStd_ByteArray::Value (const Standard_Integer index) const 
130 {
131   return myValue->Value(index);
132 }
133
134 //=======================================================================
135 //function : Lower
136 //purpose  : 
137 //=======================================================================
138 Standard_Integer TDataStd_ByteArray::Lower (void) const 
139
140   if (myValue.IsNull())
141     return 0;
142   return myValue->Lower();
143 }
144
145 //=======================================================================
146 //function : Upper
147 //purpose  : 
148 //=======================================================================
149 Standard_Integer TDataStd_ByteArray::Upper (void) const 
150
151   if (myValue.IsNull())  return -1;
152   return myValue->Upper();
153 }
154
155 //=======================================================================
156 //function : Length
157 //purpose  : 
158 //=======================================================================
159 Standard_Integer TDataStd_ByteArray::Length (void) const 
160 {
161   if (myValue.IsNull())
162     return 0;
163   return myValue->Length();
164 }
165
166 //=======================================================================
167 //function : ChangeArray
168 //purpose  : If value of <newArray> differs from <myValue>, Backup 
169 //         : performed and myValue refers to new instance of HArray1OfByte 
170 //         : that holds <newArray>
171 //=======================================================================
172 void TDataStd_ByteArray::ChangeArray (const Handle(TColStd_HArray1OfByte)& newArray,
173                                       const Standard_Boolean isCheckItems)
174 {
175
176   Standard_Integer aLower    = newArray->Lower();
177   Standard_Integer anUpper   = newArray->Upper();
178   Standard_Boolean aDimEqual = Standard_False;
179   Standard_Integer i;
180
181   if ( Lower() == aLower && Upper() == anUpper ) {
182     aDimEqual = Standard_True;
183     if(isCheckItems) {
184       Standard_Boolean isEqual = Standard_True;
185       for(i = aLower; i <= anUpper; i++) {
186         if(myValue->Value(i) != newArray->Value(i)) {
187           isEqual = Standard_False;
188           break;
189         }
190       }
191       if(isEqual)
192         return;
193     }
194   }
195   
196   Backup();
197 // Handles of myValue of current and backuped attributes will be different!
198   if(myValue.IsNull() || !aDimEqual) 
199     myValue = new TColStd_HArray1OfByte(aLower, anUpper);  
200
201   for(i = aLower; i <= anUpper; i++) 
202     myValue->SetValue(i, newArray->Value(i));
203 }
204
205 //=======================================================================
206 //function : ID
207 //purpose  : 
208 //=======================================================================
209 const Standard_GUID& TDataStd_ByteArray::ID () const 
210
211   return myID; 
212 }
213
214 //=======================================================================
215 //function : SetID
216 //purpose  :
217 //=======================================================================
218
219 void TDataStd_ByteArray::SetID( const Standard_GUID&  theGuid)
220 {  
221   if(myID == theGuid) return;
222   Backup();
223   myID = theGuid;
224 }
225
226 //=======================================================================
227 //function : SetID
228 //purpose  : sets default ID
229 //=======================================================================
230
231 void TDataStd_ByteArray::SetID()
232 {
233   Backup();
234   myID = GetID();
235 }
236
237 //=======================================================================
238 //function : NewEmpty
239 //purpose  : 
240 //=======================================================================
241 Handle(TDF_Attribute) TDataStd_ByteArray::NewEmpty () const
242 {  
243   return new TDataStd_ByteArray(); 
244 }
245
246 //=======================================================================
247 //function : Restore
248 //purpose  : 
249 //=======================================================================
250 void TDataStd_ByteArray::Restore(const Handle(TDF_Attribute)& With) 
251 {
252   Handle(TDataStd_ByteArray) anArray = Handle(TDataStd_ByteArray)::DownCast(With);
253   if (!anArray->myValue.IsNull()) 
254   {
255     const TColStd_Array1OfByte& with_array = anArray->myValue->Array1();
256     Standard_Integer lower = with_array.Lower(), i = lower, upper = with_array.Upper();
257     myValue = new TColStd_HArray1OfByte(lower, upper);
258     for (; i <= upper; i++)
259       myValue->SetValue(i, with_array.Value(i));
260     myIsDelta = anArray->myIsDelta;
261     myID = anArray->ID();
262   }
263   else
264     myValue.Nullify();
265 }
266
267 //=======================================================================
268 //function : Paste
269 //purpose  : 
270 //=======================================================================
271 void TDataStd_ByteArray::Paste (const Handle(TDF_Attribute)& Into,
272                                 const Handle(TDF_RelocationTable)& ) const
273 {
274   if (!myValue.IsNull()) 
275   {
276     Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(Into);
277     if (!anAtt.IsNull())
278     {
279       anAtt->ChangeArray( myValue, Standard_False);
280       anAtt->SetDelta(myIsDelta);
281       anAtt->SetID(myID);
282     }
283   }
284 }
285
286 //=======================================================================
287 //function : Dump
288 //purpose  : 
289 //=======================================================================
290 Standard_OStream& TDataStd_ByteArray::Dump (Standard_OStream& anOS) const
291 {  
292   anOS << "\nByteArray: ";
293   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
294   myID.ToCString(sguid);
295   anOS << sguid << std::endl;
296   return anOS;
297 }
298
299 //=======================================================================
300 //function : DeltaOnModification
301 //purpose  : 
302 //=======================================================================
303
304 Handle(TDF_DeltaOnModification) TDataStd_ByteArray::DeltaOnModification
305 (const Handle(TDF_Attribute)& OldAttribute) const
306 {
307   if(myIsDelta)
308     return new TDataStd_DeltaOnModificationOfByteArray(Handle(TDataStd_ByteArray)::DownCast (OldAttribute));
309   else return new TDF_DefaultDeltaOnModification(OldAttribute);
310 }
311