0029371: The problem of the attributes constructor call
[occt.git] / src / TDataStd / TDataStd_ExtStringArray.cxx
1 // Created on: 2002-01-16
2 // Created by: Michael PONIKAROV
3 // Copyright (c) 2002-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
17 #include <Standard_GUID.hxx>
18 #include <Standard_Type.hxx>
19 #include <TCollection_ExtendedString.hxx>
20 #include <TDataStd_DeltaOnModificationOfExtStringArray.hxx>
21 #include <TDataStd_ExtStringArray.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>
27
28 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_ExtStringArray,TDF_Attribute)
29
30 //=======================================================================
31 //function : GetID
32 //purpose  : 
33 //=======================================================================
34 const Standard_GUID& TDataStd_ExtStringArray::GetID() 
35
36   static Standard_GUID anExtStringArrayID ("2a96b624-ec8b-11d0-bee7-080009dc3333");
37   return anExtStringArrayID; 
38 }
39
40 //=======================================================================
41 //function : SetAttr
42 //purpose  : Implements Set functionality
43 //=======================================================================
44 Handle(TDataStd_ExtStringArray) SetAttr(const TDF_Label&       label,
45                                         const Standard_Integer lower,
46                                         const Standard_Integer upper,
47                                         const Standard_Boolean isDelta,
48                                         const Standard_GUID&   theGuid) 
49 {
50   Handle(TDataStd_ExtStringArray) A;
51   if (!label.FindAttribute (theGuid, A)) 
52   {
53     A = new TDataStd_ExtStringArray;
54     A->SetID(theGuid);
55     A->Init (lower, upper); 
56     A->SetDelta(isDelta); 
57     label.AddAttribute(A);
58   }
59   else if (lower != A->Lower() || upper != A->Upper())
60   {
61     A->Init(lower, upper);
62   }
63   return A;
64 }
65 //=======================================================================
66 //function : TDataStd_ExtStringArray::TDataStd_ExtStringArray
67 //purpose  : 
68 //=======================================================================
69
70 TDataStd_ExtStringArray::TDataStd_ExtStringArray() 
71   : myIsDelta(Standard_False), myID(GetID())
72 {}
73
74 //=======================================================================
75 //function : Init
76 //purpose  : 
77 //=======================================================================
78
79 void TDataStd_ExtStringArray::Init(const Standard_Integer lower,
80                                    const Standard_Integer upper)
81 {
82   Standard_RangeError_Raise_if(upper < lower,"TDataStd_ExtStringArray::Init");
83   Backup();
84   myValue = new TColStd_HArray1OfExtendedString(lower, upper, "");
85 }
86
87 //=======================================================================
88 //function : Set
89 //purpose  : 
90 //=======================================================================
91
92 Handle(TDataStd_ExtStringArray) TDataStd_ExtStringArray::Set (
93                                           const TDF_Label& label,
94                                           const Standard_Integer lower,
95                                           const Standard_Integer upper,
96                                           const Standard_Boolean isDelta) 
97
98 {
99   return SetAttr(label, lower, upper, isDelta, GetID());
100 }
101
102 //=======================================================================
103 //function : Set
104 //purpose  : Set user defined attribute with specific ID
105 //=======================================================================
106
107 Handle(TDataStd_ExtStringArray) TDataStd_ExtStringArray::Set (
108                                           const TDF_Label& label,
109                                           const Standard_GUID& theGuid,
110                                           const Standard_Integer lower,
111                                           const Standard_Integer upper,
112                                           const Standard_Boolean isDelta) 
113
114 {
115   return SetAttr(label, lower, upper, isDelta, theGuid);
116 }
117 //=======================================================================
118 //function : SetValue
119 //purpose  : 
120 //=======================================================================
121
122 void TDataStd_ExtStringArray::SetValue(const Standard_Integer index, const TCollection_ExtendedString& value) 
123 {
124   if(myValue.IsNull()) return;
125   if( myValue->Value(index) == value)
126     return; 
127
128   Backup();
129   myValue->SetValue(index, value);
130 }
131
132
133 //=======================================================================
134 //function : Value
135 //purpose  : 
136 //=======================================================================
137
138 const TCollection_ExtendedString& TDataStd_ExtStringArray::Value (const Standard_Integer index) const 
139 {
140   if (myValue.IsNull()) 
141   {
142     static TCollection_ExtendedString staticEmptyValue;
143     return staticEmptyValue;
144   }
145   return myValue->Value(index); 
146 }
147
148 //=======================================================================
149 //function : Lower
150 //purpose  : 
151 //=======================================================================
152 Standard_Integer TDataStd_ExtStringArray::Lower (void) const 
153
154   if(myValue.IsNull()) return 0;
155   return myValue->Lower(); 
156 }
157
158
159 //=======================================================================
160 //function : Upper
161 //purpose  : 
162 //=======================================================================
163 Standard_Integer TDataStd_ExtStringArray::Upper (void) const 
164
165   if(myValue.IsNull()) return 0;
166   return myValue->Upper(); 
167 }
168
169
170 //=======================================================================
171 //function : Length
172 //purpose  : 
173 //=======================================================================
174 Standard_Integer TDataStd_ExtStringArray::Length (void) const 
175 {
176   if(myValue.IsNull()) return 0;
177   return myValue->Length(); 
178 }
179
180
181
182 //=======================================================================
183 //function : ChangeArray
184 //purpose  : If value of <newArray> differs from <myValue>, Backup 
185 //         : performed and myValue refers to new instance of HArray1OfExtendedString
186 //         : that holds <newArray>
187 //=======================================================================
188
189 void TDataStd_ExtStringArray::ChangeArray(const Handle(TColStd_HArray1OfExtendedString)& newArray,
190                                           const Standard_Boolean isCheckItems) 
191 {
192   Standard_Integer aLower    = newArray->Lower();
193   Standard_Integer anUpper   = newArray->Upper();
194   Standard_Boolean aDimEqual = Standard_False;
195   Standard_Integer i;
196
197   if (Lower() == aLower && Upper() == anUpper ) {
198     aDimEqual = Standard_True;
199     Standard_Boolean isEqual = Standard_True;
200     if(isCheckItems) {
201       for(i = aLower; i <= anUpper; i++) {
202         if(myValue->Value(i) != newArray->Value(i)) {
203           isEqual = Standard_False;
204           break;
205         }
206       }
207       if(isEqual)
208         return;
209     }
210   }
211
212   Backup();
213
214 // Handles of myValue of current and backuped attributes will be different!!!
215   if(myValue.IsNull() || !aDimEqual) 
216     myValue = new TColStd_HArray1OfExtendedString(aLower, anUpper);
217
218   for(i = aLower; i <= anUpper; i++) 
219     myValue->SetValue(i, newArray->Value(i));
220 }
221
222 //=======================================================================
223 //function : ID
224 //purpose  : 
225 //=======================================================================
226
227 const Standard_GUID& TDataStd_ExtStringArray::ID () const { return myID; }
228
229 //=======================================================================
230 //function : SetID
231 //purpose  :
232 //=======================================================================
233
234 void TDataStd_ExtStringArray::SetID( const Standard_GUID&  theGuid)
235 {  
236   if(myID == theGuid) return;
237   Backup();
238   myID = theGuid;
239 }
240
241 //=======================================================================
242 //function : SetID
243 //purpose  : sets default ID
244 //=======================================================================
245
246 void TDataStd_ExtStringArray::SetID()
247 {  
248   Backup();
249   myID = GetID();
250 }
251
252 //=======================================================================
253 //function : NewEmpty
254 //purpose  : 
255 //=======================================================================
256
257 Handle(TDF_Attribute) TDataStd_ExtStringArray::NewEmpty () const
258 {  
259   return new TDataStd_ExtStringArray(); 
260 }
261
262 //=======================================================================
263 //function : Restore
264 //purpose  : 
265 //=======================================================================
266
267 void TDataStd_ExtStringArray::Restore(const Handle(TDF_Attribute)& With) 
268 {
269   Standard_Integer i, lower, upper;
270   Handle(TDataStd_ExtStringArray) anArray = Handle(TDataStd_ExtStringArray)::DownCast(With);
271   if(!anArray->myValue.IsNull()) {
272     lower = anArray->Lower();
273     upper = anArray->Upper(); 
274     myValue = new TColStd_HArray1OfExtendedString(lower, upper);
275     for(i = lower; i<=upper; i++)
276       myValue->SetValue(i, anArray->Value(i));
277     myIsDelta = anArray->myIsDelta;
278     myID = anArray->ID();
279   }
280   else
281     myValue.Nullify();
282 }
283
284 //=======================================================================
285 //function : Paste
286 //purpose  : 
287 //=======================================================================
288
289 void TDataStd_ExtStringArray::Paste (const Handle(TDF_Attribute)& Into,
290                                      const Handle(TDF_RelocationTable)& ) const
291 {
292   if(!myValue.IsNull()) {
293     Handle(TDataStd_ExtStringArray) anAtt = Handle(TDataStd_ExtStringArray)::DownCast(Into);
294     if(!anAtt.IsNull()) {
295       anAtt->ChangeArray( myValue, Standard_False );
296       anAtt->SetDelta(myIsDelta);
297       anAtt->SetID(myID);
298     }
299   }
300 }
301
302 //=======================================================================
303 //function : Dump
304 //purpose  : 
305 //=======================================================================
306
307 Standard_OStream& TDataStd_ExtStringArray::Dump (Standard_OStream& anOS) const
308 {  
309   anOS << "\nExtStringArray :";
310   if(!myValue.IsNull()) {
311     Standard_Integer i, lower, upper;
312     lower = myValue->Lower();
313     upper = myValue->Upper();
314     for(i = lower; i<=upper; i++)
315       anOS << "\t" <<myValue->Value(i)<<endl;
316   }
317   anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
318   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
319   myID.ToCString(sguid);
320   anOS << sguid;
321   anOS << endl;
322   return anOS;
323 }
324
325 //=======================================================================
326 //function : DeltaOnModification
327 //purpose  : 
328 //=======================================================================
329
330 Handle(TDF_DeltaOnModification) TDataStd_ExtStringArray::DeltaOnModification
331 (const Handle(TDF_Attribute)& OldAttribute) const
332 {
333   if(myIsDelta)
334     return new TDataStd_DeltaOnModificationOfExtStringArray(Handle(TDataStd_ExtStringArray)::DownCast (OldAttribute));
335   else return new TDF_DefaultDeltaOnModification(OldAttribute);
336 }
337