0031313: Foundation Classes - Dump improvement for classes
[occt.git] / src / TDataStd / TDataStd_ByteArray.cxx
CommitLineData
b311480e 1// Created on: 2007-05-29
2// Created by: Vlad Romashko
973c2be1 3// Copyright (c) 2007-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
bc73b006 16#include <TDataStd_ByteArray.hxx>
42cf5bc1 17
bc73b006 18#include <Standard_Dump.hxx>
42cf5bc1 19#include <Standard_GUID.hxx>
20#include <Standard_Type.hxx>
7fd59977 21#include <TDataStd_DeltaOnModificationOfByteArray.hxx>
42cf5bc1 22#include <TDF_Attribute.hxx>
7fd59977 23#include <TDF_DefaultDeltaOnModification.hxx>
42cf5bc1 24#include <TDF_DeltaOnModification.hxx>
25#include <TDF_Label.hxx>
26#include <TDF_RelocationTable.hxx>
7fd59977 27
92efcf78 28IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ByteArray,TDF_Attribute)
29
7fd59977 30//=======================================================================
31//function : GetID
32//purpose :
33//=======================================================================
34const Standard_GUID& TDataStd_ByteArray::GetID()
35{
36 static Standard_GUID TDataStd_ByteArrayID ("FD9B918F-2980-4c66-85E0-D71965475290");
37 return TDataStd_ByteArrayID;
38}
39
40//=======================================================================
41//function : TDataStd_ByteArray
42//purpose : Empty Constructor
43//=======================================================================
4a5eefb9 44TDataStd_ByteArray::TDataStd_ByteArray() : myIsDelta(Standard_False),
45 myID(GetID())
5a1271c8 46{}
7fd59977 47
48//=======================================================================
5a1271c8 49//function : SetAttr
50//purpose : Implements Set functionality
51//=======================================================================
52static 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)
57{
58 Handle(TDataStd_ByteArray) A;
59 if (!label.FindAttribute (theGuid, A))
60 {
61 A = new TDataStd_ByteArray;
62 A->Init (lower, upper);
63 A->SetDelta(isDelta);
64 A->SetID(theGuid);
65 label.AddAttribute(A);
66 }
67 else if (lower != A->Lower() || upper != A->Upper())
68 {
69 A->Init(lower, upper);
70 }
71 return A;
72}
73//=======================================================================
7fd59977 74//function : Init
75//purpose :
76//=======================================================================
77void TDataStd_ByteArray::Init(const Standard_Integer lower,
5a1271c8 78 const Standard_Integer upper)
7fd59977 79{
5a1271c8 80 Standard_RangeError_Raise_if(upper < lower,"TDataStd_ByteArray::Init");
7fd59977 81 Backup();
5a1271c8 82 myValue = new TColStd_HArray1OfByte(lower, upper, 0x00);
7fd59977 83}
84
85//=======================================================================
86//function : Set
87//purpose :
88//=======================================================================
89Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label& label,
5a1271c8 90 const Standard_Integer lower,
91 const Standard_Integer upper,
92 const Standard_Boolean isDelta)
7fd59977 93{
5a1271c8 94 return SetAttr(label, lower, upper, isDelta, GetID());
95}
96
97//=======================================================================
98//function : Set
99//purpose : Set user defined attribute with specific ID
100//=======================================================================
101Handle(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)
106{
107 return SetAttr(label, lower, upper, isDelta, theGuid);
7fd59977 108}
109
110//=======================================================================
111//function : SetValue
112//purpose :
113//=======================================================================
114void TDataStd_ByteArray::SetValue (const Standard_Integer index,
5a1271c8 115 const Standard_Byte value)
7fd59977 116{
fa13a85d 117 if (myValue.IsNull())
118 return;
7fd59977 119 if (value == myValue->Value(index))
120 return;
121 Backup();
122
123 myValue->SetValue(index, value);
124}
125
126//=======================================================================
127//function : Value
128//purpose :
129//=======================================================================
130Standard_Byte TDataStd_ByteArray::Value (const Standard_Integer index) const
131{
132 return myValue->Value(index);
133}
134
135//=======================================================================
136//function : Lower
137//purpose :
138//=======================================================================
139Standard_Integer TDataStd_ByteArray::Lower (void) const
140{
141 if (myValue.IsNull())
142 return 0;
143 return myValue->Lower();
144}
145
146//=======================================================================
147//function : Upper
148//purpose :
149//=======================================================================
150Standard_Integer TDataStd_ByteArray::Upper (void) const
151{
152 if (myValue.IsNull()) return -1;
153 return myValue->Upper();
154}
155
156//=======================================================================
157//function : Length
158//purpose :
159//=======================================================================
160Standard_Integer TDataStd_ByteArray::Length (void) const
161{
162 if (myValue.IsNull())
163 return 0;
164 return myValue->Length();
165}
166
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//=======================================================================
173void TDataStd_ByteArray::ChangeArray (const Handle(TColStd_HArray1OfByte)& newArray,
5a1271c8 174 const Standard_Boolean isCheckItems)
7fd59977 175{
176
177 Standard_Integer aLower = newArray->Lower();
178 Standard_Integer anUpper = newArray->Upper();
179 Standard_Boolean aDimEqual = Standard_False;
180 Standard_Integer i;
181
a855b7eb 182 if ( Lower() == aLower && Upper() == anUpper ) {
7fd59977 183 aDimEqual = Standard_True;
184 if(isCheckItems) {
185 Standard_Boolean isEqual = Standard_True;
186 for(i = aLower; i <= anUpper; i++) {
5a1271c8 187 if(myValue->Value(i) != newArray->Value(i)) {
188 isEqual = Standard_False;
189 break;
190 }
7fd59977 191 }
192 if(isEqual)
5a1271c8 193 return;
7fd59977 194 }
195 }
196
197 Backup();
198// Handles of myValue of current and backuped attributes will be different!
a855b7eb 199 if(myValue.IsNull() || !aDimEqual)
7fd59977 200 myValue = new TColStd_HArray1OfByte(aLower, anUpper);
201
202 for(i = aLower; i <= anUpper; i++)
203 myValue->SetValue(i, newArray->Value(i));
204}
205
206//=======================================================================
207//function : ID
208//purpose :
209//=======================================================================
210const Standard_GUID& TDataStd_ByteArray::ID () const
211{
5a1271c8 212 return myID;
213}
214
215//=======================================================================
216//function : SetID
217//purpose :
218//=======================================================================
219
220void TDataStd_ByteArray::SetID( const Standard_GUID& theGuid)
221{
222 if(myID == theGuid) return;
223 Backup();
224 myID = theGuid;
225}
226
227//=======================================================================
228//function : SetID
229//purpose : sets default ID
230//=======================================================================
231
232void TDataStd_ByteArray::SetID()
233{
234 Backup();
235 myID = GetID();
7fd59977 236}
237
238//=======================================================================
239//function : NewEmpty
240//purpose :
241//=======================================================================
242Handle(TDF_Attribute) TDataStd_ByteArray::NewEmpty () const
243{
244 return new TDataStd_ByteArray();
245}
246
247//=======================================================================
248//function : Restore
249//purpose :
250//=======================================================================
251void TDataStd_ByteArray::Restore(const Handle(TDF_Attribute)& With)
252{
253 Handle(TDataStd_ByteArray) anArray = Handle(TDataStd_ByteArray)::DownCast(With);
254 if (!anArray->myValue.IsNull())
255 {
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;
5a1271c8 262 myID = anArray->ID();
7fd59977 263 }
264 else
265 myValue.Nullify();
266}
267
268//=======================================================================
269//function : Paste
270//purpose :
271//=======================================================================
272void TDataStd_ByteArray::Paste (const Handle(TDF_Attribute)& Into,
5a1271c8 273 const Handle(TDF_RelocationTable)& ) const
7fd59977 274{
275 if (!myValue.IsNull())
276 {
277 Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(Into);
278 if (!anAtt.IsNull())
279 {
280 anAtt->ChangeArray( myValue, Standard_False);
281 anAtt->SetDelta(myIsDelta);
5a1271c8 282 anAtt->SetID(myID);
7fd59977 283 }
284 }
285}
286
287//=======================================================================
288//function : Dump
289//purpose :
290//=======================================================================
291Standard_OStream& TDataStd_ByteArray::Dump (Standard_OStream& anOS) const
292{
5a1271c8 293 anOS << "\nByteArray: ";
294 Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
295 myID.ToCString(sguid);
04232180 296 anOS << sguid << std::endl;
7fd59977 297 return anOS;
298}
299
300//=======================================================================
301//function : DeltaOnModification
302//purpose :
303//=======================================================================
304
305Handle(TDF_DeltaOnModification) TDataStd_ByteArray::DeltaOnModification
306(const Handle(TDF_Attribute)& OldAttribute) const
307{
308 if(myIsDelta)
c5f3a425 309 return new TDataStd_DeltaOnModificationOfByteArray(Handle(TDataStd_ByteArray)::DownCast (OldAttribute));
7fd59977 310 else return new TDF_DefaultDeltaOnModification(OldAttribute);
311}
312
bc73b006 313//=======================================================================
314//function : DumpJson
315//purpose :
316//=======================================================================
317void TDataStd_ByteArray::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
318{
319 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
320
321 OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
322
323 for (TColStd_Array1OfByte::Iterator aValueIt (myValue->Array1()); aValueIt.More(); aValueIt.Next())
324 {
325 const Standard_Byte& aValue = aValueIt.Value();
326 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aValue)
327 }
328 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsDelta)
329 OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myID)
330}