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