BaseElement.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef SOFA_SIMULATION_TREE_XML_BASEELEMENT_H
00026 #define SOFA_SIMULATION_TREE_XML_BASEELEMENT_H
00027
00028
00029 #include <sofa/helper/Factory.h>
00030 #include <sofa/core/objectmodel/Base.h>
00031 #include <sofa/core/objectmodel/BaseContext.h>
00032 #include <sofa/core/objectmodel/BaseObjectDescription.h>
00033 #include <string>
00034 #include <list>
00035 #include <map>
00036
00037 namespace sofa
00038 {
00039
00040 namespace simulation
00041 {
00042
00043 namespace tree
00044 {
00045
00046 namespace xml
00047 {
00048
00049 class BaseElement : public core::objectmodel::BaseObjectDescription
00050 {
00051 private:
00052
00053
00054
00055 std::string basefile;
00056 BaseElement* parent;
00057 typedef std::list<BaseElement*> ChildList;
00058 ChildList children;
00059 bool groupType;
00060 public:
00061 BaseElement(const std::string& name, const std::string& type, BaseElement* newParent=NULL);
00062
00063 virtual ~BaseElement();
00064
00066 virtual const char* getClass() const = 0;
00067
00069
00070
00072 std::string getName()
00073 { return attributes["name"]; }
00074
00075 virtual void setName(const std::string& newName)
00076 { attributes["name"] = newName; }
00077
00079 std::string getType()
00080 { return attributes["type"]; }
00081
00082 virtual void setType(const std::string& newType)
00083 { attributes["type"] = newType; }
00084
00086 sofa::core::objectmodel::BaseObjectDescription* getParent() const
00087 { return parent; }
00088
00090 BaseElement* getParentElement() const
00091 { return parent; }
00092
00094 std::string getBaseFile();
00095
00096 virtual void setBaseFile(const std::string& newBaseFile);
00097
00099 bool isFileRoot();
00100
00102 bool isGroupType(){return groupType;}
00103
00105 void setGroupType(bool b){groupType=b;}
00106
00108
00109
00111
00112
00114
00115
00117 virtual void setAttribute(const std::string& attr, const char* val);
00118
00120 virtual bool presenceAttribute(const std::string& s);
00121
00123 virtual bool removeAttribute(const std::string& attr);
00124
00126 virtual BaseElement* findNode(const char* nodeName, bool absolute=false);
00127
00129 virtual BaseObjectDescription* find(const char* nodeName, bool absolute=false)
00130 {
00131 return findNode(nodeName, absolute);
00132 }
00133
00135 template<class Sequence>
00136 void pushObjects(Sequence& result)
00137 {
00138 typename Sequence::value_type obj = dynamic_cast<typename Sequence::value_type>(getObject());
00139 if (obj!=NULL) result.push_back(obj);
00140
00141 for (child_iterator<> it = begin(); it != end(); ++it)
00142 it->pushObjects<Sequence>(result);
00143 }
00144
00146 template<class Map>
00147 void pushNamedObjects(Map& result)
00148 {
00149 typedef typename Map::value_type V;
00150 typedef typename V::second_type OPtr;
00151 OPtr obj = dynamic_cast<OPtr>(getObject());
00152 if (obj!=NULL) result.insert(std::make_pair(getFullName(),obj));
00153
00154 for (child_iterator<> it = begin(); it != end(); ++it)
00155 it->pushNamedObjects<Map>(result);
00156 }
00157
00158 protected:
00160 virtual bool setParent(BaseElement* newParent)
00161 { parent = newParent; return true; }
00162
00163 public:
00164 virtual bool addChild(BaseElement* child);
00165
00166 virtual bool removeChild(BaseElement* child);
00167
00168 virtual bool initNode() = 0;
00169
00170 virtual bool init();
00171
00172 template<class Node=BaseElement>
00173 class child_iterator
00174 {
00175 protected:
00176 BaseElement* parent;
00177 ChildList::iterator it;
00178 Node* current;
00179 child_iterator(BaseElement* parent, ChildList::iterator it)
00180 : parent(parent), it(it), current(NULL)
00181 {
00182 checkIt();
00183 }
00184 void checkIt()
00185 {
00186 while (it != parent->children.end())
00187 {
00188 current=dynamic_cast<Node*>(*it);
00189 if (current!=NULL) return;
00190 ++it;
00191 }
00192 current = NULL;
00193 }
00194 public:
00195 operator Node*() { return current; }
00196 Node* operator->() { return current; }
00197 void operator ++() { ++it; checkIt(); }
00198 bool operator==(const child_iterator<Node>& i) const
00199 {
00200 return it == i.it;
00201 }
00202 friend class BaseElement;
00203 };
00204
00205 template<class Node>
00206 child_iterator<Node> begin()
00207 {
00208 return child_iterator<Node>(this, children.begin());
00209 }
00210
00211 child_iterator<BaseElement> begin()
00212 {
00213 return begin<BaseElement>();
00214 }
00215
00216 template<class Node>
00217 child_iterator<Node> end()
00218 {
00219 return child_iterator<Node>(this, children.end());
00220 }
00221
00222 child_iterator<BaseElement> end()
00223 {
00224 return end<BaseElement>();
00225 }
00226
00227 typedef helper::Factory< std::string, BaseElement, std::pair<std::string, std::string> > NodeFactory;
00228
00229 static BaseElement* Create(const std::string& nodeClass, const std::string& name, const std::string& type);
00230
00231 template<class Node>
00232 static void create(Node*& obj, std::pair<std::string,std::string> arg)
00233 {
00234 obj = new Node(arg.first,arg.second);
00235 }
00236
00237 };
00238
00239 }
00240
00241 }
00242
00243 }
00244
00245 }
00246
00247 #endif
00248