fastdo  0.5.12
eienexpr.hpp
浏览该文件的文档.
1 #ifndef __EIENEXPR_HPP__
2 #define __EIENEXPR_HPP__
3 
4 #include "winux.hpp"
5 
7 namespace eienexpr
8 {
9 #ifdef EIENEXPR_DLL_USE
10  #if defined(_MSC_VER) || defined(WIN32)
11  #pragma warning( disable: 4251 )
12  #ifdef EIENEXPR_DLL_EXPORTS
13  #define EIENEXPR_DLL __declspec(dllexport)
14  #else
15  #define EIENEXPR_DLL __declspec(dllimport)
16  #endif
17 
18  #define EIENEXPR_API __stdcall
19  #else
20  #define EIENEXPR_DLL
21  #define EIENEXPR_API
22  #endif
23 #else
24  #define EIENEXPR_DLL
25  #define EIENEXPR_API
26 #endif
27 
28 #define EIENEXPR_FUNC_DECL(ret) EIENEXPR_DLL ret EIENEXPR_API
29 #define EIENEXPR_FUNC_IMPL(ret) ret EIENEXPR_API
30 
33 {
34 public:
35  enum
36  {
48  };
49 
50  ExprError( int errNo, winux::AnsiString const & err ) throw() : winux::Error( errNo, err ) { }
51 };
52 
55 {
56 public:
58  {
60  eatOperand
61  };
62 
63  ExprAtom();
64  virtual ~ExprAtom();
66  virtual ExprAtom * clone() const = 0;
68  ExprAtomType getAtomType() const;
69 
71  virtual winux::String toString() const = 0;
72 
73 protected:
74  ExprAtomType _atomType; // 表达式原子类型
75 
76  friend class ExprParser;
77 };
78 
79 class ExprOperand;
80 
83 {
84 public:
85  // 操作符函数类型
86  typedef bool (* OperatorFunction)( ExprOperand * arOperands[], short n, winux::SimplePointer<ExprOperand> * outRetValue, void * data );
87 
88  ExprOperator( winux::String const & oprStr = "", bool isUnary = false, bool isRight = false, short level = 0, OperatorFunction oprFn = NULL );
89  virtual ~ExprOperator();
90 
91  virtual ExprAtom * clone() const;
92  virtual winux::String toString() const;
93 
98  int nexus( ExprOperator const & opr ) const;
99 
100  bool isUnary() const { return _isUnary; }
101  bool isRight() const { return _isRight; }
102  winux::String const & getStr() const { return _oprStr; }
103 protected:
104  winux::String _oprStr; // 算符文本
105  bool _isUnary; // 是否一元
106  short _level; // 优先级别
107  bool _isRight; // 是否右结合
108  OperatorFunction _oprFn; // 对应函数
109 
110  static std::vector<ExprOperator> _Operators;
111 
112 public:
114  static bool Possibility( winux::String const & str );
115 
117  static void AddOpr( winux::String const & oprStr, bool isUnary, bool isRight, short level, OperatorFunction oprFn );
119  static bool DelOpr( winux::String const & oprStr, bool isUnary, bool isRight );
121  static bool ModifyOpr( int i, winux::String const & oprStr, bool isUnary, bool isRight, short level, OperatorFunction oprFn );
123  static int FindOpr( ExprOperator * opr, winux::String const & oprStr, bool isUnary = false, bool isRight = false );
124 
126  static int GetOpr( winux::String const & oprStr, ExprOperator * oprArr, int n );
128  static int GetAllOprs( ExprOperator * oprArr, int n );
129 
130  friend class Expression;
131  friend class ExprParser;
132 };
133 
136 {
137 public:
139  {
144  eotExpression
145  };
146 
147  ExprOperand();
148  virtual ~ExprOperand();
150  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result ) = 0;
152  winux::Mixed val();
153 
155  ExprOperandType getOperandType() const;
156 
157 protected:
158  ExprOperandType _operandType; // 操作数类型
159 
160  friend class ExprParser;
161 };
162 
165 {
166 public:
167  ExprLiteral();
168  ExprLiteral( winux::Mixed const & val );
169  virtual ~ExprLiteral();
170 
171  virtual ExprAtom * clone() const;
172  virtual winux::String toString() const;
173  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
174 
175  winux::Mixed::MixedType getValueType() const;
176  winux::Mixed const & getValue() const;
177  winux::Mixed & getValue();
178  void setValue( winux::Mixed const & val );
179 
181  static bool NumberPossibility( winux::String const & str, bool * isFloat = NULL, bool * isExp = NULL/*, bool * isMinus = NULL*/ );
182 protected:
184 
185  friend class ExprParser;
186 };
187 
188 class Expression;
189 class VarContext;
190 
193 {
194 public:
195  ExprIdentifier( Expression * exprObj, winux::String const & name );
196  virtual ~ExprIdentifier();
197 
198  virtual ExprAtom * clone() const;
199  virtual winux::String toString() const;
200  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
201 
202  winux::String const & getName() const { return _name; }
204  void setVar( winux::Mixed const & val );
205 
206  VarContext * getVarContext() const;
207 
208  static bool Possibility( winux::String const & str );
209 
210 protected:
211  Expression * _exprObj; // 所属表达式对象
212  winux::String _name; // 名称
213 
214  friend class ExprParser;
215 };
216 
219 {
220 public:
221  ExprReference( winux::Mixed & ref, winux::String const & syntax );
222  virtual ~ExprReference();
223 
224  virtual ExprAtom * clone() const;
225  virtual winux::String toString() const;
226  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
227 
228  winux::Mixed & getRef() const { return *this->_ref; }
229 protected:
230 
233 };
234 
237 {
238 public:
239  // 函数类型
240  typedef bool (* FuncFunction)( std::vector<Expression *> & params, winux::SimplePointer<ExprOperand> * outRetValue, void * data );
241  // 字符串=>函数映射
242  typedef std::map< winux::String, FuncFunction > StringFuncMap;
243 
244  ExprFunc( Expression * exprObj, winux::String const & funcName );
245  virtual ~ExprFunc();
246  ExprFunc( ExprFunc const & other );
247  ExprFunc & operator = ( ExprFunc const & other );
248 
249  virtual ExprAtom * clone() const;
250  virtual winux::String toString() const;
251  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
252 
253 //protected:
256  std::vector<Expression *> _params;
257 
258  static StringFuncMap _FuncsMap;
259 
261  void _clear();
263  void _addParam( Expression * param );
264 
265 public:
267  static void SetFunc( winux::String const & funcName, FuncFunction fn );
269  static bool DelFunc( winux::String const & funcName );
271  static bool ModifyFunc( winux::String const & funcName, winux::String const & newFuncName, FuncFunction newFn );
273  static bool FindFunc( winux::String const & funcName, FuncFunction * fn );
274 
276  static int GetAllFuncs( std::vector< std::pair< winux::String, FuncFunction > > * funcs );
277 
278  friend class ExprParser;
279 };
280 
281 class VarContext;
282 
285 {
286 public:
287  Expression( VarContext * ctx, Expression * owner, void * data );
288  virtual ~Expression();
289  Expression( Expression const & other );
290  Expression & operator = ( Expression const & other );
291 
292  virtual ExprAtom * clone() const;
293  virtual winux::String toString() const;
294  virtual bool evaluate( winux::SimplePointer<ExprOperand> * result );
295 
296  bool isEmpty() const { return this->_suffixAtoms.empty(); }
297 
298  VarContext * getVarContext() const;
300  void * getDataPtr() const;
301 //protected:
302  std::vector<ExprAtom *> _suffixAtoms;
305  void * _data;
306 
307  void _clear();
309  void _addAtom( ExprAtom * atom );
310 
311  friend class ExprIdentifier;
312  friend class ExprParser;
313 };
314 
317 {
318 public:
320  {
322  bool isNewAlloc;
323 
324  VariableStruct() : p(NULL), isNewAlloc(false)
325  {
326  }
327  };
328 
329  VarContext();
330  virtual ~VarContext();
331  VarContext( VarContext const & other );
332  VarContext & operator = ( VarContext const & other );
333 
337  void set( winux::String const & name, winux::Mixed const & v );
341  winux::Mixed & set( winux::String const & name );
345  void setPtr( winux::String const & name, winux::Mixed * v );
349  winux::Mixed * & setPtr( winux::String const & name );
350  bool unset( winux::String const & name );
351  bool has( winux::String const & name ) const;
352  bool get( winux::String const & name, winux::Mixed * * outVarPtr ) const;
353  winux::Mixed const & get( winux::String const & name ) const;
355  void clear();
356 protected:
357  std::map< winux::String, VariableStruct > _vars;
358 
359  friend class ExprParser;
360 };
361 
364 {
365 public:
366  ExprParser();
367  virtual ~ExprParser();
368 
369  void parse( Expression * e, winux::String const & str );
370 
371  //winux::String const & error() const { return _errStr; }
372  //int errNo() const { return _errNo; }
373 private:
374  //int _errNo;
375  //winux::String _errStr;
376  enum ExprParseContext
377  {
378  epcExpr, epcFuncParams, epcString, epcStrAntiSlashes
379  };
380  void _parseExpr( Expression * e, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
381  void _parseString( winux::String * v, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
382  void _parseStrAntiSlashes( winux::String * v, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
383  void _parseFuncParams( Expression * exprOwner, ExprFunc * funcAtom, winux::String const & str, int & i, std::vector<ExprParseContext> & epc );
384 
385 };
386 
387 }
388 
389 #endif //__EIENEXPR_HPP__
ExprError(int errNo, winux::AnsiString const &err)
Definition: eienexpr.hpp:50
winux::String _funcName
函数名
Definition: eienexpr.hpp:255
winux::Mixed _val
Definition: eienexpr.hpp:183
Expression * _owner
拥有者
Definition: eienexpr.hpp:304
winux::String _syntax
Definition: eienexpr.hpp:232
ExprOperandType _operandType
Definition: eienexpr.hpp:158
std::basic_string< char > AnsiString
Definition: utilities.hpp:165
OperatorFunction _oprFn
Definition: eienexpr.hpp:108
std::map< winux::String, FuncFunction > StringFuncMap
Definition: eienexpr.hpp:242
标识符(变量)操作数
Definition: eienexpr.hpp:192
winux::Mixed * _ref
Definition: eienexpr.hpp:231
winux::String _oprStr
Definition: eienexpr.hpp:104
bool isEmpty() const
Definition: eienexpr.hpp:296
表达式操作符
Definition: eienexpr.hpp:82
表达式库错误
Definition: eienexpr.hpp:32
std::vector< Expression * > _params
参数,也是表达式
Definition: eienexpr.hpp:256
void * _data
外部数据
Definition: eienexpr.hpp:305
static std::vector< ExprOperator > _Operators
Definition: eienexpr.hpp:110
std::map< winux::String, VariableStruct > _vars
Definition: eienexpr.hpp:357
表达式引擎,提供一种简单的动态解释执行的功能
Definition: eienexpr.hpp:7
#define EIENEXPR_DLL
Definition: eienexpr.hpp:24
bool isRight() const
Definition: eienexpr.hpp:101
static StringFuncMap _FuncsMap
实际函数映射表
Definition: eienexpr.hpp:258
winux::String const & getName() const
Definition: eienexpr.hpp:202
bool isNewAlloc
是否为新分配的Mixed变量
Definition: eienexpr.hpp:322
变量场景类
Definition: eienexpr.hpp:316
winux::Mixed & getRef() const
Definition: eienexpr.hpp:228
混合体,能表示多种类型的值
Definition: utilities.hpp:586
ExprAtomType _atomType
Definition: eienexpr.hpp:74
简单指针
Definition: smartptr.hpp:232
字面值操作数,无需计算
Definition: eienexpr.hpp:164
winux::String const & getStr() const
Definition: eienexpr.hpp:102
错误类
Definition: utilities.hpp:413
表达式原子
Definition: eienexpr.hpp:54
函数操作数
Definition: eienexpr.hpp:236
VarContext * _varCtx
变量场景
Definition: eienexpr.hpp:303
临时引用操作数
Definition: eienexpr.hpp:218
std::vector< ExprAtom * > _suffixAtoms
后缀式原子
Definition: eienexpr.hpp:302
bool isUnary() const
Definition: eienexpr.hpp:100
Expression * _exprObj
所属表达式对象
Definition: eienexpr.hpp:254
std::basic_string< tchar > String
Definition: utilities.hpp:162
表达式操作数
Definition: eienexpr.hpp:135