summaryrefslogtreecommitdiff
path: root/src/TObj/TObj_TIntSparseArray.cxx
blob: 5bd04ca8f8487ca717b2f8447574a9eeabe4645e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// File      : TObj_TIntSparseArray.cxx
// Created   : 16.03.2007
// Author    : Michael SAZONOV
// Copyright:   Open CASCADE  2007
// The original implementation Copyright: (C) RINA S.p.A

#include <TObj_TIntSparseArray.hxx>
#include <Standard_GUID.hxx>
#include <Standard_ImmutableObject.hxx>
#include <TDF_Data.hxx>
#include <TDF_AttributeDelta.hxx>
#include <TDF_DeltaOnModification.hxx>

IMPLEMENT_STANDARD_HANDLE(TObj_TIntSparseArray,TDF_Attribute)
IMPLEMENT_STANDARD_RTTIEXT(TObj_TIntSparseArray,TDF_Attribute)

//=======================================================================
//function : TObj_TIntSparseArray
//purpose  : Empty constructor
//=======================================================================

TObj_TIntSparseArray::TObj_TIntSparseArray ()
: myVector(100), myOldMap(100), myDoBackup (Standard_True)
{
}

//=======================================================================
//function : GetID
//purpose  :
//=======================================================================

const Standard_GUID& TObj_TIntSparseArray::GetID()
{
  static Standard_GUID GInterfaceID ("7016dc0c-b118-4433-8ef3-aecdccc79198");
  return GInterfaceID;
}

//=======================================================================
//function : ID
//purpose  :
//=======================================================================

const Standard_GUID& TObj_TIntSparseArray::ID() const
{
  return GetID();
}

//=======================================================================
//function : Set
//purpose  :
//=======================================================================

Handle(TObj_TIntSparseArray) TObj_TIntSparseArray::Set
                           (const TDF_Label& theLabel)
{
  Handle(TObj_TIntSparseArray) aTData;
  if (! theLabel.FindAttribute( GetID(), aTData))
  {
    aTData = new TObj_TIntSparseArray;
    theLabel.AddAttribute(aTData);
  }
  return aTData;
}

//=======================================================================
//function : SetValue
//purpose  : 
//=======================================================================

void TObj_TIntSparseArray::SetValue (const Standard_Integer theId,
                                         const Standard_Integer theValue)
{
  // check that modification is allowed
  if ( !Label().Data()->IsModificationAllowed() )
    Standard_ImmutableObject::Raise
      ("Attribute TObj_TIntSparseArray is changed outside transaction");

  if (theId < 1 || theValue < 1)
    Standard_OutOfRange::Raise ("TObj_TIntSparseArray::SetValue");

  Standard_Integer anOld = AbsentValue;
  Standard_Boolean isOld = myVector.HasValue(theId);
  if (isOld)
  {
    Standard_Integer& aData = myVector(theId);
    if (aData == theValue)
      // no actual modification
      return;
    anOld = aData;
    // set new value
    aData = theValue;
  }
  else
  {
    // set the new value
    myVector.SetValue (theId, theValue);
  }

  TDF_Label aLabel = Label();
  if (!aLabel.IsNull())
  {
    Handle(TDF_Data) aData = aLabel.Data();
    Standard_Integer aCurrentTransaction = aData->Transaction();
    Standard_Integer aMyTransaction = Transaction();

    if (myDoBackup && aMyTransaction < aCurrentTransaction)
      backupValue(theId, anOld, theValue);
  }
}

//=======================================================================
//function : UnsetValue
//purpose  : 
//=======================================================================

void TObj_TIntSparseArray::UnsetValue (const Standard_Integer theId)
{
  // check that modification is allowed
  if ( !Label().Data()->IsModificationAllowed() )
    Standard_ImmutableObject::Raise
      ("Attribute TObj_TIntSparseArray is changed outside transaction");

  if (theId < 1)
    Standard_OutOfRange::Raise ("TObj_TIntSparseArray::UnsetValue");

  Standard_Integer anOld = AbsentValue;
  Standard_Boolean isOld = myVector.HasValue(theId);
  if (isOld)
  {
    anOld = myVector(theId);
    // unset the value
    myVector.UnsetValue(theId);
  }
  else
    // no actual modification
    return;

  TDF_Label aLabel = Label();
  if (!aLabel.IsNull())
  {
    Handle(TDF_Data) aData = aLabel.Data();
    Standard_Integer aCurrentTransaction = aData->Transaction();
    Standard_Integer aMyTransaction = Transaction();

    if (myDoBackup && aMyTransaction < aCurrentTransaction)
      backupValue(theId, anOld, AbsentValue);
  }
}

//=======================================================================
//function : Clear
//purpose  :
//=======================================================================

