mbxmlutils  1.3.0
Multi-Body XML Utils
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 <xercesc/util/XercesDefs.hpp>
7 #include <mbxmlutilshelper/dom.h>
8 #include <mbxmlutilshelper/thislinelocation.h>
9 #include <unordered_map>
10 #include <stack>
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 
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::shared_ptr<void> Value;
92 
93  static boost::filesystem::path installPath;
94 
95  protected:
97  Eval(std::vector<boost::filesystem::path> *dependencies_);
98  public:
100  ~Eval() override;
101 
102  Eval(const Eval& other) = delete; // copy constructor
103  Eval(Eval&& other) = delete; // move constructor
104  Eval& operator=(const Eval& other) = delete; // copy assignment
105  Eval& operator=(Eval&& other) = delete; // move assignment
106 
108  static std::shared_ptr<Eval> createEvaluator(const std::string &evalName, std::vector<boost::filesystem::path> *dependencies_=nullptr);
109 
110  // Register a new evaluator.
111  template<class E>
112  static void registerEvaluator() {
113  getEvaluators()[E::getNameStatic()]=&newEvaluator<E>;
114  }
115 
117  virtual std::string getName() const=0;
118 
120  void addParam(const std::string &paramName, const Value& value);
124  void addParamSet(const xercesc::DOMElement *e);
125 
128  virtual void addImport(const std::string &code, const xercesc::DOMElement *e)=0;
129 
132  Value eval(const xercesc::DOMElement *e);
133 
137  Value eval(const xercesc::DOMAttr *a);
138 
206  template<typename T>
207  T cast(const Value &value) const;
208 
215  virtual bool valueIsOfType(const Value &value, ValueType type) const=0;
216 
220  Value stringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr, bool fullEval=true) const;
221 
227  template<class T>
228  Value create(const T& v) const;
229 
231  virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >& requiredFiles() const=0;
232 
236  virtual void convertIndex(Value &v, bool evalTo1Based)=0;
237 
239  static void setValue(xercesc::DOMElement *e, const Value &v);
240 
241  protected:
243  virtual Value createFunctionIndep(int dim) const = 0;
245  virtual Value createFunctionDep(const std::vector<Value>& v) const=0;
247  virtual Value createFunctionDep(const std::vector<std::vector<Value> >& v) const=0;
249  virtual Value createFunction(const std::vector<Value> &indeps, const Value &dep) const = 0;
250 
252  void pushContext();
253 
255  void popContext();
256 
257  std::vector<boost::filesystem::path> *dependencies;
258 
259  // map of the current parameters
260  std::unordered_map<std::string, Value> currentParam;
261  // stack of parameters
262  std::stack<std::unordered_map<std::string, Value> > paramStack;
263 
264  // current imports
265  Value currentImport;
266  // stack of imports
267  std::stack<Value> importStack;
268 
282  virtual Value callFunction(const std::string &name, const std::vector<Value>& args) const=0;
283 
285  virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e) const=0;
286 
288  std::string partialStringToString(const std::string &str, const xercesc::DOMElement *e) const;
289 
290  template<class E>
291  static std::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
292  return std::shared_ptr<E>(new E(dependencies_));
293  }
294 
295  void addStaticDependencies(const xercesc::DOMElement *e) const;
296 
297  private:
298  // virtual spezialization of cast(const Value &value)
299  virtual double cast_double (const Value &value) const=0;
300  virtual std::vector<double> cast_vector_double (const Value &value) const=0;
301  virtual std::vector<std::vector<double> > cast_vector_vector_double(const Value &value) const=0;
302  virtual std::string cast_string (const Value &value) const=0;
303  // spezialization of cast(const Value &value)
304  CodeString cast_CodeString (const Value &value) const;
305  int cast_int (const Value &value) const;
306 
307  // virtual spezialization of create(...)
308  virtual Value create_double (const double& v) const=0;
309  virtual Value create_vector_double (const std::vector<double>& v) const=0;
310  virtual Value create_vector_vector_double (const std::vector<std::vector<double> >& v) const=0;
311  virtual Value create_string (const std::string& v) const=0;
312 
313  virtual std::string serializeFunction(const Value &x) const = 0;
314 
315  Value handleUnit(const xercesc::DOMElement *e, const Value &ret);
316 
317  static std::map<std::string, std::string> units;
318 
319  static std::map<std::string, std::function<std::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
320 
321 };
322 
323 // specializations
324 template<> std::string Eval::cast<std::string>(const Value &value) const;
325 template<> CodeString Eval::cast<CodeString>(const Value &value) const;
326 template<> double Eval::cast<double>(const Value &value) const;
327 template<> int Eval::cast<int>(const Value &value) const;
328 template<> std::vector<double> Eval::cast<std::vector<double> >(const Value &value) const;
329 template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(const Value &value) const;
330 
331 // spezializations for create
332 template<> Eval::Value Eval::create<double> (const double& v) const;
333 template<> Eval::Value Eval::create<std::vector<double> > (const std::vector<double>& v) const;
334 template<> Eval::Value Eval::create<std::vector<std::vector<double> > > (const std::vector<std::vector<double> >& v) const;
335 template<> Eval::Value Eval::create<std::string> (const std::string& v) const;
336 template<> Eval::Value Eval::create<std::vector<Eval::Value>> (const std::vector<Value>& v) const;
337 template<> Eval::Value Eval::create<std::vector<std::vector<Eval::Value>>>(const std::vector<std::vector<Value> >& v) const;
338 
339 } // end namespace MBXMLUtils
340 
341 #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:76
virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e) const =0
evaluate the string str using the current parameters and return the result.
void addParam(const std::string &paramName, const Value &value)
Add a value to the current parameters.
Definition: eval.cc:214
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 Value createFunctionIndep(int dim) const =0
create a function independent variable. If dim == 0 a scalar is created else a vector.
virtual void addImport(const std::string &code, const xercesc::DOMElement *e)=0
void addParamSet(const xercesc::DOMElement *e)
Definition: eval.cc:218
virtual Value callFunction(const std::string &name, const std::vector< Value > &args) const =0
Eval(std::vector< boost::filesystem::path > *dependencies_)
Constructor.
Value create(const T &v) const
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:566
T cast(const Value &value) const
A dummy object representing a value as string in the syntax of the Eval.
Definition: eval.h:35
void pushContext()
Push the current context to a internal stack.
Definition: eval.cc:182
~Eval() override
Destructor.
virtual Value createFunction(const std::vector< Value > &indeps, const Value &dep) const =0
create a Function with n independents and a dependent function (scalar, vector or matrix)
Definition: eval.h:41
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:111
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)
std::shared_ptr< void > Value
Typedef for a shared value.
Definition: eval.h:91
virtual Value createFunctionDep(const std::vector< Value > &v) const =0
create a vector function dependent
~NewParamLevel()
Reset to the previous parameter level.
Definition: eval.cc:86
static void setValue(xercesc::DOMElement *e, const Value &v)
Set value on DOMElement (is used by Eval::cast)
Definition: eval.cc:679
void popContext()
Overwrite the current context with the top level context from the internal stack.
Definition: eval.cc:187
Value stringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr, bool fullEval=true) const
Definition: eval.cc:614
ValueType
Definition: eval.h:82