1 // Created by: DAUTRY Philippe
2 // Copyright (c) 1997-1999 Matra Datavision
3 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef _TDF_Data_HeaderFile
17 #define _TDF_Data_HeaderFile
18 
19 #include <Standard.hxx>
20 #include <Standard_Type.hxx>
21 
22 #include <TDF_LabelNodePtr.hxx>
23 #include <Standard_Integer.hxx>
24 #include <Standard_Boolean.hxx>
25 #include <TColStd_ListOfInteger.hxx>
26 #include <TDF_HAllocator.hxx>
27 #include <Standard_Transient.hxx>
28 #include <TDF_Label.hxx>
29 #include <Standard_OStream.hxx>
30 #include <NCollection_DataMap.hxx>
31 class Standard_NoMoreObject;
32 class TDF_Transaction;
33 class TDF_LabelNode;
34 class TDF_Delta;
35 class TDF_Label;
36 
37 
38 class TDF_Data;
39 DEFINE_STANDARD_HANDLE(TDF_Data, Standard_Transient)
40 
41 //! This class is used to manipulate a complete independent,
42 //! self sufficient data structure and its services:
43 //!
44 //! Access to the root label;
45 //!
46 //! Opens, aborts, commits a transaction;
47 //!
48 //! Generation and use of Delta, depending on the time.
49 //! This class uses a special allocator
50 //! (see LabelNodeAllocator() method)
51 //! for more efficient allocation of objects in memory.
52 class TDF_Data : public Standard_Transient
53 {
54 
55 public:
56 
57 
58   //! A new and empty Data structure.
59   Standard_EXPORT TDF_Data();
60 
61   //! Returns the root label of the Data structure.
62     const TDF_Label Root() const;
63 
64   //! Returns the current transaction number.
65   Standard_Integer Transaction() const;
66 
67   //! Returns the current tick. It is incremented each Commit.
68     Standard_Integer Time() const;
69 
70   //! Returns true if <aDelta> is applicable HERE and NOW.
71   Standard_EXPORT Standard_Boolean IsApplicable (const Handle(TDF_Delta)& aDelta) const;
72 
73   //! Apply <aDelta> to undo a set of attribute modifications.
74   //!
75   //! Optional <withDelta> set to True indicates a
76   //! Delta Set must be generated. (See above)
77   Standard_EXPORT Handle(TDF_Delta) Undo (const Handle(TDF_Delta)& aDelta, const Standard_Boolean withDelta = Standard_False);
78 
79   Standard_EXPORT void Destroy();
~TDF_Data()80 ~TDF_Data()
81 {
82   Destroy();
83 }
84 
85   //! Returns the undo mode status.
86     Standard_Boolean NotUndoMode() const;
87 
88   //! Dumps the Data on <aStream>.
89     Standard_EXPORT Standard_OStream& Dump (Standard_OStream& anOS) const;
operator <<(Standard_OStream & anOS) const90 Standard_OStream& operator<< (Standard_OStream& anOS) const
91 {
92   return Dump(anOS);
93 }
94 
95   //! Sets modification mode.
96     void AllowModification (const Standard_Boolean isAllowed);
97 
98   //! returns modification mode.
99     Standard_Boolean IsModificationAllowed() const;
100 
101   //! Initializes a mechanism for fast access to the labels by their entries.
102   //! The fast access is useful for large documents and often access to the labels
103   //! via entries. Internally, a table of entry - label is created,
104   //! which allows to obtain a label by its entry in a very fast way.
105   //! If the mechanism is turned off, the internal table is cleaned.
106   //! New labels are added to the table, if the mechanism is on
107   //! (no need to re-initialize the mechanism).
108   Standard_EXPORT void SetAccessByEntries (const Standard_Boolean aSet);
109 
110   //! Returns a status of mechanism for fast access to the labels via entries.
IsAccessByEntries() const111   Standard_Boolean IsAccessByEntries() const { return myAccessByEntries; }
112 
113   //! Returns a label by an entry.
114   //! Returns Standard_False, if such a label doesn't exist
115   //! or mechanism for fast access to the label by entry is not initialized.
GetLabel(const TCollection_AsciiString & anEntry,TDF_Label & aLabel)116   Standard_Boolean GetLabel (const TCollection_AsciiString& anEntry, TDF_Label& aLabel) { return myAccessByEntriesTable.Find(anEntry, aLabel); }
117 
118   //! An internal method. It is used internally on creation of new labels.
119   //! It adds a new label into internal table for fast access to the labels by entry.
120   Standard_EXPORT void RegisterLabel (const TDF_Label& aLabel);
121 
122   //! Returns TDF_HAllocator, which is an
123   //! incremental allocator used by
124   //! TDF_LabelNode.
125   //! This allocator is used to
126   //! manage TDF_LabelNode objects,
127   //! but it can also be used for
128   //! allocating memory to
129   //! application-specific data (be
130   //! careful because this
131   //! allocator does not release
132   //! the memory).
133   //! The benefits of this
134   //! allocation scheme are
135   //! noticeable when dealing with
136   //! large OCAF documents, due to:
137   //! 1.    Very quick allocation of
138   //! objects (memory heap is not
139   //! used, the algorithm that
140   //! replaces it is very simple).
141   //! 2.    Very quick destruction of
142   //! objects (memory is released not
143   //! by destructors of TDF_LabelNode,
144   //! but rather by the destructor of
145   //! TDF_Data).
146   //! 3.  TDF_LabelNode objects do not
147   //! fragmentize the memory; they are
148   //! kept compactly in a number of
149   //! arrays of 16K each.
150   //! 4.    Swapping is reduced on large
151   //! data, because each document now
152   //! occupies a smaller number of
153   //! memory pages.
154     const TDF_HAllocator& LabelNodeAllocator() const;
155 
156   //! Dumps the content of me into the stream
157   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
158 
159 friend class TDF_Transaction;
160 friend class TDF_LabelNode;
161 
162 
163   DEFINE_STANDARD_RTTIEXT(TDF_Data,Standard_Transient)
164 
165 protected:
166 
167 
168 
169 
170 private:
171 
172   //! Fixes order of Attributes' Deltas to perform undo/redo without exceptions:
173   //! puts OnRemoval deltas to the end of the list.
174   void FixOrder(const Handle(TDF_Delta)& theDelta);
175 
176   //! Increments the transaction number and returns it.
177   Standard_EXPORT Standard_Integer OpenTransaction();
178 
179   //! Decrements the transaction number and commits the
180   //! modifications.
181   //!
182   //! Raises if there is no current transaction.
183   //!
184   //! Optional <withDelta> set to True indicates a
185   //! Delta must be generated.
186   Standard_EXPORT Handle(TDF_Delta) CommitTransaction (const Standard_Boolean withDelta = Standard_False);
187 
188   //! Decrements the transaction number and commits the
189   //! modifications until AND including the transaction
190   //! <untilTransaction>.
191   Standard_EXPORT Handle(TDF_Delta) CommitUntilTransaction (const Standard_Integer untilTransaction, const Standard_Boolean withDelta = Standard_False);
192 
193   //! Decrements the transaction number and forgets the
194   //! modifications.
195   //!
196   //! Raises if there is no current transaction.
197   Standard_EXPORT void AbortTransaction();
198 
199   //! Decrements the transaction number and forgets the
200   //! modifications until AND including the transaction
201   //! <untilTransaction>.
202   Standard_EXPORT void AbortUntilTransaction (const Standard_Integer untilTransaction);
203 
204   //! Decrements the transaction number and commits the
205   //! modifications. Used to implement the recursif
206   //! commit process. The returned boolean says how many
207   //! attributes (new, modified or deleted) has been
208   //! committed from the previous transaction into the
209   //! current one.
210   Standard_EXPORT Standard_Integer CommitTransaction (const TDF_Label& aLabel, const Handle(TDF_Delta)& aDelta, const Standard_Boolean withDelta);
211 
212   TDF_LabelNodePtr myRoot;
213   Standard_Integer myTransaction;
214   Standard_Integer myNbTouchedAtt;
215   Standard_Boolean myNotUndoMode;
216   Standard_Integer myTime;
217   TColStd_ListOfInteger myTimes;
218   TDF_HAllocator myLabelNodeAllocator;
219   Standard_Boolean myAllowModification;
220   Standard_Boolean myAccessByEntries;
221   NCollection_DataMap<TCollection_AsciiString, TDF_Label> myAccessByEntriesTable;
222 };
223 
224 
225 #include <TDF_Data.lxx>
226 
227 
228 
229 
230 
231 #endif // _TDF_Data_HeaderFile
232