alaCarte Maps
Renderer for OpenStreetMap tiles
binary_operation_node.cpp
Go to the documentation of this file.
1 
24 
25 
26 namespace eval {
27 
28 
29 template<typename FloatOperator, typename IntOperation>
30 string calculation(const string& left, const string& right, FloatOperator fop, IntOperation iop)
31 {
32  float f1, f2;
33  int i1, i2;
34  if(Conv<int>(left, &i1) && Conv<int>(right, &i2))
35  {
36  int res = iop(i1, i2);
37 
38  return ToString(res);
39 
40  }else if(Conv<float>(left, &f1) && Conv<float>(right, &f2))
41  {
42  float res = fop(f1, f2);
43  return ToString(res);
44  }
45 
46  return "";
47 }
48 
57  : left(left)
58  , operation(operation)
59  , right(right)
60 {
61 }
62 
63 
64 
66 {
67  string first = left->eval(obj);
68  string second = right->eval(obj);
69 
70  switch(operation)
71  {
72  /****************** Checks if both values are equal ******************/
73  case op::Equal:
74  {
75  if(first == second)
76  {
77  return "true";
78  }
79 
80  int i1, i2;
81  if( Conv<int>(first, &i1)
82  && Conv<int>(second, &i2)
83  && i1 == i2)
84  {
85  return "true";
86  }
87 
88  return "false";
89 
90  }break;
91 
92  /****************** Checks if both values are unequal ******************/
93  case op::Unequal:
94  {
95 
96  int i1, i2;
97  if( Conv<int>(first, &i1)
98  && Conv<int>(second, &i2))
99  {
100  if(i1 != i2)
101  {
102  return "true";
103  }
104  } else {
105  if(first != second)
106  {
107  return "true";
108  }
109  }
110 
111  return "false";
112 
113  }break;
114 
115  /****************** Checks if first is less than second ******************/
116  case op::Less:
117  {
118  float f1, f2;
119  if( Conv<float>(first, &f1)
120  && Conv<float>(second, &f2))
121  {
122  if(f1 < f2)
123  {
124  return "true";
125  }
126  }
127  return "false";
128  }
129 
130  /****************** Checks if first is lesser than second ******************/
131  case op::LessEqual:
132  {
133  float f1, f2;
134  if( Conv<float>(first, &f1)
135  && Conv<float>(second, &f2))
136  {
137  if(f1 <= f2)
138  {
139  return "true";
140  }
141  }
142  return "false";
143  }
144  /****************** Checks if first is greater than second ******************/
145  case op::Greater:
146  {
147  float f1, f2;
148  if( Conv<float>(first, &f1)
149  && Conv<float>(second, &f2))
150  {
151  if(f1 > f2)
152  {
153  return "true";
154  }
155  }
156  return "false";
157  }
158  /****************** Checks if first is greater or equal to second ******************/
159  case op::GreaterEqual:
160  {
161  float f1, f2;
162  if( Conv<float>(first, &f1)
163  && Conv<float>(second, &f2))
164  {
165  if(f1 >= f2)
166  {
167  return "true";
168  }
169  }
170  return "false";
171  }
172 
173  /****************** Checks if both strings are equal ******************/
174  case op::StringEqual:
175  {
176  if(first == second)
177  return "true";
178  return "false";
179  }break;
180 
181  /****************** Checks if both strings are unequal ******************/
182  case op::StringUnequal:
183  {
184  if(first != second)
185  return "true";
186  return "false";
187  }break;
188 
189  // 2. lvl
190  /****************** Add both values ******************/
191  case op::Add:
192  return calculation( first,
193  second,
194  [](float f1, float f2)
195  {
196  return f1 + f2;
197  },
198  [](int i1, int i2)
199  {
200  return i1 + i2;
201  });
202 
203 
204 
205  /****************** Substract both values ******************/
206  case op::Minus:
207  return calculation( first,
208  second,
209  [](float f1, float f2)
210  {
211  return f1 - f2;
212  },
213  [](int i1, int i2)
214  {
215  return i1 - i2;
216  });
217 
218  // 3. lvl
219  /****************** Multipies both values ******************/
220  case op::Mul:
221  return calculation( first,
222  second,
223  [](float f1, float f2)
224  {
225  return f1 * f2;
226  },
227  [](int i1, int i2)
228  {
229  return i1 * i2;
230  });
231 
232  /****************** Devides both values ******************/
233  case op::Div:
234  {
235  // Beware of divide by zero
236  int i;
237  if(Conv<int>(second, &i) && i == 0)
238  return "";
239 
240  return calculation( first,
241  second,
242  [](float f1, float f2)
243  {
244  return f1 / f2;
245  },
246  [](int i1, int i2)
247  {
248  return i1 / i2;
249  });
250  }
251 
252 
253  /****************** Concatinates both strings ******************/
254  case op::Concatination:
255  {
256  return first + second;
257  }break;
258  }
259 
260  assert(!"Invalid operation executed!");
261  return "";
262 }
263 
264 
265 
266 }
virtual string eval(GeoObject *obj) const
Evaluates a subtree under this node.
BinaryOperationEnum
Enumeration for all binary operations.
string calculation(const string &left, const string &right, FloatOperator fop, IntOperation iop)
node_ptr left
The left expression of the operator.
op::BinaryOperationEnum operation
The type of the operation.
This file is part of alaCarte.
node_ptr right
The right expression of the operator.
string ToString(const bool &v)
BinaryOperationNode(const node_ptr &left, op::BinaryOperationEnum operation, const node_ptr &right)
Creates a new binary operation.