Data.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_DATAFIELD_H
00028 #define SOFA_CORE_OBJECTMODEL_DATAFIELD_H
00029
00030 #if !defined(__GNUC__) || (__GNUC__ > 3 || (_GNUC__ == 3 && __GNUC_MINOR__ > 3))
00031 #pragma once
00032 #endif
00033
00034 #include <sofa/core/objectmodel/BaseData.h>
00035 #include <stdlib.h>
00036 #include <string>
00037 #include <sstream>
00038 #include <fstream>
00039 #include <vector>
00040 #include <algorithm>
00041 #include <map>
00042
00043 namespace sofa
00044 {
00045
00046 namespace core
00047 {
00048
00049 namespace objectmodel
00050 {
00051
00056 template < class T = void* >
00057 class Data : public sofa::core::objectmodel::BaseData
00058 {
00059 public:
00060
00064 Data( const char* helpMsg=0, bool isDisplayed=true )
00065 : BaseData(helpMsg)
00066 , m_value(T())
00067 {
00068 m_isDisplayed = isDisplayed;
00069 }
00070
00075 Data( const T& value, const char* helpMsg=0, bool isDisplayed=true )
00076 : BaseData(helpMsg)
00077 , m_value(value)
00078 {
00079 m_isDisplayed = isDisplayed;
00080 }
00081
00082 virtual ~Data()
00083 {}
00084
00085 inline void setHelpMsg( const char* msg ){ this->help = msg; }
00086 inline void printValue(std::ostream& out) const ;
00087 inline std::string getValueString() const ;
00088 inline std::string getValueTypeString() const;
00089 inline T* beginEdit()
00090 {
00091 m_isSet = true;
00092 return &m_value;
00093 }
00094 inline void endEdit()
00095 {}
00096 inline void setValue(const T& value )
00097 {
00098 *beginEdit()=value;
00099 endEdit();
00100 }
00101 inline const T& getValue() const
00102 {
00103 return m_value;
00104 }
00105
00106 inline friend std::ostream & operator << (std::ostream &out, const Data& df)
00107 {
00108 out<<df.getValue();
00109 return out;
00110 }
00111
00112 inline bool operator ==( const T& value ) const
00113 {
00114 return getValue()==value;
00115 }
00116
00117 inline bool operator !=( const T& value ) const
00118 {
00119 return getValue()!=value;
00120 }
00121
00122 inline void operator =( const T& value )
00123 {
00124 this->setValue(value);
00125 }
00126
00130 virtual bool read( std::string& s )
00131 {
00132 if (s.empty())
00133 return false;
00134
00135 std::istringstream istr( s.c_str() );
00136 istr >> m_value;
00137 if( istr.fail() )
00138 {
00139 return false;
00140 }
00141 else
00142 {
00143 m_isSet = true;
00144 return true;
00145 }
00146 }
00147 protected:
00149 T m_value;
00150
00151
00152 };
00153
00155 template<>
00156 inline
00157 bool Data<std::string>::read( std::string& str )
00158 {
00159 m_value = str;
00160 m_isSet = true;
00161 return true;
00162 }
00163
00165 template<>
00166 inline
00167 bool Data<bool>::read( std::string& str )
00168 {
00169 if (str.empty())
00170 return false;
00171 if (str[0] == 'T' || str[0] == 't')
00172 m_value = true;
00173 else if (str[0] == 'F' || str[0] == 'f')
00174 m_value = false;
00175 else if ((str[0] >= '0' && str[0] <= '9') || str[0] == '-')
00176 m_value = (atoi(str.c_str()) != 0);
00177 else return false;
00178 m_isSet = true;
00179 return true;
00180 }
00181
00183 template<class T>
00184 inline
00185 void Data<T>::printValue( std::ostream& out=std::cout ) const
00186 {
00187 out << m_value << " ";
00188 }
00189
00191 template<class T>
00192 inline
00193 std::string Data<T>::getValueString() const
00194 {
00195 std::ostringstream out;
00196 out << m_value;
00197 return out.str();
00198 }
00199
00200 template<class T>
00201 inline
00202 std::string Data<T>::getValueTypeString() const
00203 {
00204 return BaseData::typeName(&m_value);
00205 }
00206
00207
00208 }
00209
00210 }
00211
00212 }
00213
00214 #endif