All Classes Namespaces Functions Variables 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/variant.hpp>
7 #include <xercesc/util/XercesDefs.hpp>
8 #include <mbxmlutilshelper/dom.h>
9 #include <casadi/casadi.hpp>
10 #include <unordered_map>
11 
12 #define XMLUTILS_EVAL_CONCAT1(X, Y) X##Y
13 #define XMLUTILS_EVAL_CONCAT(X, Y) XMLUTILS_EVAL_CONCAT1(X, Y)
14 #define XMLUTILS_EVAL_APPENDLINE(X) XMLUTILS_EVAL_CONCAT(X, __LINE__)
15 
17 #define XMLUTILS_EVAL_REGISTER(T) \
18  namespace { \
19  struct Reg { \
20  Reg() { Eval::registerEvaluator<T>(); } \
21  } XMLUTILS_EVAL_APPENDLINE(regDummy); \
22  }
23 
24 namespace XERCES_CPP_NAMESPACE {
25  class DOMElement;
26  class DOMAttr;
27  class DOMDocument;
28 }
29 
30 namespace MBXMLUtils {
31 
33 class CodeString : public std::string {
34  public:
35  CodeString(const std::string &str) : std::string(str) {}
36 };
37 
38 // Store the current directory in the ctor an restore in the dtor
40  public:
42  dir=boost::filesystem::current_path();
43  }
45  boost::filesystem::current_path(dir);
46  }
47  private:
48  boost::filesystem::path dir;
49 };
50 
51 class Eval;
52 
55  public:
57  NewParamLevel(const std::shared_ptr<Eval> &oe_, bool newLevel_=true);
60 
61  NewParamLevel(const NewParamLevel& other) = delete; // copy constructor
62  NewParamLevel(NewParamLevel&& other) = delete; // move constructor
63  NewParamLevel& operator=(const NewParamLevel& other) = delete; // copy assignment
64  NewParamLevel& operator=(NewParamLevel&& other) = delete; // move assignment
65  protected:
66  static void* operator new(std::size_t); // no heap allocation allowed
67  static void* operator new[](std::size_t); // no heap allocation allowed
68 
69  std::shared_ptr<Eval> oe;
70  bool newLevel;
71 };
72 
73 template<class T> struct SwigType { static std::string name; };
74 
76 class Eval : public std::enable_shared_from_this<Eval>, virtual public fmatvec::Atom {
77  public:
78  friend class NewParamLevel;
79 
82  enum ValueType {
83  ScalarType,
84  VectorType,
85  MatrixType,
86  StringType,
87  FunctionType
88  };
89 
91  typedef std::pair<std::vector<casadi::SX>, std::vector<casadi::SX>> Function;
92  typedef boost::variant<std::shared_ptr<void>, Function> Value;
93 
94  protected:
96  Eval(std::vector<boost::filesystem::path> *dependencies_);
97  public:
99  ~Eval();
100 
101  Eval(const Eval& other) = delete; // copy constructor
102  Eval(Eval&& other) = delete; // move constructor
103  Eval& operator=(const Eval& other) = delete; // copy assignment
104  Eval& operator=(Eval&& other) = delete; // move assignment
105 
107  static std::shared_ptr<Eval> createEvaluator(const std::string &evalName, std::vector<boost::filesystem::path> *dependencies_=NULL);
108 
109  // Register a new evaluator.
110  template<class E>
111  static void registerEvaluator() {
112  getEvaluators()[E::getNameStatic()]=&newEvaluator<E>;
113  }
114 
116  virtual std::string getName() const=0;
117 
119  void addParam(const std::string &paramName, const Value& value);
123  void addParamSet(const xercesc::DOMElement *e);
124 
127  virtual void addImport(const std::string &code, const xercesc::DOMElement *e)=0;
128 
131  Value eval(const xercesc::DOMElement *e);
132 
136  Value eval(const xercesc::DOMAttr *a, const xercesc::DOMElement *pe=NULL);
137 
276  template<typename T>
277  T cast(const Value &value, xercesc::DOMDocument *doc) const;
278 
280  template<typename T>
281  T cast(const Value &value) const;
282 
289  virtual bool valueIsOfType(const Value &value, ValueType type) const=0;
290 
294  Value stringToValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const;
295 
297  template<class T>
298  Value create(const T& v) const;
299 
301  virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >& requiredFiles() const=0;
302 
306  virtual void convertIndex(Value &v, bool evalTo1Based)=0;
307 
309  static void setValue(xercesc::DOMElement *e, const Value &v);
310 
311  protected:
313  void pushContext();
314 
316  void popContext();
317 
318  std::vector<boost::filesystem::path> *dependencies;
319 
320  // map of the current parameters
321  std::unordered_map<std::string, Value> currentParam;
322  // stack of parameters
323  std::stack<std::unordered_map<std::string, Value> > paramStack;
324 
325  // current imports
326  std::shared_ptr<void> currentImport;
327  // stack of imports
328  std::stack<std::shared_ptr<void> > importStack;
329 
330  template<typename T>
331  Value createSwig() const;
332 
334  virtual Value createSwigByTypeName(const std::string &typeName) const=0;
335 
349  virtual Value callFunction(const std::string &name, const std::vector<Value>& args) const=0;
350 
351  Value casadiValue;
352 
354  virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e=NULL) const=0;
355 
357  std::string partialStringToString(const std::string &str, const xercesc::DOMElement *e) const;
358 
359  template<class E>
360  static std::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
361  return std::shared_ptr<E>(new E(dependencies_));
362  }
363 
365  virtual void* getSwigThis(const Value &value) const=0;
367  virtual std::string getSwigType(const Value &value) const=0;
368 
369  void addStaticDependencies(const xercesc::DOMElement *e) const;
370 
371  private:
372  // virtual spezialization of cast(const Value &value)
373  virtual double cast_double (const Value &value) const=0;
374  virtual std::vector<double> cast_vector_double (const Value &value) const=0;
375  virtual std::vector<std::vector<double> > cast_vector_vector_double(const Value &value) const=0;
376  virtual std::string cast_string (const Value &value) const=0;
377  // spezialization of cast(const Value &value)
378  CodeString cast_CodeString (const Value &value) const;
379  int cast_int (const Value &value) const;
380  casadi::SX cast_SX (const Value &value) const;
381 
382  // spezialization of cast(const Value &value, xercesc::DOMDocument *doc)
383  xercesc::DOMElement* cast_DOMElement_p(const Value &value, xercesc::DOMDocument *doc) const;
384 
385  // virtual spezialization of create(...)
386  virtual Value create_double (const double& v) const=0;
387  virtual Value create_vector_double (const std::vector<double>& v) const=0;
388  virtual Value create_vector_vector_double(const std::vector<std::vector<double> >& v) const=0;
389  virtual Value create_string (const std::string& v) const=0;
390 
391  Value handleUnit(const xercesc::DOMElement *e, const Value &ret);
392 
393  static std::map<std::string, std::string> units;
394 
395  static std::map<std::string, std::function<std::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
396 
397 };
398 
399 template<typename T>
400 Eval::Value Eval::createSwig() const {
401  return createSwigByTypeName(SwigType<T>::name);
402 }
403 
404 template<typename T>
405 T Eval::cast(const Value &value) const {
406  // treat all types T as a swig type
407  if(getSwigType(value)!=SwigType<T>::name)
408  throw DOMEvalException("This variable is not of SWIG type "+SwigType<T>::name+".");
409  return static_cast<T>(getSwigThis(value));
410 }
411 // ... but prevere these specializations
412 template<> std::string Eval::cast<std::string>(const Value &value) const;
413 template<> CodeString Eval::cast<CodeString>(const Value &value) const;
414 template<> double Eval::cast<double>(const Value &value) const;
415 template<> int Eval::cast<int>(const Value &value) const;
416 template<> std::vector<double> Eval::cast<std::vector<double> >(const Value &value) const;
417 template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(const Value &value) const;
418 template<> casadi::SX Eval::cast<casadi::SX>(const Value &value) const;
419 template<> Eval::Function Eval::cast<Eval::Function>(const Value &value) const;
420 
421 // no template definition, only this spezialization
422 template<> xercesc::DOMElement* Eval::cast<xercesc::DOMElement*>(const Value &value, xercesc::DOMDocument *doc) const;
423 
424 // spezializations for create
425 template<> Eval::Value Eval::create<double> (const double& v) const;
426 template<> Eval::Value Eval::create<std::vector<double> > (const std::vector<double>& v) const;
427 template<> Eval::Value Eval::create<std::vector<std::vector<double> > >(const std::vector<std::vector<double> >& v) const;
428 template<> Eval::Value Eval::create<std::string> (const std::string& v) const;
429 
430 } // end namespace MBXMLUtils
431 
432 #endif
Create a new parameter level for a evaluator which is automatically resetted if the scope of this obj...
Definition: eval.h:54
NewParamLevel(const std::shared_ptr< Eval > &oe_, bool newLevel_=true)
Create a new parameter level in the evaluator oe_.
virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e=NULL) const =0
evaluate the string str using the current parameters and return the result.
Definition: eval.h:76
Value create(const T &v) const
create a value of the given type
virtual std::string getSwigType(const Value &value) const =0
get the SWIG type (class name) of this value.
void addParam(const std::string &paramName, const Value &value)
Add a value to the current parameters.
Definition: eval.cc:213
virtual void convertIndex(Value &v, bool evalTo1Based)=0
virtual std::string getName() const =0
Get the type of this evaluator.
virtual bool valueIsOfType(const Value &value, ValueType type) const =0
virtual void addImport(const std::string &code, const xercesc::DOMElement *e)=0
void addParamSet(const xercesc::DOMElement *e)
Definition: eval.cc:217
virtual void * getSwigThis(const Value &value) const =0
get the SWIG pointer of this value.
virtual Value createSwigByTypeName(const std::string &typeName) const =0
create a SWIG object of name typeName.
virtual Value callFunction(const std::string &name, const std::vector< Value > &args) const =0
Eval(std::vector< boost::filesystem::path > *dependencies_)
Constructor.
static std::shared_ptr< Eval > createEvaluator(const std::string &evalName, std::vector< boost::filesystem::path > *dependencies_=NULL)
Create a evaluator.
Definition: eval.cc:93
A dummy object representing a value as string in the syntax of the Eval.
Definition: eval.h:33
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:579
Definition: eval.h:73
T cast(const Value &value, xercesc::DOMDocument *doc) const
std::pair< std::vector< casadi::SX >, std::vector< casadi::SX > > Function
Typedef for a shared value.
Definition: eval.h:91
void pushContext()
Push the current context to a internal stack.
Definition: eval.cc:181
Exception during evaluation of the DOM tree including a location stack.
Definition: dom.h:279
Definition: eval.h:39
Value eval(const xercesc::DOMElement *e)
Value stringToValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const
Definition: eval.cc:622
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:66
static void setValue(xercesc::DOMElement *e, const Value &v)
Set value on DOMElement (is used by Eval::cast)
Definition: eval.cc:714
void popContext()
Overwrite the current context with the top level context from the internal stack. ...
Definition: eval.cc:186
~Eval()
Destructor.
Definition: eval.cc:128
ValueType
Definition: eval.h:82

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML