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 <map>
50 # include <stack>
51 # include <string>
52 # include <vector>
53 
54 class GlsMenuDictionary;
55 
56 //----------------------------------------------------------------------------
77 //----------------------------------------------------------------------------
79 {
80 public:
81  //------------------------------------------------------------------------
88  //------------------------------------------------------------------------
89  GlsBoolExpression( GlsMenuDictionary& evalDict, bool defaultVal = false );
90 
91  //------------------------------------------------------------------------
95  //------------------------------------------------------------------------
96  virtual ~GlsBoolExpression() {}
97 
98  //------------------------------------------------------------------------
103  //------------------------------------------------------------------------
104  bool operator()() const;
105 
106  //------------------------------------------------------------------------
110  //------------------------------------------------------------------------
111  virtual void Set( const std::string& val );
112 
113  //------------------------------------------------------------------------
117  //------------------------------------------------------------------------
119  {
120  if( this != &r )
121  {
125  }
126 
127  return *this;
128  }
129 
130  //------------------------------------------------------------------------
134  //------------------------------------------------------------------------
135  bool operator==( const GlsBoolExpression& r ) const
136  {
137  return _latestValue == r._latestValue;
138  }
139 
140  //------------------------------------------------------------------------
144  //------------------------------------------------------------------------
145  std::string ExpressionString() const
146  {
147  return _expressionString;
148  }
149 
150 protected:
152  {
153  OR,
155  EQ,
157  LT,
159  GT,
164 
166  };
167 
168  typedef std::vector<std::string> TokenCont_t;
169  typedef std::map<std::string, Operator_t> OperatorMap_t;
170 
174 
176  static OperatorMap_t OP;
177 
179  static const unsigned PRECEDENCE[ MAX_OPERATORS ];
180 
184 
186  std::string _expressionString;
187 
189  mutable bool _latestValue;
190 
193  TokenCont_t _RPNTokens;
194 
195  //------------------------------------------------------------------------
199  //------------------------------------------------------------------------
200  virtual std::string Evaluate( Operator_t op,
201  const std::string& operand1,
202  const std::string& operand2 ) const;
203 
204  //------------------------------------------------------------------------
208  //------------------------------------------------------------------------
209  virtual bool IsTrue( const std::string& word ) const;
210 
211  //------------------------------------------------------------------------
215  //------------------------------------------------------------------------
216  static bool IsAnOperator( const std::string& token )
217  {
218  return OP.find( token ) != OP.end();
219  }
220 
221  //------------------------------------------------------------------------
226  //------------------------------------------------------------------------
227  static TokenCont_t InfixToRPN( const TokenCont_t& infixTokens );
228 
229  //------------------------------------------------------------------------
234  //------------------------------------------------------------------------
235  static void Tokenize( const std::string& expr, TokenCont_t& tokens );
236 
237  //------------------------------------------------------------------------
242  //------------------------------------------------------------------------
243  static std::string::size_type VarLength(
244  const std::string& input,
245  std::string::size_type start );
246 
247  //------------------------------------------------------------------------
251  //------------------------------------------------------------------------
252  static std::string Uppercase( const std::string& str );
253 
254  //------------------------------------------------------------------------
258  //------------------------------------------------------------------------
259  static bool ValidInfix( const TokenCont_t& infixTokens );
260 
261 }; // end GlsBoolExpression
262 
263 //------------------------------------------------------------------------
267 //------------------------------------------------------------------------
268 inline std::ostream& operator<<( std::ostream& outstr, const GlsBoolExpression& e )
269 {
270  outstr << e.ExpressionString();
271  return outstr;
272 }
273 
274 //------------------------------------------------------------------------
278 //------------------------------------------------------------------------
279 inline std::istream& operator>>( std::istream& instr, GlsBoolExpression& e )
280 {
281  std::string exprStr;
282  std::getline( instr, exprStr );
283  e.Set( exprStr );
284  return instr;
285 }
286 
287 #endif // GLS_BOOLEXPRESSION_H
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