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 undefine it before
00017 // including octave/oct.h. Note that we can not restore the values. So you have to reinclude
00018 // your config.h after this file to get the original values.
00019 // undef macros
00020 #undef PACKAGE
00021 #undef PACKAGE_BUGREPORT
00022 #undef PACKAGE_NAME
00023 #undef PACKAGE_STRING
00024 #undef PACKAGE_TARNAME
00025 #undef PACKAGE_URL
00026 #undef PACKAGE_VERSION
00027 #undef VERSION
00028 // include
00029 #include <octave/config.h>
00030 // undef macros
00031 #undef PACKAGE
00032 #undef PACKAGE_BUGREPORT
00033 #undef PACKAGE_NAME
00034 #undef PACKAGE_STRING
00035 #undef PACKAGE_TARNAME
00036 #undef PACKAGE_URL
00037 #undef PACKAGE_VERSION
00038 #undef VERSION
00039 // We cannot reset the macros so you have to reinclude your config.h after this file
00040 // to get the original values
00041 
00042 #include <octave/symtab.h>
00043 #include "mbxmlutilshelper/casadiXML.h"
00044 #include "mbxmlutilshelper/dom.h"
00045 #include <octave/parse.h>
00046 #include <boost/filesystem.hpp>
00047 #include <boost/static_assert.hpp> 
00048 #include <boost/scoped_ptr.hpp>
00049 #include <xercesc/util/XercesDefs.hpp>
00050 #include <xercesc/dom/DOMDocument.hpp>
00051 
00052 namespace XERCES_CPP_NAMESPACE { class DOMElement; }
00053 
00054 namespace MBXMLUtils {
00055 
00056 extern bool deactivateBlock;
00057 // A class to block/unblock stderr or stdout. Block in called in the ctor, unblock in the dtor
00058 template<int T>
00059 class Block {
00060   public:
00061     Block(std::ostream &str_, std::streambuf *buf=NULL) : str(str_) {
00062       if(deactivateBlock) return;
00063       if(disableCount==0)
00064         orgcxxx=str.rdbuf(buf);
00065       disableCount++;
00066     }
00067     ~Block() {
00068       if(deactivateBlock) return;
00069       disableCount--;
00070       if(disableCount==0)
00071         str.rdbuf(orgcxxx);
00072     }
00073   private:
00074     std::ostream &str;
00075     static std::streambuf *orgcxxx;
00076     static int disableCount;
00077 };
00078 template<int T> std::streambuf *Block<T>::orgcxxx;
00079 template<int T> int Block<T>::disableCount=0;
00080 #define BLOCK_STDOUT(name) Block<1> name(std::cout)
00081 #define BLOCK_STDERR(name) Block<2> name(std::cerr)
00082 #define REDIR_STDOUT(name, buf) Block<1> name(std::cout, buf)
00083 #define REDIR_STDERR(name, buf) Block<2> name(std::cerr, buf)
00084 
00085 class OctEval;
00086 
00088 class NewParamLevel {
00089   public:
00091     NewParamLevel(OctEval &oe_, bool newLevel_=true);
00093     ~NewParamLevel();
00094   protected:
00095     OctEval &oe;
00096     bool newLevel;
00097 };
00098 
00100 class OctEval : virtual public fmatvec::Atom {
00101   public:
00102     friend class NewParamLevel;
00103 
00105     enum ValueType {
00106       ScalarType,
00107       VectorType,
00108       MatrixType,
00109       StringType,
00110       SXType,
00111       DMatrixType,
00112       SXFunctionType
00113     };
00114 
00116     OctEval(std::vector<boost::filesystem::path> *dependencies_=NULL);
00118     ~OctEval();
00119 
00121     void addParam(const std::string &paramName, const octave_value& value);
00125     void addParamSet(const xercesc::DOMElement *e);
00126 
00129     void addPath(const boost::filesystem::path &dir, const xercesc::DOMElement *e);
00130     
00133     octave_value eval(const xercesc::DOMElement *e);
00134 
00138     octave_value eval(const xercesc::DOMAttr *a, const xercesc::DOMElement *pe=NULL);
00139 
00291     template<typename T>
00292     static T cast(const octave_value &value, xercesc::DOMDocument *doc);
00293 
00295     template<typename T>
00296     static T cast(const octave_value &value);
00297 
00299     static ValueType getType(const octave_value &value);
00300 
00304     octave_value stringToOctValue(const std::string &str, const xercesc::DOMElement *e=NULL, bool fullEval=true) const;
00305 
00306   protected:
00307 
00309     // (in the later case the dtor is not called but octave must be uninitialized before exit)
00310     void deinitOctave();
00311 
00313     octave_value fullStringToOctValue(const std::string &str, const xercesc::DOMElement *e=NULL) const;
00314 
00316     std::string partialStringToOctValue(const std::string &str, const xercesc::DOMElement *e) const;
00317 
00319     void pushParams();
00320 
00322     void popParams();
00323 
00325     void pushPath();
00326 
00328     void popPath();
00329 
00331     template<typename T>
00332     static T castToSwig(const octave_value &value);
00333 
00334     std::vector<boost::filesystem::path> *dependencies;
00335 
00337     static octave_value createCasADi(const std::string &name);
00338 
00339     // map of the current parameters
00340     std::unordered_map<std::string, octave_value> currentParam;
00341     // initial path
00342     static std::string initialPath;
00343     static std::string pathSep;
00344 
00345     // stack of parameters
00346     std::stack<std::unordered_map<std::string, octave_value> > paramStack;
00347     // stack of path
00348     std::stack<std::string> pathStack;
00349 
00350     static int initCount;
00351 
00352     static std::map<std::string, std::string> units;
00353 
00354     static InitXerces initXerces;
00355 
00356     static boost::scoped_ptr<octave_value> casadiOctValue;
00357 
00358     static octave_value_list fevalThrow(octave_function *func, const octave_value_list &arg, int n=0,
00359                                         const std::string &msg=std::string());
00360 
00361     octave_value handleUnit(const xercesc::DOMElement *e, const octave_value &ret);
00362 };
00363 
00364 // Helper class which convert a void* to T* or T.
00365 template<typename T>
00366 struct Ptr {
00367   static T cast(void *ptr) { return *static_cast<T*>(ptr); }
00368 };
00369 template<typename T>
00370 struct Ptr<T*> {
00371   static T* cast(void *ptr) { return static_cast<T*>(ptr); }
00372 };
00373 
00374 // cast octave value to swig object ptr or swig object copy
00375 template<typename T>
00376 T OctEval::castToSwig(const octave_value &value) {
00377   // get the casadi pointer: octave returns a 64bit integer which represents the pointer
00378   static octave_function *swig_this=symbol_table::find_function("swig_this").function_value(); // get ones a pointer to swig_this for performance reasons
00379   octave_value swigThis;
00380   {
00381     BLOCK_STDERR(blockstderr);
00382     swigThis=feval(swig_this, value, 1)(0);
00383   }
00384   if(error_state!=0)
00385     throw std::runtime_error("Internal error: Not a swig object");
00386   void *swigPtr=reinterpret_cast<void*>(swigThis.uint64_scalar_value().value());
00387   // convert the void pointer to the correct casadi type
00388   return Ptr<T>::cast(swigPtr);
00389 }
00390 
00391 template<typename T>
00392 T OctEval::cast(const octave_value &value) {
00393   // do not allow T == DOMElement* here ...
00394   BOOST_STATIC_ASSERT_MSG((boost::is_same<T, xercesc::DOMElement*>::type), 
00395     "Calling OctEval::cast<DOMElement*>(const octave_value&) is not allowed "
00396     "use OctEval::cast<DOMElement*>(const octave_value&, DOMDocument*)"
00397   );
00398   // ... but treat all other type as octave swig types ...
00399   return castToSwig<T>(value);
00400 }
00401 // ... but prevere these specializations
00402 template<> std::string OctEval::cast<std::string>(const octave_value &value);
00403 template<> long OctEval::cast<long>(const octave_value &value);
00404 template<> double OctEval::cast<double>(const octave_value &value);
00405 template<> std::vector<double> OctEval::cast<std::vector<double> >(const octave_value &value);
00406 template<> std::vector<std::vector<double> > OctEval::cast<std::vector<std::vector<double> > >(const octave_value &value);
00407 template<> CasADi::SX OctEval::cast<CasADi::SX>(const octave_value &value);
00408 template<> CasADi::SX* OctEval::cast<CasADi::SX*>(const octave_value &value);
00409 template<> CasADi::SXFunction OctEval::cast<CasADi::SXFunction>(const octave_value &value);
00410 template<> CasADi::SXFunction* OctEval::cast<CasADi::SXFunction*>(const octave_value &value);
00411 template<> CasADi::DMatrix OctEval::cast<CasADi::DMatrix>(const octave_value &value);
00412 template<> CasADi::DMatrix* OctEval::cast<CasADi::DMatrix*>(const octave_value &value);
00413 
00414 template<typename T>
00415 T OctEval::cast(const octave_value &value, xercesc::DOMDocument *doc) {
00416   // only allow T == DOMElement* here ...
00417   BOOST_STATIC_ASSERT_MSG(!(boost::is_same<T, xercesc::DOMElement*>::type), 
00418     "Calling OctEval::cast<T>(const octave_value&, DOMDocument*) is only allowed for T=DOMElement*"
00419   );
00420 }
00421 // ... but use these spezializations
00422 template<> xercesc::DOMElement* OctEval::cast<xercesc::DOMElement*>(const octave_value &value, xercesc::DOMDocument *doc);
00423 
00424 } // end namespace MBXMLUtils
00425 
00426 #endif