DataPtr.h

Go to the documentation of this file.
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 :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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 ; //{ return std::string(typeid(*ptr).name()); }
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         //std::cerr<<"DataPtr::read "<<s.c_str()<<std::endl;
00107       std::istringstream istr( s.c_str() );
00108       istr >> *ptr;
00109       if( istr.fail() )
00110       {
00111             //std::cerr<<"field "<<getName<<" could not read value: "<<s<<std::endl;
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 } // namespace objectmodel
00182 
00183 } // namespace core
00184 
00185 } // namespace sofa
00186 
00187 #endif

Generated on Sun Nov 23 02:29:25 2008 for Sofa by  doxygen 1.5.7.1