OCC18056 Exception during copying Array attribute with array(0,0)
[occt.git] / src / TDataStd / TDataStd_IntegerArray.cxx
CommitLineData
7fd59977 1// File: TDataStd_IntegerArray.cxx
2// Created: Wed Jun 16 10:32:25 1999
3// Author: Sergey RUIN
4
5
6#include <TDataStd_IntegerArray.ixx>
7#include <TDataStd_DeltaOnModificationOfIntArray.hxx>
8#include <TDF_DefaultDeltaOnModification.hxx>
9#define OCC925
10#define OCC2932
11
12//=======================================================================
13//function : GetID
14//purpose :
15//=======================================================================
16
17const Standard_GUID& TDataStd_IntegerArray::GetID()
18{
19 static Standard_GUID TDataStd_IntegerArrayID ("2a96b61d-ec8b-11d0-bee7-080009dc3333");
20 return TDataStd_IntegerArrayID;
21}
22
23
24//=======================================================================
25//function : TDataStd_IntegerArray
26//purpose : Empty Constructor
27//=======================================================================
28
29TDataStd_IntegerArray::TDataStd_IntegerArray()
30 :myIsDelta(Standard_False) {}
31
32//=======================================================================
33//function : Init
34//purpose :
35//=======================================================================
36
37void TDataStd_IntegerArray::Init(const Standard_Integer lower,
38 const Standard_Integer upper)
39{
40#ifdef OCC925
41 Backup();
42#endif
43 myValue = new TColStd_HArray1OfInteger(lower, upper, 0);
44}
45
46//=======================================================================
47//function : Set
48//purpose : isDelta applicable only for new attributes
49//=======================================================================
50
51Handle(TDataStd_IntegerArray) TDataStd_IntegerArray::Set
52 (const TDF_Label& label,
53 const Standard_Integer lower,
54 const Standard_Integer upper,
55 const Standard_Boolean isDelta)
56
57{
58 Handle(TDataStd_IntegerArray) A;
59 if (!label.FindAttribute (TDataStd_IntegerArray::GetID(), A)) {
60 A = new TDataStd_IntegerArray;
61 A->Init (lower, upper);
62 A->SetDelta(isDelta);
63 label.AddAttribute(A);
64 }
65 else if (lower != A->Lower() || upper != A->Upper())
66 {
67 A->Init (lower, upper);
68 }
69 return A;
70}
71
72//=======================================================================
73//function : SetValue
74//purpose :
75//=======================================================================
76
77void TDataStd_IntegerArray::SetValue(const Standard_Integer index, const Standard_Integer value)
78{
79 if(myValue.IsNull()) return;
80 if( myValue->Value(index) == value)
81 return;
82 Backup();
83 myValue->SetValue(index, value);
84}
85
86
87//=======================================================================
88//function : GetValue
89//purpose :
90//=======================================================================
91
92Standard_Integer TDataStd_IntegerArray::Value (const Standard_Integer index) const
93{
94 if(myValue.IsNull()) return 0;
95 return myValue->Value(index);
96}
97
98
99
100//=======================================================================
101//function : Lower
102//purpose :
103//=======================================================================
104Standard_Integer TDataStd_IntegerArray::Lower (void) const
105{
106 if(myValue.IsNull()) return 0;
107 return myValue->Lower();
108}
109
110
111//=======================================================================
112//function : Upper
113//purpose :
114//=======================================================================
115Standard_Integer TDataStd_IntegerArray::Upper (void) const
116{
117 if(myValue.IsNull()) return 0;
118 return myValue->Upper();
119}
120
121
122//=======================================================================
123//function : Length
124//purpose :
125//=======================================================================
126Standard_Integer TDataStd_IntegerArray::Length (void) const
127{
128 if(myValue.IsNull()) return 0;
129 return myValue->Length();
130}
131
132
133//=======================================================================
134//function : ChangeArray
135//purpose : If value of <newArray> differs from <myValue>, Backup
136// : performed and myValue refers to new instance of HArray1OfInteger
137// : that holds <newArray>
138//=======================================================================
139
140void TDataStd_IntegerArray::ChangeArray(const Handle(TColStd_HArray1OfInteger)& newArray,
141 const Standard_Boolean isCheckItems)
142{
143 Standard_Integer aLower = newArray->Lower();
144 Standard_Integer anUpper = newArray->Upper();
145 Standard_Boolean aDimEqual = Standard_False;
146 Standard_Integer i;
147#ifdef OCC2932
a855b7eb
S
148
149 if(Lower() == aLower && Upper() == anUpper ) {
7fd59977 150 aDimEqual = Standard_True;
151 if(isCheckItems) {
152 Standard_Boolean isEqual = Standard_True;
153 for(i = aLower; i <= anUpper; i++) {
154 if(myValue->Value(i) != newArray->Value(i)) {
155 isEqual = Standard_False;
156 break;
157 }
158 }
159 if(isEqual)
160 return;
161 }
162 }
163#endif
164
165 Backup();
166// Handles of myValue of current and backuped attributes will be different!
a855b7eb 167 if(myValue.IsNull() || !aDimEqual)
7fd59977 168 myValue = new TColStd_HArray1OfInteger(aLower, anUpper);
169
170 for(i = aLower; i <= anUpper; i++)
171 myValue->SetValue(i, newArray->Value(i));
172}
173
174
175//=======================================================================
176//function : ID
177//purpose :
178//=======================================================================
179
180const Standard_GUID& TDataStd_IntegerArray::ID () const { return GetID(); }
181
182
183//=======================================================================
184//function : NewEmpty
185//purpose :
186//=======================================================================
187
188Handle(TDF_Attribute) TDataStd_IntegerArray::NewEmpty () const
189{
190 return new TDataStd_IntegerArray();
191}
192
193//=======================================================================
194//function : Restore
195//purpose :
196//=======================================================================
197
198void TDataStd_IntegerArray::Restore(const Handle(TDF_Attribute)& With)
199{
200 Standard_Integer i, lower, upper;
201 Handle(TDataStd_IntegerArray) anArray = Handle(TDataStd_IntegerArray)::DownCast(With);
202 if(!anArray->myValue.IsNull()) {
203 lower = anArray->Lower();
204 upper = anArray->Upper();
205 myValue = new TColStd_HArray1OfInteger(lower, upper);
206 for(i = lower; i<=upper; i++)
207 myValue->SetValue(i, anArray->Value(i));
208 myIsDelta = anArray->myIsDelta;
209 }
210 else
211 myValue.Nullify();
212}
213
214//=======================================================================
215//function : Paste
216//purpose :
217//=======================================================================
218
219void TDataStd_IntegerArray::Paste (const Handle(TDF_Attribute)& Into,
220 const Handle(TDF_RelocationTable)& ) const
221{
222
223 if(!myValue.IsNull()) {
224 Handle(TDataStd_IntegerArray) anAtt = Handle(TDataStd_IntegerArray)::DownCast(Into);
225 if(!anAtt.IsNull()) {
226 anAtt->ChangeArray( myValue, Standard_False );
227 anAtt->SetDelta(myIsDelta);
228 }
229 }
230}
231
232//=======================================================================
233//function : Dump
234//purpose :
235//=======================================================================
236
237Standard_OStream& TDataStd_IntegerArray::Dump (Standard_OStream& anOS) const
238{
239 anOS << "\nIntegerArray:: " << this <<" :";
240 if(!myValue.IsNull()) {
241 Standard_Integer i, lower, upper;
242 lower = myValue->Lower();
243 upper = myValue->Upper();
244 for(i = lower; i<=upper; i++)
245 anOS << " " <<myValue->Value(i);
246 }
247 anOS << " Delta is " << myIsDelta ? "ON":"OFF";
248 anOS << endl;
249
250 // anOS <<"\nAttribute fields: ";
251 // anOS << TDF_Attribute::Dump(anOS);
252
253 return anOS;
254}
255
256//=======================================================================
257//function : DeltaOnModification
258//purpose :
259//=======================================================================
260
261Handle(TDF_DeltaOnModification) TDataStd_IntegerArray::DeltaOnModification
262(const Handle(TDF_Attribute)& OldAttribute) const
263{
264 if(myIsDelta)
265 return new TDataStd_DeltaOnModificationOfIntArray(*((Handle(TDataStd_IntegerArray)*)&OldAttribute));
266 else return new TDF_DefaultDeltaOnModification(OldAttribute);
267}
268
269