0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / TDataStd / TDataStd_ReferenceArray.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 <TDataStd_ReferenceArray.hxx>
20 #include <TDF_Attribute.hxx>
21 #include <TDF_DataSet.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_RelocationTable.hxx>
24
25 //=======================================================================
26 //function : GetID
27 //purpose  : 
28 //=======================================================================
29 const Standard_GUID& TDataStd_ReferenceArray::GetID() 
30
31   static Standard_GUID TDataStd_ReferenceArrayID ("7EE745A6-BB50-446c-BB0B-C195B23AB5CA");
32   return TDataStd_ReferenceArrayID; 
33 }
34
35 //=======================================================================
36 //function : TDataStd_ReferenceArray
37 //purpose  : Empty Constructor
38 //=======================================================================
39 TDataStd_ReferenceArray::TDataStd_ReferenceArray() 
40 {
41
42 }
43
44 //=======================================================================
45 //function : Init
46 //purpose  : 
47 //=======================================================================
48 void TDataStd_ReferenceArray::Init(const Standard_Integer lower,
49                                    const Standard_Integer upper)
50 {
51   Standard_RangeError_Raise_if(upper < lower,"TDataStd_ReferenceArray::Init");
52   Backup();
53   myArray = new TDataStd_HLabelArray1(lower, upper);
54 }
55
56 //=======================================================================
57 //function : Set
58 //purpose  : 
59 //=======================================================================
60 Handle(TDataStd_ReferenceArray) TDataStd_ReferenceArray::Set(const TDF_Label&       label,
61                                                              const Standard_Integer lower,
62                                                              const Standard_Integer upper) 
63 {
64   Handle(TDataStd_ReferenceArray) A;
65   if (!label.FindAttribute (TDataStd_ReferenceArray::GetID(), A)) 
66   {
67     A = new TDataStd_ReferenceArray;
68     A->Init (lower, upper); 
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 //function : SetValue
80 //purpose  : 
81 //=======================================================================
82 void TDataStd_ReferenceArray::SetValue (const Standard_Integer index,
83                                         const TDF_Label&       value) 
84 {
85   if(myArray.IsNull()) return;
86   if (value == myArray->Value(index))
87     return;
88   Backup();
89
90   myArray->SetValue(index, value);
91 }
92
93 //=======================================================================
94 //function : Value
95 //purpose  : 
96 //=======================================================================
97 TDF_Label TDataStd_ReferenceArray::Value (const Standard_Integer index) const 
98 {
99   return myArray->Value(index);
100 }
101
102 //=======================================================================
103 //function : Lower
104 //purpose  : 
105 //=======================================================================
106 Standard_Integer TDataStd_ReferenceArray::Lower () const 
107
108   if (myArray.IsNull())
109     return 0;
110   return myArray->Lower();
111 }
112
113 //=======================================================================
114 //function : Upper
115 //purpose  : 
116 //=======================================================================
117 Standard_Integer TDataStd_ReferenceArray::Upper () const 
118
119   if (myArray.IsNull())
120     return -1;
121   return myArray->Upper();
122 }
123
124 //=======================================================================
125 //function : Length
126 //purpose  : 
127 //=======================================================================
128 Standard_Integer TDataStd_ReferenceArray::Length () const 
129 {
130   if (myArray.IsNull())
131     return 0;
132   return myArray->Length();
133 }
134
135 //=======================================================================
136 //function : InternalArray
137 //purpose  : 
138 //=======================================================================
139 const Handle(TDataStd_HLabelArray1)& TDataStd_ReferenceArray::InternalArray () const 
140 {
141   return myArray;
142 }
143
144 //=======================================================================
145 //function : SetInternalArray
146 //purpose  : 
147 //=======================================================================
148 void TDataStd_ReferenceArray::SetInternalArray (const Handle(TDataStd_HLabelArray1)& values,
149                                                 const Standard_Boolean)
150 {
151 //  myArray = values;
152   Standard_Integer aLower    = values->Lower();
153   Standard_Integer anUpper   = values->Upper();
154   Standard_Boolean aDimEqual = Standard_False;
155   Standard_Integer i;
156
157 #ifdef OCC2932
158   if (Lower() == aLower && Upper() == anUpper ) {
159     aDimEqual = Standard_True;
160     Standard_Boolean isEqual = Standard_True;
161     if(isCheckItems) {
162       for(i = aLower; i <= anUpper; i++) {
163         if(myArray->Value(i) != values->Value(i)) {
164           isEqual = Standard_False;
165           break;
166         }
167       }
168       if(isEqual)
169         return;
170     }
171   }
172 #endif
173
174   Backup();
175
176   if(myArray.IsNull() || !aDimEqual) 
177     myArray = new TDataStd_HLabelArray1(aLower, anUpper);
178
179   for(i = aLower; i <= anUpper; i++) 
180     myArray->SetValue(i, values->Value(i));
181 }
182
183 //=======================================================================
184 //function : ID
185 //purpose  : 
186 //=======================================================================
187 const Standard_GUID& TDataStd_ReferenceArray::ID () const 
188
189   return GetID();
190 }
191
192 //=======================================================================
193 //function : NewEmpty
194 //purpose  : 
195 //=======================================================================
196 Handle(TDF_Attribute) TDataStd_ReferenceArray::NewEmpty () const
197 {  
198   return new TDataStd_ReferenceArray(); 
199 }
200
201 //=======================================================================
202 //function : Restore
203 //purpose  : 
204 //=======================================================================
205 void TDataStd_ReferenceArray::Restore(const Handle(TDF_Attribute)& With) 
206 {
207   Handle(TDataStd_ReferenceArray) anArray = Handle(TDataStd_ReferenceArray)::DownCast(With);
208   if (!anArray->myArray.IsNull()) 
209   {
210     const TDataStd_LabelArray1& arr = anArray->myArray->Array1();
211     Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
212     Init(lower, upper);
213     for (; i <= upper; i++)
214     {
215       myArray->SetValue(i, arr.Value(i));
216     }
217   }
218   else
219   {
220     myArray.Nullify();
221   }
222 }
223
224 //=======================================================================
225 //function : Paste
226 //purpose  : 
227 //=======================================================================
228 void TDataStd_ReferenceArray::Paste (const Handle(TDF_Attribute)& Into,
229                                      const Handle(TDF_RelocationTable)& RT) const
230 {
231   Handle(TDataStd_ReferenceArray) anArray = Handle(TDataStd_ReferenceArray)::DownCast(Into);
232   if (myArray.IsNull())
233   {
234     anArray->myArray.Nullify();
235     return;
236   }
237   const TDataStd_LabelArray1& arr = myArray->Array1();
238   Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
239   if (lower != anArray->Lower() || upper != anArray->Upper())
240     anArray->Init(lower, upper);
241   for (; i <= upper; i++)
242   {
243     TDF_Label L = arr.Value(i), rL;
244     if (!L.IsNull())
245     {
246       if (!RT->HasRelocation(L, rL))
247         rL = L;
248       anArray->myArray->SetValue(i, rL);
249     }
250   }
251 }
252
253 //=======================================================================
254 //function : References
255 //purpose  : Adds the referenced attributes or labels.
256 //=======================================================================
257 void TDataStd_ReferenceArray::References(const Handle(TDF_DataSet)& aDataSet) const
258 {
259   if (!Label().IsImported() && !myArray.IsNull()) 
260   {
261     const TDataStd_LabelArray1& arr = myArray->Array1();
262     Standard_Integer lower = arr.Lower(), i = lower, upper = arr.Upper();
263     for (; i <= upper; i++)
264     {
265       if (!arr.Value(i).IsNull())
266         aDataSet->AddLabel(arr.Value(i));
267     }
268   }
269 }
270
271 //=======================================================================
272 //function : Dump
273 //purpose  : 
274 //=======================================================================
275 Standard_OStream& TDataStd_ReferenceArray::Dump (Standard_OStream& anOS) const
276 {  
277   anOS << "ReferenceArray";
278   return anOS;
279 }