alaCarte Maps
Renderer for OpenStreetMap tiles
eval.cpp
Go to the documentation of this file.
1 
23 #include <boost/spirit/include/qi.hpp>
24 
25 #include "server/eval/eval.hpp"
26 #include "server/eval/stnode.hpp"
28 
29 #include "server/eval/eval.hpp"
31 
32 #include "utils/colorTable.hpp"
33 
34 
35 
36 namespace eval {
37 
38 /*
39 template<typename T>
40 static T EasyStringConv(const string& str, T* value)
41 {
42  T extract;
43  if(Conv(str, &extract))
44  {
45  *value = extract;
46  }
47 
48  return T();
49 }
50 
51 struct IntVisitor : public boost::static_visitor<int>
52 {
53  GeoObject* obj;
54  int* target;
55  IntVisitor(GeoObject* obj, int* target)
56  : obj(obj)
57  , target(target)
58  {}
59 
60  int operator()(int i) const { return *target = i; }
61  int operator()(const Color& color) const { / * Do nothing! * / return 0; }
62  int operator()(double d) const { return *target = static_cast<int>(d); }
63  int operator()(const string& s) const { return EasyStringConv(s, target); }
64  int operator()(const eval::Eval::node_ptr& node) const { return EasyStringConv(node->eval(obj), target); }
65 };
66 
67 struct DoubleVisitor : public boost::static_visitor<double>
68 {
69  GeoObject* obj;
70  double* target;
71  DoubleVisitor(GeoObject* obj, double* target)
72  : obj(obj)
73  , target(target)
74  {}
75 
76  double operator()(int i) const { return *target = static_cast<double>(i); }
77  double operator()(const Color& color) const { / * Do nothing! * / return 0; }
78  double operator()(double d) const { return *target = d; }
79  double operator()(const string& s) const { return EasyStringConv(s, target); }
80  double operator()(const eval::Eval::node_ptr& node) const { return EasyStringConv(node->eval(obj), target); }
81 };
82 
83 struct ColorVisitor : public boost::static_visitor<Color>
84 {
85  GeoObject* obj;
86  Color* target;
87  ColorVisitor(GeoObject* obj, Color* target)
88  : obj(obj)
89  , target(target)
90  {}
91 
92  Color operator()(int i) const { / * Do nothing! * / return Color(); }
93  Color operator()(const Color& color) const { return *target = color; }
94  Color operator()(double d) const { / * Do nothing! * / return Color(); }
95  Color operator()(const string& s) const { ColorTable::Inst().resolve(s, target); return Color(); }
96  Color operator()(const eval::Eval::node_ptr& node) const { ColorTable::Inst().resolve(node->eval(obj), target); return Color(); }
97 };
98 
99 struct StringVisitor : public boost::static_visitor<string>
100 {
101  GeoObject* obj;
102  string* target;
103  StringVisitor(GeoObject* obj, string* target)
104  : obj(obj)
105  , target(target)
106  {}
107 
108  string operator()(int i) const { return (*target = ToString(i)); }
109  string operator()(const Color& color) const {return (*target = ToString(color)); }
110  string operator()(double d) const { return (*target = ToString(d)); }
111  string operator()(const string& s) const { return *target = s; }
112  string operator()(const eval::Eval::node_ptr& node) const { return *target = node->eval(obj); }
113 };*/
114 
115 
121 /*
122 Eval::Eval(const Eval::value_type& value)
123  : value(value)
124 {
125 }*/
126 
127 
134 /*
135 void Eval::overwrite(GeoObject* obj, double* dv) const
136 {
137  assert(dv);
138  boost::apply_visitor(DoubleVisitor(obj, dv), value);
139 }*/
140 
147 /*
148 void Eval::overwrite(GeoObject* obj, int* iv) const
149 {
150  assert(iv);
151  boost::apply_visitor(IntVisitor(obj, iv), value);
152 }*/
153 
160 /*
161 void Eval::overwrite(GeoObject* obj, Color* cv) const
162 {
163  assert(cv);
164  boost::apply_visitor(ColorVisitor(obj, cv), value);
165 }
166 */
167 
174 /*
175 void Eval::overwrite(GeoObject* obj, string* sv) const
176 {
177  assert(sv);
178  boost::apply_visitor(StringVisitor(obj, sv), value);
179 }
180 */
181 
182 
183 
193 shared_ptr<STNode> parseEval(string::const_iterator begin, string::const_iterator end, const shared_ptr<ParserLogger>& logger)
194 {
195  EvalGrammer eval_;
196  shared_ptr<STNode> root;
197 
198  GrammarIterator iterBegin(begin, end);
199  GrammarIterator iterEnd;
200 
201  bool result = false;
202  try {
203  result = qi::phrase_parse(iterBegin, iterEnd, (qi::lit("eval") >> '(' >> (('\"' > eval_ > '\"') | eval_) >> ')'), chs::space, root);
204  } catch(qi::expectation_failure<GrammarIterator>&)
205  {
206  result = false;
207  }
208 
209  if(!result || iterBegin != iterEnd)
210  return shared_ptr<STNode>();
211 
212  return root;
213 }
214 
215 }
Grammar used to parse eval expressions.
shared_ptr< STNode > parseEval(string::const_iterator begin, string::const_iterator end, const shared_ptr< ParserLogger > &logger)
Creates a new eval.
Definition: eval.cpp:193
classic::position_iterator2< StringIterator > GrammarIterator
Iterator wrapping the file iterator and to be used in all grammars.
Definition: mapcss_def.hpp:148
This file is part of alaCarte.