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 MBXMLUTILS_EVAL_CONCAT1(X, Y) X##Y
13 #define MBXMLUTILS_EVAL_CONCAT(X, Y) MBXMLUTILS_EVAL_CONCAT1(X, Y)
14 #define MBXMLUTILS_EVAL_APPENDLINE(X) MBXMLUTILS_EVAL_CONCAT(X, __LINE__)
15 
17 #define MBXMLUTILS_EVAL_REGISTER(T) \
18  namespace { \
19  struct Reg { \
20  Reg() { Eval::registerEvaluator<T>(); } \
21  } MBXMLUTILS_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 
32 bool tryDouble2Int(double d, int &i);
33 
35 class CodeString : public std::string {
36  public:
37  CodeString(const std::string &str) : std::string(str) {}
38 };
39 
40 // Store the current directory in the ctor an restore in the dtor
42  public:
44  dir=boost::filesystem::current_path();
45  }
47  boost::filesystem::current_path(dir);
48  }
49  private:
50  boost::filesystem::path dir;
51 };
52 
53 class Eval;
54 
57  public:
59  NewParamLevel(std::shared_ptr<Eval> oe_, bool newLevel_=true);
62 
63  NewParamLevel(const NewParamLevel& other) = delete; // copy constructor
64  NewParamLevel(NewParamLevel&& other) = delete; // move constructor
65  NewParamLevel& operator=(const NewParamLevel& other) = delete; // copy assignment
66  NewParamLevel& operator=(NewParamLevel&& other) = delete; // move assignment
67  protected:
68  static void* operator new(std::size_t); // no heap allocation allowed
69  static void* operator new[](std::size_t); // no heap allocation allowed
70 
71  std::shared_ptr<Eval> oe;
72  bool newLevel;
73 };
74 
75 template<class T> struct SwigType { static std::string name; };
76 
78 class Eval : public std::enable_shared_from_this<Eval>, virtual public fmatvec::Atom {
79  public:
80  friend class NewParamLevel;
81 
84  enum ValueType {
85  ScalarType,
86  VectorType,
87  MatrixType,
88  StringType,
89  FunctionType
90  };
91 
93  typedef std::pair<std::vector<casadi::SX>, std::vector<casadi::SX>> Function;
94  typedef boost::variant<std::shared_ptr<void>, Function> Value;
95 
96  protected:
98  Eval(std::vector<boost::filesystem::path> *dependencies_);
99  public:
101  ~Eval() override;
102 
103  Eval(const Eval& other) = delete; // copy constructor
104  Eval(Eval&& other) = delete; // move constructor
105  Eval& operator=(const Eval& other) = delete; // copy assignment
106  Eval& operator=(Eval&& other) = delete; // move assignment
107 
109  static std::shared_ptr<Eval> createEvaluator(const std::string &evalName, std::vector<boost::filesystem::path> *dependencies_=nullptr);
110 
111  // Register a new evaluator.
112  template<class E>
113  static void registerEvaluator() {
114  getEvaluators()[E::getNameStatic()]=&newEvaluator<E>;
115  }
116 
118  virtual std::string getName() const=0;
119 
121  void addParam(const std::string &paramName, const Value& value);
125  void addParamSet(const xercesc::DOMElement *e);
126 
129  virtual void addImport(const std::string &code, const xercesc::DOMElement *e)=0;
130 
133  Value eval(const xercesc::DOMElement *e);
134 
138  Value eval(const xercesc::DOMAttr *a);
139 
278  template<typename T>
279  T cast(const Value &value, xercesc::DOMDocument *doc) const;
280 
282  template<typename T>
283  T cast(const Value &value) const;
284 
291  virtual bool valueIsOfType(const Value &value, ValueType type) const=0;
292 
296  Value stringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr, bool fullEval=true) const;
297 
299  template<class T>
300  Value create(const T& v) const;
301 
303  virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >& requiredFiles() const=0;
304 
308  virtual void convertIndex(Value &v, bool evalTo1Based)=0;
309 
311  static void setValue(xercesc::DOMElement *e, const Value &v);
312 
313  protected:
315  void pushContext();
316 
318  void popContext();
319 
320  std::vector<boost::filesystem::path> *dependencies;
321 
322  // map of the current parameters
323  std::unordered_map<std::string, Value> currentParam;
324  // stack of parameters
325  std::stack<std::unordered_map<std::string, Value> > paramStack;
326 
327  // current imports
328  std::shared_ptr<void> currentImport;
329  // stack of imports
330  std::stack<std::shared_ptr<void> > importStack;
331 
332  template<typename T>
333  Value createSwig() const;
334 
336  virtual Value createSwigByTypeName(const std::string &typeName) const=0;
337 
351  virtual Value callFunction(const std::string &name, const std::vector<Value>& args) const=0;
352 
353  Value casadiValue;
354 
356  virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr) const=0;
357 
359  std::string partialStringToString(const std::string &str, const xercesc::DOMElement *e) const;
360 
361  template<class E>
362  static std::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
363  return std::shared_ptr<E>(new E(dependencies_));
364  }
365 
367  virtual void* getSwigThis(const Value &value) const=0;
370  virtual std::string getSwigType(const Value &value) const=0;
371 
372  void addStaticDependencies(const xercesc::DOMElement *e) const;
373 
374  private:
375  // virtual spezialization of cast(const Value &value)
376  virtual double cast_double (const Value &value) const=0;
377  virtual std::vector<double> cast_vector_double (const Value &value) const=0;
378  virtual std::vector<std::vector<double> > cast_vector_vector_double(const Value &value) const=0;
379  virtual std::string cast_string (const Value &value) const=0;
380  // spezialization of cast(const Value &value)
381  CodeString cast_CodeString (const Value &value) const;
382  int cast_int (const Value &value) const;
383  casadi::SX cast_SX (const Value &value) const;
384 
385  // spezialization of cast(const Value &value, xercesc::DOMDocument *doc)
386  xercesc::DOMElement* cast_DOMElement_p(const Value &value, xercesc::DOMDocument *doc) const;
387 
388  // virtual spezialization of create(...)
389  virtual Value create_double (const double& v) const=0;
390  virtual Value create_vector_double (const std::vector<double>& v) const=0;
391  virtual Value create_vector_vector_double(const std::vector<std::vector<double> >& v) const=0;
392  virtual Value create_string (const std::string& v) const=0;
393 
394  Value handleUnit(const xercesc::DOMElement *e, const Value &ret);
395 
396  static std::map<std::string, std::string> units;
397 
398  static std::map<std::string, std::function<std::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
399 
400 };
401 
402 template<typename T>
403 Eval::Value Eval::createSwig() const {
404  return createSwigByTypeName(SwigType<T>::name);
405 }
406 
407 template<typename T>
408 T Eval::cast(const Value &value) const {
409  // treat all types T as a swig type
410  if(getSwigType(value)!=SwigType<T>::name)
411  throw std::runtime_error("This variable is not of SWIG type "+SwigType<T>::name+".");
412  return static_cast<T>(getSwigThis(value));
413 }
414 // ... but prevere these specializations
415 template<> std::string Eval::cast<std::string>(const Value &value) const;
416 template<> CodeString Eval::cast<CodeString>(const Value &value) const;
417 template<> double Eval::cast<double>(const Value &value) const;
418 template<> int Eval::cast<int>(const Value &value) const;
419 template<> std::vector<double> Eval::cast<std::vector<double> >(const Value &value) const;
420 template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(const Value &value) const;
421 template<> casadi::SX Eval::cast<casadi::SX>(const Value &value) const;
422 template<> Eval::Function Eval::cast<Eval::Function>(const Value &value) const;
423 
424 // no template definition, only this spezialization
425 template<> xercesc::DOMElement* Eval::cast<xercesc::DOMElement*>(const Value &value, xercesc::DOMDocument *doc) const;
426 
427 // spezializations for create
428 template<> Eval::Value Eval::create<double> (const double& v) const;
429 template<> Eval::Value Eval::create<std::vector<double> > (const std::vector<double>& v) const;
430 template<> Eval::Value Eval::create<std::vector<std::vector<double> > >(const std::vector<std::vector<double> >& v) const;
431 template<> Eval::Value Eval::create<std::string> (const std::string& v) const;
432 
433 } // end namespace MBXMLUtils
434 
435 #endif
Create a new parameter level for a evaluator which is automatically resetted if the scope of this obj...
Definition: eval.h:56
Definition: eval.h:78
Value create(const T &v) const
create a value of the given type
virtual std::string getSwigType(const Value &value) const =0
void addParam(const std::string &paramName, const Value &value)
Add a value to the current parameters.
Definition: eval.cc:225
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:229
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.
Value stringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr, bool fullEval=true) const
Definition: eval.cc:638
A dummy object representing a value as string in the syntax of the Eval.
Definition: eval.h:35
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:594
Definition: eval.h:75
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:93
void pushContext()
Push the current context to a internal stack.
Definition: eval.cc:193
~Eval() override
Destructor.
Definition: eval.h:41
virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr) const =0
evaluate the string str using the current parameters and return the result.
Value eval(const xercesc::DOMElement *e)
static std::shared_ptr< Eval > createEvaluator(const std::string &evalName, std::vector< boost::filesystem::path > *dependencies_=nullptr)
Create a evaluator.
Definition: eval.cc:106
NewParamLevel(std::shared_ptr< Eval > oe_, bool newLevel_=true)
Create a new parameter level in the evaluator oe_.
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:79
static void setValue(xercesc::DOMElement *e, const Value &v)
Set value on DOMElement (is used by Eval::cast)
Definition: eval.cc:726
void popContext()
Overwrite the current context with the top level context from the internal stack. ...
Definition: eval.cc:198
ValueType
Definition: eval.h:84

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home