Hosted by: BerliOS Developer Logo

OpenMBV C++ Interface

BerliOS Project Page
 All Classes Functions Enumerations
/home/user/MBSimWindows/openmbv/mbxmlutils/mbxmlutils/octeval.h
00001 #ifndef _MBXMLUTILS_OCTEVAL_H_
00002 #define _MBXMLUTILS_OCTEVAL_H_
00003 
00004 #include <fmatvec/atom.h>
00005 #include <string>
00006 #include <stack>
00007 #ifdef HAVE_UNORDERED_MAP
00008 #  include <unordered_map>
00009 #else
00010 #  include <map>
00011 #  define unordered_map map
00012 #endif
00013 
00014 // Include octave/config.h first. This is normally not allowed since config.h should only
00015 // be included in .cc files but is required by octave.
00016 // To avoid macro redefined warnings/errors we save some macros, undefine it before
00017 // including octave/oct.h and undefine and reset it after the include.
00018 // save macros
00019 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE           PACKAGE
00020 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_BUGREPORT PACKAGE_BUGREPORT
00021 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_NAME      PACKAGE_NAME
00022 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_STRING    PACKAGE_STRING
00023 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_TARNAME   PACKAGE_TARNAME
00024 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_URL       PACKAGE_URL
00025 #define MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_VERSION   PACKAGE_VERSION
00026 #define MBXMLUTILS_OCTEVAL_H_SAVED_VERSION           VERSION
00027 // undef macros
00028 #undef PACKAGE
00029 #undef PACKAGE_BUGREPORT
00030 #undef PACKAGE_NAME
00031 #undef PACKAGE_STRING
00032 #undef PACKAGE_TARNAME
00033 #undef PACKAGE_URL
00034 #undef PACKAGE_VERSION
00035 #undef VERSION
00036 // include
00037 #include <octave/config.h>
00038 // undef macros
00039 #undef PACKAGE
00040 #undef PACKAGE_BUGREPORT
00041 #undef PACKAGE_NAME
00042 #undef PACKAGE_STRING
00043 #undef PACKAGE_TARNAME
00044 #undef PACKAGE_URL
00045 #undef PACKAGE_VERSION
00046 #undef VERSION
00047 // reset macros
00048 #define PACKAGE           MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE
00049 #define PACKAGE_BUGREPORT MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_BUGREPORT
00050 #define PACKAGE_NAME      MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_NAME
00051 #define PACKAGE_STRING    MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_STRING
00052 #define PACKAGE_TARNAME   MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_TARNAME
00053 #define PACKAGE_URL       MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_URL
00054 #define PACKAGE_VERSION   MBXMLUTILS_OCTEVAL_H_SAVED_PACKAGE_VERSION
00055 #define VERSION           MBXMLUTILS_OCTEVAL_H_SAVED_VERSION
00056 
00057 #include <octave/symtab.h>
00058 #include "mbxmlutilshelper/casadiXML.h"
00059 #include "mbxmlutilshelper/dom.h"
00060 #include <octave/parse.h>
00061 #include <boost/filesystem.hpp>
00062 #include <boost/static_assert.hpp> 
00063 #include <xercesc/util/XercesDefs.hpp>
00064 #include <xercesc/dom/DOMDocument.hpp>
00065 
00066 namespace XERCES_CPP_NAMESPACE { class DOMElement; }
00067 
00068 namespace MBXMLUtils {
00069 
00070 extern bool deactivateBlock;
00071 // A class to block/unblock stderr or stdout. Block in called in the ctor, unblock in the dtor
00072 template<int T>
00073 class Block {
00074   public:
00075     Block(std::ostream &str_, std::streambuf *buf=NULL) : str(str_) {
00076       if(deactivateBlock) return;
00077       if(disableCount==0)
00078         orgcxxx=str.rdbuf(buf);
00079       disableCount++;
00080     }
00081     ~Block() {
00082       if(deactivateBlock) return;
00083       disableCount--;
00084       if(disableCount==0)
00085         str.rdbuf(orgcxxx);
00086     }
00087   private:
00088     std::ostream &str;
00089     static std::streambuf *orgcxxx;
00090     static int disableCount;
00091 };
00092 template<int T> std::streambuf *Block<T>::orgcxxx;
00093 template<int T> int Block<T>::disableCount=0;
00094 #define BLOCK_STDOUT(name) Block<1> name(std::cout)
00095 #define BLOCK_STDERR(name) Block<2> name(std::cerr)
00096 #define REDIR_STDOUT(name, buf) Block<1> name(std::cout, buf)
00097 #define REDIR_STDERR(name, buf) Block<2> name(std::cerr, buf)
00098 
00099 class OctEval;
00100 
00102 class NewParamLevel {
00103   public:
00105     NewParamLevel(OctEval &oe_, bool newLevel_=true);
00107     ~NewParamLevel();
00108   protected:
00109     OctEval &oe;
00110     bool newLevel;
00111 };
00112 
00114 class OctEval : public fmatvec::Atom {
00115   public:
00116     friend class NewParamLevel;
00117 
00119     enum ValueType {
00120       ScalarType,
00121       VectorType,
00122       MatrixType,
00123       StringType,
00124       SXMatrixType,
00125       DMatrixType,
00126       SXFunctionType
00127     };
00128 
00130     OctEval(std::vector<boost::filesystem::path> *dependencies_=NULL);
00132     ~OctEval();
00133 
00135     void addParam(const std::string &paramName, const octave_value& value);
00139     void addParamSet(const xercesc::DOMElement *e);
00140 
00142     void addPath(const boost::filesystem::path &dir);
00143     
00146     octave_value eval(const xercesc::DOMElement *e);
00147 
00151     octave_value eval(const xercesc::DOMAttr *a, const xercesc::DOMElement *pe=NULL);
00152 
00304     template<typename T>
00305     static T cast(const octave_value &value, xercesc::DOMDocument *doc);
00306 
00308     template<typename T>
00309     static T cast(const octave_value &value);
00310 
00312     static ValueType getType(const octave_value &value);
00313 
00317     octave_value stringToOctValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const;
00318 
00319   protected:
00320 
00322     octave_value fullStringToOctValue(const std::string &str, const xercesc::DOMElement *e=NULL) const;
00323 
00325     std::string partialStringToOctValue(const std::string &str, const xercesc::DOMElement *e) const;
00326 
00328     void pushParams();
00329 
00331     void popParams();
00332 
00334     void pushPath();
00335 
00337     void popPath();
00338 
00340     template<typename T>
00341     static T castToSwig(const octave_value &value);
00342 
00343     std::vector<boost::filesystem::path> *dependencies;
00344 
00346     static octave_value createCasADi(const std::string &name);
00347 
00348     // map of the current parameters
00349     std::unordered_map<std::string, octave_value> currentParam;
00350     // current octave path
00351     std::string currentPath;
00352     // the initial octave search path (of octave) (set ones during the first initialization)
00353     static std::string initialOctavePath;
00354     // the initial octave search path (of OctEval) (set ones during the first initialization)
00355     static std::string initialOctEvalPath;
00356 
00357     static std::string pathSep;
00358 
00359     // stack of parameters
00360     std::stack<std::unordered_map<std::string, octave_value> > paramStack;
00361     // stack of path
00362     std::stack<std::string> pathStack;
00363 
00364     static int initCount;
00365 
00366     static std::map<std::string, std::string> units;
00367 
00368     static InitXerces initXerces;
00369 
00370     static octave_value casadiOctValue;
00371 
00372     static octave_value_list fevalThrow(octave_function *func, const octave_value_list &arg, int n=0,
00373                                         const std::string &msg=std::string(), const xercesc::DOMElement *e=NULL);
00374 
00375     octave_value handleUnit(const xercesc::DOMElement *e, const octave_value &ret);
00376 };
00377 
00378 // Helper class which convert a void* to T* or T.
00379 template<typename T>
00380 struct Ptr {
00381   static T cast(void *ptr) { return *static_cast<T*>(ptr); }
00382 };
00383 template<typename T>
00384 struct Ptr<T*> {
00385   static T* cast(void *ptr) { return static_cast<T*>(ptr); }
00386 };
00387 
00388 // cast octave value to swig object ptr or swig object copy
00389 template<typename T>
00390 T OctEval::castToSwig(const octave_value &value) {
00391   // get the casadi pointer: octave returns a 64bit integer which represents the pointer
00392   static octave_function *swig_this=symbol_table::find_function("swig_this").function_value(); // get ones a pointer to swig_this for performance reasons
00393   octave_value swigThis;
00394   {
00395     BLOCK_STDERR(blockstderr);
00396     swigThis=feval(swig_this, value, 1)(0);
00397   }
00398   if(error_state!=0)
00399     throw std::runtime_error("Internal error: Not a swig object");
00400   void *swigPtr=reinterpret_cast<void*>(swigThis.uint64_scalar_value().value());
00401   // convert the void pointer to the correct casadi type
00402   return Ptr<T>::cast(swigPtr);
00403 }
00404 
00405 template<typename T>
00406 T OctEval::cast(const octave_value &value) {
00407   // do not allow T == DOMElement* here ...
00408   BOOST_STATIC_ASSERT_MSG((boost::is_same<T, xercesc::DOMElement*>::type), 
00409     "Calling OctEval::cast<DOMElement*>(const octave_value&) is not allowed "
00410     "use OctEval::cast<DOMElement*>(const octave_value&, DOMDocument*)"
00411   );
00412   // ... but treat all other type as octave swig types ...
00413   return castToSwig<T>(value);
00414 }
00415 // ... but prevere these specializations
00416 template<> std::string OctEval::cast<std::string>(const octave_value &value);
00417 template<> long OctEval::cast<long>(const octave_value &value);
00418 template<> double OctEval::cast<double>(const octave_value &value);
00419 template<> std::vector<double> OctEval::cast<std::vector<double> >(const octave_value &value);
00420 template<> std::vector<std::vector<double> > OctEval::cast<std::vector<std::vector<double> > >(const octave_value &value);
00421 template<> CasADi::SXMatrix OctEval::cast<CasADi::SXMatrix>(const octave_value &value);
00422 template<> CasADi::SXMatrix* OctEval::cast<CasADi::SXMatrix*>(const octave_value &value);
00423 template<> CasADi::SXFunction OctEval::cast<CasADi::SXFunction>(const octave_value &value);
00424 template<> CasADi::SXFunction* OctEval::cast<CasADi::SXFunction*>(const octave_value &value);
00425 template<> CasADi::DMatrix OctEval::cast<CasADi::DMatrix>(const octave_value &value);
00426 template<> CasADi::DMatrix* OctEval::cast<CasADi::DMatrix*>(const octave_value &value);
00427 
00428 template<typename T>
00429 T OctEval::cast(const octave_value &value, xercesc::DOMDocument *doc) {
00430   // only allow T == DOMElement* here ...
00431   BOOST_STATIC_ASSERT_MSG(!(boost::is_same<T, xercesc::DOMElement*>::type), 
00432     "Calling OctEval::cast<T>(const octave_value&, DOMDocument*) is only allowed for T=DOMElement*"
00433   );
00434 }
00435 // ... but use these spezializations
00436 template<> xercesc::DOMElement* OctEval::cast<xercesc::DOMElement*>(const octave_value &value, xercesc::DOMDocument *doc);
00437 
00438 } // end namespace MBXMLUtils
00439 
00440 #endif