1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
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>
27 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ByteArray,TDF_Attribute)
29 //=======================================================================
32 //=======================================================================
33 const Standard_GUID& TDataStd_ByteArray::GetID()
35 static Standard_GUID TDataStd_ByteArrayID ("FD9B918F-2980-4c66-85E0-D71965475290");
36 return TDataStd_ByteArrayID;
39 //=======================================================================
40 //function : TDataStd_ByteArray
41 //purpose : Empty Constructor
42 //=======================================================================
43 TDataStd_ByteArray::TDataStd_ByteArray() : myIsDelta(Standard_False),
47 //=======================================================================
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)
57 Handle(TDataStd_ByteArray) A;
58 if (!label.FindAttribute (theGuid, A))
60 A = new TDataStd_ByteArray;
61 A->Init (lower, upper);
64 label.AddAttribute(A);
66 else if (lower != A->Lower() || upper != A->Upper())
68 A->Init(lower, upper);
72 //=======================================================================
75 //=======================================================================
76 void TDataStd_ByteArray::Init(const Standard_Integer lower,
77 const Standard_Integer upper)
79 Standard_RangeError_Raise_if(upper < lower,"TDataStd_ByteArray::Init");
81 myValue = new TColStd_HArray1OfByte(lower, upper, 0x00);
84 //=======================================================================
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)
93 return SetAttr(label, lower, upper, isDelta, GetID());
96 //=======================================================================
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)
106 return SetAttr(label, lower, upper, isDelta, theGuid);
109 //=======================================================================
110 //function : SetValue
112 //=======================================================================
113 void TDataStd_ByteArray::SetValue (const Standard_Integer index,
114 const Standard_Byte value)
116 if (myValue.IsNull())
118 if (value == myValue->Value(index))
122 myValue->SetValue(index, value);
125 //=======================================================================
128 //=======================================================================
129 Standard_Byte TDataStd_ByteArray::Value (const Standard_Integer index) const
131 return myValue->Value(index);
134 //=======================================================================
137 //=======================================================================
138 Standard_Integer TDataStd_ByteArray::Lower (void) const
140 if (myValue.IsNull())
142 return myValue->Lower();
145 //=======================================================================
148 //=======================================================================
149 Standard_Integer TDataStd_ByteArray::Upper (void) const
151 if (myValue.IsNull()) return -1;
152 return myValue->Upper();
155 //=======================================================================
158 //=======================================================================
159 Standard_Integer TDataStd_ByteArray::Length (void) const
161 if (myValue.IsNull())
163 return myValue->Length();
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)
176 Standard_Integer aLower = newArray->Lower();
177 Standard_Integer anUpper = newArray->Upper();
178 Standard_Boolean aDimEqual = Standard_False;
181 if ( Lower() == aLower && Upper() == anUpper ) {
182 aDimEqual = Standard_True;
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;
197 // Handles of myValue of current and backuped attributes will be different!
198 if(myValue.IsNull() || !aDimEqual)
199 myValue = new TColStd_HArray1OfByte(aLower, anUpper);
201 for(i = aLower; i <= anUpper; i++)
202 myValue->SetValue(i, newArray->Value(i));
205 //=======================================================================
208 //=======================================================================
209 const Standard_GUID& TDataStd_ByteArray::ID () const
214 //=======================================================================
217 //=======================================================================
219 void TDataStd_ByteArray::SetID( const Standard_GUID& theGuid)
221 if(myID == theGuid) return;
226 //=======================================================================
228 //purpose : sets default ID
229 //=======================================================================
231 void TDataStd_ByteArray::SetID()
237 //=======================================================================
238 //function : NewEmpty
240 //=======================================================================
241 Handle(TDF_Attribute) TDataStd_ByteArray::NewEmpty () const
243 return new TDataStd_ByteArray();
246 //=======================================================================
249 //=======================================================================
250 void TDataStd_ByteArray::Restore(const Handle(TDF_Attribute)& With)
252 Handle(TDataStd_ByteArray) anArray = Handle(TDataStd_ByteArray)::DownCast(With);
253 if (!anArray->myValue.IsNull())
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();
267 //=======================================================================
270 //=======================================================================
271 void TDataStd_ByteArray::Paste (const Handle(TDF_Attribute)& Into,
272 const Handle(TDF_RelocationTable)& ) const
274 if (!myValue.IsNull())
276 Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(Into);
279 anAtt->ChangeArray( myValue, Standard_False);
280 anAtt->SetDelta(myIsDelta);
286 //=======================================================================
289 //=======================================================================
290 Standard_OStream& TDataStd_ByteArray::Dump (Standard_OStream& anOS) const
292 anOS << "\nByteArray: ";
293 Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
294 myID.ToCString(sguid);
295 anOS << sguid << std::endl;
299 //=======================================================================
300 //function : DeltaOnModification
302 //=======================================================================
304 Handle(TDF_DeltaOnModification) TDataStd_ByteArray::DeltaOnModification
305 (const Handle(TDF_Attribute)& OldAttribute) const
308 return new TDataStd_DeltaOnModificationOfByteArray(Handle(TDataStd_ByteArray)::DownCast (OldAttribute));
309 else return new TDF_DefaultDeltaOnModification(OldAttribute);