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_Type.hxx>
20 #include <Standard_GUID.hxx>
21 #include <NCollection_Array1.hxx>
22 #include <TCollection_AsciiString.hxx>
23 #include <TDF_AttributeDelta.hxx>
24 #include <TDF_AttributeIterator.hxx>
25 #include <TDF_ChildIterator.hxx>
26 #include <TDF_Data.hxx>
27 #include <TDF_Delta.hxx>
28 #include <TDF_DeltaOnAddition.hxx>
29 #include <TDF_DeltaOnForget.hxx>
30 #include <TDF_DeltaOnModification.hxx>
31 #include <TDF_DeltaOnRemoval.hxx>
32 #include <TDF_DeltaOnResume.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDF_LabelNode.hxx>
35 #include <TDF_Tool.hxx>
37 typedef NCollection_Array1<Handle(TDF_AttributeDelta)> TDF_Array1OfAttributeIDelta;
39 IMPLEMENT_STANDARD_RTTIEXT(TDF_Data,Standard_Transient)
41 #undef DEB_DELTA_CREATION
42 #define TDF_DATA_COMMIT_OPTIMIZED
44 #ifdef OCCT_DEBUG_DELTA
45 #define TDF_Data_DebugModified(ACTION) \
46 std::cout<<"After "<<ACTION<<" #"<<myTransaction+1<<", DF "<<this<<" had "<<myNbTouchedAtt<<" attribute(s) touched. Time = "<<myTime<<std::endl; \
47 if (!myTransaction) { \
48 TCollection_AsciiString entry; \
49 for (TDF_ChildIterator itr(Root(),Standard_True); itr.More(); itr.Next()) { \
50 const TDF_LabelNode* lnp = itr.Value().myLabelNode; \
51 if (lnp->AttributesModified() || lnp->MayBeModified()) { \
52 TDF_Tool::Entry(itr.Value(),entry); \
53 std::cout<<ACTION<<" on "<<entry<<" : flag(s) "; \
54 if (lnp->AttributesModified()) std::cout<<"AttributesModified "; \
55 if (lnp->MayBeModified()) std::cout<<"MayBeModified already set in transaction 0! Please contact TDF developer."; \
56 std::cout<<std::endl; \
57 std::cout<<itr.Value()<<std::endl; \
61 #define TDF_Data_DebugModified(ACTION)
64 #ifdef OCCT_DEBUG_DELTA_CREATION
65 #define TDF_DataDebugDeltaCreation(DELTATYPE) \
67 TCollection_AsciiString entry; \
68 TDF_Tool::Entry(currentAtt->Label(),entry); \
69 std::cout<<"Creation of a DeltaOn"<<DELTATYPE<<" \tat "<<entry<<" \ton "<<currentAtt->DynamicType()<<std::endl; \
72 #define TDF_DataDebugDeltaCreation(DELTATYPE)
75 #define TDF_Data_DeltaCreation(DELTACOMMENT,DELTACREATION) \
77 TDF_DataDebugDeltaCreation(DELTACOMMENT); \
78 aDelta->AddAttributeDelta(DELTACREATION); \
81 //=======================================================================
83 //purpose : empty constructor
84 //=======================================================================
86 TDF_Data::TDF_Data() :
89 myNotUndoMode (Standard_True),
91 myAllowModification (Standard_True),
92 myAccessByEntries (Standard_False)
94 const Handle(NCollection_IncAllocator) anIncAllocator=
95 new NCollection_IncAllocator (16000);
96 myLabelNodeAllocator = anIncAllocator;
97 myRoot = new (anIncAllocator) TDF_LabelNode (this);
100 //=======================================================================
102 //purpose : Used to implement the destructor ~.
103 //=======================================================================
105 void TDF_Data::Destroy()
107 AbortUntilTransaction(1);
108 // Forget the Owner attribute from the root label to avoid referencing document before
109 // desctuction of the framework (on custom attributes forget). Don't call ForgetAll because
110 // it may call backup.
111 while(!myRoot->FirstAttribute().IsNull()) {
112 static Handle(TDF_Attribute) anEmpty;
113 Handle(TDF_Attribute) aFirst = myRoot->FirstAttribute();
114 myRoot->RemoveAttribute(anEmpty, aFirst);
116 myAccessByEntriesTable.Clear();
117 myRoot->Destroy (myLabelNodeAllocator);
122 //=======================================================================
123 //function : OpenTransaction
125 //=======================================================================
127 Standard_Integer TDF_Data::OpenTransaction()
129 myTimes.Prepend(myTime);
130 return ++myTransaction;
134 //=======================================================================
135 //function : CommitTransaction
136 //purpose : Commits the current transaction.
137 //=======================================================================
139 Handle(TDF_Delta) TDF_Data::CommitTransaction
140 (const Standard_Boolean withDelta)
142 Handle(TDF_Delta) delta;
143 if (myTransaction>0) {
144 if (withDelta) delta = new TDF_Delta();
145 #ifdef OCCT_DEBUG_DELTA
146 std::cout<<"TDF_Data::Begin Commit #"<<myTransaction<<std::endl;
148 #ifdef TDF_DATA_COMMIT_OPTIMIZED
150 if (Root().myLabelNode->MayBeModified())
153 TDF_Data::CommitTransaction(Root(),delta,withDelta);
155 if (myNbTouchedAtt && !(withDelta && delta->IsEmpty())) ++myTime;
158 if (!delta->IsEmpty()) {
159 delta->Validity(myTimes.First(),myTime);
160 #ifdef OCCT_DEBUG_DELTA
161 if (myTransaction == 0) {
162 std::cout<<"TDF_Data::Commit generated this delta in t=0:"<<std::endl;
163 delta->Dump(std::cout);
167 #ifdef OCCT_DEBUG_DELTA
169 if (myTransaction == 0)
170 std::cout<<"TDF_Data::Commit generated NO delta."<<std::endl;
174 myTimes.RemoveFirst();
176 TDF_Data_DebugModified("COMMIT");
181 //=======================================================================
182 //function : CommitUntilTransaction
183 //purpose : Commits the transactions until AND including
184 // the given transaction index.
185 //=======================================================================
187 Handle(TDF_Delta) TDF_Data::CommitUntilTransaction
188 (const Standard_Integer untilTransaction,
189 const Standard_Boolean withDelta)
191 Handle(TDF_Delta) delta;
192 if ((untilTransaction>0) && (myTransaction >= untilTransaction)) {
193 while (myTransaction > untilTransaction) {
194 delta = TDF_Data::CommitTransaction(Standard_False);
196 delta = TDF_Data::CommitTransaction(withDelta);
202 //=======================================================================
203 //function : CommitTransaction
204 //purpose : Recursive method used to implement the commit action.
205 //=======================================================================
207 Standard_Integer TDF_Data::CommitTransaction
208 (const TDF_Label& aLabel,
209 const Handle(TDF_Delta)& aDelta,
210 const Standard_Boolean withDelta)
212 aLabel.myLabelNode->MayBeModified(Standard_False);
213 Standard_Integer nbTouchedAtt = 0;
214 #ifdef TDF_DATA_COMMIT_OPTIMIZED
215 Standard_Boolean attMod = aLabel.myLabelNode->AttributesModified();
217 Standard_Boolean attMod = Standard_True;
221 Handle(TDF_Attribute) lastAtt;
222 Handle(TDF_Attribute) backupAtt;
223 Standard_Boolean currentIsRemoved = Standard_False;
224 attMod = Standard_False;
226 TDF_AttributeIterator itr1(aLabel, Standard_False);
227 while (itr1.More()) {
228 Handle(TDF_Attribute) aPtrCurrentAtt = itr1.Value();
230 // currentAtt = itr1.Value();
233 aPtrCurrentAtt->BeforeCommitTransaction();
235 backupAtt = aPtrCurrentAtt->myBackup;
237 if (aPtrCurrentAtt->myTransaction == myTransaction) {
239 --(aPtrCurrentAtt->myTransaction);
241 // ------------------------------------------------------- Forgotten
242 if (aPtrCurrentAtt->IsForgotten()) {
243 if (aPtrCurrentAtt->mySavedTransaction >=
244 aPtrCurrentAtt->myTransaction)
246 const Handle(TDF_Attribute) currentAtt = aPtrCurrentAtt;
247 // Collision with a not forgotten version.
248 if (backupAtt.IsNull()) {
249 TDF_Data_DeltaCreation
251 currentAtt->DeltaOnRemoval());
252 if (myNotUndoMode) currentAtt->BeforeRemoval();
253 aLabel.myLabelNode->RemoveAttribute(lastAtt,currentAtt);
254 currentIsRemoved = Standard_True;
255 attMod = Standard_True;
258 // Modified then Forgotten...
259 // Forgotten flag spreading?
260 currentAtt->Resume();
261 currentAtt->Restore(backupAtt);
262 currentAtt->myTransaction = backupAtt->myTransaction;
263 currentAtt->RemoveBackup();
264 backupAtt = currentAtt->myBackup;
265 if (myTransaction == 1) {
266 TDF_Data_DeltaCreation
268 currentAtt->DeltaOnRemoval());
269 if (myNotUndoMode) currentAtt->BeforeRemoval();
270 aLabel.myLabelNode->RemoveAttribute(lastAtt,currentAtt);
271 currentIsRemoved = Standard_True;
274 // BeforeForget has already been called once.
275 // if (myNotUndoMode) currentAtt->BeforeForget();
276 currentAtt->Forget(myTransaction-1);
277 TDF_Data_DeltaCreation
279 currentAtt->DeltaOnForget());
280 attMod = Standard_True;
285 // Forgotten in lower transaction than the current one.
286 TDF_Data_DeltaCreation
288 aPtrCurrentAtt->DeltaOnForget());
291 // ---------------------------------------------------------- Resumed.
292 else if (aPtrCurrentAtt->mySavedTransaction < 0) {
293 TDF_Data_DeltaCreation
295 aPtrCurrentAtt->DeltaOnResume());
296 aPtrCurrentAtt->mySavedTransaction = 0;
297 attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
300 // ------------------------------------------------------------ Added.
301 else if (backupAtt.IsNull()) {
302 TDF_Data_DeltaCreation
304 aPtrCurrentAtt->DeltaOnAddition());
305 attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
307 // --------------------------------------------------------- Modified.
309 const TDF_Attribute* anAttrPtr = aPtrCurrentAtt.operator->(); // to avoid ambiguity
310 TDF_Data_DeltaCreation
312 anAttrPtr->DeltaOnModification(backupAtt));
313 if (aPtrCurrentAtt->myTransaction == backupAtt->myTransaction)
314 aPtrCurrentAtt->RemoveBackup();
315 attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
319 else attMod = attMod || (aPtrCurrentAtt->myTransaction > 0);
321 if (currentIsRemoved) currentIsRemoved = Standard_False;
322 else lastAtt = aPtrCurrentAtt;
324 aLabel.myLabelNode->AttributesModified(attMod);
327 // Iteration on the children to do the same!
328 //------------------------------------------
329 for (TDF_ChildIterator itr2(aLabel); itr2.More(); itr2.Next()) {
330 #ifdef TDF_DATA_COMMIT_OPTIMIZED
331 if (itr2.Value().myLabelNode->MayBeModified())
334 TDF_Data::CommitTransaction(itr2.Value(),aDelta,withDelta);
341 //=======================================================================
342 //function : AbortTransaction
343 //purpose : Aborts the current transaction.
344 //=======================================================================
346 void TDF_Data::AbortTransaction()
349 Undo(TDF_Data::CommitTransaction(Standard_True),Standard_False);
350 TDF_Data_DebugModified("New ABORT");
354 //=======================================================================
355 //function : AbortUntilTransaction
356 //purpose : Aborts the transactions until AND including the given index.
357 //=======================================================================
359 void TDF_Data::AbortUntilTransaction(const Standard_Integer untilTransaction)
361 if (untilTransaction>0)
362 Undo(TDF_Data::CommitUntilTransaction(untilTransaction,Standard_True),Standard_False);
366 //=======================================================================
367 //function : IsApplicable
369 //=======================================================================
371 Standard_Boolean TDF_Data::IsApplicable
372 (const Handle(TDF_Delta)& aDelta) const
374 return !aDelta.IsNull() && aDelta->IsApplicable(myTime);
377 //=======================================================================
378 //function : FixOrder
380 //=======================================================================
381 void TDF_Data::FixOrder(const Handle(TDF_Delta)& theDelta)
383 // make all OnRemoval (which will cause addition of the attribute) are in the end
384 // to do not put two attributes with the same GUID at one label during undo/redo
385 TDF_AttributeDeltaList anOrderedList;
387 const TDF_AttributeDeltaList& attList = theDelta->AttributeDeltas();
388 TDF_ListIteratorOfAttributeDeltaList anIt(attList);
389 for (; anIt.More(); anIt.Next()) { // append not-removal
390 Handle(TDF_AttributeDelta) attDelta = anIt.Value();
391 if (!attDelta->IsKind(STANDARD_TYPE(TDF_DeltaOnRemoval))) {
392 anOrderedList.Append(attDelta);
395 for (anIt.Initialize(attList); anIt.More(); anIt.Next()) { // append removal
396 Handle(TDF_AttributeDelta) attDelta = anIt.Value();
397 if (attDelta->IsKind(STANDARD_TYPE(TDF_DeltaOnRemoval))) {
398 anOrderedList.Append(attDelta);
401 theDelta->ReplaceDeltaList(anOrderedList);
403 //=======================================================================
405 //purpose : Applies a delta to undo actions.
406 //=======================================================================
408 Handle(TDF_Delta) TDF_Data::Undo(const Handle(TDF_Delta)& aDelta,
409 const Standard_Boolean withDelta)
411 Handle(TDF_Delta) newDelta;
412 if (!aDelta.IsNull ()) {
413 if (aDelta->IsApplicable(myTime)) {
414 if (withDelta) OpenTransaction();
415 #ifdef OCCT_DEBUG_DELTA
416 std::cout<<"TDF_Data::Undo applies this delta:"<<std::endl;
417 aDelta->Dump(std::cout);
419 aDelta->BeforeOrAfterApply(Standard_True);
420 myNotUndoMode = Standard_False;
423 myNotUndoMode = Standard_True;
425 newDelta = CommitTransaction(Standard_True);
426 newDelta->Validity(aDelta->EndTime(),aDelta->BeginTime());
427 #ifdef OCCT_DEBUG_DELTA
428 std::cout<<"TDF_Data::Undo, after validity correction, Delta is now available from time \t#"<<newDelta->BeginTime()<<" to time \t#"<<newDelta->EndTime()<<std::endl;
431 myTime = aDelta->BeginTime();
432 aDelta->BeforeOrAfterApply(Standard_False);
438 //=======================================================================
439 //function : SetAccessByEntries
441 //=======================================================================
443 void TDF_Data::SetAccessByEntries(const Standard_Boolean aSet)
445 myAccessByEntries = aSet;
447 myAccessByEntriesTable.Clear();
448 if (myAccessByEntries) {
450 TCollection_AsciiString anEntry;
451 TDF_Tool::Entry (myRoot, anEntry);
452 myAccessByEntriesTable.Bind (anEntry, myRoot);
454 // Add all other labels.
455 TDF_ChildIterator itr (myRoot, Standard_True);
456 for (; itr.More(); itr.Next()) {
457 const TDF_Label aLabel = itr.Value();
458 TDF_Tool::Entry (aLabel, anEntry);
459 myAccessByEntriesTable.Bind (anEntry, aLabel);
464 //=======================================================================
465 //function : RegisterLabel
467 //=======================================================================
469 void TDF_Data::RegisterLabel(const TDF_Label& aLabel)
471 TCollection_AsciiString anEntry;
472 TDF_Tool::Entry (aLabel, anEntry);
473 myAccessByEntriesTable.Bind (anEntry, aLabel);
476 //=======================================================================
479 //=======================================================================
481 Standard_OStream& TDF_Data::Dump(Standard_OStream& anOS) const
483 anOS<<"Dump of a TDF_Data."<<std::endl;
484 anOS<<"Current transaction: "<<myTransaction;
485 anOS<<"; Current tick: "<<myTime<<";"<<std::endl;
489 //=======================================================================
490 //function : DumpJson
492 //=======================================================================
493 void TDF_Data::DumpJson (Standard_OStream& theOStream, Standard_Integer /*theDepth*/) const
495 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
497 TCollection_AsciiString aStrForTDF_Label;
498 TDF_Tool::Entry (myRoot, aStrForTDF_Label);
499 OCCT_DUMP_FIELD_VALUE_STRING (theOStream, aStrForTDF_Label)
501 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTransaction)
502 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myNbTouchedAtt)
503 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myNotUndoMode)
504 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTime)
505 for (TColStd_ListOfInteger::Iterator aTimeIt (myTimes); aTimeIt.More(); aTimeIt.Next())
507 const Standard_Integer aTime = aTimeIt.Value();
508 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, aTime)
510 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAllowModification)