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<>
433  RTT_API bool Property<PropertyBag>::update( const Property<PropertyBag>& orig);
434 
435  template<>
436  RTT_API bool Property<PropertyBag>::refresh( const Property<PropertyBag>& orig);
437 
438  template<>
439  RTT_API bool Property<PropertyBag>::copy( const Property<PropertyBag>& orig);
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
Definition: Property.hpp:393
boost::call_traits< value_t >::reference reference_t
Definition: Property.hpp:87
Property(const std::string &name)
Definition: Property.hpp:104
virtual result_t get() const =0
virtual bool refresh(const base::PropertyBase *other)
Definition: Property.hpp:314
virtual bool copy(const base::PropertyBase *other)
Definition: Property.hpp:323
Property(base::PropertyBase *source)
Definition: Property.hpp:142
virtual Property< T > * clone() const
Definition: Property.hpp:371
internal::AssignableDataSource< DataSourceType >::shared_ptr getAssignableDataSource() const
Definition: Property.hpp:397
virtual void set(param_t t)=0
virtual void introspect(PropertyBase *p)
Definition: Property.cpp:58
virtual void identify(base::PropertyIntrospection *pi)
Definition: Property.hpp:462
bool update(const Property< T > &orig)
Definition: Property.hpp:349
const_reference_t rvalue() const
Definition: Property.hpp:285
internal::AssignableDataSource< DataSourceType >::shared_ptr _value
Definition: Property.hpp:425
Property(const Property< T > &orig)
Definition: Property.hpp:127
Property(const std::string &name, const std::string &description, param_t value=value_t())
Definition: Property.hpp:117
bool copy(const Property< T > &orig)
Definition: Property.hpp:335
Property(const std::string &name, const std::string &description, const typename internal::AssignableDataSource< DataSourceType >::shared_ptr &datasource)
Definition: Property.hpp:167
virtual Property< T > * create(const base::DataSourceBase::shared_ptr &datasource) const
Definition: Property.hpp:381
reference_t value()
Definition: Property.hpp:277
Property< T > & doc(const std::string &descr)
Definition: Property.hpp:228
boost::call_traits< value_t >::const_reference const_reference_t
Definition: Property.hpp:88
bool refresh(const Property< T > &orig)
Definition: Property.hpp:363
static std::string GetType()
virtual const_reference_t rvalue() const =0
A property represents a named value of any type with a description.
Definition: Property.hpp:76
virtual bool setDataSource(const base::DataSourceBase::shared_ptr &dsb)
Definition: Property.hpp:406
const std::string & getDescription() const
virtual const types::TypeInfo * getTypeInfo() const
Definition: Property.hpp:421
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
Property< T > & operator=(param_t value)
Definition: Property.hpp:181
boost::remove_const< typename boost::remove_reference< T >::type >::type value_t
Definition: Property.hpp:85
void setName(const std::string &name)
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
static const types::TypeInfo * GetTypeInfo()
virtual std::string getType() const
Definition: Property.hpp:417
virtual bool update(const base::PropertyBase *other)
Definition: Property.hpp:305
virtual void identify(PropertyIntrospection *pi)=0
const std::string & getName() const
static Property< T > * narrow(base::PropertyBase *prop)
Definition: Property.hpp:451
Property< T > & operator=(base::PropertyBase *source)
Definition: Property.hpp:191
boost::intrusive_ptr< DataSourceBase > shared_ptr
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
virtual Property< T > * create() const
Definition: Property.hpp:376
static Logger & log()
Definition: Logger.hpp:350
value_t DataSourceType
Definition: Property.hpp:89
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
virtual DataSourceBase::shared_ptr getDataSource() const =0
virtual bool evaluate() const
boost::call_traits< value_t >::param_type param_t
Definition: Property.hpp:86
void setDescription(const std::string &desc)


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:26