0024947: Redesign OCCT legacy type system
[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 #include <TDataStd_ExtStringArray.ixx>
17 #include <TDataStd_DeltaOnModificationOfExtStringArray.hxx>
18 #include <TDF_DefaultDeltaOnModification.hxx>
19
20 //=======================================================================
21 //function : GetID
22 //purpose  : 
23 //=======================================================================
24
25 const Standard_GUID& TDataStd_ExtStringArray::GetID() 
26
27   static Standard_GUID anExtStringArrayID ("2a96b624-ec8b-11d0-bee7-080009dc3333");
28   return anExtStringArrayID; 
29 }
30
31
32 //=======================================================================
33 //function : TDataStd_ExtStringArray::TDataStd_ExtStringArray
34 //purpose  : 
35 //=======================================================================
36
37 TDataStd_ExtStringArray::TDataStd_ExtStringArray() 
38     : myIsDelta(Standard_False){}
39      
40 //=======================================================================
41 //function : Init
42 //purpose  : 
43 //=======================================================================
44
45 void TDataStd_ExtStringArray::Init(const Standard_Integer lower,
46                                 const Standard_Integer upper)
47 {
48   Backup();
49   myValue = new TColStd_HArray1OfExtendedString(lower, upper, "");
50 }
51
52 //=======================================================================
53 //function : Set
54 //purpose  : 
55 //=======================================================================
56
57 Handle(TDataStd_ExtStringArray) TDataStd_ExtStringArray::Set (
58                                           const TDF_Label& label,
59                                           const Standard_Integer lower,
60                                           const Standard_Integer upper,
61                                           const Standard_Boolean isDelta) 
62
63 {
64   Handle(TDataStd_ExtStringArray) A;
65   if (!label.FindAttribute (TDataStd_ExtStringArray::GetID(), A)) {
66     A = new TDataStd_ExtStringArray;
67     A->Init (lower, upper);
68     A->SetDelta(isDelta); 
69     label.AddAttribute(A);
70   }
71   else if (lower != A->Lower() || upper != A->Upper())
72   {
73     A->Init (lower, upper); 
74   }
75   return A;
76 }
77
78
79 //=======================================================================
80 //function : SetValue
81 //purpose  : 
82 //=======================================================================
83
84 void TDataStd_ExtStringArray::SetValue(const Standard_Integer index, const TCollection_ExtendedString& value) 
85 {
86   if(myValue.IsNull()) return;
87   if( myValue->Value(index) == value)
88     return; 
89
90   Backup();
91   myValue->SetValue(index, value);
92 }
93
94
95 //=======================================================================
96 //function : Value
97 //purpose  : 
98 //=======================================================================
99
100 const TCollection_ExtendedString& TDataStd_ExtStringArray::Value (const Standard_Integer index) const 
101 {
102     if (myValue.IsNull()) 
103     {
104         static TCollection_ExtendedString staticEmptyValue;
105         return staticEmptyValue;
106     }
107     return myValue->Value(index); 
108 }
109
110 //=======================================================================
111 //function : Lower
112 //purpose  : 
113 //=======================================================================
114 Standard_Integer TDataStd_ExtStringArray::Lower (void) const 
115
116   if(myValue.IsNull()) return 0;
117   return myValue->Lower(); 
118 }
119
120
121 //=======================================================================
122 //function : Upper
123 //purpose  : 
124 //=======================================================================
125 Standard_Integer TDataStd_ExtStringArray::Upper (void) const 
126
127   if(myValue.IsNull()) return 0;
128   return myValue->Upper(); 
129 }
130
131
132 //=======================================================================
133 //function : Length
134 //purpose  : 
135 //=======================================================================
136 Standard_Integer TDataStd_ExtStringArray::Length (void) const 
137 {
138   if(myValue.IsNull()) return 0;
139   return myValue->Length(); 
140 }
141
142
143
144 //=======================================================================
145 //function : ChangeArray
146 //purpose  : If value of <newArray> differs from <myValue>, Backup 
147 //         : performed and myValue refers to new instance of HArray1OfExtendedString
148 //         : that holds <newArray>
149 //=======================================================================
150
151 void TDataStd_ExtStringArray::ChangeArray(const Handle(TColStd_HArray1OfExtendedString)& newArray,
152                                           const Standard_Boolean isCheckItems) 
153 {
154   Standard_Integer aLower    = newArray->Lower();
155   Standard_Integer anUpper   = newArray->Upper();
156   Standard_Boolean aDimEqual = Standard_False;
157   Standard_Integer i;
158
159   if (Lower() == aLower && Upper() == anUpper ) {
160     aDimEqual = Standard_True;
161     Standard_Boolean isEqual = Standard_True;
162     if(isCheckItems) {
163       for(i = aLower; i <= anUpper; i++) {
164         if(myValue->Value(i) != newArray->Value(i)) {
165           isEqual = Standard_False;
166           break;
167         }
168       }
169       if(isEqual)
170         return;
171     }
172   }
173
174   Backup();
175
176 // Handles of myValue of current and backuped attributes will be different!!!
177   if(myValue.IsNull() || !aDimEqual) 
178     myValue = new TColStd_HArray1OfExtendedString(aLower, anUpper);
179
180   for(i = aLower; i <= anUpper; i++) 
181     myValue->SetValue(i, newArray->Value(i));
182 }
183
184 //=======================================================================
185 //function : ID
186 //purpose  : 
187 //=======================================================================
188
189 const Standard_GUID& TDataStd_ExtStringArray::ID () const { return GetID(); }
190
191
192 //=======================================================================
193 //function : NewEmpty
194 //purpose  : 
195 //=======================================================================
196
197 Handle(TDF_Attribute) TDataStd_ExtStringArray::NewEmpty () const
198 {  
199   return new TDataStd_ExtStringArray(); 
200 }
201
202 //=======================================================================
203 //function : Restore
204 //purpose  : 
205 //=======================================================================
206
207 void TDataStd_ExtStringArray::Restore(const Handle(TDF_Attribute)& With) 
208 {
209   Standard_Integer i, lower, upper;
210   Handle(TDataStd_ExtStringArray) anArray = Handle(TDataStd_ExtStringArray)::DownCast(With);
211   if(!anArray->myValue.IsNull()) {
212     lower = anArray->Lower();
213     upper = anArray->Upper(); 
214     myValue = new TColStd_HArray1OfExtendedString(lower, upper);
215     for(i = lower; i<=upper; i++)
216       myValue->SetValue(i, anArray->Value(i));
217     myIsDelta = anArray->myIsDelta;
218   }
219   else
220     myValue.Nullify();
221 }
222
223 //=======================================================================
224 //function : Paste
225 //purpose  : 
226 //=======================================================================
227
228 void TDataStd_ExtStringArray::Paste (const Handle(TDF_Attribute)& Into,
229                                    const Handle(TDF_RelocationTable)& ) const
230 {
231   if(!myValue.IsNull()) {
232     Handle(TDataStd_ExtStringArray) anAtt = Handle(TDataStd_ExtStringArray)::DownCast(Into);
233     if(!anAtt.IsNull()) {
234       anAtt->ChangeArray( myValue, Standard_False );
235       anAtt->SetDelta(myIsDelta);
236     }
237   }
238 }
239
240 //=======================================================================
241 //function : Dump
242 //purpose  : 
243 //=======================================================================
244
245 Standard_OStream& TDataStd_ExtStringArray::Dump (Standard_OStream& anOS) const
246 {  
247   anOS << "ExtStringArray :";
248   if(!myValue.IsNull()) {
249     Standard_Integer i, lower, upper;
250     lower = myValue->Lower();
251     upper = myValue->Upper();
252     for(i = lower; i<=upper; i++)
253       anOS << "\t" <<myValue->Value(i)<<endl;
254   }
255   anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
256   anOS << endl;
257   return anOS;
258 }
259
260 //=======================================================================
261 //function : DeltaOnModification
262 //purpose  : 
263 //=======================================================================
264
265 Handle(TDF_DeltaOnModification) TDataStd_ExtStringArray::DeltaOnModification
266 (const Handle(TDF_Attribute)& OldAttribute) const
267 {
268   if(myIsDelta)
269     return new TDataStd_DeltaOnModificationOfExtStringArray(*((Handle(TDataStd_ExtStringArray)*)&OldAttribute));
270   else return new TDF_DefaultDeltaOnModification(OldAttribute);
271 }
272