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