1 // Created by: DAUTRY Philippe
2 // Copyright (c) 1997-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <NCollection_IncAllocator.hxx>
18 #include <Standard_Dump.hxx>
19 #include <Standard_NoMoreObject.hxx>
20 #include <Standard_NullObject.hxx>
21 #include <Standard_Type.hxx>
22 #include <Standard_GUID.hxx>
23 #include <NCollection_Array1.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TDF_Attribute.hxx>
26 #include <TDF_AttributeDelta.hxx>
27 #include <TDF_AttributeIndexedMap.hxx>
28 #include <TDF_AttributeIterator.hxx>
29 #include <TDF_ChildIterator.hxx>
30 #include <TDF_Data.hxx>
31 #include <TDF_Delta.hxx>
32 #include <TDF_DeltaOnAddition.hxx>
33 #include <TDF_DeltaOnForget.hxx>
34 #include <TDF_DeltaOnModification.hxx>
35 #include <TDF_DeltaOnRemoval.hxx>
36 #include <TDF_DeltaOnResume.hxx>
37 #include <TDF_Label.hxx>
38 #include <TDF_LabelNode.hxx>
39 #include <TDF_LabelNodePtr.hxx>
40 #include <TDF_Tool.hxx>
41 #include <TDF_Transaction.hxx>
43 typedef NCollection_Array1<Handle(TDF_AttributeDelta)> TDF_Array1OfAttributeIDelta;
45 IMPLEMENT_STANDARD_RTTIEXT(TDF_Data,Standard_Transient)
47 #undef DEB_DELTA_CREATION
48 #undef TDF_DATA_COMMIT_OPTIMIZED
50 #ifdef OCCT_DEBUG_DELTA
51 #define TDF_Data_DebugModified(ACTION) \
52 std::cout<<"After "<<ACTION<<" #"<<myTransaction+1<<", DF "<<this<<" had "<<myNbTouchedAtt<<" attribute(s) touched. Time = "<<myTime<<std::endl; \
53 if (!myTransaction) { \
54 TCollection_AsciiString entry; \
55 for (TDF_ChildIterator itr(Root(),Standard_True); itr.More(); itr.Next()) { \
56 const TDF_LabelNode* lnp = itr.Value().myLabelNode; \
57 if (lnp->AttributesModified() || lnp->MayBeModified()) { \
58 TDF_Tool::Entry(itr.Value(),entry); \
59 std::cout<<ACTION<<" on "<<entry<<" : flag(s) "; \
60 if (lnp->AttributesModified()) std::cout<<"AttributesModified "; \
61 if (lnp->MayBeModified()) std::cout<<"MayBeModified already set in transaction 0! Please contact TDF developer."; \
62 std::cout<<std::endl; \
63 std::cout<<itr.Value()<<std::endl; \
67 #define TDF_Data_DebugModified(ACTION)
70 #ifdef OCCT_DEBUG_DELTA_CREATION
71 #define TDF_DataDebugDeltaCreation(DELTATYPE) \
73 TCollection_AsciiString entry; \
74 TDF_Tool::Entry(currentAtt->Label(),entry); \
75 std::cout<<"Creation of a DeltaOn"<<DELTATYPE<<" \tat "<<entry<<" \ton "<<currentAtt->DynamicType()<<std::endl; \
78 #define TDF_DataDebugDeltaCreation(DELTATYPE)
81 #define TDF_Data_DeltaCreation(DELTACOMMENT,DELTACREATION) \
83 TDF_DataDebugDeltaCreation(DELTACOMMENT); \
84 aDelta->AddAttributeDelta(DELTACREATION); \
87 //=======================================================================
89 //purpose : empty constructor
90 //=======================================================================
92 TDF_Data::TDF_Data() :
95 myNotUndoMode (Standard_True),
97 myAllowModification (Standard_True)
99 const Handle(NCollection_IncAllocator) anIncAllocator=
100 new NCollection_IncAllocator (16000);
101 myLabelNodeAllocator = anIncAllocator;
102 myRoot = new (anIncAllocator) TDF_LabelNode (this);
105 //=======================================================================
107 //purpose : Used to implement the destructor ~.
108 //=======================================================================
110 void TDF_Data::Destroy()
112 AbortUntilTransaction(1);
113 myRoot->Destroy (myLabelNodeAllocator);
118 //=======================================================================
119 //function : OpenTransaction
121 //=======================================================================
123 Standard_Integer TDF_Data::OpenTransaction()
125 myTimes.Prepend(myTime);
126 return ++myTransaction;
130 //=======================================================================
131 //function : CommitTransaction
132 //purpose : Commits the current transaction.
133 //=======================================================================
135 Handle(TDF_Delta) TDF_Data::CommitTransaction
136 (const Standard_Boolean withDelta)
138 Handle(TDF_Delta) delta;
139 if (myTransaction>0) {
140 if (withDelta) delta = new TDF_Delta();
141 #ifdef OCCT_DEBUG_DELTA
142 std::cout<<"TDF_Data::Begin Commit #"<<myTransaction<<std::endl;
144 #ifdef TDF_DATA_COMMIT_OPTIMIZED
146 if (Root().myLabelNode->MayBeModified())
149 TDF_Data::CommitTransaction(Root(),delta,withDelta);
151 if (myNbTouchedAtt && !(withDelta && delta->IsEmpty())) ++myTime;
154 if (!delta->IsEmpty()) {
155 delta->Validity(myTimes.First(),myTime);
156 #ifdef OCCT_DEBUG_DELTA
157 if (myTransaction == 0) {
158 std::cout<<"TDF_Data::Commit generated this delta in t=0:"<<std::endl;
159 delta->Dump(std::cout);
163 #ifdef OCCT_DEBUG_DELTA
165 if (myTransaction == 0)
166 std::cout<<"TDF_Data::Commit generated NO delta."<<std::endl;
170 myTimes.RemoveFirst();
172 TDF_Data_DebugModified("COMMIT");
177 //=======================================================================
178 //function : CommitUntilTransaction
179 //purpose : Commits the transactions until AND including
180 // the given transaction index.
181 //=======================================================================
183 Handle(TDF_Delta) TDF_Data::CommitUntilTransaction
184 (const Standard_Integer untilTransaction,
185 const Standard_Boolean withDelta)
187 Handle(TDF_Delta) delta;
188 if ((untilTransaction>0) && (myTransaction >= untilTransaction)) {
189 while (myTransaction > untilTransaction) {
190 delta = TDF_Data::CommitTransaction(Standard_False);
192 delta = TDF_Data::CommitTransaction(withDelta);
198 //=======================================================================
199 //function : CommitTransaction
200 //purpose : Recursive method used to implement the commit action.
201 //=======================================================================
203 Standard_Integer TDF_Data::CommitTransaction
204 (const TDF_Label& aLabel,
205 const Handle(TDF_Delta)& aDelta,
206 const Standard_Boolean withDelta)
208 aLabel.myLabelNode->MayBeModified(Standard_False);
209 Standard_Integer nbTouchedAtt = 0;
210 #ifdef TDF_DATA_COMMIT_OPTIMIZED
211 Standard_Boolean attMod = aLabel.myLabelNode->AttributesModified();
213 Standard_Boolean attMod = Standard_True;
217 Handle(TDF_Attribute) lastAtt;
218 Handle(TDF_Attribute) backupAtt;
219 Standard_Boolean currentIsRemoved = Standard_False;
220 attMod = Standard_False;
222 TDF_AttributeIterator itr1(aLabel, Standard_False);
223 while (itr1.More()) {
224 Handle(TDF_Attribute) aPtrCurrentAtt = itr1.Value();
226 // currentAtt = itr1.Value();
229 aPtrCurrentAtt->BeforeCommitTransaction();
231 backupAtt = aPtrCurrentAtt->myBackup;
233 if (aPtrCurrentAtt->myTransaction == myTransaction) {
235 --(aPtrCurrentAtt->myTransaction);
237 // ------------------------------------------------------- Forgotten
238 if (aPtrCurrentAtt->IsForgotten()) {
239 if (aPtrCurrentAtt->mySavedTransaction >=
240 aPtrCurrentAtt->myTransaction)
242 const Handle(TDF_Attribute) currentAtt = aPtrCurrentAtt;
243 // Collision with a not forgotten version.
244 if (backupAtt.IsNull()) {
245 TDF_Data_DeltaCreation
247 currentAtt->DeltaOnRemoval());
248 if (myNotUndoMode) currentAtt->BeforeRemoval();
249 aLabel.myLabelNode->RemoveAttribute(lastAtt,currentAtt);
250 currentIsRemoved = Standard_True;
251 attMod = Standard_True;
254 // Modified then Forgotten...
255 // Forgotten flag spreading?
256 currentAtt->Resume();
257 currentAtt->Restore(backupAtt);
258 currentAtt->myTransaction = backupAtt->myTransaction;
259 currentAtt->RemoveBackup();
260 backupAtt = currentAtt->myBackup;
261 if (myTransaction == 1) {
262 TDF_Data_DeltaCreation
264 currentAtt->DeltaOnRemoval());
265 if (myNotUndoMode) currentAtt->BeforeRemoval();
266 aLabel.myLabelNode->RemoveAttribute(lastAtt,currentAtt);
267 currentIsRemoved = Standard_True;
270 // BeforeForget has already been called once.
271 // if (myNotUndoMode) currentAtt->BeforeForget();
272 currentAtt->Forget(myTransaction-1);
273 TDF_Data_DeltaCreation
275 currentAtt->DeltaOnForget());
276 attMod = Standard_True;
281 // Forgotten in lower transaction than the current one.
282 TDF_Data_DeltaCreation
284 aPtrCurrentAtt->DeltaOnForget());
287 // ---------------------------------------------------------- Resumed.
288 else if (aPtrCurrentAtt->mySavedTransaction < 0) {
289 TDF_Data_DeltaCreation
291 aPtrCurrentAtt->DeltaOnResume());
292 aPtrCurrentAtt->mySavedTransaction = 0;
293 attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
296 // ------------------------------------------------------------ Added.
297 else if (backupAtt.IsNull()) {
298 TDF_Data_DeltaCreation
300 aPtrCurrentAtt->DeltaOnAddition());
301 attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
303 // --------------------------------------------------------- Modified.
305 const TDF_Attribute* anAttrPtr = aPtrCurrentAtt.operator->(); // to avoid ambiguity
306 TDF_Data_DeltaCreation
308 anAttrPtr->DeltaOnModification(backupAtt));
309 if (aPtrCurrentAtt->myTransaction == backupAtt->myTransaction)
310 aPtrCurrentAtt->RemoveBackup();
311 attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
315 else attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
317 if (currentIsRemoved) currentIsRemoved = Standard_False;
318 else lastAtt = aPtrCurrentAtt;
320 aLabel.myLabelNode->AttributesModified(attMod);
323 // Iteration on the children to do the same!
324 //------------------------------------------
325 for (TDF_ChildIterator itr2(aLabel); itr2.More(); itr2.Next()) {
326 #ifdef TDF_DATA_COMMIT_OPTIMIZED
327 if (itr2.Value().myLabelNode->MayBeModified())
330 TDF_Data::CommitTransaction(itr2.Value(),aDelta,withDelta);
337 //=======================================================================
338 //function : AbortTransaction
339 //purpose : Aborts the current transaction.
340 //=======================================================================
342 void TDF_Data::AbortTransaction()
345 Undo(TDF_Data::CommitTransaction(Standard_True),Standard_False);
346 TDF_Data_DebugModified("New ABORT");
350 //=======================================================================
351 //function : AbortUntilTransaction
352 //purpose : Aborts the transactions until AND including the given index.
353 //=======================================================================
355 void TDF_Data::AbortUntilTransaction(const Standard_Integer untilTransaction)
357 if (untilTransaction>0)
358 Undo(TDF_Data::CommitUntilTransaction(untilTransaction,Standard_True),Standard_False);
362 //=======================================================================
363 //function : IsApplicable
365 //=======================================================================
367 Standard_Boolean TDF_Data::IsApplicable
368 (const Handle(TDF_Delta)& aDelta) const
370 return !aDelta.IsNull() && aDelta->IsApplicable(myTime);
373 //=======================================================================
374 //function : FixOrder
376 //=======================================================================
377 void TDF_Data::FixOrder(const Handle(TDF_Delta)& theDelta)
379 // make all OnRemoval (which will cause addition of the attribute) are in the end
380 // to do not put two attributes with the same GUID at one label during undo/redo
381 TDF_AttributeDeltaList anOrderedList;
383 const TDF_AttributeDeltaList& attList = theDelta->AttributeDeltas();
384 TDF_ListIteratorOfAttributeDeltaList anIt(attList);
385 for (; anIt.More(); anIt.Next()) { // append not-removal
386 Handle(TDF_AttributeDelta) attDelta = anIt.Value();
387 if (!attDelta->IsKind(STANDARD_TYPE(TDF_DeltaOnRemoval))) {
388 anOrderedList.Append(attDelta);
391 for (anIt.Initialize(attList); anIt.More(); anIt.Next()) { // append removal
392 Handle(TDF_AttributeDelta) attDelta = anIt.Value();
393 if (attDelta->IsKind(STANDARD_TYPE(TDF_DeltaOnRemoval))) {
394 anOrderedList.Append(attDelta);
397 theDelta->ReplaceDeltaList(anOrderedList);
399 //=======================================================================
401 //purpose : Applies a delta to undo actions.
402 //=======================================================================
404 Handle(TDF_Delta) TDF_Data::Undo(const Handle(TDF_Delta)& aDelta,
405 const Standard_Boolean withDelta)
407 Handle(TDF_Delta) newDelta;
408 if (!aDelta.IsNull ()) {
409 if (aDelta->IsApplicable(myTime)) {
410 if (withDelta) OpenTransaction();
411 #ifdef OCCT_DEBUG_DELTA
412 std::cout<<"TDF_Data::Undo applies this delta:"<<std::endl;
413 aDelta->Dump(std::cout);
415 aDelta->BeforeOrAfterApply(Standard_True);
416 myNotUndoMode = Standard_False;
419 myNotUndoMode = Standard_True;
421 newDelta = CommitTransaction(Standard_True);
422 newDelta->Validity(aDelta->EndTime(),aDelta->BeginTime());
423 #ifdef OCCT_DEBUG_DELTA
424 std::cout<<"TDF_Data::Undo, after validity correction, Delta is now available from time \t#"<<newDelta->BeginTime()<<" to time \t#"<<newDelta->EndTime()<<std::endl;
427 myTime = aDelta->BeginTime();
428 aDelta->BeforeOrAfterApply(Standard_False);
436 //=======================================================================
439 //=======================================================================
441 Standard_OStream& TDF_Data::Dump(Standard_OStream& anOS) const
443 anOS<<"Dump of a TDF_Data."<<std::endl;
444 anOS<<"Current transaction: "<<myTransaction;
445 anOS<<"; Current tick: "<<myTime<<";"<<std::endl;
449 //=======================================================================
450 //function : DumpJson
452 //=======================================================================
453 void TDF_Data::DumpJson (Standard_OStream& theOStream, Standard_Integer /*theDepth*/) const
455 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
457 TCollection_AsciiString aStrForTDF_Label;
458 TDF_Tool::Entry (myRoot, aStrForTDF_Label);
459 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aStrForTDF_Label)
461 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTransaction)
462 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myNbTouchedAtt)
463 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myNotUndoMode)
464 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTime)
465 for (TColStd_ListOfInteger::Iterator aTimeIt (myTimes); aTimeIt.More(); aTimeIt.Next())
467 const Standard_Integer aTime = aTimeIt.Value();
468 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aTime)
470 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAllowModification)