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 <H5Cpp.h>
00026 #include <sstream>
00027 #include <openmbvcppinterface/objectfactory.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 : 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       Group* parent;
00057 
00058       virtual void createHDF5File()=0;
00059       virtual void openHDF5File()=0;
00060       H5::Group *hdf5Group;
00061       virtual void terminate()=0;
00062 
00064       virtual ~Object();
00065     public:
00067       Object();
00068 
00071       virtual void destroy() const;
00072 
00074       virtual std::string getClassName()=0;
00075 
00077       void setEnable(bool enable) { enableStr=(enable==true)?"true":"false"; }
00078 
00079       bool getEnable() { return enableStr=="true"?true:false; }
00080 
00082       void setBoundingBox(bool bbox) { boundingBoxStr=(bbox==true)?"true":"false"; }
00083 
00084       bool getBoundingBox() { return boundingBoxStr=="true"?true:false; }
00085 
00087       void setName(const std::string& name_) { name=name_; }
00088 
00089       std::string getName() { return name; }
00090 
00092       virtual std::string getFullName(bool includingFileName=false, bool stopAtSeparateFile=false);
00093 
00095       virtual void initializeUsingXML(xercesc::DOMElement *element);
00096 
00097       virtual xercesc::DOMElement *writeXMLFile(xercesc::DOMNode *parent);
00098 
00100       Group* getSeparateGroup();
00101 
00103       Group* getTopLevelGroup();
00104 
00105       Group* getParent() { return parent; }
00106 
00108       std::string getID() const { return ID; }
00110       void setID(std::string ID_) { ID=ID_; }
00111 
00113       bool getSelected() const { return selected; }
00115       void setSelected(bool selected_) { selected=selected_; }
00116 
00117       // FROM NOW ONLY CONVENIENCE FUNCTIONS FOLLOW !!!
00118       static double getDouble(xercesc::DOMElement *e);
00119       static std::vector<double> getVec(xercesc::DOMElement *e, unsigned int rows=0);
00120       static std::vector<std::vector<double> > getMat(xercesc::DOMElement *e, unsigned int rows=0, unsigned int cols=0);
00121 
00122       static std::string numtostr(int i) { std::ostringstream oss; oss << i; return oss.str(); }
00123       static std::string numtostr(double d) { std::ostringstream oss; oss << d; return oss.str(); } 
00124 
00125 
00126       template<class T>
00127       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const T &value) {
00128         std::ostringstream oss;
00129         oss<<value;
00130         xercesc::DOMElement *ele = MBXMLUtils::D(parent->getOwnerDocument())->createElement(name);
00131         ele->insertBefore(parent->getOwnerDocument()->createTextNode(MBXMLUtils::X()%oss.str()), NULL);
00132         parent->insertBefore(ele, NULL);
00133       }
00134       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, double value, double def);
00135       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const std::vector<double> &value);
00136       static void addElementText(xercesc::DOMElement *parent, const MBXMLUtils::FQN &name, const std::vector<std::vector<double> > &value);
00137 
00138       template <class T>
00139       static void addAttribute(xercesc::DOMNode *node, std::string name, T value) {
00140         xercesc::DOMElement *ele = dynamic_cast<xercesc::DOMElement*>(node);
00141         if(ele) {
00142           std::ostringstream oss;
00143           oss<<value;
00144           MBXMLUtils::E(ele)->setAttribute(name, oss.str());
00145         }
00146       }
00147 
00148       template <class T>
00149       static void addAttribute(xercesc::DOMNode *node, std::string name, T value, std::string def) {
00150         if(value!=def) addAttribute(node, name, value);
00151       }
00152 
00153     protected:
00154       static std::vector<double> toVector(std::string str);
00155       static std::vector<std::vector<double> > toMatrix(std::string str);
00156   };
00157 
00158 }
00159 
00160 #endif