1 #ifndef _MBXMLUTILS_EVAL_H_
2 #define _MBXMLUTILS_EVAL_H_
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>
15 # define unordered_map map
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__)
23 #define XMLUTILS_EVAL_REGISTER(T) \
26 Reg() { Eval::registerEvaluator<T>(); } \
27 } XMLUTILS_EVAL_APPENDLINE(regDummy); \
30 namespace XERCES_CPP_NAMESPACE {
36 namespace MBXMLUtils {
41 CodeString(
const std::string &str) : std::string(str) {}
48 dir=boost::filesystem::current_path();
51 boost::filesystem::current_path(dir);
54 boost::filesystem::path dir;
63 NewParamLevel(
const boost::shared_ptr<Eval> &oe_,
bool newLevel_=
true);
67 static void*
operator new(std::size_t);
68 static void*
operator new[](std::size_t);
70 boost::shared_ptr<Eval> oe;
74 template<
class T>
struct SwigType {
static std::string name; };
77 class Eval :
public boost::enable_shared_from_this<Eval>,
public boost::noncopyable,
virtual public fmatvec::Atom {
93 Eval(std::vector<boost::filesystem::path> *dependencies_);
99 static boost::shared_ptr<Eval>
createEvaluator(
const std::string &evalName, std::vector<boost::filesystem::path> *dependencies_=NULL);
103 static void registerEvaluator() {
104 getEvaluators()[E::getNameStatic()]=&newEvaluator<E>;
108 virtual std::string
getName()
const=0;
111 void addParam(
const std::string ¶mName,
const boost::shared_ptr<void>& value);
119 virtual void addImport(
const std::string &code,
const xercesc::DOMElement *e,
bool deprecated=
false)=0;
123 boost::shared_ptr<void>
eval(
const xercesc::DOMElement *e);
128 boost::shared_ptr<void>
eval(
const xercesc::DOMAttr *a,
const xercesc::DOMElement *pe=NULL);
239 T
cast(
const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
const;
243 T
cast(
const boost::shared_ptr<void> &value)
const;
256 boost::shared_ptr<void>
stringToValue(
const std::string &str,
const xercesc::DOMElement *e=NULL,
bool fullEval=
true)
const;
260 boost::shared_ptr<void>
create(
const T& v)
const;
263 virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >&
requiredFiles()
const=0;
275 std::vector<boost::filesystem::path> *dependencies;
278 std::unordered_map<std::string, boost::shared_ptr<void> > currentParam;
280 std::stack<std::unordered_map<std::string, boost::shared_ptr<void> > > paramStack;
283 boost::shared_ptr<void> currentImport;
285 std::stack<boost::shared_ptr<void> > importStack;
288 boost::shared_ptr<void> createSwig()
const;
306 virtual boost::shared_ptr<void>
callFunction(
const std::string &name,
const std::vector<boost::shared_ptr<void> >& args)
const=0;
308 static boost::shared_ptr<void> casadiValue;
311 virtual boost::shared_ptr<void>
fullStringToValue(
const std::string &str,
const xercesc::DOMElement *e=NULL)
const=0;
317 static boost::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
318 return boost::shared_ptr<E>(
new E(dependencies_));
322 virtual void*
getSwigThis(
const boost::shared_ptr<void> &value)
const=0;
324 virtual std::string
getSwigType(
const boost::shared_ptr<void> &value)
const=0;
326 void addStaticDependencies(
const xercesc::DOMElement *e)
const;
330 virtual double cast_double (
const boost::shared_ptr<void> &value)
const=0;
331 virtual std::vector<double> cast_vector_double (
const boost::shared_ptr<void> &value)
const=0;
332 virtual std::vector<std::vector<double> > cast_vector_vector_double(
const boost::shared_ptr<void> &value)
const=0;
333 virtual std::string cast_string (
const boost::shared_ptr<void> &value)
const=0;
335 CodeString cast_CodeString (
const boost::shared_ptr<void> &value)
const;
336 int cast_int (
const boost::shared_ptr<void> &value)
const;
339 xercesc::DOMElement* cast_DOMElement_p(
const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
const;
342 virtual boost::shared_ptr<void> create_double (
const double& v)
const=0;
343 virtual boost::shared_ptr<void> create_vector_double (
const std::vector<double>& v)
const=0;
344 virtual boost::shared_ptr<void> create_vector_vector_double(
const std::vector<std::vector<double> >& v)
const=0;
345 virtual boost::shared_ptr<void> create_string (
const std::string& v)
const=0;
347 boost::shared_ptr<void> handleUnit(
const xercesc::DOMElement *e,
const boost::shared_ptr<void> &ret);
349 static std::map<std::string, std::string> units;
351 static std::map<std::string, boost::function<boost::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
356 boost::shared_ptr<void> Eval::createSwig()
const {
364 throw DOMEvalException(
"This variable is not of SWIG type "+
SwigType<T>::name+
".");
368 template<> std::string Eval::cast<std::string>(
const boost::shared_ptr<void> &value)
const;
369 template<>
CodeString Eval::cast<CodeString>(
const boost::shared_ptr<void> &value)
const;
370 template<>
double Eval::cast<double>(
const boost::shared_ptr<void> &value)
const;
371 template<>
int Eval::cast<int>(
const boost::shared_ptr<void> &value)
const;
372 template<> std::vector<double> Eval::cast<std::vector<double> >(
const boost::shared_ptr<void> &value)
const;
373 template<> std::vector<std::vector<double> > Eval::cast<std::vector<std::vector<double> > >(
const boost::shared_ptr<void> &value)
const;
376 template<> xercesc::DOMElement* Eval::cast<xercesc::DOMElement*>(
const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
const;
379 template<> boost::shared_ptr<void> Eval::create<double> (
const double& v)
const;
380 template<> boost::shared_ptr<void> Eval::create<std::vector<double> > (
const std::vector<double>& v)
const;
381 template<> boost::shared_ptr<void> Eval::create<std::vector<std::vector<double> > >(
const std::vector<std::vector<double> >& v)
const;
382 template<> boost::shared_ptr<void> Eval::create<std::string> (
const std::string& v)
const;
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
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:545
virtual std::string getName() const =0
Get the type of this evaluator.
void addParam(const std::string ¶mName, const boost::shared_ptr< void > &value)
Add a value to the current parameters.
Definition: eval.cc:170
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:174
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:52
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:70
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:507
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:138
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:143
virtual boost::shared_ptr< void > createSwigByTypeName(const std::string &typeName) const =0
create a SWIG object of name typeName.
~Eval()
Destructor.
Definition: eval.cc:95
ValueType
Definition: eval.h:83