Orocos Real-Time Toolkit  2.9.0
Property.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:19 CET 2004 Property.hpp
3 
4  Property.hpp - description
5  -------------------
6  begin : Mon January 19 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 #ifndef ORO_PROPERTY_HPP
39 #define ORO_PROPERTY_HPP
40 
41 #include "rtt-config.h"
42 #include "base/PropertyBase.hpp"
43 #include "PropertyBag.hpp"
44 #include "internal/DataSources.hpp"
45 #include <boost/type_traits/remove_const.hpp>
46 #include <boost/call_traits.hpp>
47 #include "Logger.hpp"
48 
49 #include <string>
50 #include <ostream>
51 
52 #ifdef ORO_PRAGMA_INTERFACE
53 #pragma interface
54 #endif
55 
56 namespace RTT
57 {
75  template<typename T>
76  class Property
77  : public base::PropertyBase
78  {
79  public:
85  typedef typename boost::remove_const<typename boost::remove_reference<T>::type>::type value_t;
86  typedef typename boost::call_traits<value_t>::param_type param_t;
87  typedef typename boost::call_traits<value_t>::reference reference_t;
88  typedef typename boost::call_traits<value_t>::const_reference const_reference_t;
89  typedef value_t DataSourceType;
90 
96  {}
97 
104  explicit Property(const std::string& name)
105  : base::PropertyBase(name,""), _value( new internal::ValueDataSource<value_t>() )
106  {
107  }
108 
117  Property(const std::string& name, const std::string& description, param_t value = value_t() )
118  : base::PropertyBase(name, description), _value( new internal::ValueDataSource<value_t>( value ) )
119  {
120  }
121 
127  Property( const Property<T>& orig)
128  : base::PropertyBase(orig.getName(), orig.getDescription()),
129  _value( orig._value ? orig._value->clone() : 0 )
130  {
131  // need to do this on the clone() in order to have access to set()/rvalue() of the data source.
132  if (_value)
133  _value->evaluate();
134  }
135 
143  : base::PropertyBase(source ? source->getName() : "", source ? source->getDescription() : "")
144  {
145  if ( source ) {
147  if ( !setDataSource(dsb) ) {
148  log(Error) << "Cannot initialize Property from " << source->getName() << ": ";
149  if ( dsb ) {
150  log() << "incompatible type ( destination type: " << getType() << ", source type: " << dsb->getTypeName() << ")." << endlog();
151  } else {
152  log() << "source Property was not ready." << endlog();
153  }
154  }
155  }
156  }
157 
167  Property(const std::string& name, const std::string& description,
169  : base::PropertyBase(name, description), _value( datasource )
170  {
171  // need to do this on the datasource in order to have access to set()/rvalue() of the data source.
172  if (_value)
173  _value->evaluate();
174  }
175 
182  {
183  _value->set( value );
184  return *this;
185  }
186 
192  {
193  if ( this == source )
194  return *this;
195 
196  if ( source ) {
197  this->setName( source->getName() );
198  this->setDescription( source->getDescription() );
199 
201  if ( this->setDataSource(dsb) ) {
202  return *this;
203  }
204  }
205 
206  // wrong assignment: mark not ready.
207  this->setName( "" );
208  this->setDescription( "" );
209  _value = 0;
210  return *this;
211  }
212 
217  Property<T>& operator<<=(Property<T> &p)
218  {
219  this->update( p );
220  return *this;
221  }
222 
228  Property<T>& doc(const std::string& descr) {
229  setDescription(descr);
230  return *this;
231  }
232 
237  operator value_t() const
238  {
239  return _value->get();
240  }
241 
246  DataSourceType get() const
247  {
248  return _value->get();
249  }
250 
257  reference_t set()
258  {
259  return _value->set();
260  }
261 
265  void set(param_t v)
266  {
267  _value->set(v);
268  }
269 
277  reference_t value()
278  {
279  return set();
280  }
281 
285  const_reference_t rvalue() const
286  {
287  return _value->rvalue();
288  }
289 
299  static Property<T>* narrow( base::PropertyBase* prop );
300 
301  virtual void identify( base::PropertyIntrospection* pi);
302 
303  virtual void identify( base::PropertyBagVisitor* pi);
304 
305  virtual bool update( const base::PropertyBase* other)
306  {
307  const Property<T>* origin = dynamic_cast< const Property<T>* >( other );
308  if ( origin != 0 ) {
309  return this->update( *origin );
310  }
311  return false;
312  }
313 
314  virtual bool refresh( const base::PropertyBase* other)
315  {
316  const Property<T>* origin = dynamic_cast< const Property<T>* >( other );
317  if ( origin != 0 && _value ) {
318  return this->refresh( *origin );
319  }
320  return false;
321  }
322 
323  virtual bool copy( const base::PropertyBase* other )
324  {
325  const Property<T>* origin = dynamic_cast< const Property<T>* >( other );
326  if ( origin != 0 && _value ) {
327  return this->copy( *origin );
328  }
329  return false;
330  }
331 
335  bool copy( const Property<T>& orig)
336  {
337  if ( !ready() )
338  return false;
339  _description = orig.getDescription();
340  _name = orig.getName();
341  _value->set( orig.rvalue() );
342  return true;
343  }
344 
349  bool update( const Property<T>& orig)
350  {
351  if ( !ready() )
352  return false;
353  if ( _description.empty() )
354  _description = orig.getDescription();
355  _value->set( orig.rvalue() );
356  return true;
357  }
358 
363  bool refresh( const Property<T>& orig)
364  {
365  if ( !ready() )
366  return false;
367  _value->set( orig.rvalue() );
368  return true;
369  }
370 
371  virtual Property<T>* clone() const
372  {
373  return new Property<T>(*this);
374  }
375 
376  virtual Property<T>* create() const
377  {
378  return new Property<T>( _name, _description, T() );
379  }
380 
381  virtual Property<T>* create( const base::DataSourceBase::shared_ptr& datasource ) const
382  {
386  if ( datasource && !prop->ready() ) {
387  log(Error) << "Cannot initialize Property: "
388  << "incompatible type ( destination type: " << getType() << ", source type: " << datasource->getTypeName() << ")." << endlog();
389  }
390  return prop;
391  }
392 
394  return _value;
395  }
396 
398  return _value;
399  }
400 
407  {
410  if (vptr) {
411  _value.swap(vptr);
412  return true;
413  }
414  return false;
415  }
416 
417  virtual std::string getType() const {
419  }
420 
421  virtual const types::TypeInfo* getTypeInfo() const {
423  }
424  protected:
426  };
427 
428 
432  template<>
434 
435  template<>
437 
438  template<>
440 
441  template<typename T>
442  std::ostream& operator<<(std::ostream &os, Property<T> &p)
443  {
444 #ifdef OS_HAVE_STREAMS
445  os << p.getDataSource();
446 #endif
447  return os;
448  }
449 
450  template<class T>
452  Property<T>* res = dynamic_cast<Property<T>*>( prop );
453  return res;
454  }
455 }
456 
458 
459 namespace RTT
460 {
461  template< class T>
463  {
464  pi->introspect( *this );
465  }
466 
467  template< class T>
469  {
470  return base::PropertyBase::identify(pi);
471  }
472 
473  template<>
475 }
476 
477 #endif
virtual base::DataSourceBase::shared_ptr getDataSource() const
Get an assignable base::DataSource through which this PropertyBase can be manipulated.
Definition: Property.hpp:393
boost::call_traits< value_t >::reference reference_t
Definition: Property.hpp:87
Property(const std::string &name)
The constructor which initializes the property&#39;s value to the default.
Definition: Property.hpp:104
virtual result_t get() const =0
Return the data as type T.
virtual bool refresh(const base::PropertyBase *other)
Refresh the value of this Property with the value of an other Property.
Definition: Property.hpp:314
bool ready() const
Inspect if this Property is correctly initialised and ready for usage.
virtual bool copy(const base::PropertyBase *other)
Copy an other Property onto this property.
Definition: Property.hpp:323
Property(base::PropertyBase *source)
Create a Property mirroring another PropertyBase.
Definition: Property.hpp:142
virtual Property< T > * clone() const
Deliver an identical clone of this PropertyBase.
Definition: Property.hpp:371
internal::AssignableDataSource< DataSourceType >::shared_ptr getAssignableDataSource() const
Definition: Property.hpp:397
Base class for all properties.
virtual void set(param_t t)=0
Set this DataSource with a value.
virtual void introspect(PropertyBase *p)
Fall-back function of the last resort when the type is not decomposable and not known to the introspe...
Definition: Property.cpp:58
virtual void identify(base::PropertyIntrospection *pi)
A call on this method will lead to a call to the PropertyIntrospection interface identifying this Pro...
Definition: Property.hpp:462
bool update(const Property< T > &orig)
Update the value, optionally also the description if current description is empty.
Definition: Property.hpp:349
const_reference_t rvalue() const
Read-only (const&) access to the value of the Property.
Definition: Property.hpp:285
internal::AssignableDataSource< DataSourceType >::shared_ptr _value
Definition: Property.hpp:425
#define RTT_API
Definition: rtt-config.h:97
Property(const Property< T > &orig)
Copy constructors copies the name, description and value as deep copies.
Definition: Property.hpp:127
Property(const std::string &name, const std::string &description, param_t value=value_t())
The constructor which initializes the property&#39;s value.
Definition: Property.hpp:117
bool copy(const Property< T > &orig)
Copy the value, complete overwrite of this Property with orig.
Definition: Property.hpp:335
Property(const std::string &name, const std::string &description, const typename internal::AssignableDataSource< DataSourceType >::shared_ptr &datasource)
The constructor which initializes the property with a DataSource.
Definition: Property.hpp:167
virtual Property< T > * create(const base::DataSourceBase::shared_ptr &datasource) const
Create a new instance of the PropertyBase with a custom data source.
Definition: Property.hpp:381
reference_t value()
Access to the value of the Property.
Definition: Property.hpp:277
static const types::TypeInfo * GetTypeInfo()
Return the Orocos type info.
Definition: DataSource.inl:49
Property< T > & doc(const std::string &descr)
Documents this property.
Definition: Property.hpp:228
boost::call_traits< value_t >::const_reference const_reference_t
Definition: Property.hpp:88
std::string _name
A short name for this PropertyBase.
A simple introspection interface to visit PropertyBags.
bool refresh(const Property< T > &orig)
Refresh only the value from a Property.
Definition: Property.hpp:363
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
A property represents a named value of any type with a description.
Definition: Property.hpp:76
Property()
Create an empty Property with no name, no description and no value.
Definition: Property.hpp:95
virtual bool setDataSource(const base::DataSourceBase::shared_ptr &dsb)
Assign an external assignable base::DataSource to this property.
Definition: Property.hpp:406
A class for representing a user type, and which can build instances of that type. ...
Definition: TypeInfo.hpp:67
const std::string & getDescription() const
Get a description of the property.
virtual const types::TypeInfo * getTypeInfo() const
Returns the types::TypeInfo object of this Property.
Definition: Property.hpp:421
Property< T > & operator=(param_t value)
Set the property&#39;s value.
Definition: Property.hpp:181
boost::remove_const< typename boost::remove_reference< T >::type >::type value_t
The types of this property type.
Definition: Property.hpp:85
void setName(const std::string &name)
Set the name of the property.
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
An interface which all classes which wish to visit a Property should implement.
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
This method narrows a base::DataSourceBase to a typeded AssignableDataSource, possibly returning a ne...
Definition: DataSource.inl:72
std::string _description
A lengthy description for this PropertyBase.
virtual std::string getType() const
Returns the type of this PropertyBase.
Definition: Property.hpp:417
virtual bool update(const base::PropertyBase *other)
Update the value of this Property with the value of an other Property.
Definition: Property.hpp:305
virtual void identify(PropertyIntrospection *pi)=0
A call on this method will lead to a call to the PropertyIntrospection interface identifying this Pro...
const std::string & getName() const
Get the name of the property.
static Property< T > * narrow(base::PropertyBase *prop)
Use this method instead of dynamic_cast<> to cast from base::PropertyBase to Property<T>.
Definition: Property.hpp:451
Property< T > & operator=(base::PropertyBase *source)
Mirror another PropertyBase (name, description and value).
Definition: Property.hpp:191
boost::intrusive_ptr< DataSourceBase > shared_ptr
Use this type to store a pointer to a DataSourceBase.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
virtual bool evaluate() const
Force an evaluation of the DataSourceBase.
Definition: DataSource.inl:52
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
virtual Property< T > * create() const
Create a new default instance of the PropertyBase.
Definition: Property.hpp:376
static std::string GetType()
Return usefull type info in a human readable format.
Definition: DataSource.inl:32
value_t DataSourceType
Definition: Property.hpp:89
virtual DataSourceBase::shared_ptr getDataSource() const =0
Get an assignable base::DataSource through which this PropertyBase can be manipulated.
boost::call_traits< value_t >::param_type param_t
Definition: Property.hpp:86
void setDescription(const std::string &desc)
Set the description of the property.