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