GlsMenu
gls_bool_expression.h
Go to the documentation of this file.
1 
42 #if !defined(GLS_BOOLEXPRESSION_H)
43 #define GLS_BOOLEXPRESSION_H
44 
45 #ifdef WIN32
46 #pragma warning (disable:4786)
47 #endif
48 
49 #include <string>
50 #include <map>
51 #include <vector>
52 #include <stack>
53 
54 class GlsMenuDictionary;
55 
56 //----------------------------------------------------------------------------
77 //----------------------------------------------------------------------------
79 {
80 public:
81 
82  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  GlsBoolExpression(GlsMenuDictionary& evalDict, bool defaultVal = false);
91 
92  //------------------------------------------------------------------------
96  //------------------------------------------------------------------------
97  virtual ~GlsBoolExpression() {}
98 
99  //------------------------------------------------------------------------
104  //------------------------------------------------------------------------
105  bool operator()() const;
106 
107  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
112  virtual void Set(const std::string& val);
113 
114  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
120  {
121  if (this != &r)
122  {
126  }
127 
128  return *this;
129  }
130 
131  //------------------------------------------------------------------------
135  //------------------------------------------------------------------------
136  bool operator==(const GlsBoolExpression& r) const
137  {
138  return _latestValue == r._latestValue;
139  }
140 
141 
142  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  std::string ExpressionString() const
148  {
149  return _expressionString;
150  }
151 
152 
153 protected:
154 
156  {
157  OR,
158  AND,
159  EQ,
161  LT,
163  GT,
168 
170  };
171 
172  typedef std::vector<std::string> TokenCont_t;
173  typedef std::map<std::string, Operator_t> OperatorMap_t;
174 
178 
180  static OperatorMap_t OP;
181 
183  static const unsigned PRECEDENCE[MAX_OPERATORS];
184 
188 
190  std::string _expressionString;
191 
193  mutable bool _latestValue;
194 
197  TokenCont_t _RPNTokens;
198 
199 
200  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
205  virtual std::string Evaluate(Operator_t op,
206  const std::string& operand1,
207  const std::string& operand2) const;
208 
209  //------------------------------------------------------------------------
213  //------------------------------------------------------------------------
214  virtual bool IsTrue(const std::string& word) const;
215 
216  //------------------------------------------------------------------------
220  //------------------------------------------------------------------------
221  static bool IsAnOperator(const std::string& token)
222  {
223  return OP.find(token) != OP.end();
224  }
225 
226  //------------------------------------------------------------------------
231  //------------------------------------------------------------------------
232  static TokenCont_t InfixToRPN(const TokenCont_t& infixTokens);
233 
234  //------------------------------------------------------------------------
239  //------------------------------------------------------------------------
240  static void Tokenize(const std::string& expr, TokenCont_t& tokens);
241 
242  //------------------------------------------------------------------------
247  //------------------------------------------------------------------------
248  static std::string::size_type VarLength(
249  const std::string& input,
250  std::string::size_type start);
251 
252  //------------------------------------------------------------------------
256  //------------------------------------------------------------------------
257  static std::string Uppercase(const std::string& str);
258 
259 
260  //------------------------------------------------------------------------
264  //------------------------------------------------------------------------
265  static bool ValidInfix(const TokenCont_t& infixTokens);
266 
267 }; // end GlsBoolExpression
268 
269 
270 //------------------------------------------------------------------------
274 //------------------------------------------------------------------------
275 inline std::ostream& operator<<(std::ostream& outstr, const GlsBoolExpression& e)
276 {
277  outstr << e.ExpressionString();
278  return outstr;
279 }
280 
281 //------------------------------------------------------------------------
285 //------------------------------------------------------------------------
286 inline std::istream& operator>>(std::istream& instr, GlsBoolExpression& e)
287 {
288  std::string exprStr;
289  std::getline(instr, exprStr);
290  e.Set(exprStr);
291  return instr;
292 }
293 
294 
295 #endif // GLS_BOOLEXPRESSION_H
296 
static OperatorMap_t OP
A map to convert from operator strings to their enum equivalent.
static const unsigned PRECEDENCE[MAX_OPERATORS]
Operator precedence values used when evaluating the expression.
static TokenCont_t InfixToRPN(const TokenCont_t &infixTokens)
bool operator()() const
std::string ExpressionString() const
std::vector< std::string > TokenCont_t
std::istream & operator>>(std::istream &instr, GlsBoolExpression &e)
bool operator==(const GlsBoolExpression &r) const
virtual void Set(const std::string &val)
static void Tokenize(const std::string &expr, TokenCont_t &tokens)
GlsBoolExpression & operator=(const GlsBoolExpression &r)
virtual bool IsTrue(const std::string &word) const
GlsBoolExpression(GlsMenuDictionary &evalDict, bool defaultVal=false)
virtual std::string Evaluate(Operator_t op, const std::string &operand1, const std::string &operand2) const
static bool ValidInfix(const TokenCont_t &infixTokens)
static bool IsAnOperator(const std::string &token)
static std::string Uppercase(const std::string &str)
GlsMenuDictionary & _evalDict
The dictionary to use to evaluate meta-data variables found in the expression.
TokenCont_t _RPNTokens
These are the tokens that were extracted from the expression string and are stored in Reverse Polish ...
std::ostream & operator<<(std::ostream &outstr, const GlsBoolExpression &e)
static std::string::size_type VarLength(const std::string &input, std::string::size_type start)
static bool _initializedStaticData
Whether or not the static class data has been initialized. This needs to be done only once by the fir...
bool _latestValue
The latest calculated result of the expression.
std::string _expressionString
Storage of the original string for the expression.
std::map< std::string, Operator_t > OperatorMap_t