0023024: Update headers of OCCT files
[occt.git] / src / TDataStd / TDataStd_ByteArray.cxx
1 // Created on: 2007-05-29
2 // Created by: Vlad Romashko
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <TDataStd_ByteArray.ixx>
22 #include <TDataStd_DeltaOnModificationOfByteArray.hxx>
23 #include <TDF_DefaultDeltaOnModification.hxx>
24
25 //=======================================================================
26 //function : GetID
27 //purpose  : 
28 //=======================================================================
29 const Standard_GUID& TDataStd_ByteArray::GetID() 
30
31   static Standard_GUID TDataStd_ByteArrayID ("FD9B918F-2980-4c66-85E0-D71965475290");
32   return TDataStd_ByteArrayID; 
33 }
34
35 //=======================================================================
36 //function : TDataStd_ByteArray
37 //purpose  : Empty Constructor
38 //=======================================================================
39 TDataStd_ByteArray::TDataStd_ByteArray() : myIsDelta(Standard_False){}
40
41
42 //=======================================================================
43 //function : Init
44 //purpose  : 
45 //=======================================================================
46 void TDataStd_ByteArray::Init(const Standard_Integer lower,
47                               const Standard_Integer upper)
48 {
49   Backup();
50
51   if (upper >= lower)
52     myValue = new TColStd_HArray1OfByte(lower, upper, 0x00);
53 }
54
55 //=======================================================================
56 //function : Set
57 //purpose  : 
58 //=======================================================================
59 Handle(TDataStd_ByteArray) TDataStd_ByteArray::Set(const TDF_Label&       label,
60                                                    const Standard_Integer lower,
61                                                    const Standard_Integer upper,
62                                                  const Standard_Boolean isDelta) 
63 {
64   Handle(TDataStd_ByteArray) A;
65   if (!label.FindAttribute (TDataStd_ByteArray::GetID(), A)) 
66   {
67     A = new TDataStd_ByteArray;
68     A->Init (lower, upper);
69     A->SetDelta(isDelta); 
70     label.AddAttribute(A);
71   }
72   return A;
73 }
74
75 //=======================================================================
76 //function : SetValue
77 //purpose  : 
78 //=======================================================================
79 void TDataStd_ByteArray::SetValue (const Standard_Integer index,
80                                    const Standard_Byte value) 
81 {
82   if (value == myValue->Value(index))
83     return;
84   Backup();
85
86   myValue->SetValue(index, value);
87 }
88
89 //=======================================================================
90 //function : Value
91 //purpose  : 
92 //=======================================================================
93 Standard_Byte TDataStd_ByteArray::Value (const Standard_Integer index) const 
94 {
95   return myValue->Value(index);
96 }
97
98 //=======================================================================
99 //function : Lower
100 //purpose  : 
101 //=======================================================================
102 Standard_Integer TDataStd_ByteArray::Lower (void) const 
103
104   if (myValue.IsNull())
105     return 0;
106   return myValue->Lower();
107 }
108
109 //=======================================================================
110 //function : Upper
111 //purpose  : 
112 //=======================================================================
113 Standard_Integer TDataStd_ByteArray::Upper (void) const 
114
115   if (myValue.IsNull())  return -1;
116   return myValue->Upper();
117 }
118
119 //=======================================================================
120 //function : Length
121 //purpose  : 
122 //=======================================================================
123 Standard_Integer TDataStd_ByteArray::Length (void) const 
124 {
125   if (myValue.IsNull())
126     return 0;
127   return myValue->Length();
128 }
129
130 //=======================================================================
131 //function : ChangeArray
132 //purpose  : If value of <newArray> differs from <myValue>, Backup 
133 //         : performed and myValue refers to new instance of HArray1OfByte 
134 //         : that holds <newArray>
135 //=======================================================================
136 void TDataStd_ByteArray::ChangeArray (const Handle(TColStd_HArray1OfByte)& newArray,
137                                       const Standard_Boolean isCheckItems)
138 {
139
140   Standard_Integer aLower    = newArray->Lower();
141   Standard_Integer anUpper   = newArray->Upper();
142   Standard_Boolean aDimEqual = Standard_False;
143   Standard_Integer i;
144
145   if ( Lower() == aLower && Upper() == anUpper ) {
146     aDimEqual = Standard_True;
147     if(isCheckItems) {
148       Standard_Boolean isEqual = Standard_True;
149       for(i = aLower; i <= anUpper; i++) {
150         if(myValue->Value(i) != newArray->Value(i)) {
151           isEqual = Standard_False;
152           break;
153         }
154       }
155       if(isEqual)
156         return;
157     }
158   }
159   
160   Backup();
161 // Handles of myValue of current and backuped attributes will be different!
162   if(myValue.IsNull() || !aDimEqual) 
163     myValue = new TColStd_HArray1OfByte(aLower, anUpper);  
164
165   for(i = aLower; i <= anUpper; i++) 
166     myValue->SetValue(i, newArray->Value(i));
167 }
168
169 //=======================================================================
170 //function : ID
171 //purpose  : 
172 //=======================================================================
173 const Standard_GUID& TDataStd_ByteArray::ID () const 
174
175   return GetID(); 
176 }
177
178 //=======================================================================
179 //function : NewEmpty
180 //purpose  : 
181 //=======================================================================
182 Handle(TDF_Attribute) TDataStd_ByteArray::NewEmpty () const
183 {  
184   return new TDataStd_ByteArray(); 
185 }
186
187 //=======================================================================
188 //function : Restore
189 //purpose  : 
190 //=======================================================================
191 void TDataStd_ByteArray::Restore(const Handle(TDF_Attribute)& With) 
192 {
193   Handle(TDataStd_ByteArray) anArray = Handle(TDataStd_ByteArray)::DownCast(With);
194   if (!anArray->myValue.IsNull()) 
195   {
196     const TColStd_Array1OfByte& with_array = anArray->myValue->Array1();
197     Standard_Integer lower = with_array.Lower(), i = lower, upper = with_array.Upper();
198     myValue = new TColStd_HArray1OfByte(lower, upper);
199     for (; i <= upper; i++)
200       myValue->SetValue(i, with_array.Value(i));
201     myIsDelta = anArray->myIsDelta;
202   }
203   else
204     myValue.Nullify();
205 }
206
207 //=======================================================================
208 //function : Paste
209 //purpose  : 
210 //=======================================================================
211 void TDataStd_ByteArray::Paste (const Handle(TDF_Attribute)& Into,
212                                 const Handle(TDF_RelocationTable)& ) const
213 {
214   if (!myValue.IsNull()) 
215   {
216     Handle(TDataStd_ByteArray) anAtt = Handle(TDataStd_ByteArray)::DownCast(Into);
217     if (!anAtt.IsNull())
218     {
219       anAtt->ChangeArray( myValue, Standard_False);
220       anAtt->SetDelta(myIsDelta);
221     }
222   }
223 }
224
225 //=======================================================================
226 //function : Dump
227 //purpose  : 
228 //=======================================================================
229 Standard_OStream& TDataStd_ByteArray::Dump (Standard_OStream& anOS) const
230 {  
231   anOS << "ByteArray";
232   return anOS;
233 }
234
235 //=======================================================================
236 //function : DeltaOnModification
237 //purpose  : 
238 //=======================================================================
239
240 Handle(TDF_DeltaOnModification) TDataStd_ByteArray::DeltaOnModification
241 (const Handle(TDF_Attribute)& OldAttribute) const
242 {
243   if(myIsDelta)
244     return new TDataStd_DeltaOnModificationOfByteArray(*((Handle(TDataStd_ByteArray)*)&OldAttribute));
245   else return new TDF_DefaultDeltaOnModification(OldAttribute);
246 }
247