0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / TDataStd / TDataStd_RealList.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_RealList.hxx>
17
18 #include <Standard_Dump.hxx>
19 #include <Standard_GUID.hxx>
20 #include <Standard_Type.hxx>
21 #include <TColStd_ListIteratorOfListOfReal.hxx>
22 #include <TDF_Attribute.hxx>
23 #include <TDF_Label.hxx>
24 #include <TDF_RelocationTable.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(TDataStd_RealList,TDF_Attribute)
27
28 //=======================================================================
29 //function : GetID
30 //purpose  : 
31 //=======================================================================
32 const Standard_GUID& TDataStd_RealList::GetID() 
33
34   static Standard_GUID TDataStd_RealListID ("349ACE18-7CD6-4525-9938-FBBF22AA54D3");
35   return TDataStd_RealListID; 
36 }
37
38 //=======================================================================
39 //function : SetAttr
40 //purpose  : Implements Set functionality
41 //=======================================================================
42 static Handle(TDataStd_RealList) SetAttr(const TDF_Label&       label,
43                                          const Standard_GUID&   theGuid) 
44 {
45   Handle(TDataStd_RealList) A;
46   if (!label.FindAttribute (theGuid, A)) 
47   {
48     A = new TDataStd_RealList;
49     A->SetID(theGuid);
50     label.AddAttribute(A);
51   }
52   return A;
53 }
54
55 //=======================================================================
56 //function : TDataStd_RealList
57 //purpose  : Empty Constructor
58 //=======================================================================
59 TDataStd_RealList::TDataStd_RealList() : myID(GetID())
60 {}
61
62 //=======================================================================
63 //function : Set
64 //purpose  : 
65 //=======================================================================
66 Handle(TDataStd_RealList) TDataStd_RealList::Set(const TDF_Label& label) 
67 {
68   return SetAttr(label, GetID());
69 }
70
71 //=======================================================================
72 //function : Set
73 //purpose  : Set user defined attribute with specific ID
74 //=======================================================================
75 Handle(TDataStd_RealList) TDataStd_RealList::Set(const TDF_Label& label,
76                                                  const Standard_GUID&   theGuid) 
77 {
78   return SetAttr(label, theGuid);
79 }
80
81 //=======================================================================
82 //function : IsEmpty
83 //purpose  : 
84 //=======================================================================
85 Standard_Boolean TDataStd_RealList::IsEmpty() const
86 {
87   return myList.IsEmpty();
88 }
89
90 //=======================================================================
91 //function : Extent
92 //purpose  : 
93 //=======================================================================
94 Standard_Integer TDataStd_RealList::Extent() const
95 {
96   return myList.Extent();
97 }
98
99 //=======================================================================
100 //function : Prepend
101 //purpose  : 
102 //=======================================================================
103 void TDataStd_RealList::Prepend(const Standard_Real value)
104 {
105   Backup();
106   myList.Prepend(value);
107 }
108
109 //=======================================================================
110 //function : Append
111 //purpose  : 
112 //=======================================================================
113 void TDataStd_RealList::Append(const Standard_Real value)
114 {
115   Backup();
116   myList.Append(value);
117 }
118
119 //=======================================================================
120 //function : InsertBefore
121 //purpose  : 
122 //=======================================================================
123 Standard_Boolean TDataStd_RealList::InsertBefore(const Standard_Real value,
124                                                  const Standard_Real before_value)
125 {
126   TColStd_ListIteratorOfListOfReal itr(myList);
127   for (; itr.More(); itr.Next())
128   {
129     if (itr.Value() == before_value)
130     {
131       Backup();
132       myList.InsertBefore(value, itr);
133       return Standard_True;
134     }
135   }
136   return Standard_False;
137 }
138
139 // Inserts the <value> before the <index> position.
140 // The indices start with 1 .. Extent().
141 Standard_Boolean TDataStd_RealList::InsertBeforeByIndex (const Standard_Integer index,
142                                                          const Standard_Real before_value)
143 {
144   Standard_Integer i(1);
145   Standard_Boolean found(Standard_False);
146   TColStd_ListIteratorOfListOfReal itr(myList);
147   for (; itr.More(); itr.Next(), ++i)
148   {
149     if (i == index)
150     {
151       Backup();
152       myList.InsertBefore(before_value, itr);
153       found = Standard_True;
154       break;
155     }
156   }
157   return found;
158 }
159
160 //=======================================================================
161 //function : InsertAfter
162 //purpose  : 
163 //=======================================================================
164 Standard_Boolean TDataStd_RealList::InsertAfter(const Standard_Real value,
165                                                 const Standard_Real after_value)
166 {
167   TColStd_ListIteratorOfListOfReal itr(myList);
168   for (; itr.More(); itr.Next())
169   {
170     if (itr.Value() == after_value)
171     {
172       Backup();
173       myList.InsertAfter(value, itr);
174       return Standard_True;
175     }
176   }
177   return Standard_False;
178 }
179
180 // Inserts the <value> after the <index> position.
181 // The indices start with 1 .. Extent().
182 Standard_Boolean TDataStd_RealList::InsertAfterByIndex (const Standard_Integer index,
183                                                         const Standard_Real after_value)
184 {
185   Standard_Integer i(1);
186   Standard_Boolean found(Standard_False);
187   TColStd_ListIteratorOfListOfReal itr(myList);
188   for (; itr.More(); itr.Next(), ++i)
189   {
190     if (i == index)
191     {
192       Backup();
193       myList.InsertAfter(after_value, itr);
194       found = Standard_True;
195       break;
196     }
197   }
198   return found;
199 }
200
201 //=======================================================================
202 //function : Remove
203 //purpose  : 
204 //=======================================================================
205 Standard_Boolean TDataStd_RealList::Remove(const Standard_Real value)
206 {
207   TColStd_ListIteratorOfListOfReal itr(myList);
208   for (; itr.More(); itr.Next())
209   {
210     if (itr.Value() == value)
211     {
212       Backup();
213       myList.Remove(itr);
214       return Standard_True;
215     }
216   }
217   return Standard_False;
218 }
219
220 //=======================================================================
221 //function : Remove
222 //purpose  : Removes a value at the <index> position.
223 //=======================================================================
224 Standard_Boolean TDataStd_RealList::RemoveByIndex (const Standard_Integer index)
225 {
226   Standard_Integer i(1);
227   Standard_Boolean found(Standard_False);
228   TColStd_ListIteratorOfListOfReal itr(myList);
229   for (; itr.More(); itr.Next(), ++i)
230   {
231     if (i == index)
232     {
233       Backup();
234       myList.Remove(itr);
235       found = Standard_True;
236       break;
237     }
238   }
239   return found;
240 }
241
242 //=======================================================================
243 //function : Clear
244 //purpose  : 
245 //=======================================================================
246 void TDataStd_RealList::Clear()
247 {
248   Backup();
249   myList.Clear();
250 }
251
252 //=======================================================================
253 //function : First
254 //purpose  : 
255 //=======================================================================
256 Standard_Real TDataStd_RealList::First() const
257 {
258   return myList.First();
259 }
260
261 //=======================================================================
262 //function : Last
263 //purpose  : 
264 //=======================================================================
265 Standard_Real TDataStd_RealList::Last() const
266 {
267   return myList.Last();
268 }
269
270 //=======================================================================
271 //function : List
272 //purpose  : 
273 //=======================================================================
274 const TColStd_ListOfReal& TDataStd_RealList::List() const
275 {
276   return myList;
277 }
278
279 //=======================================================================
280 //function : ID
281 //purpose  : 
282 //=======================================================================
283 const Standard_GUID& TDataStd_RealList::ID () const 
284
285   return myID; 
286 }
287
288 //=======================================================================
289 //function : SetID
290 //purpose  :
291 //=======================================================================
292 void TDataStd_RealList::SetID( const Standard_GUID&  theGuid)
293 {  
294   if(myID == theGuid) return;
295   Backup();
296   myID = theGuid;
297 }
298
299 //=======================================================================
300 //function : SetID
301 //purpose  : sets default ID
302 //=======================================================================
303 void TDataStd_RealList::SetID()
304 {  
305   Backup();
306   myID = GetID();
307 }
308
309 //=======================================================================
310 //function : NewEmpty
311 //purpose  : 
312 //=======================================================================
313 Handle(TDF_Attribute) TDataStd_RealList::NewEmpty () const
314 {  
315   return new TDataStd_RealList(); 
316 }
317
318 //=======================================================================
319 //function : Restore
320 //purpose  : 
321 //=======================================================================
322 void TDataStd_RealList::Restore(const Handle(TDF_Attribute)& With) 
323 {
324   myList.Clear();
325   Handle(TDataStd_RealList) aList = Handle(TDataStd_RealList)::DownCast(With);
326   TColStd_ListIteratorOfListOfReal itr(aList->List());
327   for (; itr.More(); itr.Next())
328   {
329     myList.Append(itr.Value());
330   }
331   myID = aList->ID();
332 }
333
334 //=======================================================================
335 //function : Paste
336 //purpose  : 
337 //=======================================================================
338 void TDataStd_RealList::Paste (const Handle(TDF_Attribute)& Into,
339                                const Handle(TDF_RelocationTable)& ) const
340 {
341   Handle(TDataStd_RealList) aList = Handle(TDataStd_RealList)::DownCast(Into);
342   aList->Clear();
343   TColStd_ListIteratorOfListOfReal itr(myList);
344   for (; itr.More(); itr.Next())
345   {
346     aList->Append(itr.Value());
347   }
348   aList->SetID(myID);
349 }
350
351 //=======================================================================
352 //function : Dump
353 //purpose  : 
354 //=======================================================================
355 Standard_OStream& TDataStd_RealList::Dump (Standard_OStream& anOS) const
356 {  
357   anOS << "\nRealList: ";
358   Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
359   myID.ToCString(sguid);
360   anOS << sguid;
361   anOS << std::endl;
362   return anOS;
363 }
364
365 //=======================================================================
366 //function : DumpJson
367 //purpose  : 
368 //=======================================================================
369 void TDataStd_RealList::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
370 {
371   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
372
373   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
374
375   for (TColStd_ListOfReal::Iterator aListIt (myList); aListIt.More(); aListIt.Next())
376   {
377     const Standard_Real& aValue = aListIt.Value();
378     OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aValue)
379   }
380
381   OCCT_DUMP_FIELD_VALUE_GUID (theOStream, myID)
382 }