0003513: There is no check for boundary of array in method Set for array attributes
[occt.git] / src / TDataStd / TDataStd_RealArray.cxx
CommitLineData
b311480e 1// Created on: 1999-06-16
2// Created by: Sergey RUIN
3// Copyright (c) 1999-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22
23#include <TDataStd_RealArray.ixx>
24#include <TDataStd_DeltaOnModificationOfRealArray.hxx>
25#include <TDF_DefaultDeltaOnModification.hxx>
26#define OCC2932
27
28//=======================================================================
29//function : GetID
30//purpose :
31//=======================================================================
32
33const Standard_GUID& TDataStd_RealArray::GetID()
34{
35 static Standard_GUID TDataStd_RealArrayID ("2a96b61e-ec8b-11d0-bee7-080009dc3333");
36 return TDataStd_RealArrayID;
37}
38
39
40//=======================================================================
41//function : TDataStd_RealArray
42//purpose : Empty Constructor
43//=======================================================================
44
45TDataStd_RealArray::TDataStd_RealArray() : myIsDelta(Standard_False){}
46
47//=======================================================================
48//function : Init
49//purpose :
50//=======================================================================
51
52void TDataStd_RealArray::Init(const Standard_Integer lower,
53 const Standard_Integer upper)
54{
55 Backup(); // jfa 15.01.2003 for LH3D1378
56
57 myValue = new TColStd_HArray1OfReal(lower, upper, 0.);
58}
59
60//=======================================================================
61//function : Set
62//purpose :
63//=======================================================================
64
65Handle(TDataStd_RealArray) TDataStd_RealArray::Set
66 (const TDF_Label& label,
67 const Standard_Integer lower,
68 const Standard_Integer upper,
69 const Standard_Boolean isDelta)
70{
71 Handle(TDataStd_RealArray) A;
72 if (!label.FindAttribute (TDataStd_RealArray::GetID(), A)) {
73 A = new TDataStd_RealArray;
74 A->Init (lower, upper);
75 A->SetDelta(isDelta);
76 label.AddAttribute(A);
77 }
78 else if (lower != A->Lower() || upper != A->Upper())
79 {
80 A->Init (lower, upper);
81 }
82 return A;
83}
84
85
86//=======================================================================
87//function : SetValue
88//purpose :
89//=======================================================================
90
91void TDataStd_RealArray::SetValue (const Standard_Integer index,
92 const Standard_Real value)
93{
94 // OCC2932 correction
95 if(myValue.IsNull()) return;
96 if(myValue->Value(index) == value)
fa13a85d 97 return;
7fd59977 98 Backup();
99 myValue->SetValue(index, value);
100}
101
102
103//=======================================================================
104//function : GetValue
105//purpose :
106//=======================================================================
107
108Standard_Real TDataStd_RealArray::Value (const Standard_Integer index) const
109{
110 if(myValue.IsNull()) return RealFirst();
111 return myValue->Value(index);
112}
113
114
115
116//=======================================================================
117//function : Lower
118//purpose :
119//=======================================================================
120Standard_Integer TDataStd_RealArray::Lower (void) const
121{
122 if(myValue.IsNull()) return 0;
123 return myValue->Lower();
124}
125
126
127//=======================================================================
128//function : Upper
129//purpose :
130//=======================================================================
131Standard_Integer TDataStd_RealArray::Upper (void) const
132{
133 if(myValue.IsNull()) return 0;
134 return myValue->Upper();
135}
136
137
138//=======================================================================
139//function : Length
140//purpose :
141//=======================================================================
142Standard_Integer TDataStd_RealArray::Length (void) const
143{
144 if(myValue.IsNull()) return 0;
145 return myValue->Length();
146}
147
148
149//=======================================================================
150//function : ChangeArray
151//purpose : If value of <newArray> differs from <myValue>, Backup
152// : performed and myValue refers to new instance of HArray1OfReal
153// : that holds <newArray>
154//=======================================================================
155
156void TDataStd_RealArray::ChangeArray(const Handle(TColStd_HArray1OfReal)& newArray,
157 const Standard_Boolean isCheckItems)
158{
159 Standard_Integer aLower = newArray->Lower();
160 Standard_Integer anUpper = newArray->Upper();
161 Standard_Boolean aDimEqual = Standard_False;
162 Standard_Integer i;
163
164#ifdef OCC2932
a855b7eb 165 if (Lower() == aLower && Upper() == anUpper ) {
7fd59977 166 aDimEqual = Standard_True;
167 Standard_Boolean isEqual = Standard_True;
168 if(isCheckItems) {
169 for(i = aLower; i <= anUpper; i++) {
170 if(myValue->Value(i) != newArray->Value(i)) {
171 isEqual = Standard_False;
172 break;
173 }
174 }
175 if(isEqual)
176 return;
177 }
178 }
179#endif
180
181 Backup();
182
a855b7eb 183 if(myValue.IsNull() || !aDimEqual)
7fd59977 184 myValue = new TColStd_HArray1OfReal(aLower, anUpper);
185
186 for(i = aLower; i <= anUpper; i++)
187 myValue->SetValue(i, newArray->Value(i));
188}
189
190//=======================================================================
191//function : ID
192//purpose :
193//=======================================================================
194
195const Standard_GUID& TDataStd_RealArray::ID () const { return GetID(); }
196
197
198//=======================================================================
199//function : NewEmpty
200//purpose :
201//=======================================================================
202
203Handle(TDF_Attribute) TDataStd_RealArray::NewEmpty () const
204{
205 return new TDataStd_RealArray();
206}
207
208//=======================================================================
209//function : Restore
210//purpose :
211//=======================================================================
212
213void TDataStd_RealArray::Restore(const Handle(TDF_Attribute)& With)
214{
215 Standard_Integer i, lower, upper;
216 Handle(TDataStd_RealArray) anArray = Handle(TDataStd_RealArray)::DownCast(With);
217 if(!anArray->myValue.IsNull()) {
218 lower = anArray->Lower();
219 upper = anArray->Upper();
220 myIsDelta = anArray->myIsDelta;
221 myValue = new TColStd_HArray1OfReal(lower, upper);
222 for(i = lower; i<=upper; i++)
223 myValue->SetValue(i, anArray->Value(i));
224 }
225 else
226 myValue.Nullify();
227}
228
229//=======================================================================
230//function : Paste
231//purpose :
232//=======================================================================
233
234void TDataStd_RealArray::Paste (const Handle(TDF_Attribute)& Into,
235 const Handle(TDF_RelocationTable)& ) const
236{
237 if(!myValue.IsNull()) {
238 Handle(TDataStd_RealArray) anAtt = Handle(TDataStd_RealArray)::DownCast(Into);
239 if(!anAtt.IsNull()) {
240 anAtt->ChangeArray( myValue, Standard_False );
241 anAtt->SetDelta(myIsDelta);
242 }
243 }
244}
245
246//=======================================================================
247//function : Dump
248//purpose :
249//=======================================================================
250
251Standard_OStream& TDataStd_RealArray::Dump (Standard_OStream& anOS) const
252{
253 anOS << "\nRealArray::" << this <<" :";
254 if(!myValue.IsNull()) {
255 Standard_Integer i, lower, upper;
256 lower = myValue->Lower();
257 upper = myValue->Upper();
258 for(i = lower; i<=upper; i++)
259 anOS << " " <<myValue->Value(i);
260 }
eb901da6 261 anOS << " Delta is " << (myIsDelta ? "ON":"OFF");
7fd59977 262 anOS << endl;
263 return anOS;
264}
265
266//=======================================================================
267//function : DeltaOnModification
268//purpose :
269//=======================================================================
270
271Handle(TDF_DeltaOnModification) TDataStd_RealArray::DeltaOnModification
272(const Handle(TDF_Attribute)& OldAtt) const
273{
274 if(myIsDelta)
275 return new TDataStd_DeltaOnModificationOfRealArray(*((Handle(TDataStd_RealArray)*)&OldAtt));
276 else return new TDF_DefaultDeltaOnModification(OldAtt);
277}