DynamicAttributes.hxx 4 KB
Newer Older
1
2
3
4
5
6
/* ***** BEGIN LICENSE BLOCK *****
 * FW4SPL - Copyright (C) IRCAD, 2009-2010.
 * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
 * published by the Free Software Foundation.
 * ****** END LICENSE BLOCK ****** */

Nicolas Philipps's avatar
Nicolas Philipps committed
7
8
9
#ifndef _FWTOOLS_DYNAMIC_ATTRIBUTE_HXX_
#define _FWTOOLS_DYNAMIC_ATTRIBUTE_HXX_

10
11
#include <vector>

12
#include <boost/ref.hpp>
13
#include <boost/foreach.hpp>
14

15
16
17
#include <fwCore/base.hpp>
#include <fwCore/Demangler.hpp>

18
19
20
21
22
23
24
#include "fwTools/macros.hpp"

namespace fwTools {

template<class CLASS>
class DynamicAttributes
{
Julien Waechter's avatar
Julien Waechter committed
25

26
public:
27
28
    typedef std::string  AttrNameType;
    typedef SPTR(CLASS)  AttrType;
29
    typedef std::vector< AttrNameType > AttrNameVectorType;
30
31
32
    typedef ::boost::reference_wrapper< AttrType > AttrRefType;
    typedef std::map< AttrNameType, AttrRefType > AttrMapType;

33
34
    typedef CSPTR(CLASS) ConstAttrType;

35
36
    DynamicAttributes();
    virtual ~DynamicAttributes();
Julien Waechter's avatar
Julien Waechter committed
37

38
    virtual ConstAttrType getConstAttribute( AttrNameType attrName ) const;
39
    virtual AttrType getAttribute( AttrNameType attrName );
40
    bool        hasAttribute( AttrNameType attrName );
Julien Waechter's avatar
Julien Waechter committed
41

42
    virtual fwToolsSetAttributeSignatureMacro() {};
43
44
45

    virtual AttrNameVectorType getAttributeNames();

Julien Waechter's avatar
Julien Waechter committed
46
    virtual fwToolsRegisterAttributeSignatureMacro() {};
47
protected:
48
    virtual AttrRefType getAttributeRef( AttrNameType attrName );
49
50
51
52
53
    AttrMapType __FWTOOLS_ATTRIBUTE_MAP_NAME;


};

54
//------------------------------------------------------------------------------
55
56
57
58
59
60

template< class CLASS >
DynamicAttributes< CLASS >::DynamicAttributes()
{
}

61
62
//------------------------------------------------------------------------------

63
64
65
66
67
template< class CLASS >
DynamicAttributes< CLASS >::~DynamicAttributes()
{
}

68
69
//------------------------------------------------------------------------------

70
template< class CLASS >
71
72
73
74
75
76
77
78
79
typename DynamicAttributes< CLASS >::AttrType DynamicAttributes< CLASS >::getAttribute( AttrNameType attrName )
{
    return this->getAttributeRef(attrName);
}

//------------------------------------------------------------------------------

template< class CLASS >
typename DynamicAttributes< CLASS >::AttrRefType DynamicAttributes< CLASS >::getAttributeRef( AttrNameType attrName )
80
81
82
83
84
85
86
87
88
{
    typename DynamicAttributes::AttrMapType::iterator iter = this->__FWTOOLS_ATTRIBUTE_MAP_NAME.find(attrName);
    OSLM_FATAL_IF(
            "Object "<< ::fwCore::getFullClassname< CLASS >() << "has no attribute named '"<< attrName << "'",
            iter == this->__FWTOOLS_ATTRIBUTE_MAP_NAME.end()
            );
    return (*iter).second;
}

89
90
//------------------------------------------------------------------------------

91
92
93
94
95
96
97
98
99
100
101
102
103
template< class CLASS >
typename DynamicAttributes< CLASS >::ConstAttrType DynamicAttributes< CLASS >::getConstAttribute( AttrNameType attrName ) const
{
    typename DynamicAttributes::AttrMapType::const_iterator iter = this->__FWTOOLS_ATTRIBUTE_MAP_NAME.find(attrName);
    OSLM_FATAL_IF(
            "Object "<< ::fwCore::getFullClassname< CLASS >() << "has no attribute named '"<< attrName << "'",
            iter == this->__FWTOOLS_ATTRIBUTE_MAP_NAME.end()
            );
    return (*iter).second.get();
}

//------------------------------------------------------------------------------

104
template< class CLASS >
Julien Waechter's avatar
Julien Waechter committed
105
bool DynamicAttributes< CLASS >::hasAttribute( AttrNameType attrName )
106
107
108
109
110
{
    typename DynamicAttributes::AttrMapType::iterator iter = this->__FWTOOLS_ATTRIBUTE_MAP_NAME.find(attrName);
    return iter != this->__FWTOOLS_ATTRIBUTE_MAP_NAME.end();
}

111
112
113
114
115
116
//------------------------------------------------------------------------------

template< class CLASS >
typename DynamicAttributes< CLASS >::AttrNameVectorType  DynamicAttributes< CLASS >::getAttributeNames()
{
    AttrNameVectorType names;
Nicolas Philipps's avatar
Nicolas Philipps committed
117
118
    typename DynamicAttributes::AttrMapType::const_iterator iter;
    for ( iter = this->__FWTOOLS_ATTRIBUTE_MAP_NAME.begin(); iter != this->__FWTOOLS_ATTRIBUTE_MAP_NAME.end(); ++iter )
119
    {
Nicolas Philipps's avatar
Nicolas Philipps committed
120
        names.push_back((*iter).first);
121
122
123
    }
    return names;
}
124

Julien Waechter's avatar
Julien Waechter committed
125
} //end namespace fwTools
126

Nicolas Philipps's avatar
Nicolas Philipps committed
127
#endif //_FWTOOLS_DYNAMIC_ATTRIBUTE_HXX_