1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | // Evaluateur d'expression C - R. Ansari 03/2004
|
---|
3 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
4 |
|
---|
5 | #ifndef CEXPREVAL_SEEN
|
---|
6 | #define CEXPREVAL_SEEN
|
---|
7 |
|
---|
8 | #include "machdefs.h"
|
---|
9 | #include <iostream>
|
---|
10 | #include <string>
|
---|
11 | #include <vector>
|
---|
12 | #include <map>
|
---|
13 | #include "pexceptions.h"
|
---|
14 |
|
---|
15 | using namespace std;
|
---|
16 | namespace SOPHYA {
|
---|
17 |
|
---|
18 | /*!
|
---|
19 | \ingroup SysTools
|
---|
20 | \brief Exception class used by CExpressionEvaluator
|
---|
21 | */
|
---|
22 | class CExprException : public PException {
|
---|
23 | public:
|
---|
24 | explicit CExprException(const char * m) throw() : PException(m) {}
|
---|
25 | explicit CExprException(const string& m) throw() : PException(m) {}
|
---|
26 | };
|
---|
27 |
|
---|
28 | //--------------------------------------------------------------------
|
---|
29 | /*!
|
---|
30 | \ingroup SysTools
|
---|
31 | \brief Base class for arithmetic expressions used by CExpressionEvaluator
|
---|
32 | \sa CExpressionEvaluator
|
---|
33 | */
|
---|
34 | class CExprBase {
|
---|
35 | public:
|
---|
36 | explicit CExprBase();
|
---|
37 | virtual ~CExprBase();
|
---|
38 | virtual double Evaluate() const = 0;
|
---|
39 | virtual bool CheckE(string& errmsg) const { return true; }
|
---|
40 | virtual void CheckThrow(const char * msg) const;
|
---|
41 | inline void CheckThrow(string const & msg) const { CheckThrow(msg.c_str()); }
|
---|
42 | virtual void Print(ostream& os) const = 0;
|
---|
43 | static long NbCreate() { return totnexp_create; }
|
---|
44 | static long NbDelete() { return totnexp_delete; }
|
---|
45 | protected:
|
---|
46 | static long totnexp_create;
|
---|
47 | static long totnexp_delete;
|
---|
48 | };
|
---|
49 |
|
---|
50 |
|
---|
51 | /*!
|
---|
52 | \ingroup SysTools
|
---|
53 | \brief For formatted write (print) of expressions in a stream
|
---|
54 | */
|
---|
55 | inline ostream& operator << (ostream& s, CExprBase const & ex)
|
---|
56 | { ex.Print(s); return(s); }
|
---|
57 |
|
---|
58 | /*!
|
---|
59 | \ingroup SysTools
|
---|
60 | \brief Interface class used by the CExpressionEvaluator class to represent a list of variables
|
---|
61 | or named values accessible trough a pointer.
|
---|
62 |
|
---|
63 | Inherited class should define the \b GetVarPointer() method, and can optionaly redefine
|
---|
64 | the \b Update() method.
|
---|
65 | \sa CExpressionEvaluator
|
---|
66 | */
|
---|
67 | class CE_VarListInterface {
|
---|
68 | public:
|
---|
69 | //! To be called to update values corresponding to the pointers. Default implementation does nothing.
|
---|
70 | virtual void Update() { return; }
|
---|
71 | //! Return a pointer to a double containing the value associated with \t name.
|
---|
72 | virtual double* GetVarPointer(std::string const& name) = 0;
|
---|
73 | };
|
---|
74 |
|
---|
75 |
|
---|
76 | class CE_BinExp;
|
---|
77 | class CE_FuncExp;
|
---|
78 | //---------------------------------------------------------
|
---|
79 | class CExpressionEvaluator : public CExprBase {
|
---|
80 | public:
|
---|
81 | CExpressionEvaluator(const char* sexp, CE_VarListInterface* pvlist=NULL);
|
---|
82 | CExpressionEvaluator(string const & sex, CE_VarListInterface* pvlist=NULL);
|
---|
83 |
|
---|
84 | virtual ~CExpressionEvaluator();
|
---|
85 | //! Evaluate the expression and returns the corresponding value.
|
---|
86 | virtual double Evaluate() const;
|
---|
87 | //! Alias for Evaluate()
|
---|
88 | inline double Value() const { return Evaluate(); }
|
---|
89 | //! Formatted output on a stream
|
---|
90 | virtual void Print(ostream& os) const;
|
---|
91 |
|
---|
92 | protected:
|
---|
93 | //! Parse variable names or constants and return CE_NumberExp object.
|
---|
94 | CExprBase* VarNameOrNumber(string const & s);
|
---|
95 | //! Does the parsing and builds an CExprBase object.
|
---|
96 | CExprBase* ParseString(int extype, string fname, string const & sex,
|
---|
97 | size_t off, size_t& stop, string& errmsg);
|
---|
98 |
|
---|
99 | CExprBase * _exp;
|
---|
100 | CE_VarListInterface* _varlist;
|
---|
101 | };
|
---|
102 |
|
---|
103 | } // End of namespace SOPHYA
|
---|
104 |
|
---|
105 | /* end of ifdef CEXPREVAL_SEEN */
|
---|
106 | #endif
|
---|