summaryrefslogtreecommitdiff
path: root/src/TDF/TDF_Data.cxx
blob: 5943d23275d89635c699eedae06139f8d8b738a8 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// File:        TDF_Data.cxx
//              -------------
// Author:      DAUTRY Philippe
//              <fid@fox.paris1.matra-dtv.fr>
// Copyright:   Matra Datavision 1997

// Version:     0.0
// History:     Version Date            Purpose
//              0.0     Feb  6 1997     Creation



#include <TDF_Data.ixx>

#include <TCollection_AsciiString.hxx>

#include <TDF_Attribute.hxx>
#include <TDF_AttributeIterator.hxx>
#include <TDF_AttributeIndexedMap.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDF_Delta.hxx>
#include <TDF_AttributeDelta.hxx>
#include <TDF_DeltaOnAddition.hxx>
#include <TDF_DeltaOnForget.hxx>
#include <TDF_DeltaOnModification.hxx>
#include <TDF_DeltaOnRemoval.hxx>
#include <TDF_DeltaOnResume.hxx>
#include <TDF_Label.hxx>
#include <TDF_LabelNode.hxx>
#include <TDF_LabelNodePtr.hxx>
#include <TDF_Tool.hxx>

#include <Standard_NoMoreObject.hxx>
#include <Standard_NullObject.hxx>

#undef DEB_DELTA_CREATION
#undef TDF_DATA_COMMIT_OPTIMIZED

#define BUC60879

#ifdef DEB_DELTA
#define TDF_Data_DebugModified(ACTION) \
  cout<<"After "<<ACTION<<" #"<<myTransaction+1<<", DF "<<this<<" had "<<myNbTouchedAtt<<" attribute(s) touched. Time = "<<myTime<<endl; \
if (!myTransaction) { \
  TCollection_AsciiString entry; \
  for (TDF_ChildIterator itr(Root(),Standard_True); itr.More(); itr.Next()) { \
    const TDF_LabelNode* lnp = itr.Value().myLabelNode; \
    if (lnp->AttributesModified() || lnp->MayBeModified()) { \
      TDF_Tool::Entry(itr.Value(),entry); \
      cout<<ACTION<<" on "<<entry<<" : flag(s) "; \
      if (lnp->AttributesModified()) cout<<"AttributesModified "; \
      if (lnp->MayBeModified()) cout<<"MayBeModified already set in transaction 0! Please contact TDF developer."; \
      cout<<endl; \
      cout<<itr.Value()<<endl; \
      entry.Clear(); \
    }}}
#else
#define TDF_Data_DebugModified(ACTION)
#endif

#ifdef DEB_DELTA_CREATION
#define TDF_DataDebugDeltaCreation(DELTATYPE) \
{ \
TCollection_AsciiString entry; \
TDF_Tool::Entry(currentAtt->Label(),entry); \
cout<<"Creation of a DeltaOn"<<DELTATYPE<<" \tat "<<entry<<" \ton "<<currentAtt->DynamicType()<<endl; \
}
#else
#define TDF_DataDebugDeltaCreation(DELTATYPE)
#endif

#define TDF_Data_DeltaCreation(DELTACOMMENT,DELTACREATION) \
if (withDelta) { \
  TDF_DataDebugDeltaCreation(DELTACOMMENT); \
  aDelta->AddAttributeDelta(DELTACREATION); \
}

//=======================================================================
//function : TDF_Data
//purpose  : empty constructor
//=======================================================================

TDF_Data::TDF_Data() :
myTransaction           (0),
myNbTouchedAtt          (0),
myNotUndoMode           (Standard_True),
myTime                  (0),
myAllowModification     (Standard_True)
{
  const Handle(NCollection_IncAllocator) anIncAllocator=
    new NCollection_IncAllocator (16000);
  myLabelNodeAllocator = anIncAllocator;
  myRoot = new (anIncAllocator) TDF_LabelNode (this);
}

//=======================================================================
//function : Destroy
//purpose  : Used to implement the destructor ~.
//=======================================================================

void TDF_Data::Destroy()
{
  AbortUntilTransaction(1);
  delete myRoot;
}


//=======================================================================
//function : OpenTransaction
//purpose  : 
//=======================================================================

Standard_Integer TDF_Data::OpenTransaction() 
{
  myTimes.Push(myTime);
  return ++myTransaction;
}


//=======================================================================
//function : CommitTransaction
//purpose  : Commits the current transaction.
//=======================================================================

