Initial revision
[TestXSLT.git] / libsablot / src / engine / guard.h
1 /* 
2  * The contents of this file are subject to the Mozilla Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/MPL/
6  * 
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  * 
12  * The Original Code is the Sablotron XSLT Processor.
13  * 
14  * The Initial Developer of the Original Code is Ginger Alliance Ltd.
15  * Portions created by Ginger Alliance are Copyright (C) 2000-2002
16  * Ginger Alliance Ltd. All Rights Reserved.
17  * 
18  * Contributor(s):
19  * 
20  * Alternatively, the contents of this file may be used under the
21  * terms of the GNU General Public License Version 2 or later (the
22  * "GPL"), in which case the provisions of the GPL are applicable 
23  * instead of those above.  If you wish to allow use of your 
24  * version of this file only under the terms of the GPL and not to
25  * allow others to use your version of this file under the MPL,
26  * indicate your decision by deleting the provisions above and
27  * replace them with the notice and other provisions required by
28  * the GPL.  If you do not delete the provisions above, a recipient
29  * may use your version of this file under either the MPL or the
30  * GPL.
31  */
32
33 #ifndef GuardHIncl
34 #define GuardHIncl
35
36 // GP: clean
37
38 /*
39  *
40  *  macro definitions for guarded pointers
41  *  (i.e. pointers that automatically get deallocated on exit from a function)
42  *
43  */
44
45 /*
46  *  DeclGuard(T)    declares a class for use in place of T*
47  *  [use DeclArrGuard for pointers to arrays!]
48  *
49  *  Methods:
50  *      keep()      forgets about the allocation so the block is preserved
51  *      del()       deallocates the block explicitly
52  *      delArray()  deallocates the array explicitly
53  *      operator()  returns the pointer, for use in  v() -> item
54  *      cast to T*  returns the pointer
55  *      operator[]  behaves as if used with the pointer
56  *      operator*   behaves as if used with the pointer
57  *      operator=   behaves as if used with the pointer
58  */
59
60 #define DeclGuard( TYPE ) \
61 class TYPE##_G \
62 { \
63 public: \
64     TYPE##_G(TYPE *ptr_ = NULL): ptr(ptr_), kill(TRUE) {} \
65     TYPE* keep() {kill = FALSE; return ptr;} \
66     TYPE* unkeep() {kill = TRUE; return ptr;} \
67     void assign(TYPE* ptr_) {kill = FALSE; ptr = ptr_;} \
68     TYPE* operator= (TYPE *ptr_) {kill = ptr_? TRUE : FALSE; return ptr = ptr_;} \
69     operator TYPE*&() {return ptr;} \
70     TYPE& operator*(void) {assert(ptr); return *ptr;} \
71     TYPE* operator()(void) {return ptr;} \
72     void del(void) {if (ptr) delete ptr; ptr = NULL; kill = FALSE;} \
73     void delArray(void) {if (ptr) delete[] ptr; ptr = NULL; kill = FALSE;} \
74     ~TYPE##_G() {if (kill) del();} \
75 protected: \
76     TYPE *ptr; \
77     Bool kill; \
78 };
79
80 /*
81  *  DeclArrGuard(T) - as above, for pointers pointing to an array
82  */  
83
84 #define DeclArrGuard( TYPE ) \
85 class TYPE##_ArrG: public TYPE##_G \
86 { \
87 public: \
88     TYPE##_ArrG(TYPE *ptr_ = NULL): TYPE##_G(ptr_) {} \
89     ~TYPE##_ArrG() {if (kill) delArray();} \
90     TYPE& operator [](long nIndex_) { return ptr[nIndex_]; } \
91 };
92
93 /*
94  *  DeclDelGuard(T) - for autodeleting lists (automatic freeall(FALSE))
95  */  
96
97 #define DeclDelGuard( TYPE ) \
98 class TYPE##_DelG: public TYPE##_G \
99 { \
100 public: \
101     TYPE##_DelG(TYPE *ptr_ = NULL): TYPE##_G(ptr_), autoDel(FALSE) {} \
102     ~TYPE##_DelG() {if (kill) { if (autoDel && ptr) ptr -> freeall(FALSE); del();}} \
103     TYPE* keep() {autoDel = FALSE; return TYPE##_G::keep();} \
104     void autodelete() {autoDel = TRUE;}\
105 private:\
106     Bool autoDel;\
107 };
108
109
110
111 /*
112  *  GP() declares an actual guarded pointer,
113  *  e.g. 
114  *      GP( Vertex ) v;
115  *  replaces
116  *      Vertex *v;
117  */
118
119 #define GP( TYPE ) TYPE##_G 
120
121 /*
122  *  GPA() declares a guarded pointer pointing to an array
123  */
124
125 #define GPA( TYPE ) TYPE##_ArrG 
126
127 /*
128  *  GPD() declares a guarded pointer pointing to an autodeleting list
129  */
130
131 #define GPD( TYPE ) TYPE##_DelG 
132
133 /*
134  *
135  *  guard declarations
136  *
137  */
138
139 #include "proc.h"
140 #include "verts.h"
141 #include "context.h"
142 #include "tree.h"
143 #include "parser.h"
144 #include "encoding.h"
145
146 DeclGuard(Vertex);
147 DeclGuard(Processor);
148 DeclGuard(Expression);
149 DeclGuard(SortDef);
150 DeclGuard(QName);
151 DeclGuard(EQName);
152 DeclGuard(Context);
153 DeclGuard(Str);
154 typedef const char* ConstCharP;
155 DeclGuard(ConstCharP);
156 typedef char GChar; //guarded character
157 DeclGuard(GChar);
158 DeclGuard(OutputHistoryItem);
159 DeclGuard(ExprList);
160 DeclGuard(DataLine);
161 DeclGuard(UriList);
162 DeclGuard(Tree);
163 DeclGuard(OutputterObj);
164 DeclGuard(OutputDefinition);
165 DeclGuard(OutputDocument);
166 DeclGuard(TreeConstructer);
167 DeclArrGuard(ConstCharP);
168 DeclArrGuard(GChar);
169 DeclDelGuard(ExprList);
170 DeclGuard(ConvInfo);
171
172 #endif // ifndef GuardHIncl