Orocos Real-Time Toolkit  2.8.3
Attribute.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Dec 21 22:43:08 CET 2004 Attribute.hpp
3 
4  Attribute.hpp - description
5  -------------------
6  begin : Tue December 21 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_CORELIB_ATTRIBUTE_HPP
40 #define ORO_CORELIB_ATTRIBUTE_HPP
41 
42 #include "internal/DataSource.hpp"
43 #include "internal/DataSources.hpp"
44 #include "base/AttributeBase.hpp"
45 
46 namespace RTT
47 {
55  template<typename T>
56  class Attribute
57  : public base::AttributeBase
58  {
60  public:
61 
66  {
67  }
68 
74  explicit Attribute(const std::string& name)
75  : base::AttributeBase(name),
76  data( new internal::ValueDataSource<T>( T() ) )
77  {}
78 
85  Attribute(const std::string& name, T t)
86  : base::AttributeBase(name),
87  data( new internal::ValueDataSource<T>( t ) )
88  {
89  }
90 
100  template<class Owner>
101  Attribute(const std::string& name, T t, Owner o)
102  : base::AttributeBase(name),
103  data( new internal::ValueDataSource<T>( t ) )
104  {
105  o->addAttribute(this);
106  }
107 
114  Attribute( const std::string& name, internal::AssignableDataSource<T>* d)
115  : base::AttributeBase(name),
116  data( d )
117  {
118  }
119 
124  : base::AttributeBase( a.mname ),
125  data( a.data->clone() )
126  {
127  }
128 
133  {
134  if ( this == &a )
135  return *this;
136  mname = a.mname;
137  data = a.data->clone();
138  return *this;
139  }
140 
151  : base::AttributeBase( ab ? ab->getName() : "" ),
152  data( ab ? internal::AssignableDataSource<T>::narrow( ab->getDataSource().get() ) : 0 )
153  {
154  }
155 
165  {
166  if ( ab == this )
167  return *this;
168 
169  if (!ab) {
170  data = 0;
171  mname.clear();
172  return *this;
173  }
175  = boost::dynamic_pointer_cast<internal::AssignableDataSource<T> >( ab->getDataSource() );
176  if (a) {
177  data = a;
178  mname = ab->getName();
179  } else {
180  data = 0;
181  }
182  return *this;
183  }
184 
188  T const& get() const
189  {
190  data->evaluate();
191  return data->rvalue();
192  }
193 
199  void set( T const& t )
200  {
201  data->set(t);
202  }
203 
211  return data->set();
212  }
213 
215  {
216  return data;
217  }
218 
220  {
221  return data;
222  }
223 
225  {
226  return new Attribute<T>( mname, data.get() );
227  }
228 
229  Attribute<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replacements, bool instantiate )
230  {
231  if ( instantiate ) {
232  // by taking a clone(), the DS has a chance to instantiate itself.
233  // A clone() of an internal::UnboundDataSource returns the bound type.
234  internal::AssignableDataSource<T>* instds = data->clone();
235  replacements[ data.get() ] = instds;
236  return new Attribute<T>( mname, instds );
237  }
238  else {
239  return new Attribute<T>( mname, data->copy( replacements ) );
240  }
241  }
242  };
243 
248  template<typename T>
249  class Constant
250  : public base::AttributeBase
251  {
252  public:
254 
259  {
260  }
261 
267  Constant(const std::string& name, T t)
268  : base::AttributeBase(name),
269  data( new internal::ConstantDataSource<T>( t ) )
270  {
271  }
272 
282  template<class Owner>
283  Constant(const std::string& name, T t, Owner owner)
284  : base::AttributeBase(name),
285  data( new internal::ConstantDataSource<T>( t ) )
286  {
287  owner->addAttribute( this );
288  }
289 
293  Constant(const std::string& name, internal::DataSource<T>* d )
294  : base::AttributeBase(name),
295  data( d )
296  {
297  }
298 
308  : base::AttributeBase( ab ? ab->getName() : ""),
309  data( ab ? internal::DataSource<T>::narrow( ab->getDataSource().get() ) : 0 )
310  {
311  }
312 
322  {
323  if ( ab == this)
324  return *this;
325  if (!ab) {
326  data = 0;
327  mname.clear();
328  return *this;
329  }
331  = boost::dynamic_pointer_cast<internal::DataSource<T> >( ab->getDataSource() );
332  if (a) {
333  data = a;
334  mname = ab->getName();
335  } else {
336  data = 0;
337  }
338  return *this;
339  }
340 
344  T get() const
345  {
346  return data->get();
347  }
348 
350  {
351  return data;
352  }
353 
355  {
356  return new Constant<T>( mname, data.get() );
357  }
358 
359  Constant<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replacements, bool instantiate )
360  {
361  // 'symbolic' copy, internal::ConstantDataSource returns 'this' on copy...
362  Constant<T>* ret = new Constant<T>( mname, data.get() );
363  return ret;
364  }
365  };
366 
376  : public base::AttributeBase
377  {
379  public:
380  Alias(const std::string& name, base::DataSourceBase::shared_ptr d );
381 
391  template<class Owner>
392  Alias(const std::string& name, base::DataSourceBase::shared_ptr d, Owner owner)
393  : base::AttributeBase(name),
394  data( d )
395  {
396  owner->addAttribute( this );
397  }
398 
400 
401  Alias* clone() const;
402 
403  Alias* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replacements, bool );
404  };
405 }
406 #endif
DataSource is a base class representing a generic way to read data of type T.
Definition: DataSource.hpp:94
Constant(const std::string &name, T t, Owner owner)
Create a constant with a fixed value t and an owner.
Definition: Attribute.hpp:283
Constant()
Create a Constant with no name and no value.
Definition: Attribute.hpp:258
virtual result_t get() const =0
Return the data as type T.
Attribute< T > & operator=(const Attribute< T > &a)
Assignment copies both name and deep copy of the data.
Definition: Attribute.hpp:132
Constant(base::AttributeBase *ab)
Create a constant which mirrors an Attribute.
Definition: Attribute.hpp:307
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
Attribute< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replacements, bool instantiate)
Returns a copy of this AttributeBase.
Definition: Attribute.hpp:229
virtual void set(param_t t)=0
Set this DataSource with a value.
Constant(const std::string &name, internal::DataSource< T > *d)
Create a constant wich holds a internal::DataSource d.
Definition: Attribute.hpp:293
Constant< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replacements, bool instantiate)
Returns a copy of this AttributeBase.
Definition: Attribute.hpp:359
#define RTT_API
Definition: rtt-config.h:97
T const & get() const
Get the value of this Attribute.
Definition: Attribute.hpp:188
Attribute(const std::string &name, T t, Owner o)
Create an Attribute with a name, a given value t and an owner.
Definition: Attribute.hpp:101
virtual DataSourceBase::shared_ptr getDataSource() const =0
Return a internal::DataSource which contains the same contents.
internal::AssignableDataSource< T >::shared_ptr getAssignableDataSource() const
Definition: Attribute.hpp:219
Attribute(const std::string &name, T t)
Create an Attribute with a name and a given value t.
Definition: Attribute.hpp:85
base::DataSourceBase::shared_ptr getDataSource() const
Return a internal::DataSource which contains the same contents.
Definition: Attribute.hpp:214
An attribute is a minimalistic, named placeholder for data.
Attribute(const std::string &name, internal::AssignableDataSource< T > *d)
Create an Attribute which uses a internal::DataSource d.
Definition: Attribute.hpp:114
Attribute< T > * clone() const
Returns a clone of this AttributeBase.
Definition: Attribute.hpp:224
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
virtual AssignableDataSource< T > * clone() const =0
Return a shallow clone of this DataSource.
base::DataSourceBase::shared_ptr getDataSource() const
Return a internal::DataSource which contains the same contents.
Definition: Attribute.hpp:349
Attribute(const std::string &name)
Create an Attribute with a given name and a default value.
Definition: Attribute.hpp:74
Constant< T > & operator=(base::AttributeBase *ab)
Initialise an Attribute which mirrors an AttributeBase.
Definition: Attribute.hpp:321
Constant< T > * clone() const
Returns a clone of this AttributeBase.
Definition: Attribute.hpp:354
boost::intrusive_ptr< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
A DataSource which holds a constant value and returns it in its get() method.
A DataSource which has set() methods.
Definition: DataSource.hpp:184
Alias(const std::string &name, base::DataSourceBase::shared_ptr d, Owner owner)
Create an alias from a datasource with an owner.
Definition: Attribute.hpp:392
Attribute(base::AttributeBase *ab)
Create an Attribute which mirrors a base::AttributeBase ab.
Definition: Attribute.hpp:150
Attribute(const Attribute< T > &a)
Copy constructor copies both name and deep copy of the data.
Definition: Attribute.hpp:123
boost::intrusive_ptr< DataSourceBase > shared_ptr
Use this type to store a pointer to a DataSourceBase.
An Attribute has a name and contains data which can be set and get.
Definition: Attribute.hpp:56
Constant(const std::string &name, T t)
Create a constant with a fixed value t.
Definition: Attribute.hpp:267
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
const std::string & getName() const
Get the name of this instance.
Definition: Attribute.cpp:59
internal::DataSource< T >::shared_ptr data
Definition: Attribute.hpp:253
virtual bool evaluate() const
Force an evaluation of the DataSourceBase.
Definition: DataSource.inl:52
As opposed to a Attribute, a Constant can not be assigned to a new value after creation.
Definition: Attribute.hpp:249
This class is the most basic Attribute implementation (only suitable for reading a internal::DataSour...
Definition: Attribute.hpp:375
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
Attribute()
Create an Attribute with no name and no value.
Definition: Attribute.hpp:65
Attribute< T > & operator=(base::AttributeBase *ab)
Initialise an Attribute which mirrors an base::AttributeBase ab.
Definition: Attribute.hpp:164
virtual AssignableDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const =0
Create a deep copy of this internal::DataSource, unless it is already cloned.