Handle(TDF_Delta) TDF_Data::CommitTransaction
(const Standard_Boolean withDelta) 
{
  Handle(TDF_Delta) delta;
  if (myTransaction>0) {
    if (withDelta) delta = new TDF_Delta();
#ifdef DEB_DELTA
    cout<<"TDF_Data::Begin Commit #"<<myTransaction<<endl;    
#endif
#ifdef TDF_DATA_COMMIT_OPTIMIZED
    myNbTouchedAtt = 0;
    if (Root().myLabelNode->MayBeModified())
#endif
      myNbTouchedAtt =
        TDF_Data::CommitTransaction(Root(),delta,withDelta);

    if (myNbTouchedAtt && !(withDelta && delta->IsEmpty())) ++myTime;
    --myTransaction;
    if (withDelta) {
      if (!delta->IsEmpty()) {
        delta->Validity(myTimes.Top(),myTime);
#ifdef DEB_DELTA
        if (myTransaction == 0) {
          cout<<"TDF_Data::Commit generated this delta in t=0:"<<endl;
          delta->Dump(cout);
        }
#endif
      }
#ifdef DEB_DELTA
      else {
        if (myTransaction == 0)
          cout<<"TDF_Data::Commit generated NO delta."<<endl;
      }
#endif
    }
    myTimes.Pop();
  }
  TDF_Data_DebugModified("COMMIT");
  return delta;
}


//=======================================================================
//function : CommitUntilTransaction
//purpose  : Commits the transactions until AND including
//           the given transaction index.
//=======================================================================

Handle(TDF_Delta) TDF_Data::CommitUntilTransaction
(const Standard_Integer untilTransaction,
 const Standard_Boolean withDelta)
{
  Handle(TDF_Delta) delta;
  if ((untilTransaction>0) && (myTransaction >= untilTransaction)) {
    while (myTransaction > untilTransaction) {
      delta = TDF_Data::CommitTransaction(Standard_False);
    }
    delta = TDF_Data::CommitTransaction(withDelta);
  }
  return delta;
}


//=======================================================================
//function : CommitTransaction
//purpose  : Recursive method used to implement the commit action.
//=======================================================================

Standard_Integer TDF_Data::CommitTransaction
(const TDF_Label& aLabel,
 const Handle(TDF_Delta)& aDelta,
 const Standard_Boolean withDelta)
{
  aLabel.myLabelNode->MayBeModified(Standard_False);
  Standard_Integer nbTouchedAtt = 0;
#ifdef TDF_DATA_COMMIT_OPTIMIZED
  Standard_Boolean attMod = aLabel.myLabelNode->AttributesModified();
#else
  Standard_Boolean attMod = Standard_True;
#endif

  if (attMod) {
    Handle(TDF_Attribute)    lastAtt;
    Handle(TDF_Attribute)  backupAtt;
    Standard_Boolean currentIsRemoved = Standard_False;
    attMod = Standard_False;

    TDF_AttributeIterator itr1(aLabel, Standard_False);
    while (itr1.More()) {
      TDF_Attribute * aPtrCurrentAtt = itr1.Value();
      itr1.Next();
      //      currentAtt = itr1.Value();

#ifdef BUC60879
      // A callback:
      aPtrCurrentAtt->BeforeCommitTransaction();
#endif

      backupAtt  = aPtrCurrentAtt->myBackup;

      if (aPtrCurrentAtt->myTransaction == myTransaction) {
        ++nbTouchedAtt;
        --(aPtrCurrentAtt->myTransaction);

        // ------------------------------------------------------- Forgotten
        if (aPtrCurrentAtt->IsForgotten()) {
          if (aPtrCurrentAtt->mySavedTransaction >=
              aPtrCurrentAtt->myTransaction)
          {
            const Handle(TDF_Attribute) currentAtt = aPtrCurrentAtt;
            // Collision with a not forgotten version.
            if (backupAtt.IsNull()) {
              TDF_Data_DeltaCreation
                ("Removal(1)",
                 currentAtt->DeltaOnRemoval());
              if (myNotUndoMode) currentAtt->BeforeRemoval();
              aLabel.myLabelNode->RemoveAttribute(lastAtt,currentAtt);
              currentIsRemoved = Standard_True;
              attMod = Standard_True;
            }
            else {
              // Modified then Forgotten...
              // Forgotten flag spreading?
              currentAtt->Resume();
              currentAtt->Restore(backupAtt);
              currentAtt->myTransaction = backupAtt->myTransaction;
              currentAtt->RemoveBackup();
              backupAtt = currentAtt->myBackup;
              if (myTransaction == 1) {
                TDF_Data_DeltaCreation
                  ("Removal(2)",
                   currentAtt->DeltaOnRemoval());
                if (myNotUndoMode) currentAtt->BeforeRemoval();
                aLabel.myLabelNode->RemoveAttribute(lastAtt,currentAtt);
                currentIsRemoved = Standard_True;
              }
              else {
                // BeforeForget has already been called once.
                // if (myNotUndoMode) currentAtt->BeforeForget();
                currentAtt->Forget(myTransaction-1);
                TDF_Data_DeltaCreation
                  ("Forget(1)",
                   currentAtt->DeltaOnForget());
                attMod = Standard_True;
              }
            }
          }
          else {
            // Forgotten in lower transaction than the current one.
            TDF_Data_DeltaCreation
              ("Forget(2)",
               aPtrCurrentAtt->DeltaOnForget());
          }
        }
        // ---------------------------------------------------------- Resumed.
        else if (aPtrCurrentAtt->mySavedTransaction < 0) {
          TDF_Data_DeltaCreation
            ("Resume",
             aPtrCurrentAtt->DeltaOnResume());
          aPtrCurrentAtt->mySavedTransaction = 0;
          attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
        }

        // ------------------------------------------------------------ Added.
        else if (backupAtt.IsNull()) {
          TDF_Data_DeltaCreation
            ("Addition",
             aPtrCurrentAtt->DeltaOnAddition());
          attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
        }
        // --------------------------------------------------------- Modified.
        else {
          TDF_Data_DeltaCreation
            ("Modification",
             aPtrCurrentAtt->DeltaOnModification(backupAtt));
          if (aPtrCurrentAtt->myTransaction == backupAtt->myTransaction)
            aPtrCurrentAtt->RemoveBackup();
          attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
        }

      }
      else attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);

      if (currentIsRemoved) currentIsRemoved = Standard_False;
      else lastAtt = aPtrCurrentAtt;
    }
    aLabel.myLabelNode->AttributesModified(attMod);
  }

  // Iteration on the children to do the same!
  //------------------------------------------
  for (TDF_ChildIterator itr2(aLabel); itr2.More(); itr2.Next()) {
#ifdef TDF_DATA_COMMIT_OPTIMIZED
    if (itr2.Value().myLabelNode->MayBeModified())
#endif
      nbTouchedAtt +=
        TDF_Data::CommitTransaction(itr2.Value(),aDelta,withDelta);
  }

  return nbTouchedAtt;
}


