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