Data.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_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()) // BUGFIX (Jeremie A.): Force initialization of basic types to 0 (bool, int, float, etc).
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; // { return std::string(typeid(m_value).name()); }
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         //std::cerr<<"Field::read "<<s.c_str()<<std::endl;
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 } // namespace objectmodel
00209 
00210 } // namespace core
00211 
00212 } // namespace sofa
00213 
00214 #endif

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