0024023: Revamp the OCCT Handle -- downcast (automatic)
[occt.git] / src / TDataStd / TDataStd_RealArray.cxx
1 // Created on: 1999-06-16
2 // Created by: Sergey RUIN
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <TDataStd_RealArray.ixx>
18 #include <TDataStd_DeltaOnModificationOfRealArray.hxx>
19 #include <TDF_DefaultDeltaOnModification.hxx>
20
21 //=======================================================================
22 //function : GetID
23 //purpose  : 
24 //=======================================================================
25
26 const Standard_GUID& TDataStd_RealArray::GetID() 
27
28   static Standard_GUID TDataStd_RealArrayID ("2a96b61e-ec8b-11d0-bee7-080009dc3333");
29   return TDataStd_RealArrayID; 
30 }
31
32
33 //=======================================================================
34 //function : TDataStd_RealArray
35 //purpose  : Empty Constructor
36 //=======================================================================
37
38 TDataStd_RealArray::TDataStd_RealArray() : myIsDelta(Standard_False){}
39
40 //=======================================================================
41 //function : Init
42 //purpose  : 
43 //=======================================================================
44
45 void TDataStd_RealArray::Init(const Standard_Integer lower,
46                               const Standard_Integer upper)
47 {
48   Backup(); // jfa 15.01.2003 for LH3D1378
49
50   myValue = new TColStd_HArray1OfReal(lower, upper, 0.);
51 }
52
53 //=======================================================================
54 //function : Set
55 //purpose  : 
56 //=======================================================================
57
58 Handle(TDataStd_RealArray) TDataStd_RealArray::Set
59                                           (const TDF_Label&       label,
60                                            const Standard_Integer lower,
61                                            const Standard_Integer upper,
62                                            const Standard_Boolean isDelta) 
63 {
64   Handle(TDataStd_RealArray) A;
65   if (!label.FindAttribute (TDataStd_RealArray::GetID(), A)) {
66     A = new TDataStd_RealArray;
67     A->Init (lower, upper);
68     A->SetDelta(isDelta); 
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 //=======================================================================
80 //function : SetValue
81 //purpose  : 
82 //=======================================================================
83
84 void TDataStd_RealArray::SetValue (const Standard_Integer index,
85                                    const Standard_Real value) 
86 {
87   // OCC2932 correction
88   if(myValue.IsNull()) return;
89   if(myValue->Value(index) == value)
90     return;  
91   Backup();
92   myValue->SetValue(index, value);
93 }
94
95
96 //=======================================================================
97 //function : GetValue
98 //purpose  : 
99 //=======================================================================
100
101 Standard_Real TDataStd_RealArray::Value (const Standard_Integer index) const 
102 {
103   if(myValue.IsNull()) return RealFirst();
104   return myValue->Value(index); 
105 }
106
107
108
109 //=======================================================================
110 //function : Lower
111 //purpose  : 
112 //=======================================================================
113 Standard_Integer TDataStd_RealArray::Lower (void) const 
114
115   if(myValue.IsNull()) return 0;
116   return myValue->Lower(); 
117 }
118
119
120 //=======================================================================
121 //function : Upper
122 //purpose  : 
123 //=======================================================================
124 Standard_Integer TDataStd_RealArray::Upper (void) const 
125
126   if(myValue.IsNull()) return 0;
127   return myValue->Upper(); 
128 }
129
130
131 //=======================================================================
132 //function : Length
133 //purpose  : 
134 //=======================================================================
135 Standard_Integer TDataStd_RealArray::Length (void) const 
136 {
137   if(myValue.IsNull()) return 0;
138   return myValue->Length(); 
139 }
140
141
142 //=======================================================================
143 //function : ChangeArray
144 //purpose  : If value of <newArray> differs from <myValue>, Backup 
145 //         : performed and myValue refers to new instance of HArray1OfReal
146 //         : that holds <newArray>
147 //=======================================================================
148
149 void TDataStd_RealArray::ChangeArray(const Handle(TColStd_HArray1OfReal)& newArray,
150                                         const Standard_Boolean isCheckItems) 
151 {
152   Standard_Integer aLower    = newArray->Lower();
153   Standard_Integer anUpper   = newArray->Upper();
154   Standard_Boolean aDimEqual = Standard_False;
155   Standard_Integer i;
156
157   if (Lower() == aLower && Upper() == anUpper ) {
158     aDimEqual = Standard_True;
159     Standard_Boolean isEqual = Standard_True;
160     if(isCheckItems) {
161       for(i = aLower; i <= anUpper; i++) {
162         if(myValue->Value(i) != newArray->Value(i)) {
163           isEqual = Standard_False;
164           break;
165         }
166       }
167       if(isEqual)
168         return;
169     }
170   }
171
172   Backup();
173
174   if(myValue.IsNull() || !aDimEqual) 
175     myValue = new TColStd_HArray1OfReal(aLower, anUpper);
176
177   for(i = aLower; i <= anUpper; i++) 
178     myValue->SetValue(i, newArray->Value(i));
179 }
180
181 //=======================================================================
182 //function : ID
183 //purpose  : 
184 //=======================================================================
185
186 const Standard_GUID& TDataStd_RealArray::ID () const { return GetID(); }
187
188
189 //=======================================================================
190 //function : NewEmpty
191 //purpose  : 
192 //=======================================================================
193
194 Handle(TDF_Attribute) TDataStd_RealArray::NewEmpty () const
195 {  
196   return new TDataStd_RealArray(); 
197 }
198
199 //=======================================================================
200 //function : Restore
201 //purpose  : 
202 //=======================================================================
203
204 void TDataStd_RealArray::Restore(const Handle(TDF_Attribute)& With) 
205 {
206   Standard_Integer i, lower, upper;
207   Handle(TDataStd_RealArray) anArray = Handle(TDataStd_RealArray)::DownCast(With);
208   if(!anArray->myValue.IsNull()) {
209     lower = anArray->Lower();
210     upper = anArray->Upper();
211     myIsDelta = anArray->myIsDelta;
212     myValue = new TColStd_HArray1OfReal(lower, upper);
213     for(i = lower; i<=upper; i++)
214       myValue->SetValue(i, anArray->Value(i)); 
215   }
216   else
217     myValue.Nullify();
218 }
219
220 //=======================================================================
221 //function : Paste
222 //purpose  : 
223 //=======================================================================
224
225 void TDataStd_RealArray::Paste (const Handle(TDF_Attribute)& Into,
226                                 const Handle(TDF_RelocationTable)& ) const
227 {
228   if(!myValue.IsNull()) {    
229     Handle(TDataStd_RealArray) anAtt = Handle(TDataStd_RealArray)::DownCast(Into);
230     if(!anAtt.IsNull()) {
231       anAtt->ChangeArray( myValue, Standard_False );
232       anAtt->SetDelta(myIsDelta);
233     }
234   }
235 }
236
237 //=======================================================================
238 //function : Dump
239 //purpose  : 
240 //=======================================================================
241
242 Standard_OStream& TDataStd_RealArray::Dump (Standard_OStream& anOS) const
243 {  
244   anOS << "\nRealArray::" << this <<" :";
245   if(!myValue.IsNull()) {
246     Standard_Integer i, lower, upper;
247     lower = myValue->Lower();
248     upper = myValue->Upper();
249     for(i = lower; i<=upper; i++)
250       anOS << " " <<myValue->Value(i);
251   }
252   anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
253   anOS << endl;
254   return anOS;
255 }
256
257 //=======================================================================
258 //function : DeltaOnModification
259 //purpose  : 
260 //=======================================================================
261
262 Handle(TDF_DeltaOnModification) TDataStd_RealArray::DeltaOnModification
263 (const Handle(TDF_Attribute)& OldAtt) const
264 {
265   if(myIsDelta)
266     return new TDataStd_DeltaOnModificationOfRealArray(Handle(TDataStd_RealArray)::DownCast (OldAtt));
267   else return new TDF_DefaultDeltaOnModification(OldAtt);
268 }