b5cf0dd1c4ef99b4433ee0112d5ccdbb099deb50
[occt.git] / src / Dico / Dico_DictionaryOfTransient.hxx
1 // Created on: 1992-07-28
2 // Created by: Christian CAILLET
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _Dico_DictionaryOfTransient_HeaderFile
18 #define _Dico_DictionaryOfTransient_HeaderFile
19
20 #include <Standard.hxx>
21 #include <Standard_Type.hxx>
22
23 #include <Standard_Character.hxx>
24 #include <MMgt_TShared.hxx>
25 #include <Standard_Boolean.hxx>
26 #include <Standard_CString.hxx>
27 #include <Standard_Size.hxx>
28 #include <Standard_Integer.hxx>
29 class Standard_Transient;
30 class Standard_NoSuchObject;
31 class Dico_IteratorOfDictionaryOfTransient;
32 class Dico_StackItemOfDictionaryOfTransient;
33 class TCollection_AsciiString;
34
35
36 class Dico_DictionaryOfTransient;
37 DEFINE_STANDARD_HANDLE(Dico_DictionaryOfTransient, MMgt_TShared)
38
39
40 class Dico_DictionaryOfTransient : public MMgt_TShared
41 {
42
43 public:
44
45   
46   //! Creates a Dictionary cell.
47   //! A Dictionary is always created then handled by its first cell
48   //! After creating a Dictionary, user can call methods HasItem,
49   //! Item, SetItem ... (internal cells and entries are also
50   //! defined as objects from the same class)
51   //! Also iteration can be made, by an Iterator on this Dictionary
52   Standard_EXPORT Dico_DictionaryOfTransient();
53   
54   //! Returns True if an Item is bound to a Name in the Dictionnary
55   //! <exact> : if True, commands exact matching
56   //! if False, accept completion, only if ONE AND ONLY ONE
57   //! Dictionnary Entry has <name> as beginning of its name
58   Standard_EXPORT Standard_Boolean HasItem (const Standard_CString name, const Standard_Boolean exact = Standard_False) const;
59   
60   //! Works as above method but accepts a String from TCollection
61   Standard_EXPORT Standard_Boolean HasItem (const TCollection_AsciiString& name, const Standard_Boolean exact = Standard_True) const;
62   
63   //! Returns item bound to a name in the Dictionnary
64   //! <exact> : same as for HasItem
65   Standard_EXPORT const Handle(Standard_Transient)& Item (const Standard_CString name, const Standard_Boolean exact = Standard_True) const;
66   
67   //! Works as above method but accepts a String from TCollection
68   Standard_EXPORT const Handle(Standard_Transient)& Item (const TCollection_AsciiString& name, const Standard_Boolean exact = Standard_True) const;
69   
70   //! Gathers HasItem and Item, in a less regular but faster way
71   //! If return is True, <anitem> is returned too, else it is not
72   //! <exact> : same as for HasItem
73   Standard_EXPORT Standard_Boolean GetItem (const Standard_CString name, Handle(Standard_Transient)& anitem, const Standard_Boolean exact = Standard_True) const;
74   
75   //! Safe variant of GetItem() for arbitrary type of argument
76   template <class T> 
77   Standard_Boolean GetItem (const Standard_CString theName, Handle(T)& theItem, const Standard_Boolean theExact = Standard_True) const
78   { 
79     Handle(Standard_Transient) anItem = theItem;
80     return GetItem (theName, anItem, theExact) && ! (theItem = Handle(T)::DownCast(anItem)).IsNull();
81   }
82
83   //! Works as above method but accepts a String from TCollection
84   Standard_EXPORT Standard_Boolean GetItem (const TCollection_AsciiString& name, Handle(Standard_Transient)& anitem, const Standard_Boolean exact = Standard_True) const;
85   
86   //! Safe variant of GetItem() for arbitrary type of argument
87   template <class T> 
88   Standard_Boolean GetItem (const TCollection_AsciiString& theName, Handle(T)& theItem, const Standard_Boolean theExact = Standard_True) const
89   { 
90     Handle(Standard_Transient) anItem = theItem;
91     return GetItem (theName, anItem, theExact) && ! (theItem = Handle(T)::DownCast(anItem)).IsNull();
92   }
93
94   //! Binds an item to a dictionnary entry
95   //! If <name> is already known in the dictionary, its value
96   //! is changed. Else, the dictionary entry is created.
97   //! If <exact> is given False, completion is tried, it is accepted
98   //! If it gives a UNIQUE entry : hence this one will be modified
99   //! Else, new entry is created with the exact name given
100   Standard_EXPORT void SetItem (const Standard_CString name, const Handle(Standard_Transient)& anitem, const Standard_Boolean exact = Standard_True);
101   
102   //! Works as above method but accepts a String from TCollection
103   Standard_EXPORT void SetItem (const TCollection_AsciiString& name, const Handle(Standard_Transient)& anitem, const Standard_Boolean exact = Standard_True);
104   
105   //! Returns the Item AS AN ADDRESS which corresponds to a Name,
106   //! in order to be changed or set.
107   //! If this name is not yet recorded, the Dictionary creates it.
108   //! <isvalued> is returned True if the Item is recorded in the
109   //! Dictionary, False else, in that case the Item is reserved and
110   //! the name is noted as beeing valued now.
111   Standard_EXPORT Handle(Standard_Transient)& NewItem (const Standard_CString name, Standard_Boolean& isvalued, const Standard_Boolean exact = Standard_True);
112   
113   //! Works as above method but accepts a String from TCollection
114   Standard_EXPORT Handle(Standard_Transient)& NewItem (const TCollection_AsciiString& name, Standard_Boolean& isvalued, const Standard_Boolean exact = Standard_True);
115   
116   //! Removes a dictionary entry given by its name then Returns True
117   //! If the entry does not exists, Does nothing then Returns False
118   //! <exact> : as for HasItem, if completion works, the found entry
119   //! is removed (else returned value is False)
120   //! <cln> commands cleaning dictionary (to recover memory space)
121   //! For an isolated call, it is recommanded to give it at True
122   //! For a sequence of calls, rather give False, then call Clean
123   Standard_EXPORT Standard_Boolean RemoveItem (const Standard_CString name, const Standard_Boolean cln = Standard_True, const Standard_Boolean exact = Standard_True);
124   
125   //! Works as above method but accepts a String from TCollection
126   Standard_EXPORT Standard_Boolean RemoveItem (const TCollection_AsciiString& name, const Standard_Boolean cln = Standard_True, const Standard_Boolean exact = Standard_True);
127   
128   //! Deletes physically in one step the entries which were removed
129   //! (can be used for a more efficient Memory Management : first
130   //! Remove several Items (<cln> = False), then Clean the Memory)
131   Standard_EXPORT void Clean();
132   
133   //! Returns True if no Item is recorded
134   Standard_EXPORT Standard_Boolean IsEmpty() const;
135   
136   //! Clears all the Dictionary : all recorded Items are removed
137   Standard_EXPORT void Clear();
138   
139   //! Copies the Dictionary as a Tree, without Copying the Items
140   Standard_EXPORT Handle(Dico_DictionaryOfTransient) Copy() const;
141   
142   //! Internal routine used for completion (returns True if success)
143   Standard_EXPORT Standard_Boolean Complete (Handle(Dico_DictionaryOfTransient)& acell) const;
144
145
146 friend class Dico_IteratorOfDictionaryOfTransient;
147
148
149   DEFINE_STANDARD_RTTI_INLINE(Dico_DictionaryOfTransient,MMgt_TShared)
150
151 protected:
152
153
154
155
156 private:
157
158   
159   //! Defines cell's character (internal use, to build dict. tree)
160   Standard_EXPORT void SetChar (const Standard_Character car);
161   
162   //! Returns True if this cell has a subcell
163   Standard_EXPORT Standard_Boolean HasSub() const;
164   
165   //! Returns subcell
166   Standard_EXPORT Handle(Dico_DictionaryOfTransient) Sub() const;
167   
168   //! Returns True if this cell has a next cell
169   Standard_EXPORT Standard_Boolean HasNext() const;
170   
171   //! Returns next cell
172   Standard_EXPORT Handle(Dico_DictionaryOfTransient) Next() const;
173   
174   //! Defines subcell
175   Standard_EXPORT void SetSub (const Handle(Dico_DictionaryOfTransient)& acell);
176   
177   //! Defines next cell
178   Standard_EXPORT void SetNext (const Handle(Dico_DictionaryOfTransient)& acell);
179   
180   //! Internal method used to get an entry from a given name
181   Standard_EXPORT void SearchCell (const Standard_CString name, const Standard_Size lmax, const Standard_Character car, const Standard_Size level, Handle(Dico_DictionaryOfTransient)& acell, Standard_Size& reslev, Standard_Integer& stat) const;
182   
183   //! Internal method used to create a new entry for a name
184   Standard_EXPORT void NewCell (const Standard_CString name, const Standard_Size namlen, Handle(Dico_DictionaryOfTransient)& acell, const Standard_Size reslev, const Standard_Integer stat);
185   
186   //! Returns True if a cell has an associated item value
187   Standard_EXPORT Standard_Boolean HasIt() const;
188   
189   //! Returns item value associated to a cell
190   Standard_EXPORT const Handle(Standard_Transient)& It() const;
191   
192   //! Returns item address associated to a cell
193   Standard_EXPORT Handle(Standard_Transient)& ItAdr();
194   
195   //! Binds an item value to a cell
196   Standard_EXPORT void SetIt (const Handle(Standard_Transient)& anitem);
197   
198   //! Declares a cell as Valued : used by NewItem (when an Item
199   //! is created if it did not exist and is returned)
200   Standard_EXPORT void DeclIt();
201   
202   //! Removes item bound to a cell (cancels effect of DeclIt)
203   Standard_EXPORT void RemoveIt();
204   
205   //! Returns cell's character as a node feature
206   Standard_EXPORT Standard_Character CellChar() const;
207   
208   //! Performs Copy from an original <fromcell> to <me>
209   //! Called by Copy
210   Standard_EXPORT void GetCopied (const Handle(Dico_DictionaryOfTransient)& fromcell);
211
212   Standard_Character thecars[4];
213   Handle(Dico_DictionaryOfTransient) thesub;
214   Handle(Dico_DictionaryOfTransient) thenext;
215   Handle(Standard_Transient) theitem;
216
217
218 };
219
220
221
222
223
224
225
226 #endif // _Dico_DictionaryOfTransient_HeaderFile