DataPtr.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_DATAPTR_H
00028 #define SOFA_CORE_OBJECTMODEL_DATAPTR_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
00044 namespace sofa
00045 {
00046
00047 namespace core
00048 {
00049
00050 namespace objectmodel
00051 {
00052
00057 template < class T = void* >
00058 class DataPtr : public BaseData
00059 {
00060 public:
00065 DataPtr( T* t, const char* h, bool isDisplayed=true )
00066 : BaseData(h)
00067 , ptr(t)
00068 {
00069 m_isDisplayed = isDisplayed;
00070 }
00071
00072 virtual ~DataPtr()
00073 {}
00074
00075 inline void printValue(std::ostream& out) const ;
00076 inline std::string getValueString() const ;
00077 inline std::string getValueTypeString() const ;
00078 inline T* beginEdit()
00079 {
00080 m_isSet = true;
00081 return ptr;
00082 }
00083 inline void endEdit()
00084 {}
00085 inline void setPointer(T* p )
00086 {
00087 ptr = p;
00088 }
00089 inline void setValue(const T& value )
00090 {
00091 *beginEdit()=value;
00092 endEdit();
00093 }
00094 inline const T& getValue() const
00095 {
00096 return *ptr;
00097 }
00098
00102 virtual bool read( std::string& s )
00103 {
00104 if (s.empty())
00105 return false;
00106
00107 std::istringstream istr( s.c_str() );
00108 istr >> *ptr;
00109 if( istr.fail() )
00110 {
00111
00112 return false;
00113 }
00114 else
00115 {
00116 m_isSet = true;
00117 return true;
00118 }
00119 }
00120 protected:
00122 T* ptr;
00123
00124
00125
00126 };
00127
00129 template<>
00130 inline
00131 bool DataPtr<std::string>::read( std::string& str )
00132 {
00133 *ptr = str;
00134 m_isSet = true;
00135 return true;
00136 }
00137
00139 template<>
00140 inline
00141 bool DataPtr<bool>::read( std::string& str )
00142 {
00143 if (str.empty())
00144 return false;
00145 if (str[0] == 'T' || str[0] == 't')
00146 *ptr = true;
00147 else if (str[0] == 'F' || str[0] == 'f')
00148 *ptr = false;
00149 else if ((str[0] >= '0' && str[0] <= '9') || str[0] == '-')
00150 *ptr = (atoi(str.c_str()) != 0);
00151 else return false;
00152 m_isSet = true;
00153 return true;
00154 }
00155
00157 template<class T>
00158 inline
00159 void DataPtr<T>::printValue( std::ostream& out=std::cout ) const
00160 {
00161 out << *ptr << " ";
00162 }
00163
00165 template<class T>
00166 inline
00167 std::string DataPtr<T>::getValueString() const
00168 {
00169 std::ostringstream out;
00170 out << *ptr;
00171 return out.str();
00172 }
00173
00174 template<class T>
00175 inline
00176 std::string DataPtr<T>::getValueTypeString() const
00177 {
00178 return BaseData::typeName(ptr);
00179 }
00180
00181 }
00182
00183 }
00184
00185 }
00186
00187 #endif