MarlinMT  0.1.0
LogicalExpressions.h
Go to the documentation of this file.
1 #ifndef LogicalExpressions_h
2 #define LogicalExpressions_h 1
3 
4 #include <map>
5 #include <string>
6 #include <list>
7 #include <vector>
8 #include <ostream>
9 
10 typedef std::map< const std::string, std::string > ConditionsMap ;
11 typedef std::map< const std::string, bool > ResultMap ;
12 
13 
14 namespace marlinmt{
15 
16 
22  struct Expression{
23 
24  Expression() : Operation( AND ), isNot( false ), Value("") {
25  }
26  enum Operator{ OR, AND } ;
27 
29  bool isNot ;
30  std::string Value ;
31  };
32 
33  std::ostream& operator<< ( std::ostream& s, Expression& e ) ;
34 
39  class Tokenizer{
40 
41  enum state{
44  operation
45  } ;
46 
47  std::vector< Expression >& _tokens ;
48  char _last ;
49  bool needToken ;
50  int openPar ;
51  int closedPar ;
53 
54  public:
55 
56  Tokenizer( std::vector< Expression >& tokens ) : _tokens(tokens) , _last(0), needToken(true)
57  , openPar(0), closedPar(0) ,
58  _state( newtoken ) {
59  }
60 
61 
63  void operator()(const char& c) {
64 
65  if( c != ' ' && c != '\t' ) { // ignore whitespaces and tabs
66 
67  if( c == '(' ) ++openPar ;
68 
69  if( c == ')' ) ++closedPar ;
70 
71  switch( _state ){
72 
73  case( newtoken ):
74 
75  if( needToken ){
76  _tokens.push_back( Expression() ) ; // create a new object
77  needToken = false ;
78  }
79  if( c == '!' )
80  _tokens.back().isNot = true ;
81 
82  else if( c == '(' )
83  _state = parenthesis ;
84 
85  else {
86  _tokens.back().Value += c ;
87  _state = operation ;
88  }
89  break ;
90 
91  case( parenthesis ):
92 
93  if( closedPar == openPar ) {
94 
95  _state = operation ;
96 
97  } else {
98 
99  _tokens.back().Value += c ;
100  }
101  break ;
102 
103  case( operation ): // need to accumulate values until && or ||
104 
105  if( c == '&' || c=='|' ){
106 
107  if ( c == '&' && _last == '&' ) {
108  _tokens.push_back( Expression() ) ; // create a new object
109  _tokens.back().Operation = Expression::AND ;
110  _state = newtoken ;
111  }
112  if ( c == '|' && _last == '|' ) {
113  _tokens.push_back( Expression() ) ; // create a new object
114  _tokens.back().Operation = Expression::OR ;
115  _state = newtoken ;
116  }
117 
118  } else {
119 
120  _tokens.back().Value += c ;
121  }
122 
123  break ;
124  }
125 
126  }
127  _last = c ;
128  }
129 
131  }
132 
133  std::vector<Expression> & result() {
134 
135  return _tokens ;
136 
137  }
138  };
139 
144 
145  public:
146 
150 
152  virtual ~LogicalExpressions() {}
153 
157  void addCondition( const std::string& name, const std::string& expression ) ;
158 
160  void clear() ;
161 
163  bool conditionIsTrue( const std::string& name ) const ;
164 
166  bool expressionIsTrue( const std::string& expression ) const ;
167 
169  void setValue( const std::string& key, bool val ) ;
170 
171 
172  protected:
173 
175  bool getValue( const std::string& key ) const ;
176 
177  ConditionsMap _condMap{};
178  ResultMap _resultMap{};
179 
180  } ;
181 } // end namespace
182 
183 #endif
std::map< const std::string, bool > ResultMap
std::ostream & operator<<(std::ostream &stream, const Configuration &cfg)
Stream operator.
std::vector< Expression > & _tokens
Helper struct for LogicalExpression.
std::vector< Expression > & result()
Tokenizer(std::vector< Expression > &tokens)
std::map< const std::string, std::string > ConditionsMap
virtual ~LogicalExpressions()
Virtual d&#39;tor.
Helper class that holds named boolean values and named conditions that are expressions of these value...
Helper class for LogicalExpressions that splits the expression into subexpressions - needs to be apll...
void operator()(const char &c)
Save the current character and change state if necessary.