00001 /****************************************************************************** 00002 * SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3 * 00003 * (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS * 00004 * * 00005 * This library is free software; you can redistribute it and/or modify it * 00006 * under the terms of the GNU Lesser General Public License as published by * 00007 * the Free Software Foundation; either version 2.1 of the License, or (at * 00008 * your option) any later version. * 00009 * * 00010 * This library is distributed in the hope that it will be useful, but WITHOUT * 00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * 00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * 00013 * for more details. * 00014 * * 00015 * You should have received a copy of the GNU Lesser General Public License * 00016 * along with this library; if not, write to the Free Software Foundation, * 00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 00018 ******************************************************************************* 00019 * SOFA :: Modules * 00020 * * 00021 * Authors: The SOFA Team and external contributors (see Authors.txt) * 00022 * * 00023 * Contact information: contact@sofa-framework.org * 00024 ******************************************************************************/ 00025 #ifndef SOFA_SIMULATION_TREE_XML_ELEMENT_H 00026 #define SOFA_SIMULATION_TREE_XML_ELEMENT_H 00027 00028 #include <vector> 00029 #include <sofa/simulation/tree/xml/BaseElement.h> 00030 #include <sofa/helper/Factory.h> 00031 00032 namespace sofa 00033 { 00034 00035 namespace simulation 00036 { 00037 00038 namespace tree 00039 { 00040 00041 namespace xml 00042 { 00043 00044 00045 template<class Object> 00046 class Element : public BaseElement 00047 { 00048 private: 00049 Object* object; 00050 public: 00051 Element(const std::string& name, const std::string& type, BaseElement* newParent=NULL); 00052 00053 virtual ~Element(); 00054 00055 Object* getTypedObject(); 00056 00057 virtual void setObject(Object* newObject); 00058 00060 virtual core::objectmodel::Base* getObject(); 00061 00062 virtual bool initNode(); 00063 00064 typedef helper::Factory< std::string, Object, Element<Object>* > Factory; 00065 00066 }; 00067 /* 00068 template<class Object> 00069 void createWithFilename(Object*& obj, BaseElement* arg) 00070 { 00071 const char* filename = arg->getAttribute("filename"); 00072 if (!filename) 00073 { 00074 std::cerr << arg->getType() << " requires a filename attribute\n"; 00075 obj = NULL; 00076 } 00077 else 00078 obj = new Object(filename); 00079 } 00080 00081 template<class Object, class ParentObject> 00082 void createWithParentAndFilename(Object*& obj, BaseElement* arg) 00083 { 00084 obj = NULL; 00085 const char* filename = arg->getAttribute("filename"); 00086 if (!filename || arg->getParent()==NULL) 00087 { 00088 std::cerr << arg->getType() << " requires a filename attribute and a parent node\n"; 00089 return; 00090 } 00091 ParentObject* object = dynamic_cast<ParentObject*>(arg->getParent()->getObject()); 00092 if (object==NULL) 00093 { // look for mechanicalmodel 00094 core::objectmodel::BaseContext* ctx = dynamic_cast<core::objectmodel::BaseContext*>(arg->getParent()->getObject()); 00095 if (ctx!=NULL) 00096 object = dynamic_cast<ParentObject*>(ctx->getMechanicalState()); 00097 } 00098 if (object==NULL) return; 00099 obj = new Object(object, filename); 00100 } 00101 00102 template<class Object, class ParentObject> 00103 void createWithParent(Object*& obj, BaseElement* arg) 00104 { 00105 obj = NULL; 00106 ParentObject* object = dynamic_cast<ParentObject*>(arg->getParent()->getObject()); 00107 if (object==NULL) 00108 { // look for mechanicalmodel 00109 core::objectmodel::BaseContext* ctx = dynamic_cast<core::objectmodel::BaseContext*>(arg->getParent()->getObject()); 00110 if (ctx!=NULL) 00111 object = dynamic_cast<ParentObject*>(ctx->getMechanicalState()); 00112 } 00113 if (object==NULL) return; 00114 obj = new Object(object); 00115 } 00116 00117 template<class Object, class Object1, class Object2> 00118 void createWith2ObjectsAndFilename(Object*& obj, BaseElement* arg) 00119 { 00120 obj = NULL; 00121 const char* filename = arg->getAttribute("filename"); 00122 const char* object1 = arg->getAttribute("object1","../.."); 00123 const char* object2 = arg->getAttribute("object2",".."); 00124 if (!filename || !object1 || !object2) 00125 { 00126 std::cerr << arg->getType()<< " requires filename, object1 and object2 attributes\n"; 00127 return; 00128 } 00129 core::objectmodel::Base* pbase1 = arg->findObject(object1); 00130 if (pbase1==NULL) 00131 { 00132 std::cerr << arg->getType()<< " object1 \""<<object1<<"\" not found\n"; 00133 return; 00134 } 00135 core::objectmodel::Base* pbase2 = arg->findObject(object2); 00136 if (pbase2==NULL) 00137 { 00138 std::cerr << arg->getType()<< " object2 \""<<object2<<"\" not found\n"; 00139 return; 00140 } 00141 Object1* pobject1 = dynamic_cast<Object1*>(pbase1); 00142 if (pobject1==NULL && pbase1!=NULL) 00143 { // look for mechanicalmodel 00144 core::objectmodel::BaseContext* ctx = dynamic_cast<core::objectmodel::BaseContext*>(pbase1); 00145 if (ctx!=NULL) 00146 pobject1 = dynamic_cast<Object1*>(ctx->getMechanicalState()); 00147 } 00148 Object2* pobject2 = dynamic_cast<Object2*>(pbase2); 00149 if (pobject2==NULL && pbase2!=NULL) 00150 { // look for mechanicalmodel 00151 core::objectmodel::BaseContext* ctx = dynamic_cast<core::objectmodel::BaseContext*>(pbase2); 00152 if (ctx!=NULL) 00153 pobject2 = dynamic_cast<Object2*>(ctx->getMechanicalState()); 00154 } 00155 if (pobject1==NULL || pobject2==NULL) 00156 { 00157 //std::cerr << arg->getType()<<": object1 "<<(pobject1?"OK":arg->findObject(object1)?"INVALID":"NULL") 00158 // <<", object2 "<<(pobject2?"OK":arg->findObject(object2)?"INVALID":"NULL")<<std::endl; 00159 return; 00160 } 00161 obj = new Object(pobject1, pobject2, filename); 00162 } 00163 00164 template<class Object, class Object1, class Object2> 00165 void createWith2Objects(Object*& obj, BaseElement* arg) 00166 { 00167 obj = NULL; 00168 const char* object1 = arg->getAttribute("object1","../.."); 00169 const char* object2 = arg->getAttribute("object2",".."); 00170 if (!object1 || !object2) 00171 { 00172 std::cerr << arg->getType()<< " requires object1 and object2 attributes\n"; 00173 return; 00174 } 00175 core::objectmodel::Base* pbase1 = arg->findObject(object1); 00176 if (pbase1==NULL) 00177 { 00178 std::cerr << arg->getType()<< " object1 \""<<object1<<"\" not found\n"; 00179 return; 00180 } 00181 core::objectmodel::Base* pbase2 = arg->findObject(object2); 00182 if (pbase2==NULL) 00183 { 00184 std::cerr << arg->getType()<< " object2 \""<<object2<<"\" not found\n"; 00185 return; 00186 } 00187 Object1* pobject1 = dynamic_cast<Object1*>(pbase1); 00188 if (pobject1==NULL && pbase1!=NULL) 00189 { // look for mechanicalmodel 00190 core::objectmodel::BaseContext* ctx = dynamic_cast<core::objectmodel::BaseContext*>(pbase1); 00191 if (ctx!=NULL) 00192 pobject1 = dynamic_cast<Object1*>(ctx->getMechanicalState()); 00193 } 00194 Object2* pobject2 = dynamic_cast<Object2*>(pbase2); 00195 if (pobject2==NULL && pbase2!=NULL) 00196 { // look for mechanicalmodel 00197 core::objectmodel::BaseContext* ctx = dynamic_cast<core::objectmodel::BaseContext*>(pbase2); 00198 if (ctx!=NULL) 00199 pobject2 = dynamic_cast<Object2*>(ctx->getMechanicalState()); 00200 } 00201 if (pobject1==NULL || pobject2==NULL) 00202 { 00203 //std::cerr << arg->getType()<<": object1 "<<(pobject1?"OK":arg->findObject(object1)?"INVALID":"NULL") 00204 // <<", object2 "<<(pobject2?"OK":arg->findObject(object2)?"INVALID":"NULL")<<std::endl; 00205 return; 00206 } 00207 obj = new Object(pobject1, pobject2); 00208 } 00209 */ 00210 00211 } // namespace xml 00212 00213 } // namespace tree 00214 00215 } // namespace simulation 00216 00217 } // namespace sofa 00218 00219 #endif
1.5.7.1