1#ifndef _MBXMLUTILS_EVAL_H_
2#define _MBXMLUTILS_EVAL_H_
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>
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__)
17#define MBXMLUTILS_EVAL_REGISTER(T) \
20 Reg() { Eval::registerEvaluator<T>(); } \
21 } MBXMLUTILS_EVAL_APPENDLINE(regDummy); \
24namespace XERCES_CPP_NAMESPACE {
32bool tryDouble2Int(
double d,
int &i);
37 CodeString(
const std::string &str) : std::string(str) {}
44 dir=boost::filesystem::current_path();
47 boost::filesystem::current_path(dir);
50 boost::filesystem::path dir;
68 static void*
operator new(std::size_t);
69 static void*
operator new[](std::size_t);
71 std::shared_ptr<Eval> oe;
76class Eval :
public std::enable_shared_from_this<Eval>,
virtual public fmatvec::Atom {
91 using Value = std::shared_ptr<void>;
93 static boost::filesystem::path installPath;
97 Eval(std::vector<boost::filesystem::path> *dependencies_);
104 Eval& operator=(
const Eval& other) =
delete;
105 Eval& operator=(
Eval&& other) =
delete;
108 static std::shared_ptr<Eval>
createEvaluator(
const std::string &evalName, std::vector<boost::filesystem::path> *dependencies_=
nullptr);
112 static void registerEvaluator() {
113 getEvaluators()[E::getNameStatic()]=&newEvaluator<E>;
120 void addParam(
const std::string ¶mName,
const Value& value);
128 virtual void addImport(
const std::string &code,
const xercesc::DOMElement *e,
const std::string &action=
"")=0;
141 Value eval(
const std::string &str,
const xercesc::DOMElement *e=
nullptr,
bool skipRet=
false);
227 Value stringToValue(
const std::string &str,
const xercesc::DOMElement *e=
nullptr,
bool fullEval=
true,
bool skipRet=
false)
const;
238 virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >&
requiredFiles()
const=0;
246 static void setValue(xercesc::DOMElement *e,
const Value &v);
263 size_t getStackSize() { assert(paramStack.size()==importStack.size());
return paramStack.size(); }
281 std::vector<boost::filesystem::path> *dependencies;
284 std::unordered_map<std::string, Value> currentParam;
286 std::stack<std::unordered_map<std::string, Value> > paramStack;
289 std::unordered_map<std::string, Value> currentImport;
291 std::stack<std::unordered_map<std::string, Value> > importStack;
300 static std::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
301 return std::shared_ptr<E>(
new E(dependencies_));
304 void addStaticDependencies(
const xercesc::DOMElement *e)
const;
306 static void printEvaluatorMsg(
const std::ostringstream &str, MsgType msgType);
310 virtual double cast_double (
const Value &value)
const=0;
311 virtual std::vector<double> cast_vector_double (
const Value &value)
const=0;
312 virtual std::vector<std::vector<double> > cast_vector_vector_double(
const Value &value)
const=0;
313 virtual std::string cast_string (
const Value &value)
const=0;
316 int cast_int (
const Value &value)
const;
319 virtual Value create_double (
const double& v)
const=0;
320 virtual Value create_vector_double (
const std::vector<double>& v)
const=0;
321 virtual Value create_vector_vector_double (
const std::vector<std::vector<double> >& v)
const=0;
322 virtual Value create_string (
const std::string& v)
const=0;
324 virtual std::string serializeFunction(
const Value &x)
const = 0;
326 Value handleUnit(
const xercesc::DOMElement *e,
const Value &ret);
328 static std::map<std::string, std::string> units;
330 static std::map<std::string, std::function<std::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
335template<> std::string Eval::cast<std::string>(
const Value &value)
const;
336template<> CodeString Eval::cast<CodeString>(
const Value &value)
const;
337template<>
double Eval::cast<double>(
const Value &value)
const;
338template<>
int Eval::cast<int>(
const Value &value)
const;
339template<> std::vector<double> Eval::cast<std::vector<double> >(
const Value &value)
const;
340template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(
const Value &value)
const;
343template<>
Eval::Value Eval::create<double> (
const double& v)
const;
344template<>
Eval::Value Eval::create<std::vector<double> > (
const std::vector<double>& v)
const;
345template<>
Eval::Value Eval::create<std::vector<std::vector<double> > > (
const std::vector<std::vector<double> >& v)
const;
346template<>
Eval::Value Eval::create<std::string> (
const std::string& v)
const;
A dummy object representing a value as string in the syntax of the Eval.
Definition: eval.h:35
virtual std::string getName() const =0
Get the type of this evaluator.
ValueType
Definition: eval.h:82
void addParam(const std::string ¶mName, const Value &value)
Add a value to the current parameters.
Definition: eval.cc:219
std::shared_ptr< void > Value
Typedef for a shared value.
Definition: eval.h:91
static std::shared_ptr< Eval > createEvaluator(const std::string &evalName, std::vector< boost::filesystem::path > *dependencies_=nullptr)
Create a evaluator.
Definition: eval.cc:116
virtual Value callFunction(const std::string &name, const std::vector< Value > &args) const =0
~Eval() override
Destructor.
T cast(const Value &value) const
void popContext()
Overwrite the current context with the top level context from the internal stack.
Definition: eval.cc:192
virtual Value createFunctionIndep(int dim) const =0
create a function independent variable. If dim == 0 a scalar is created else a vector.
virtual void convertIndex(Value &v, bool evalTo1Based)=0
virtual Value createFunctionDep(const std::vector< std::vector< Value > > &v) const =0
create a matrix function dependent
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)
virtual std::string getStringRepresentation(const Value &x) const
Returns a evaluator specific string representation of x. This is only useful for display to the user.
Definition: eval.cc:706
static void setValue(xercesc::DOMElement *e, const Value &v)
Set value on DOMElement (is used by Eval::cast)
Definition: eval.cc:719
Value eval(const xercesc::DOMElement *e)
Value stringToValue(const std::string &str, const xercesc::DOMElement *e=nullptr, bool fullEval=true, bool skipRet=false) const
Definition: eval.cc:650
virtual void addImport(const std::string &code, const xercesc::DOMElement *e, const std::string &action="")=0
Value create(const T &v) const
virtual Value fullStringToValue(const std::string &str, const xercesc::DOMElement *e, bool skipRet=false) const =0
evaluate the string str using the current parameters and return the result.
void pushContext()
Push the current context to a internal stack.
Definition: eval.cc:187
void addParamSet(const xercesc::DOMElement *e)
Definition: eval.cc:223
virtual bool valueIsOfType(const Value &value, ValueType type) const =0
Value eval(const std::string &str, const xercesc::DOMElement *e=nullptr, bool skipRet=false)
virtual Value createFunctionDep(const std::vector< Value > &v) const =0
create a vector function dependent
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)
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:602
Eval(std::vector< boost::filesystem::path > *dependencies_)
Constructor.
Create a new parameter level for a evaluator which is automatically resetted if the scope of this obj...
Definition: eval.h:56
~NewParamLevel()
Reset to the previous parameter level.
Definition: eval.cc:91
NewParamLevel(std::shared_ptr< Eval > oe_, bool newLevel_=true)
Create a new parameter level in the evaluator oe_.