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.
16 #include <TDataStd_ByteArray.hxx>
18 #include <Standard_Dump.hxx>
19 #include <Standard_GUID.hxx>
20 #include <Standard_Type.hxx>
21 #include <TDataStd_DeltaOnModificationOfByteArray.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <TDF_DefaultDeltaOnModification.hxx>
24 #include <TDF_DeltaOnModification.hxx>
25 #include <TDF_Label.hxx>
26 #include <TDF_RelocationTable.hxx>
28 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ByteArray,TDF_Attribute)
30 //=======================================================================
33 //=======================================================================
34 const Standard_GUID& TDataStd_ByteArray::GetID()
36 static Standard_GUID TDataStd_ByteArrayID ("FD9B918F-2980-4c66-85E0-D71965475290");
37 return TDataStd_ByteArrayID;
40 //=======================================================================
41 //function : TDataStd_ByteArray
42 //purpose : Empty Constructor
43 //=======================================================================
44 TDataStd_ByteArray::TDataStd_ByteArray() : myIsDelta(Standard_False),
48 //=======================================================================
50 //purpose : Implements Set functionality
51 //=======================================================================
52 static Handle(TDataStd_ByteArray) SetAttr(const TDF_Label& label,
53 const Standard_Integer lower,
54 const Standard_Integer upper,
55 const Standard_Boolean isDelta,
56 const Standard_GUID& theGuid)
58 Handle(TDataStd_ByteArray) A;
59 if (!label.FindAttribute (theGuid, A))
61 A = new TDataStd_ByteArray;
62 A->Init (lower, upper);
65 label.AddAttribute(A);
67 else if (lower != A->Lower() || upper != A->Upper())
69 A->Init(lower, upper);
73 //=======================================================================
76 //=======================================================================
77 void TDataStd_ByteArray::Init(const Standard_Integer lower,
78 const Standard_Integer upper)
80 Standard_RangeError_Raise_if(upper < lower,"TDataStd_ByteArray::Init");
82 myValue = new TColStd_HArray1OfByte(lower, upper, 0x00);
85 //=======================================================================
88 //=======================================================================
89 Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label& label,
90 const Standard_Integer lower,
91 const Standard_Integer upper,
92 const Standard_Boolean isDelta)
94 return SetAttr(label, lower, upper, isDelta, GetID());
97 //=======================================================================
99 //purpose : Set user defined attribute with specific ID
100 //=======================================================================
101 Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label& label,
102 const Standard_GUID& theGuid,
103 const Standard_Integer lower,
104 const Standard_Integer upper,
105 const Standard_Boolean isDelta)
107 return SetAttr(label, lower, upper, isDelta, theGuid);
110 //=======================================================================
111 //function : SetValue
113 //=======================================================================
114 void TDataStd_ByteArray::SetValue (const Standard_Integer index,
115 const Standard_Byte value)
117 if (myValue.IsNull())
119 if (value == myValue->Value(index))
123 myValue->SetValue(index, value);
126 //=======================================================================
129 //=======================================================================
130 Standard_Byte TDataStd_ByteArray::Value (const Standard_Integer index) const
132 return myValue->Value(index);
135 //=======================================================================
138 //=======================================================================
139 Standard_Integer TDataStd_ByteArray::Lower (void) const
141 if (myValue.IsNull())
143 return myValue->Lower();
146 //=======================================================================
149 //=======================================================================
150 Standard_Integer TDataStd_ByteArray::Upper (void) const
152 if (myValue.IsNull()) return -1;
153 return myValue->Upper();
156 //=======================================================================
159 //=======================================================================
160 Standard_Integer TDataStd_ByteArray::Length (void) const
162 if (myValue.IsNull())
164 return myValue->Length();
167 //=======================================================================
168 //function : ChangeArray
169 //purpose : If value of <newArray> differs from <myValue>, Backup
170 // : performed and myValue refers to new instance of HArray1OfByte
171 // : that holds <newArray>
172 //=======================================================================
173 void TDataStd_ByteArray::ChangeArray (const Handle(TColStd_HArray1OfByte)& newArray,
174 const Standard_Boolean isCheckItems)
177 Standard_Integer aLower = newArray->Lower();
178 Standard_Integer anUpper = newArray->Upper();
179 Standard_Boolean aDimEqual = Standard_False;
182 if ( Lower() == aLower && Upper() == anUpper ) {
183 aDimEqual = Standard_True;
185 Standard_Boolean isEqual = Standard_True;
186 for(i = aLower; i <= anUpper; i++) {
187 if(myValue->Value(i) != newArray->Value(i)) {
188 isEqual = Standard_False;
198 // Handles of myValue of current and backuped attributes will be different!
199 if(myValue.IsNull() || !aDimEqual)
200 myValue = new TColStd_HArray1OfByte(aLower, anUpper);
202 for(i = aLower; i <= anUpper; i++)
203 myValue->SetValue(i, newArray->Value(i));
206 //=======================================================================
209 //=======================================================================
210 const Standard_GUID& TDataStd_ByteArray::ID () const
215 //=======================================================================
218 //=======================================================================
220 void TDataStd_ByteArray::SetID( const Standard_GUID& theGuid)
222 if(myID == theGuid) return;
227 //=======================================================================
229 //purpose : sets default ID
230 //=======================================================================
232 void TDataStd_ByteArray::SetID()
238 //=======================================================================
239 //function : NewEmpty
241 //=======================================================================
242 Handle(TDF_Attribute) TDataStd_ByteArray::NewEmpty () const
244 return new TDataStd_ByteArray();
247 //=======================================================================
250 //=======================================================================
251 void TDataStd_ByteArray::Restore(const Handle(TDF_Attribute)& With)
253 Handle(TDataStd_ByteArray) anArray = Handle(TDataStd_ByteArray)::DownCast(With);
254 if (!anArray->myValue.IsNull())
256 const TColStd_Array1OfByte& with_array = anArray->myValue->Array1();
257 Standard_Integer lower = with_array.Lower(), i = lower, upper = with_array.Upper();
258 myValue = new TColStd_HArray1OfByte(lower, upper);
259 for (; i <= upper; i++)
260 myValue->SetValue(i, with_array.Value(i));
261 myIsDelta = anArray->myIsDelta;
262 myID = anArray->ID();
268 //=======================================================================
271 //=======================================================================
272 void TDataStd_ByteArray::Paste (const Handle(TDF_Attribute)& Into,
273 const Handle(TDF_RelocationTable)& ) const
275 if (!myValue.IsNull())
277 Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(Into);
280 anAtt->ChangeArray( myValue, Standard_False);
281 anAtt->SetDelta(myIsDelta);
287 //=======================================================================
290 //=======================================================================
291 Standard_OStream& TDataStd_ByteArray::Dump (Standard_OStream& anOS) const
293 anOS << "\nByteArray: ";
294 Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
295 myID.ToCString(sguid);
296 anOS << sguid << std::endl;
300 //=======================================================================
301 //function : DeltaOnModification
303 //=======================================================================
305 Handle(TDF_DeltaOnModification) TDataStd_ByteArray::DeltaOnModification
306 (const Handle(TDF_Attribute)& OldAttribute) const
309 return new TDataStd_DeltaOnModificationOfByteArray(Handle(TDataStd_ByteArray)::DownCast (OldAttribute));
310 else return new TDF_DefaultDeltaOnModification(OldAttribute);
313 //=======================================================================
314 //function : DumpJson
316 //=======================================================================
317 void TDataStd_ByteArray::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
319 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
321 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
323 for (TColStd_Array1OfByte::Iterator aValueIt (myValue->Array1()); aValueIt.More(); aValueIt.Next())
325 const Standard_Byte& aValue = aValueIt.Value();
326 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aValue)
328 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsDelta)
329 OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myID)