void TObj_TIntSparseArray::Clear ()
{
  // backup old values
  TDF_Label aLabel = Label();
  if (!aLabel.IsNull())
  {
    Handle(TDF_Data) aData = aLabel.Data();
    Standard_Integer aCurrentTransaction = aData->Transaction();
    Standard_Integer aMyTransaction = Transaction();

    if (myDoBackup && aMyTransaction < aCurrentTransaction)
    {
      TObj_TIntSparseArray_VecOfData::Iterator anIt (myVector);
      for (; anIt.More(); anIt.Next())
      {
        Standard_Integer anId = anIt.Key();
        Standard_Integer aVal = anIt.Value();
        backupValue(anId, aVal, AbsentValue);
      }
    }
  }
  myVector.Clear();
}

//=======================================================================
//function : backupValue
//purpose  :
//=======================================================================

void TObj_TIntSparseArray::backupValue (const Standard_Integer theId,
                                            const Standard_Integer theCurrValue,
                                            const Standard_Integer theNewValue)
{
  // save the current value if it has not been saved in previous time
  if ( !myOldMap.IsBound( theId ) )
    myOldMap.Bind(theId, theCurrValue);
  else
  {
    // if value in Undo is the same as the new one, the item in Undo map may be cleared
    Standard_Integer aUData = myOldMap.Value(theId);
    if (aUData == theNewValue)
      myOldMap.UnBind(theId);
  }
}

//=======================================================================
//function : NewEmpty
//purpose  :
//=======================================================================

Handle(TDF_Attribute) TObj_TIntSparseArray::NewEmpty () const
{
  return new TObj_TIntSparseArray;
}

//=======================================================================
//function : BackupCopy
//purpose  : Moves <this> delta into a new other attribute.
//=======================================================================

Handle(TDF_Attribute) TObj_TIntSparseArray::BackupCopy() const
{
  Handle(TObj_TIntSparseArray) aCopy = 
    Handle(TObj_TIntSparseArray)::DownCast(NewEmpty());

  // save delta data in a copy
  if (!myOldMap.IsEmpty())
    aCopy->myOldMap.Exchange ( (TObj_TIntSparseArray_MapOfData&)myOldMap );

  return aCopy;
}

//=======================================================================
//function : Restore
//purpose  : Restores contents of this with theDelta
//=======================================================================

void TObj_TIntSparseArray::Restore(const Handle(TDF_Attribute)& theDelta)
{
  Handle(TObj_TIntSparseArray) aDelta = 
    Handle(TObj_TIntSparseArray)::DownCast(theDelta);
  if (aDelta.IsNull())
    return;

  // restore the values from aDelta->myOldMap
  if (!aDelta->myOldMap.IsEmpty())
  {
    TObj_TIntSparseArray_MapOfData::Iterator anIt (aDelta->myOldMap);
    for (; anIt.More(); anIt.Next())
    {
      Standard_Integer anId = anIt.Key();
      Standard_Integer anOld = anIt.Value();
      if (anOld == AbsentValue)
        UnsetValue (anId);
      else
        SetValue (anId, anOld);
    }
  }
}

//=======================================================================
//function : Paste
//purpose  : copy this
//=======================================================================

void TObj_TIntSparseArray::Paste (const Handle(TDF_Attribute)& theInto,
                                    const Handle(TDF_RelocationTable)&) const
{
  Handle(TObj_TIntSparseArray) aInto =
    Handle(TObj_TIntSparseArray)::DownCast(theInto);
  if(aInto.IsNull())
    return;

  aInto->myVector.Assign(myVector);
}

//=======================================================================
//function : BeforeCommitTransaction
//purpose  : It is called just before Commit or Abort transaction
//=======================================================================

void TObj_TIntSparseArray::BeforeCommitTransaction()
{
  if (!myOldMap.IsEmpty())
  {
    Backup();
    ClearDelta();
  }
}

//=======================================================================
//function : DeltaOnModification
//purpose  : Applies aDelta to <me>
//=======================================================================

void TObj_TIntSparseArray::DeltaOnModification
                        (const Handle(TDF_DeltaOnModification)& theDelta)
{
  // we do not call Backup here, because a backup data is formed inside Restore.
  // Backup is called rather from BeforeCommitTransaction
  Restore(theDelta->Attribute());
}

//=======================================================================
//function : AfterUndo
//purpose  : After application of a TDF_Delta.
//=======================================================================

Standard_Boolean TObj_TIntSparseArray::AfterUndo
                        (const Handle(TDF_AttributeDelta)&,
                         const Standard_Boolean)
{
  // we must be sure that a delta in <me> is cleared
  ClearDelta();
  return Standard_True;
}