public
Fork of psoetens/orocos-rtt
Description: Orocos Real-Time Toolkit
Homepage: www.orocos.org/rtt
Clone URL: git://github.com/doudou/orocos-rtt.git
Janosch Machowinski (author)
Wed Nov 26 07:50:12 -0800 2008
Sylvain Joyeux (committer)
Fri Dec 19 07:31:38 -0800 2008
orocos-rtt / src / Attribute.hpp
100644 338 lines (304 sloc) 10.981 kb
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/***************************************************************************
tag: Peter Soetens Tue Dec 21 22:43:08 CET 2004 Attribute.hpp
 
Attribute.hpp - description
-------------------
begin : Tue December 21 2004
copyright : (C) 2004 Peter Soetens
email : peter.soetens@mech.kuleuven.ac.be
 
***************************************************************************
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public *
* License as published by the Free Software Foundation; *
* version 2 of the License. *
* *
* As a special exception, you may use this file as part of a free *
* software library without restriction. Specifically, if other files *
* instantiate templates or use macros or inline functions from this *
* file, or you compile this file and link it with other files to *
* produce an executable, this file does not by itself cause the *
* resulting executable to be covered by the GNU General Public *
* License. This exception does not however invalidate any other *
* reasons why the executable file might be covered by the GNU General *
* Public License. *
* *
* 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 General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307 USA *
* *
***************************************************************************/
 
 
#ifndef ORO_CORELIB_ATTRIBUTE_HPP
#define ORO_CORELIB_ATTRIBUTE_HPP
 
#include "DataSource.hpp"
#include "DataSources.hpp"
#include "AttributeBase.hpp"
#include "BuildType.hpp"
 
namespace RTT
{
    /**
* An Attribute has a name and contains data which can be set and get.
* @param T The type of data this attribute holds.
* @ingroup CoreLib
*/
    template<typename T>
    class Attribute
        : public AttributeBase
    {
        typename AssignableDataSource<T>::shared_ptr data;
    public:
 
        /**
* Create an Attribute with no name and \b no value.
*/
        Attribute()
        {
        }
 
        /**
* Create an Attribute with a given name and a \b default value.
*
* @param name The name of this instance.
*/
        Attribute(const std::string& name)
            : AttributeBase(name),
              data( detail::BuildType<T>::Value( T() ) )
        {}
 
        /**
* Create an Attribute with a name and a given value \a t.
*
* @param name The name of this instance.
* @param t The value for initialisation.
*/
        explicit Attribute(const std::string& name, T t)
            : AttributeBase(name),
              data( detail::BuildType<T>::Value( t ) )
        {
        }
 
        /**
* Create an Attribute which uses a DataSource \a d.
*
* @param name The name
* @param d The data source to read from and write to.
*/
        Attribute( const std::string& name, AssignableDataSource<T>* d)
            : AttributeBase(name),
              data( d )
        {
        }
 
        /**
* Copy constructor copies both name and \b deep copy of the data.
*/
        Attribute( const Attribute<T>& a)
            : AttributeBase( a.mname ),
              data( a.data->clone() )
        {
        }
 
        /**
* Assignment copies both name and \b deep copy of the data.
*/
        Attribute<T>& operator=( const Attribute<T>& a)
        {
            if ( this == &a )
                return *this;
            mname = a.mname;
            data = a.data->clone();
            return *this;
        }
 
        /**
* Create an Attribute which \b mirrors a AttributeBase \a ab.
* If successful, this attribute will always have the same
* value as \a ab and vice versa.
*
* @param ab The Attribute to mirror.
* @see ready() to check if \a ab was accepted.
*/
        Attribute( AttributeBase* ab)
            : AttributeBase( ab ? ab->getName() : "" ),
              data( ab ? AssignableDataSource<T>::narrow( ab->getDataSource().get() ) : 0 )
        {
        }
 
        /**
* Initialise an Attribute which \a mirrors an AttributeBase \a ab.
* If successful, this attribute will always have the same
* value as \a ab and vice versa.
* @see ready() to check if ab was accepted.
*/
        Attribute<T>& operator=(AttributeBase* ab)
        {
            if (!ab) {
                data = 0;
                return *this;
            }
            mname = ab->getName();
            Attribute<T>* a = dynamic_cast<Attribute<T>*>(ab);
            if (a)
                data = a->getAssignableDataSource();
            return *this;
        }
 
        /**
* Get the value of this Attribute.
*/
        T get() const
        {
            return data->get();
        }
 
        /**
* Set the value of this Attribute.
*/
        void set( T t )
        {
            data->set(t);
        }
 
        /**
* Set the value of this Attribute.
*/
        typename AssignableDataSource<T>::reference_t set() {
            return data->set();
        }
 
        DataSourceBase::shared_ptr getDataSource() const
        {
            return data;
        }
 
        typename AssignableDataSource<T>::shared_ptr getAssignableDataSource() const
        {
            return data;
        }
 
        Attribute<T>* clone() const
        {
            return new Attribute<T>( mname, data.get() );
        }
 
        Attribute<T>* copy( std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool instantiate )
        {
            if ( instantiate ) {
                // by taking a clone(), the DS has a chance to instantiate itself.
                // A clone() of an UnboundDataSource returns the bound type.
                AssignableDataSource<T>* instds = data->clone();
                replacements[ data.get() ] = instds;
                return new Attribute<T>( mname, instds );
            }
            else {
                return new Attribute<T>( mname, data->copy( replacements ) );
            }
        }
    };
 
