All Classes Namespaces Functions Typedefs Enumerations
eval.h
1 #ifndef _MBXMLUTILS_EVAL_H_
2 #define _MBXMLUTILS_EVAL_H_
3 
4 #include <fmatvec/atom.h>
5 #include <boost/filesystem.hpp>
6 #include <boost/function.hpp>
7 #include <boost/enable_shared_from_this.hpp>
8 #include <xercesc/util/XercesDefs.hpp>
9 #include <mbxmlutilshelper/dom.h>
10 #include <casadi/core/function/sx_function.hpp>
11 #ifdef HAVE_UNORDERED_MAP
12 # include <unordered_map>
13 #else
14 # include <map>
15 # define unordered_map map
16 #endif
17 
18 #define XMLUTILS_EVAL_CONCAT1(X, Y) X##Y
19 #define XMLUTILS_EVAL_CONCAT(X, Y) XMLUTILS_EVAL_CONCAT1(X, Y)
20 #define XMLUTILS_EVAL_APPENDLINE(X) XMLUTILS_EVAL_CONCAT(X, __LINE__)
21 
23 #define XMLUTILS_EVAL_REGISTER(T) \
24  namespace { \
25  struct Reg { \
26  Reg() { Eval::registerEvaluator<T>(); } \
27  } XMLUTILS_EVAL_APPENDLINE(regDummy); \
28  }
29 
30 namespace XERCES_CPP_NAMESPACE {
31  class DOMElement;
32  class DOMAttr;
33  class DOMDocument;
34 }
35 
36 namespace MBXMLUtils {
37 
39 class CodeString : public std::string {
40  public:
41  CodeString(const std::string &str) : std::string(str) {}
42 };
43 
44 // Store the current directory in the ctor an restore in the dtor
46  public:
48  dir=boost::filesystem::current_path();
49  }
51  boost::filesystem::current_path(dir);
52  }
53  private:
54  boost::filesystem::path dir;
55 };
56 
57 class Eval;
58 
60 class NewParamLevel : public boost::noncopyable {
61  public:
63  NewParamLevel(const boost::shared_ptr<Eval> &oe_, bool newLevel_=true);
66  protected:
67  static void* operator new(std::size_t); // no heap allocation allowed
68  static void* operator new[](std::size_t); // no heap allocation allowed
69 
70  boost::shared_ptr<Eval> oe;
71  bool newLevel;
72 };
73 
74 template<class T> struct SwigType { static std::string name; };
75 
77 class Eval : public boost::enable_shared_from_this<Eval>, public boost::noncopyable, virtual public fmatvec::Atom {
78  public:
79  friend class NewParamLevel;
80 
83  enum ValueType {
84  ScalarType,
85  VectorType,
86  MatrixType,
87  StringType,
88  SXFunctionType
89  };
90 
91  protected:
93  Eval(std::vector<boost::filesystem::path> *dependencies_);
94  public:
96  ~Eval();
97 
99  static boost::shared_ptr<Eval> createEvaluator(const std::string &evalName, std::vector<boost::filesystem::path> *dependencies_=NULL);
100 
101  // Register a new evaluator.
102  template<class E>
103  static void registerEvaluator() {
104  getEvaluators()[E::getNameStatic()]=&newEvaluator<E>;
105  }
106 
108  virtual std::string getName() const=0;
109 
111  void addParam(const std::string &paramName, const boost::shared_ptr<void>& value);
115  void addParamSet(const xercesc::DOMElement *e);
116 
119  virtual void addImport(const std::string &code, const xercesc::DOMElement *e, bool deprecated=false)=0;// MISSING: fullEval is just to support a deprected feature
120 
123  boost::shared_ptr<void> eval(const xercesc::DOMElement *e);
124 
128  boost::shared_ptr<void> eval(const xercesc::DOMAttr *a, const xercesc::DOMElement *pe=NULL);
129 
238  template<typename T>
239  T cast(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc) const;
240 
242  template<typename T>
243  T cast(const boost::shared_ptr<void> &value) const;
244 
251  virtual bool valueIsOfType(const boost::shared_ptr<void> &value, ValueType type) const=0;
252 
256  boost::shared_ptr<void> stringToValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const;
257 
259  template<class T>
260  boost::shared_ptr<void> create(const T& v) const;
261 
263  virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >& requiredFiles() const=0;
264 
266  virtual bool useOneBasedIndexes()=0;
267 
268  protected:
270  void pushContext();
271 
273  void popContext();
274 
275  std::vector<boost::filesystem::path> *dependencies;
276 
277  // map of the current parameters
278  std::unordered_map<std::string, boost::shared_ptr<void> > currentParam;
279  // stack of parameters
280  std::stack<std::unordered_map<std::string, boost::shared_ptr<void> > > paramStack;
281 
282  // current imports
283  boost::shared_ptr<void> currentImport;
284  // stack of imports
285  std::stack<boost::shared_ptr<void> > importStack;
286 
287  template<typename T>
288  boost::shared_ptr<void> createSwig() const;
289 
291  virtual boost::shared_ptr<void> createSwigByTypeName(const std::string &typeName) const=0;
292 
306  virtual boost::shared_ptr<void> callFunction(const std::string &name, const std::vector<boost::shared_ptr<void> >& args) const=0;
307 
308  static boost::shared_ptr<void> casadiValue;
309 
311  virtual boost::shared_ptr<void> fullStringToValue(const std::string &str, const xercesc::DOMElement *e=NULL) const=0;
312 
314  std::string partialStringToString(const std::string &str, const xercesc::DOMElement *e) const;
315 
316  template<class E>
317  static boost::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
318  return boost::shared_ptr<E>(new E(dependencies_));
319  }
320 
322  virtual void* getSwigThis(const boost::shared_ptr<void> &value) const=0;
324  virtual std::string getSwigType(const boost::shared_ptr<void> &value) const=0;
325 
326  void addStaticDependencies(const xercesc::DOMElement *e) const;
327 
328  private:
329  // virtual spezialization of cast(const boost::shared_ptr<void> &value)
330  virtual double cast_double (const boost::shared_ptr<void> &value) const=0;
331  virtual std::vector<double> cast_vector_double (const boost::shared_ptr<void> &value) const=0;
332  virtual std::vector<std::vector<double> > cast_vector_vector_double(const boost::shared_ptr<void> &value) const=0;
333  virtual std::string cast_string (const boost::shared_ptr<void> &value) const=0;
334  // spezialization of cast(const boost::shared_ptr<void> &value)
335  CodeString cast_CodeString (const boost::shared_ptr<void> &value) const;
336  int cast_int (const boost::shared_ptr<void> &value) const;
337 
338  // spezialization of cast(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
339  xercesc::DOMElement* cast_DOMElement_p(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc) const;
340 
341  // virtual spezialization of create(...)
342  virtual boost::shared_ptr<void> create_double (const double& v) const=0;
343  virtual boost::shared_ptr<void> create_vector_double (const std::vector<double>& v) const=0;
344  virtual boost::shared_ptr<void> create_vector_vector_double(const std::vector<std::vector<double> >& v) const=0;
345  virtual boost::shared_ptr<void> create_string (const std::string& v) const=0;
346 
347  boost::shared_ptr<void> handleUnit(const xercesc::DOMElement *e, const boost::shared_ptr<void> &ret);
348 
349  static std::map<std::string, std::string> units;
350 
351  static std::map<std::string, boost::function<boost::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
352 
353 };
354 
355 template<typename T>
356 boost::shared_ptr<void> Eval::createSwig() const {
357  return createSwigByTypeName(SwigType<T>::name);
358 }
359 
360 template<typename T>
361 T Eval::cast(const boost::shared_ptr<void> &value) const {
362  // treat all types T as a swig type
363  if(getSwigType(value)!=SwigType<T>::name)
364  throw DOMEvalException("This variable is not of SWIG type "+SwigType<T>::name+".");
365  return static_cast<T>(getSwigThis(value));
366 }
367 // ... but prevere these specializations
368 template<> std::string Eval::cast<std::string>(const boost::shared_ptr<void> &value) const;
369 template<> CodeString Eval::cast<CodeString>(const boost::shared_ptr<void> &value) const;
370 template<> double Eval::cast<double>(const boost::shared_ptr<void> &value) const;
371 template<> int Eval::cast<int>(const boost::shared_ptr<void> &value) const;
372 template<> std::vector<double> Eval::cast<std::vector<double> >(const boost::shared_ptr<void> &value) const;
373 template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(const boost::shared_ptr<void> &value) const;
374 
375 // no template definition, only this spezialization
376 template<> xercesc::DOMElement* Eval::cast<xercesc::DOMElement*>(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc) const;
377 
378 // spezializations for create
379 template<> boost::shared_ptr<void> Eval::create<double> (const double& v) const;
380 template<> boost::shared_ptr<void> Eval::create<std::vector<double> > (const std::vector<double>& v) const;
381 template<> boost::shared_ptr<void> Eval::create<std::vector<std::vector<double> > >(const std::vector<std::vector<double> >& v) const;
382 template<> boost::shared_ptr<void> Eval::create<std::string> (const std::string& v) const;
383 
384 } // end namespace MBXMLUtils
385 
386 #endif
Create a new parameter level for a evaluator which is automatically resetted if the scope of this obj...
Definition: eval.h:60
virtual boost::shared_ptr< void > callFunction(const std::string &name, const std::vector< boost::shared_ptr< void > > &args) const =0
Definition: eval.h:77
virtual bool valueIsOfType(const boost::shared_ptr< void > &value, ValueType type) const =0
boost::shared_ptr< void > stringToValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const
Definition: eval.cc:545
virtual std::string getName() const =0
Get the type of this evaluator.
void addParam(const std::string &paramName, const boost::shared_ptr< void > &value)
Add a value to the current parameters.
Definition: eval.cc:170
virtual bool useOneBasedIndexes()=0
Return true if the evaluator used one based indexes or false if zero based indexes are used...
void addParamSet(const xercesc::DOMElement *e)
Definition: eval.cc:174
virtual void addImport(const std::string &code, const xercesc::DOMElement *e, bool deprecated=false)=0
T cast(const boost::shared_ptr< void > &value, xercesc::DOMDocument *doc) const
virtual boost::shared_ptr< void > fullStringToValue(const std::string &str, const xercesc::DOMElement *e=NULL) const =0
evaluate the string str using the current parameters and return the result.
Eval(std::vector< boost::filesystem::path > *dependencies_)
Constructor.
Definition: eval.cc:52
virtual std::string getSwigType(const boost::shared_ptr< void > &value) const =0
get the SWIG type (class name) of this value.
static boost::shared_ptr< Eval > createEvaluator(const std::string &evalName, std::vector< boost::filesystem::path > *dependencies_=NULL)
Create a evaluator.
Definition: eval.cc:70
A dummy object representing a value as string in the syntax of the Eval.
Definition: eval.h:39
std::string partialStringToString(const std::string &str, const xercesc::DOMElement *e) const
evaluate str partially and return result as an std::string
Definition: eval.cc:507
Definition: eval.h:74
boost::shared_ptr< void > create(const T &v) const
create a value of the given type
virtual void * getSwigThis(const boost::shared_ptr< void > &value) const =0
get the SWIG pointer of this value.
void pushContext()
Push the current context to a internal stack.
Definition: eval.cc:138
Definition: eval.h:45
boost::shared_ptr< void > eval(const xercesc::DOMElement *e)
NewParamLevel(const boost::shared_ptr< Eval > &oe_, bool newLevel_=true)
Create a new parameter level in the evaluator oe_.
Definition: eval.cc:35
virtual std::map< boost::filesystem::path, std::pair< boost::filesystem::path, bool > > & requiredFiles() const =0
return a list of all required files of the evaluator (excluding dependent files of libraries) ...
~NewParamLevel()
Reset to the previous parameter level.
Definition: eval.cc:40
void popContext()
Overwrite the current context with the top level context from the internal stack. ...
Definition: eval.cc:143
virtual boost::shared_ptr< void > createSwigByTypeName(const std::string &typeName) const =0
create a SWIG object of name typeName.
~Eval()
Destructor.
Definition: eval.cc:95
ValueType
Definition: eval.h:83

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML