0022627: Change OCCT memory management defaults
[occt.git] / src / TDataStd / TDataStd_ByteArray.cxx
1 // File:        TDataStd_ByteArray.cxx
2 // Created:     May 29 11:40:00 2007
3 // Author:      Vlad Romashko
4 //              <vladislav.romashko@opencascade.com>
5 //Copyright:    Open CasCade SA 2007
6
7 #include <TDataStd_ByteArray.ixx>
8 #include <TDataStd_DeltaOnModificationOfByteArray.hxx>
9 #include <TDF_DefaultDeltaOnModification.hxx>
10
11 //=======================================================================
12 //function : GetID
13 //purpose  : 
14 //=======================================================================
15 const Standard_GUID& TDataStd_ByteArray::GetID() 
16
17   static Standard_GUID TDataStd_ByteArrayID ("FD9B918F-2980-4c66-85E0-D71965475290");
18   return TDataStd_ByteArrayID; 
19 }
20
21 //=======================================================================
22 //function : TDataStd_ByteArray
23 //purpose  : Empty Constructor
24 //=======================================================================
25 TDataStd_ByteArray::TDataStd_ByteArray() : myIsDelta(Standard_False){}
26
27
28 //=======================================================================
29 //function : Init
30 //purpose  : 
31 //=======================================================================
32 void TDataStd_ByteArray::Init(const Standard_Integer lower,
33                               const Standard_Integer upper)
34 {
35   Backup();
36
37   if (upper >= lower)
38     myValue = new TColStd_HArray1OfByte(lower, upper, 0x00);
39 }
40
41 //=======================================================================
42 //function : Set
43 //purpose  : 
44 //=======================================================================
45 Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label&       label,
46                                                    const Standard_Integer lower,
47                                                    const Standard_Integer upper,
48                                                  const Standard_Boolean isDelta) 
49 {
50   Handle(TDataStd_ByteArray) A;
51   if (!label.FindAttribute (TDataStd_ByteArray::GetID(), A)) 
52   {
53     A = new TDataStd_ByteArray;
54     A->Init (lower, upper);
55     A->SetDelta(isDelta); 
56     label.AddAttribute(A);
57   }
58   return A;
59 }
60
61 //=======================================================================
62 //function : SetValue
63 //purpose  : 
64 //=======================================================================
65 void TDataStd_ByteArray::SetValue (const Standard_Integer index,
66                                    const Standard_Byte value) 
67 {
68   if (value == myValue->Value(index))
69     return;
70   Backup();
71
72   myValue->SetValue(index, value);
73 }
74
75 //=======================================================================
76 //function : Value
77 //purpose  : 
78 //=======================================================================
79 Standard_Byte TDataStd_ByteArray::Value (const Standard_Integer index) const 
80 {
81   return myValue->Value(index);
82 }
83
84 //=======================================================================
85 //function : Lower
86 //purpose  : 
87 //=======================================================================
88 Standard_Integer TDataStd_ByteArray::Lower (void) const 
89
90   if (myValue.IsNull())
91     return 0;
92   return myValue->Lower();
93 }
94
95 //=======================================================================
96 //function : Upper
97 //purpose  : 
98 //=======================================================================
99 Standard_Integer TDataStd_ByteArray::Upper (void) const 
100
101   if (myValue.IsNull())  return -1;
102   return myValue->Upper();
103 }
104
105 //=======================================================================
106 //function : Length
107 //purpose  : 
108 //=======================================================================
109 Standard_Integer TDataStd_ByteArray::Length (void) const 
110 {
111   if (myValue.IsNull())
112     return 0;
113   return myValue->Length();
114 }
115
116 //=======================================================================
117 //function : ChangeArray
118 //purpose  : If value of <newArray> differs from <myValue>, Backup 
119 //         : performed and myValue refers to new instance of HArray1OfByte 
120 //         : that holds <newArray>
121 //=======================================================================
122 void TDataStd_ByteArray::ChangeArray (const Handle(TColStd_HArray1OfByte)& newArray,
123                                       const Standard_Boolean isCheckItems)
124 {
125
126   Standard_Integer aLower    = newArray->Lower();
127   Standard_Integer anUpper   = newArray->Upper();
128   Standard_Boolean aDimEqual = Standard_False;
129   Standard_Integer i;
130
131   if ( Lower() == aLower && Upper() == anUpper ) {
132     aDimEqual = Standard_True;
133     if(isCheckItems) {
134       Standard_Boolean isEqual = Standard_True;
135       for(i = aLower; i <= anUpper; i++) {
136         if(myValue->Value(i) != newArray->Value(i)) {
137           isEqual = Standard_False;
138           break;
139         }
140       }
141       if(isEqual)
142         return;
143     }
144   }
145   
146   Backup();
147 // Handles of myValue of current and backuped attributes will be different!
148   if(myValue.IsNull() || !aDimEqual) 
149     myValue = new TColStd_HArray1OfByte(aLower, anUpper);  
150
151   for(i = aLower; i <= anUpper; i++) 
152     myValue->SetValue(i, newArray->Value(i));
153 }
154
155 //=======================================================================
156 //function : ID
157 //purpose  : 
158 //=======================================================================
159 const Standard_GUID& TDataStd_ByteArray::ID () const 
160
161   return GetID(); 
162 }
163
164 //=======================================================================
165 //function : NewEmpty
166 //purpose  : 
167 //=======================================================================
168 Handle(TDF_Attribute) TDataStd_ByteArray::NewEmpty () const
169 {  
170   return new TDataStd_ByteArray(); 
171 }
172
173 //=======================================================================
174 //function : Restore
175 //purpose  : 
176 //=======================================================================
177 void TDataStd_ByteArray::Restore(const Handle(TDF_Attribute)& With) 
178 {
179   Handle(TDataStd_ByteArray) anArray = Handle(TDataStd_ByteArray)::DownCast(With);
180   if (!anArray->myValue.IsNull()) 
181   {
182     const TColStd_Array1OfByte& with_array = anArray->myValue->Array1();
183     Standard_Integer lower = with_array.Lower(), i = lower, upper = with_array.Upper();
184     myValue = new TColStd_HArray1OfByte(lower, upper);
185     for (; i <= upper; i++)
186       myValue->SetValue(i, with_array.Value(i));
187     myIsDelta = anArray->myIsDelta;
188   }
189   else
190     myValue.Nullify();
191 }
192
193 //=======================================================================
194 //function : Paste
195 //purpose  : 
196 //=======================================================================
197 void TDataStd_ByteArray::Paste (const Handle(TDF_Attribute)& Into,
198                                 const Handle(TDF_RelocationTable)& ) const
199 {
200   if (!myValue.IsNull()) 
201   {
202     Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(Into);
203     if (!anAtt.IsNull())
204     {
205       anAtt->ChangeArray( myValue, Standard_False);
206       anAtt->SetDelta(myIsDelta);
207     }
208   }
209 }
210
211 //=======================================================================
212 //function : Dump
213 //purpose  : 
214 //=======================================================================
215 Standard_OStream& TDataStd_ByteArray::Dump (Standard_OStream& anOS) const
216 {  
217   anOS << "ByteArray";
218   return anOS;
219 }
220
221 //=======================================================================
222 //function : DeltaOnModification
223 //purpose  : 
224 //=======================================================================
225
226 Handle(TDF_DeltaOnModification) TDataStd_ByteArray::DeltaOnModification
227 (const Handle(TDF_Attribute)& OldAttribute) const
228 {
229   if(myIsDelta)
230     return new TDataStd_DeltaOnModificationOfByteArray(*((Handle(TDataStd_ByteArray)*)&OldAttribute));
231   else return new TDF_DefaultDeltaOnModification(OldAttribute);
232 }
233