]> git.saurik.com Git - wxWidgets.git/blob - include/wx/wxexpr.h
event/leave events
[wxWidgets.git] / include / wx / wxexpr.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wxexpr.h
3 // Purpose: Prolog-like file I/O, used by resource system.
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __WXEXPRH__
13 #define __WXEXPRH__
14
15 #ifdef __GNUG__
16 #pragma interface "wxexpr.h"
17 #endif
18
19 #include <stdio.h>
20
21 #include "wx/defs.h"
22 #include "wx/string.h"
23
24 #if USE_IOSTREAMH
25 #include <iostream.h>
26 #else
27 #include <iostream>
28 #endif
29
30 #include "wx/list.h"
31 #include "wx/hash.h"
32
33 #include "wx/expr.h"
34
35 // Compatibility
36 #define PrologExpr wxExpr
37 #define PrologDatabase wxExprDatabase
38 #define proioErrorHandler wxExprErrorHandler
39 #define PROIO_ERROR_GENERAL 1
40 #define PROIO_ERROR_SYNTAX 2
41 #define PrologNull wxExprNull
42 #define PrologInteger wxExprInteger
43 #define PrologReal wxExprReal
44 #define PrologWord wxExprWord
45 #define PrologString wxExprString
46 #define PrologList wxExprList
47 #define PrologType wxExprType
48
49 // Error types
50 #define WXEXPR_ERROR_GENERAL 1
51 #define WXEXPR_ERROR_SYNTAX 2
52
53 // Error handler function definition. If app returns TRUE,
54 // carry on processing.
55 typedef bool (*wxExprErrorHandler) (int errorType, char *msg);
56
57 WXDLLEXPORT_DATA(extern wxExprErrorHandler) currentwxExprErrorHandler;
58
59 WXDLLEXPORT_DATA(extern "C" FILE*) yyin;
60
61 extern "C" int WXDLLEXPORT yyparse(void);
62
63 typedef enum {
64 wxExprNull,
65 wxExprInteger,
66 wxExprReal,
67 wxExprWord,
68 wxExprString,
69 wxExprList
70 } wxExprType;
71
72 class WXDLLEXPORT wxExprDatabase;
73
74 class WXDLLEXPORT wxExpr
75 {
76 public:
77 wxObject *client_data;
78 wxExprType type;
79 union {
80 long integer;
81 char *word;
82 char *string;
83 float real;
84 wxExpr *first; // If is a list expr, points to the first node
85 } value;
86
87 wxExpr *next; // If this is a node in a list, points to the next node
88 wxExpr *last; // If is a list expr, points to the last node
89
90 wxExpr(wxExprType the_type, char *word_or_string, bool allocate);
91 wxExpr(const wxString& functor); // Assume this is a new clause - pass functor
92 wxExpr(wxExprType the_type, const wxString& word_or_string = "");
93 wxExpr(long the_integer);
94 wxExpr(float the_real);
95 wxExpr(wxList *the_list);
96 ~wxExpr(void);
97
98 inline wxExprType Type(void) const { return type; }
99 inline long IntegerValue(void) const
100 {
101 if (type == wxExprInteger)
102 return value.integer;
103 else if (type == wxExprReal)
104 return (long)value.real;
105 else return 0;
106 }
107
108 inline float RealValue(void) const {
109 if (type == wxExprReal)
110 return value.real;
111 else if (type == wxExprInteger)
112 return (float)value.integer;
113 else return (float)0.0;
114 }
115
116 inline wxString WordValue(void) const {
117 if (type == wxExprWord)
118 return value.word;
119 else if (type == wxExprString)
120 return wxString(value.string);
121 else return wxString("");
122 }
123
124 inline wxString StringValue(void) const {
125 if (type == wxExprString)
126 return wxString(value.string);
127 else if (type == wxExprWord)
128 return wxString(value.word);
129 else return wxString("");
130 }
131
132 // Get nth arg of clause (starting from 1)
133 wxExpr *Arg(wxExprType type, int arg) const;
134
135 // Return nth argument of a list expression (starting from zero)
136 wxExpr *Nth(int arg) const;
137
138 // Returns the number of elements in a list expression
139 int Number(void) const;
140
141 // Make a clone
142 wxExpr *Copy(void) const;
143
144 wxExpr *GetAttributeValueNode(const wxString& word) const; // Use only for a clause or list
145 wxExpr *AttributeValue(const wxString& word) const; // Use only for a clause
146 wxString Functor(void) const; // Only for a clause
147 bool IsFunctor(const wxString& s) const; // Only for a clause
148 void WriteClause(ostream& stream); // Write this expression as a top-level clause
149 void WriteExpr(ostream& stream); // Write as any other subexpression
150 void WriteLispExpr(ostream& stream);
151
152 // Append an expression to a list
153 void Append(wxExpr *expr);
154 // Insert at beginning of list
155 void Insert(wxExpr *expr);
156
157 // Get first expr in list
158 inline wxExpr *GetFirst(void) const { return ((type == wxExprList) ? value.first : (wxExpr*)NULL); }
159
160 // Get next expr if this is a node in a list
161 inline wxExpr *GetNext(void) const { return next; }
162
163 // Get last expr in list
164 inline wxExpr *GetLast(void) const { return ((type == wxExprList) ? last : (wxExpr*)NULL); }
165
166 // This should really be called SetAttributeValue since any existing
167 // attribute-value is deleted first.
168 void AddAttributeValue(const wxString& attribute, long value);
169 void AddAttributeValue(const wxString& attribute, float value);
170 void AddAttributeValueWord(const wxString& attribute, const wxString& value);
171 void AddAttributeValueString(const wxString& attribute, const wxString& value);
172 void AddAttributeValue(const wxString& attribute, wxList *value);
173 void AddAttributeValue(const wxString& attribute, wxExpr *value);
174 void AddAttributeValueStringList(const wxString& attribute, wxList *string_list);
175
176 void DeleteAttributeValue(const wxString& attribute);
177
178 bool GetAttributeValue(const wxString& att, int& var) const;
179 bool GetAttributeValue(const wxString& att, long& var) const;
180 bool GetAttributeValue(const wxString& att, float& var) const;
181 bool GetAttributeValue(const wxString& att, wxString& var) const; // Word OR string -> string
182 bool GetAttributeValue(const wxString& att, wxExpr **var) const;
183
184 // Compatibility with old PrologIO
185 inline void AssignAttributeValue(char *att, int *var) const { GetAttributeValue(att, *var); }
186 inline void AssignAttributeValue(char *att, long *var) const { GetAttributeValue(att, *var); }
187 inline void AssignAttributeValue(char *att, float *var) const { GetAttributeValue(att, *var); }
188 inline void AssignAttributeValue(char *att, wxExpr **var) const { GetAttributeValue(att, var); }
189 void AssignAttributeValue(char *att, char **var) const ; // Word OR string -> string
190
191 // Add string items to list if the list attribute exists
192 bool GetAttributeValueStringList(const wxString& att, wxList *var) const;
193
194 // Associate other data with this expression, e.g. when reading in a
195 // number of linked items - store C++ object pointer with the expression
196 // so we can index into the wxExpr database and fish out the pointer.
197 inline void SetClientData(wxObject *data) { client_data = data; }
198 inline wxObject *GetClientData(void) const { return client_data; }
199 };
200
201 class WXDLLEXPORT wxExprDatabase: public wxList
202 {
203 DECLARE_DYNAMIC_CLASS(wxExprDatabase)
204 private:
205 wxNode *position; // Where we are in a search
206 wxHashTable *hash_table;
207 wxString attribute_to_hash;
208 public:
209 int noErrors;
210
211 wxExprDatabase(wxExprErrorHandler handler = 0);
212
213 // Use hashing on both the functor, and the attribute of
214 // specified type (wxExprString or wxExprInteger) and name.
215 // So to find node 45
216 // (i.e. match the clause node(id=45, ...))
217 // it usually requires 1 look-up: the keys for functor and attribute
218 // are added together.
219 // Obviously if the attribute was missing in a clause, it would
220 // fail to be found by this method, but could be retrieved by a
221 // linear search using BeginFind and FindClauseByFunctor,
222 // or just searching through the list as per usual.
223
224 wxExprDatabase(wxExprType type, const wxString& attribute, int size = 500,
225 wxExprErrorHandler handler = 0);
226
227 ~wxExprDatabase(void);
228
229 void BeginFind(void) ; // Initialise a search
230 wxExpr *FindClause(long id) ; // Find a term based on an integer id attribute
231 // e.g. node(id=23, type=rectangle, ....).
232
233 // Find on basis of attribute/value pairs, e.g. type=rectangle
234 // This doesn't use hashing; it's a linear search.
235 wxExpr *FindClause(const wxString& word, const wxString& value);
236 wxExpr *FindClause(const wxString& word, long value);
237 wxExpr *FindClause(const wxString& word, float value);
238 wxExpr *FindClauseByFunctor(const wxString& functor);
239
240 wxExpr *HashFind(const wxString& functor, const wxString& value) const;
241 wxExpr *HashFind(const wxString& functor, long value) const;
242
243 void Append(wxExpr *expr); // Does cleverer things if hashing is on
244 void ClearDatabase(void);
245 inline int GetErrorCount() const { return noErrors; }
246 bool Read(const wxString& filename);
247 bool ReadFromString(const wxString& buffer);
248 bool Write(const wxString& fileName);
249 bool Write(ostream& stream);
250 void WriteLisp(ostream& stream);
251
252 // Compatibility
253 inline bool ReadProlog(char *filename) { return Read(wxString(filename)); }
254 inline bool ReadPrologFromString(char *buffer) { return ReadFromString(wxString(buffer)); }
255 inline void WriteProlog(ostream& stream) { Write(stream); }
256 };
257
258 // Function call-style interface - some more convenience wrappers/unwrappers
259
260 // Make a call
261 wxExpr* WXDLLEXPORT wxExprMakeCall(const wxString& functor ...);
262
263 #define wxExprMakeInteger(x) (new wxExpr((long)x))
264 #define wxExprMakeReal(x) (new wxExpr((float)x))
265 #define wxExprMakeString(x) (new wxExpr(wxExprString, x))
266 #define wxExprMakeWord(x) (new wxExpr(wxExprWord, x))
267 #define wxExprMake(x) (new wxExpr(x))
268
269 // Checks functor
270 bool WXDLLEXPORT wxExprIsFunctor(wxExpr *expr, const wxString& functor);
271
272 // Temporary variable for communicating between wxexpr.cpp and YACC/LEX
273 WXDLLEXPORT_DATA(extern wxExprDatabase*) thewxExprDatabase;
274
275 // YACC/LEX can leave memory lying around...
276 extern "C" WXDLLEXPORT wxExprCleanUp();
277
278 #endif
279