Base.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
00026
00027 #ifndef SOFA_CORE_OBJECTMODEL_BASE_H
00028 #define SOFA_CORE_OBJECTMODEL_BASE_H
00029
00030 #include <sofa/helper/system/config.h>
00031 #include <sofa/core/objectmodel/DataPtr.h>
00032 #include <sofa/core/objectmodel/Data.h>
00033 #include <sofa/core/objectmodel/BaseObjectDescription.h>
00034 #include <string>
00035
00036 using sofa::core::objectmodel::DataPtr;
00037 using sofa::core::objectmodel::Data;
00038
00039 namespace sofa
00040 {
00041
00042 namespace core
00043 {
00044
00045 namespace objectmodel
00046 {
00047
00055 class Base
00056 {
00057 public:
00058 Base();
00059 virtual ~Base();
00060
00062 Data<std::string> name;
00063
00065 std::string getName() const;
00066
00068 void setName(const std::string& n);
00069
00071 virtual std::string getTypeName() const
00072 {
00073 return decodeTypeName(typeid(*this));
00074 }
00075
00077 virtual std::string getClassName() const
00078 {
00079 return decodeClassName(typeid(*this));
00080 }
00081
00083 virtual std::string getTemplateName() const
00084 {
00085 return decodeTemplateName(typeid(*this));
00086 }
00087
00089 static std::string decodeTypeName(const std::type_info& t);
00090
00092 static std::string decodeClassName(const std::type_info& t);
00093
00095 static std::string decodeNamespaceName(const std::type_info& t);
00096
00098 static std::string decodeTemplateName(const std::type_info& t);
00099
00105 template<class T>
00106 static std::string typeName(const T* = NULL)
00107 {
00108 return decodeTypeName(typeid(T));
00109 }
00110
00116 template<class T>
00117 static std::string className(const T* = NULL)
00118 {
00119 return decodeClassName(typeid(T));
00120 }
00121
00127 template<class T>
00128 static std::string namespaceName(const T* = NULL)
00129 {
00130 return decodeNamespaceName(typeid(T));
00131 }
00132
00138 template<class T>
00139 static std::string templateName(const T* = NULL)
00140 {
00141 return decodeTemplateName(typeid(T));
00142 }
00143
00145 void parseFields ( std::list<std::string> str );
00146
00148 virtual void parseFields ( const std::map<std::string,std::string*>& str );
00149
00151 void writeDatas (std::map<std::string,std::string*>& str);
00152
00154 void writeDatas (std::ostream& out);
00155
00157 void xmlWriteNodeDatas (std::ostream& out, unsigned level);
00158
00160 void xmlWriteDatas (std::ostream& out, unsigned level);
00161
00163 unsigned int findField( const char* name ) const
00164 {
00165 std::string ln(name);
00166 unsigned int i;
00167 for ( i=0;i<m_fieldVec.size();i++)
00168 {
00169 if (m_fieldVec[i].first == ln) return i;
00170 }
00171 return i;
00172 }
00173 unsigned int findField( const std::string &name ) const
00174 {
00175 unsigned int i;
00176 for ( i=0;i<m_fieldVec.size();i++)
00177 {
00178 if (m_fieldVec[i].first == name) return i;
00179 }
00180 return i;
00181 }
00183 template<class T>
00184 Data<T> initData( Data<T>* field, const char* name, const char* help, bool isDisplayed=true )
00185 {
00186 std::string ln(name);
00187 if( ln.size()>0 && findField(ln)!=m_fieldVec.size() )
00188 {
00189 std::cerr << "field name " << ln << " already used in this class or in a parent class !...aborting" << std::endl;
00190 exit( 1 );
00191 }
00192
00193 m_fieldVec.push_back( std::make_pair(ln,field));
00194 return Data<T>(help,isDisplayed);
00195 }
00196
00198 template<class T>
00199 Data<T> initData( Data<T>* field, const T& value, const char* name, const char* help, bool isDisplayed=true )
00200 {
00201 std::string ln(name);
00202 if( ln.size()>0 && findField(ln)!=m_fieldVec.size() )
00203 {
00204 std::cerr << "field name " << ln << " already used in this class or in a parent class !...aborting" << std::endl;
00205 exit( 1 );
00206 }
00207
00208 m_fieldVec.push_back( std::make_pair(ln,field));
00209 return Data<T>(value,help,isDisplayed);
00210 }
00211
00213 template<class T>
00214 DataPtr<T> initDataPtr( DataPtr<T>* field, T* ptr, const char* name, const char* help, bool isDisplayed=true )
00215 {
00216 std::string ln(name);
00217 if( ln.size()>0 && findField(ln)!=m_fieldVec.size() )
00218 {
00219 std::cerr << "field name " << ln << " already used in this class or in a parent class !...aborting" << std::endl;
00220 exit( 1 );
00221 }
00222
00223 m_fieldVec.push_back( std::make_pair(ln,field));
00224 return DataPtr<T>(ptr,help,isDisplayed);
00225 }
00226
00227
00229 virtual void parse ( BaseObjectDescription* arg );
00230
00232 std::vector< std::pair<std::string, BaseData*> > getFields() { return m_fieldVec; }
00233
00234 protected:
00236 std::vector< std::pair<std::string, BaseData*> > m_fieldVec;
00237
00239 void addField( BaseData* f, const char* name )
00240 {
00241 std::string ln(name);
00242 if( ln.size()>0 && findField(ln)!=m_fieldVec.size() )
00243 {
00244 std::cerr << "field name " << ln << " already used in this class or in a parent class !...aborting" << std::endl;
00245 exit( 1 );
00246 }
00247 m_fieldVec.push_back( std::make_pair(ln,f));
00248 }
00249 };
00250
00251 }
00252
00253 }
00254
00255 }
00256
00257 #endif
00258