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 
268  template<typename T>
269  T cast(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc) const;
270 
272  template<typename T>
273  T cast(const boost::shared_ptr<void> &value) const;
274 
281  virtual bool valueIsOfType(const boost::shared_ptr<void> &value, ValueType type) const=0;
282 
286  boost::shared_ptr<void> stringToValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const;
287 
289  template<class T>
290  boost::shared_ptr<void> create(const T& v) const;
291 
293  virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >& requiredFiles() const=0;
294 
296  virtual bool useOneBasedIndexes()=0;
297 
298  protected:
300  void pushContext();
301 
303  void popContext();
304 
305  std::vector<boost::filesystem::path> *dependencies;
306 
307  // map of the current parameters
308  std::unordered_map<std::string, boost::shared_ptr<void> > currentParam;
309  // stack of parameters
310  std::stack<std::unordered_map<std::string, boost::shared_ptr<void> > > paramStack;
311 
312  // current imports
313  boost::shared_ptr<void> currentImport;
314  // stack of imports
315  std::stack<boost::shared_ptr<void> > importStack;
316 
317  template<typename T>
318  boost::shared_ptr<void> createSwig() const;
319 
321  virtual boost::shared_ptr<void> createSwigByTypeName(const std::string &typeName) const=0;
322 
336  virtual boost::shared_ptr<void> callFunction(const std::string &name, const std::vector<boost::shared_ptr<void> >& args) const=0;
337 
338  boost::shared_ptr<void> casadiValue;
339 
341  virtual boost::shared_ptr<void> fullStringToValue(const std::string &str, const xercesc::DOMElement *e=NULL) const=0;
342 
344  std::string partialStringToString(const std::string &str, const xercesc::DOMElement *e) const;
345 
346  template<class E>
347  static boost::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
348  return boost::shared_ptr<E>(new E(dependencies_));
349  }
350 
352  virtual void* getSwigThis(const boost::shared_ptr<void> &value) const=0;
354  virtual std::string getSwigType(const boost::shared_ptr<void> &value) const=0;
355 
356  void addStaticDependencies(const xercesc::DOMElement *e) const;
357 
358  private:
359  // virtual spezialization of cast(const boost::shared_ptr<void> &value)
360  virtual double cast_double (const boost::shared_ptr<void> &value) const=0;
361  virtual std::vector<double> cast_vector_double (const boost::shared_ptr<void> &value) const=0;
362  virtual std::vector<std::vector<double> > cast_vector_vector_double(const boost::shared_ptr<void> &value) const=0;
363  virtual std::string cast_string (const boost::shared_ptr<void> &value) const=0;
364  // spezialization of cast(const boost::shared_ptr<void> &value)
365  CodeString cast_CodeString (const boost::shared_ptr<void> &value) const;
366  int cast_int (const boost::shared_ptr<void> &value) const;
367  casadi::SX cast_SX (const boost::shared_ptr<void> &value) const;
368 
369  // spezialization of cast(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
370  xercesc::DOMElement* cast_DOMElement_p(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc) const;
371 
372  // virtual spezialization of create(...)
373  virtual boost::shared_ptr<void> create_double (const double& v) const=0;
374  virtual boost::shared_ptr<void> create_vector_double (const std::vector<double>& v) const=0;
375  virtual boost::shared_ptr<void> create_vector_vector_double(const std::vector<std::vector<double> >& v) const=0;
376  virtual boost::shared_ptr<void> create_string (const std::string& v) const=0;
377 
378  boost::shared_ptr<void> handleUnit(const xercesc::DOMElement *e, const boost::shared_ptr<void> &ret);
379 
380  static std::map<std::string, std::string> units;
381 
382  static std::map<std::string, boost::function<boost::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
383 
384 };
385 
386 template<typename T>
387 boost::shared_ptr<void> Eval::createSwig() const {
388  return createSwigByTypeName(SwigType<T>::name);
389 }
390 
391 template<typename T>
392 T Eval::cast(const boost::shared_ptr<void> &value) const {
393  // treat all types T as a swig type
394  if(getSwigType(value)!=SwigType<T>::name)
395  throw DOMEvalException("This variable is not of SWIG type "+SwigType<T>::name+".");
396  return static_cast<T>(getSwigThis(value));
397 }
398 // ... but prevere these specializations
399 template<> std::string Eval::cast<std::string>(const boost::shared_ptr<void> &value) const;
400 template<> CodeString Eval::cast<CodeString>(const boost::shared_ptr<void> &value) const;
401 template<> double Eval::cast<double>(const boost::shared_ptr<void> &value) const;
402 template<> int Eval::cast<int>(const boost::shared_ptr<void> &value) const;
403 template<> std::vector<double> Eval::cast<std::vector<double> >(const boost::shared_ptr<void> &value) const;
404 template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(const boost::shared_ptr<void> &value) const;
405 template<> casadi::SX Eval::cast<casadi::SX>(const boost::shared_ptr<void> &value) const;
406 
407 // no template definition, only this spezialization
408 template<> xercesc::DOMElement* Eval::cast<xercesc::DOMElement*>(const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc) const;
409 
410 // spezializations for create
411 template<> boost::shared_ptr<void> Eval::create<double> (const double& v) const;
412 template<> boost::shared_ptr<void> Eval::create<std::vector<double> > (const std::vector<double>& v) const;
413 template<> boost::shared_ptr<void> Eval::create<std::vector<std::vector<double> > >(const std::vector<std::vector<double> >& v) const;
414 template<> boost::shared_ptr<void> Eval::create<std::string> (const std::string& v) const;
415 
416 } // end namespace MBXMLUtils
417 
418 #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:547
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:172
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:176
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:50
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:68
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:509
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:140
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:145
virtual boost::shared_ptr< void > createSwigByTypeName(const std::string &typeName) const =0
create a SWIG object of name typeName.
~Eval()
Destructor.
Definition: eval.cc:92
ValueType
Definition: eval.h:83

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML