File: Base.h

package info (click to toggle)
sofa-framework 1.0~beta4-11
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 88,820 kB
  • ctags: 27,300
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 68
file content (315 lines) | stat: -rw-r--r-- 12,024 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* This library is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this library; if not, write to the Free Software Foundation,     *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
*******************************************************************************
*                              SOFA :: Framework                              *
*                                                                             *
* Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
* H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
* M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
#ifndef SOFA_CORE_OBJECTMODEL_BASE_H
#define SOFA_CORE_OBJECTMODEL_BASE_H

#include <sofa/helper/system/config.h>
#include <sofa/helper/system/SofaOStream.h>
#include <sofa/helper/vector.h>
#include <sofa/core/objectmodel/DataPtr.h>
#include <sofa/core/objectmodel/Data.h>
#include <sofa/core/objectmodel/BaseObjectDescription.h>
#include <string>
#include <map>

using sofa::core::objectmodel::DataPtr;
using sofa::core::objectmodel::Data;

namespace sofa
{

namespace core
{

namespace objectmodel
{

/**
 *  \brief Base class for everything
 *
 *  This class contains all functionnality shared by every objects in SOFA.
 *  Most importantly it defines how to retrieve information about an object (name, type, fields).
 *
 */
class SOFA_CORE_API Base
{
public:
    Base();
    virtual ~Base();

    /// Name of the object.
    Data<std::string> name;

    /// @name debug
    ///   Methods related to debugging
    ///@{
    Data<bool> f_printLog;


    /// Accessor to the object name
    std::string getName() const;

    /// Set the name of this object
    void setName(const std::string& n);

    /// Get the type name of this object (i.e. class and template types)
    virtual std::string getTypeName() const
    {
        return decodeTypeName(typeid(*this));
    }

    /// Get the class name of this object
    virtual std::string getClassName() const
    {
        return decodeClassName(typeid(*this));
    }

    /// Get the template type names (if any) used to instantiate this object
    virtual std::string getTemplateName() const
    {
        return decodeTemplateName(typeid(*this));
    }

    /// Helper method to decode the type name to a more readable form if possible
    static std::string decodeTypeName(const std::type_info& t);

    /// Helper method to extract the class name (removing namespaces and templates)
    static std::string decodeClassName(const std::type_info& t);

    /// Helper method to extract the namespace (removing class name and templates)
    static std::string decodeNamespaceName(const std::type_info& t);

    /// Helper method to extract the template name (removing namespaces and class name)
    static std::string decodeTemplateName(const std::type_info& t);

    /// Helper method to get the type name of a type derived from this class
    ///
    /// This method should be used as follow :
    /// \code  T* ptr = NULL; std::string type = T::typeName(ptr); \endcode
    /// This way derived classes can redefine the typeName method
    template<class T>
    static std::string typeName(const T* = NULL)
    {
        return decodeTypeName(typeid(T));
    }

    /// Helper method to get the class name of a type derived from this class
    ///
    /// This method should be used as follow :
    /// \code  T* ptr = NULL; std::string type = T::className(ptr); \endcode
    /// This way derived classes can redefine the className method
    template<class T>
    static std::string className(const T* = NULL)
    {
        return decodeClassName(typeid(T));
    }

    /// Helper method to get the namespace name of a type derived from this class
    ///
    /// This method should be used as follow :
    /// \code  T* ptr = NULL; std::string type = T::namespaceName(ptr); \endcode
    /// This way derived classes can redefine the namespaceName method
    template<class T>
    static std::string namespaceName(const T* = NULL)
    {
        return decodeNamespaceName(typeid(T));
    }

    /// Helper method to get the template name of a type derived from this class
    ///
    /// This method should be used as follow :
    /// \code  T* ptr = NULL; std::string type = T::templateName(ptr); \endcode
    /// This way derived classes can redefine the templateName method
    template<class T>
    static std::string templateName(const T* = NULL)
    {
        return decodeTemplateName(typeid(T));
    }

    /// Assign the field values stored in the given list of name + value pairs of strings
    void parseFields ( std::list<std::string> str );

    /// Assign the field values stored in the given map of name -> value pairs
    virtual void parseFields ( const std::map<std::string,std::string*>& str );

    /// Write the current field values to the given map of name -> value pairs
    void writeDatas (std::map<std::string,std::string*>& str);

    /// Write the current Node values to the given XML output stream
    void xmlWriteNodeDatas (std::ostream& out, unsigned level);

    /// Write the current field values to the given XML output stream
    void xmlWriteDatas (std::ostream& out, unsigned level, bool compact);

    /// Find a field given its name, if not found, the index is the size of the vector
    BaseData* findField( const char* name ) const
    {
        std::string ln(name);
	for ( unsigned int i=0;i<m_fieldVec.size();i++)
	{
	    if (m_fieldVec[i].first == ln) return m_fieldVec[i].second;
	}
	return NULL;
    }
    BaseData* findField( const std::string &name ) const
    {
	for ( unsigned int i=0;i<m_fieldVec.size();i++)
	{
	    if (m_fieldVec[i].first == name) return m_fieldVec[i].second;
	}
	return NULL;
    }

    /// Find fields given a name: several can be found as we look into the alias map
    std::vector< BaseData* > findGlobalField( const char* name ) const
    {
        std::string ln(name);
        std::vector<BaseData*> dataCorresponding;
        //Search in the list of Datas
        BaseData *f=findField(name);
        if (f) dataCorresponding.push_back(f);
        //Search in the aliases
        typedef std::multimap< std::string, BaseData* >::const_iterator multimapIterator;
        std::pair< multimapIterator, multimapIterator> range;
        multimapIterator itAlias;

        range=m_aliasData.equal_range(ln);
        for (itAlias=range.first;itAlias!=range.second;itAlias++)dataCorresponding.push_back(itAlias->second);
        return dataCorresponding;
    }
    std::vector< BaseData* > findGlobalField( const std::string &name ) const
    {
        std::string ln(name);
        std::vector<BaseData*> dataCorresponding;
        //Search in the list of Datas
        BaseData *f=findField(name);
        if (f) dataCorresponding.push_back(f);
        //Search in the aliases
        typedef std::multimap< std::string, BaseData* >::const_iterator multimapIterator;
        std::pair< multimapIterator, multimapIterator> range;
        multimapIterator itAlias;

        range=m_aliasData.equal_range(name);
        for (itAlias=range.first;itAlias!=range.second;itAlias++)dataCorresponding.push_back(itAlias->second);
        return dataCorresponding;
    }

    /// Helper method used to initialize a field containing a value of type T
    template<class T>
    Data<T> initData( Data<T>* field, const char* name, const char* help, bool isDisplayed=true, bool isReadOnly=false )
    {
        std::string ln(name);
        if( ln.size()>0 && findField(ln) )
        {
            serr << "field name " << ln << " already used in this class or in a parent class !...aborting" << sendl;
            exit( 1 );
        }
        //field = tmp;
	m_fieldVec.push_back( std::make_pair(ln,field));
	return Data<T>(help,isDisplayed,isReadOnly);
    }

    /// Helper method used to initialize a field containing a value of type T
    template<class T>
	Data<T> initData( Data<T>* field, const T& value, const char* name, const char* help, bool isDisplayed=true, bool isReadOnly=false  )
    {
        std::string ln(name);
        if( ln.size()>0 && findField(ln)  )
        {
            serr << "field name " << ln << " already used in this class or in a parent class !...aborting" << sendl;
            exit( 1 );
        }
        //field = tmp;
	m_fieldVec.push_back( std::make_pair(ln,field));
        return Data<T>(value,help,isDisplayed,isReadOnly);
    }

    /// Helper method used to initialize a field pointing to a value of type T
    template<class T>
	DataPtr<T> initDataPtr( DataPtr<T>* field, T* ptr, const char* name, const char* help, bool isDisplayed=true, bool isReadOnly=false  )
    {
        std::string ln(name);
        if( ln.size()>0 && findField(ln) )
        {
            serr << "field name " << ln << " already used in this class or in a parent class !...aborting" << sendl;
            exit( 1 );
        }
        //field = tmp;
	m_fieldVec.push_back( std::make_pair(ln,field));
	return DataPtr<T>(ptr,help,isDisplayed,isReadOnly);
    }

    /// Helper method used to add an alias to a DataPtr

    void addAlias( BaseData* field, const char* alias)
    {
      m_aliasData.insert(std::make_pair(std::string(alias),field));
    }

    /// Parse the given description to assign values to this object's fields and potentially other parameters
    virtual void parse ( BaseObjectDescription* arg );

    /// Accessor to the vector containing all the fields of this object
    std::vector< std::pair<std::string, BaseData*> > getFields() { return m_fieldVec; }
    /// Accessor to the map containing all the aliases of this object
    std::multimap< std::string, BaseData* > getAliases() { return m_aliasData; }

    mutable sofa::helper::system::SofaOStream sendl;
    std::ostringstream               &serr;
    std::ostringstream               &sout;


protected:


    /// name -> Field object
    std::vector< std::pair<std::string, BaseData*> > m_fieldVec;
    std::multimap< std::string, BaseData* > m_aliasData;

    /// Add a field. Note that this method should only be called if the field was not initialized with the initData<T> of field<T> methods
    void addField( BaseData* f, const char* name )
    {
        std::string ln(name);
        if( ln.size()>0 && findField(ln) )
        {
            serr << "field name " << ln << " already used in this class or in a parent class !...aborting" << sendl;
            exit( 1 );
        }
	m_fieldVec.push_back( std::make_pair(ln,f));
    }
};

} // namespace objectmodel

} // namespace core

} // namespace sofa



#endif