Hosted by: BerliOS Developer Logo

OpenMBV C++ Interface

BerliOS Project Page
/home/user/MBSimWindows/openmbv/openmbvcppinterface/openmbvcppinterface/object.h
00001 /*
00002     OpenMBV - Open Multi Body Viewer.
00003     Copyright (C) 2009 Markus Friedrich
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 #ifndef _OPENMBV_OBJECT_H_
00021 #define _OPENMBV_OBJECT_H_
00022 
00023 #include <fmatvec/atom.h>
00024 #include <string>
00025 #include <sstream>
00026 #include <openmbvcppinterface/objectfactory.h>
00027 #include <hdf5serie/group.h>
00028 #include <mbxmlutilshelper/dom.h>
00029 #include <xercesc/dom/DOMElement.hpp>
00030 #include <xercesc/dom/DOMNode.hpp>
00031 #include <xercesc/dom/DOMDocument.hpp>
00032 #include <vector>
00033 
00034 namespace XERCES_CPP_NAMESPACE {
00035   class DOMNode;
00036   class DOMElement;
00037 }
00038 
00039 namespace OpenMBV {
00040 
00041 #ifndef SWIG // SWIG can not parse this (swig bug?). However it is not needed for the swig interface -> removed for swig
00042   const MBXMLUtils::NamespaceURI OPENMBV("http://openmbv.berlios.de/OpenMBV");
00043   const MBXMLUtils::NamespaceURI MBXMLUTILSPARAM("http://openmbv.berlios.de/MBXMLUtils/parameter");
00044 #endif
00045 
00046   class Group;
00047 
00049   class Object : virtual public fmatvec::Atom {
00050     friend class Group;
00051     protected:
00052       std::string name;
00053       std::string enableStr, boundingBoxStr;
00054       std::string ID; // Note: the ID is metadata and stored as a processing instruction in XML
00055       bool selected; // Note: the selected flag is metadata and not stored in XML but used by OpenMBVGUI
00056       boost::weak_ptr<Group> parent;
00057 
00058       virtual void createHDF5File()=0;
00059       virtual void openHDF5File()=0;
00060       H5::GroupBase *hdf5Group;
00061       virtual void terminate()=0;
00062 
00063       Object();
00064       virtual ~Object();
00065     public:
00066 
00068       virtual std::string getClassName()=0;
00069 
00071       void setEnable(bool enable) { enableStr=(enable==true)?"true":"false"; }
00072 
00073       bool getEnable() { return enableStr=="true"?true:false; }
00074 
00076       void setBoundingBox(bool bbox) { boundingBoxStr=(bbox==true)?"true":"false"; }
00077 
00078       bool getBoundingBox() { return boundingBoxStr=="true"?true:false; }
00079 
00081       void setName(const std::string& name_) { name=name_; }
00082 
00083       std::string getName() { return name; }
00084 
00086       virtual std::string getFullName(bool includingFileName=false, bool stopAtSeparateFile=false);
00087 
00089       virtual void initializeUsingXML(xercesc::DOMElement *element);
00090 
00091       virtual xercesc::DOMElement *writeXMLFile(xercesc::DOMNode *parent);
00092 
00094       boost::shared_ptr<Group> getSeparateGroup();
00095 
00097       boost::shared_ptr<Group> getTopLevelGroup();
00098 
00099       boost::weak_ptr<Group> getParent() { return parent; }
00100 
00101       H5::GroupBase *getHDF5Group() { return hdf5Group; };
00102 
00104       std::string getID() const { return ID; }
00106       void setID(std::string ID_) { ID=ID_; }
00107 
00109       bool getSelected() const { return selected; }
00111       void setSelected(bool selected_) { selected=selected_; }
00112 
00113       // FROM NOW ONLY CONVENIENCE FUNCTIONS FOLLOW !!!
00114       static double getDouble(xercesc::DOMElement *e);
00115       static std::vector<double> getVec(xercesc::DOMElement *e, unsigned int rows=0);
00116       static std::vector<std::vector<double> > getMat(xercesc::DOMElement *e, unsigned int rows=0, unsigned int cols=0);
00117 
00118       static std::string numtostr(int i) { std::ostringstream oss; oss << i; return oss.str(); }
00119       static std::string numtostr(double d) { std::ostringstream oss; oss << d; return oss.str(); } 
00120 
00121 
00122       template<class T>
00123       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const T &value) {
00124         std::ostringstream oss;
00125         oss<<value;
00126         xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(name);
00127         ele->insertBefore(parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%oss.str()), NULL);
00128         parent->insertBefore(ele, NULL);
00129       }
00130       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, double value, double def);
00131       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const std::vector<double> &value);
00132       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const std::vector<std::vector<double> > &value);
00133 
00134       template <class T>
00135       static void addAttribute(xercesc::DOMNode *node, std::string name, T value) {
00136         xercesc::DOMElement *ele = dynamic_cast<xercesc::DOMElement*>(node);
00137         if(ele) {
00138           std::ostringstream oss;
00139           oss<<value;
00140           MBXMLUtils::E(ele)->setAttribute(name, oss.str());
00141         }
00142       }
00143 
00144       template <class T>
00145       static void addAttribute(xercesc::DOMNode *node, std::string name, T value, std::string def) {
00146         if(value!=def) addAttribute(node, name, value);
00147       }
00148 
00149     protected:
00150       static std::vector<double> toVector(std::string str);
00151       static std::vector<std::vector<double> > toMatrix(std::string str);
00152   };
00153 
00154 }
00155 
00156 #endif