//=======================================================================
//function : AbortTransaction
//purpose  : Aborts the current transaction.
//=======================================================================

void TDF_Data::AbortTransaction() 
{
  if (myTransaction>0)
    Undo(TDF_Data::CommitTransaction(Standard_True),Standard_False);
  TDF_Data_DebugModified("New ABORT");
}


//=======================================================================
//function : AbortUntilTransaction
//purpose  : Aborts the transactions until AND including the given index.
//=======================================================================

void TDF_Data::AbortUntilTransaction(const Standard_Integer untilTransaction)
{
  if (untilTransaction>0)
    Undo(TDF_Data::CommitUntilTransaction(untilTransaction,Standard_True),Standard_False);
}


//=======================================================================
//function : IsApplicable
//purpose  : 
//=======================================================================

Standard_Boolean TDF_Data::IsApplicable
(const Handle(TDF_Delta)& aDelta) const
{
  return !aDelta.IsNull() && aDelta->IsApplicable(myTime);
}


//=======================================================================
//function : Undo
//purpose  : Applies a delta to undo  actions.
//=======================================================================

Handle(TDF_Delta) TDF_Data::Undo
(const Handle(TDF_Delta)& aDelta,
 const Standard_Boolean withDelta)
{
  Handle(TDF_Delta) newDelta;
  if (!aDelta.IsNull ()) {
    if (aDelta->IsApplicable(myTime)) {
      if (withDelta) OpenTransaction();
#ifdef DEB_DELTA
      cout<<"TDF_Data::Undo applies this delta:"<<endl;
      aDelta->Dump(cout);
#endif
      aDelta->BeforeOrAfterApply(Standard_True);
      myNotUndoMode = Standard_False;
      aDelta->Apply ();
      myNotUndoMode = Standard_True;
      if (withDelta) {
        newDelta = CommitTransaction(Standard_True);
        newDelta->Validity(aDelta->EndTime(),aDelta->BeginTime());
#ifdef DEB_DELTA
        cout<<"TDF_Data::Undo, after validity correction, Delta is now available from time \t#"<<newDelta->BeginTime()<<" to time \t#"<<newDelta->EndTime()<<endl;
#endif
      }
      myTime = aDelta->BeginTime();
      aDelta->BeforeOrAfterApply(Standard_False);
    }
  }
  return newDelta;
}



//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

Standard_OStream& TDF_Data::Dump(Standard_OStream& anOS) const
{
  anOS<<"Dump of a TDF_Data."<<endl;
  anOS<<"Current transaction: "<<myTransaction;
  anOS<<"; Current tick: "<<myTime<<";"<<endl;
  return anOS;
}