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