    /**
* As opposed to a Attribute, a Constant can not be assigned to a new value
* after creation.
* @ingroup CoreLib
*/
    template<typename T>
    class Constant
        : public AttributeBase
    {
    public:
        typename DataSource<T>::shared_ptr data;
 
        /**
* Create a constant with a fixed value \a t.
*/
        Constant(const std::string& name, T t)
            : AttributeBase(name),
              data( new ConstantDataSource<T>( t ) )
        {
        }
 
        /**
* Create a constant wich holds a DataSource \a d.
*/
        Constant(const std::string& name, DataSource<T>* d )
            : AttributeBase(name),
              data( d )
        {
        }
 
        /**
* Create a constant which mirrors an Attribute.
* If successful, this constant will always have the same
* value as \a ab.
* In case \a ab is non constant, it is not accepted.
* @see ready() to check if ab was accepted.
*/
        Constant( AttributeBase* ab )
            : AttributeBase( ab ? ab->getName() : "")
        {
            if ( !ab )
                return;
            Constant<T>* c = dynamic_cast<Constant<T>*>(ab);
            if (c)
                data = c->getDataSource();
        }
 
        /**
* Initialise an Attribute which mirrors an AttributeBase.
* If successful, this constant will always have the same
* value as \a ab.
* In case \a ab is non constant, it is not accepted.
* @see ready() to check if ab was accepted.
*/
        Constant<T>& operator=(AttributeBase* ab)
        {
            if (!ab) {
                data = 0;
                return *this;
            }
            mname = ab->getName();
            Constant<T>* c = dynamic_cast<Constant<T>*>(ab);
            if (c)
                data = c->getDataSource();
        }
 
        /**
* Get the value of this Constant.
*/
        T get() const
        {
            return data->get();
        }
 
        DataSourceBase::shared_ptr getDataSource() const
        {
            return data;
        }
 
        Constant<T>* clone() const
        {
            return new Constant<T>( mname, data.get() );
        }
 
        Constant<T>* copy( std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool instantiate )
        {
            // 'symbolic' copy, ConstantDataSource returns 'this' on copy...
            Constant<T>* ret = new Constant<T>( mname, data.get() );
            return ret;
        }
    };
 
    /**
* This class is the most basic Attribute implementation
* (only suitable for reading a DataSource), does
* not allow any assignment, just stores a DataSource<T>, and
* returns it. This also makes it useful as an alias of
* temporary expressions like literal values, and rhs
* expressions.
*/
    template<typename T>
    class Alias
        : public AttributeBase
    {
        typename DataSource<T>::shared_ptr data;
    public:
        Alias(const std::string& name, typename DataSource<T>::shared_ptr d )
            : AttributeBase(name),
              data( d )
        {
        }
 
        DataSourceBase::shared_ptr getDataSource() const
        {
            return data;
        }
 
        Alias<T>* clone() const
        {
            return new Alias<T>( mname, data.get() );
        }
        Alias<T>* copy( std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool )
        {
            // instantiate does not apply.
            return new Alias<T>( mname, data->copy( replacements ) );
        }
    };
}
#endif