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