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);
269 T
cast(
const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
const;
273 T
cast(
const boost::shared_ptr<void> &value)
const;
286 boost::shared_ptr<void>
stringToValue(
const std::string &str,
const xercesc::DOMElement *e=NULL,
bool fullEval=
true)
const;
290 boost::shared_ptr<void>
create(
const T& v)
const;
293 virtual std::map<boost::filesystem::path, std::pair<boost::filesystem::path, bool> >&
requiredFiles()
const=0;
305 std::vector<boost::filesystem::path> *dependencies;
308 std::unordered_map<std::string, boost::shared_ptr<void> > currentParam;
310 std::stack<std::unordered_map<std::string, boost::shared_ptr<void> > > paramStack;
313 boost::shared_ptr<void> currentImport;
315 std::stack<boost::shared_ptr<void> > importStack;
318 boost::shared_ptr<void> createSwig()
const;
336 virtual boost::shared_ptr<void>
callFunction(
const std::string &name,
const std::vector<boost::shared_ptr<void> >& args)
const=0;
338 boost::shared_ptr<void> casadiValue;
341 virtual boost::shared_ptr<void>
fullStringToValue(
const std::string &str,
const xercesc::DOMElement *e=NULL)
const=0;
347 static boost::shared_ptr<Eval> newEvaluator(std::vector<boost::filesystem::path>* dependencies_) {
348 return boost::shared_ptr<E>(
new E(dependencies_));
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;
356 void addStaticDependencies(
const xercesc::DOMElement *e)
const;
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;
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;
370 xercesc::DOMElement* cast_DOMElement_p(
const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
const;
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;
378 boost::shared_ptr<void> handleUnit(
const xercesc::DOMElement *e,
const boost::shared_ptr<void> &ret);
380 static std::map<std::string, std::string> units;
382 static std::map<std::string, boost::function<boost::shared_ptr<Eval>(std::vector<boost::filesystem::path>*)> >& getEvaluators();
387 boost::shared_ptr<void> Eval::createSwig()
const {
395 throw DOMEvalException(
"This variable is not of SWIG type "+
SwigType<T>::name+
".");
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;
408 template<> xercesc::DOMElement* Eval::cast<xercesc::DOMElement*>(
const boost::shared_ptr<void> &value, xercesc::DOMDocument *doc)
const;
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;
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:547
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: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
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
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