Orocos Real-Time Toolkit  2.9.0
ConfigurationInterface.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Dec 21 22:43:08 CET 2004 ConfigurationInterface.hpp
3 
4  ConfigurationInterface.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 RTT_CONFIGURATIONINTERFACE_HPP
40 #define RTT_CONFIGURATIONINTERFACE_HPP
41 
42 #include <memory>
43 #include <map>
44 #include "Attribute.hpp"
45 #include "internal/DataSources.hpp"
47 #include "Property.hpp"
48 #include "PropertyBag.hpp"
49 
50 namespace RTT
51 {
61  {
62  public:
63 
69 
74  typedef std::vector<std::string> AttributeNames;
75 
80  typedef std::vector<base::AttributeBase*> AttributeObjects;
81 
85  void clear();
86 
90  bool hasAttribute( const std::string& name ) const;
91 
100  template<class T>
101  bool addAttribute( const std::string& name, T& attr) {
102  if ( !chkPtr("addAttribute", name, &attr) ) return false;
103  Alias a(name, new internal::ReferenceDataSource<T>(attr));
104  return this->addAttribute( a );
105  }
106 
114  template<class T>
115  Attribute<T>& addAttribute( const std::string& name, Attribute<T>& attr) {
116  if ( !chkPtr("addAttribute", name, &attr) ) return attr;
117  if ( !attr.ready() )
118  attr = Attribute<T>(name);
119  else
120  attr.setName(name);
121  this->addAttribute( attr );
122  assert(attr.ready());
123  return attr;
124  }
125 
134  template<class T>
135  bool addConstant( const std::string& name, const T& cnst) {
136  if ( !chkPtr("addConstant", name, &cnst) ) return false;
137  Alias a(name, new internal::ConstReferenceDataSource<T>(cnst));
138  return this->addAttribute( a );
139  }
140 
149  template<class T>
150  Constant<T>& addConstant( const std::string& name, Constant<T>& cnst) {
151  if ( !chkPtr("addConstant", name, &cnst) ) return cnst;
152  if ( !cnst.ready() )
153  cnst = Constant<T>(name, T());
154  else
155  cnst.setName(name);
156  this->addConstant( cnst );
157  assert(cnst.ready());
158  return cnst;
159  }
160 
170  template<class T>
171  Property<T>& addProperty( const std::string& name, T& prop) {
172  if ( !chkPtr("addProperty", name, &prop) ) return internal::NA<Property<T>& >::na();
173  return this->properties()->addProperty( name, prop );
174  }
175 
183  template<class T>
184  Property<T>& addProperty( const std::string& name, Property<T>& prop) {
185  if ( !chkPtr("addProperty", name, &prop) ) return prop;
186  if ( !prop.ready() )
187  prop = Property<T>(name);
188  else
189  prop.setName(name);
190  this->properties()->addProperty( prop );
191  assert(prop.ready());
192  return prop;
193  }
194 
205  {
206  if ( !chkPtr("addAttribute", "AttributeBase", &a) ) return false;
207  return a.getDataSource() ? setValue( a.clone() ) : false;
208  }
209 
218  base::AttributeBase* getAttribute( const std::string& name ) const
219  {
220  return this->getValue( name );
221  }
222 
226  void removeAttribute( const std::string& name );
227 
233  {
234  return c.getDataSource() ? setValue( c.clone() ) : false;
235  }
236 
245  base::AttributeBase* getConstant( const std::string& name ) const
246  {
247  return this->getValue( name );
248  }
249 
253  bool hasProperty( const std::string& name ) const;
254 
260  bool addProperty( base::PropertyBase& pb );
261 
267 
275  base::PropertyBase* getProperty(const std::string& name) const
276  {
277  return bag.find(name);
278  }
279 
285  bool setValue( base::AttributeBase* ab );
286 
296  base::AttributeBase* getValue( const std::string& name ) const;
297 
301  bool removeValue(const std::string& name );
302 
310  ConfigurationInterface* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& repl, bool instantiate ) const;
311 
315  void loadValues( AttributeObjects const& new_values);
316 
320  AttributeObjects const& getValues() const {
321  return values;
322  }
323 
327  AttributeNames getAttributeNames() const;
328 
332  PropertyBag* properties();
333 
334  protected:
335  bool chkPtr(const std::string &where, const std::string& name, const void* ptr);
336  typedef std::vector<base::AttributeBase*> map_t;
337  map_t values;
339  };
340 }
341 
342 #endif
bool ready() const
Inspect if this Property is correctly initialised and ready for usage.
bool addAttribute(base::AttributeBase &a)
Add an base::AttributeBase which remains owned by the user.
virtual AttributeBase * clone() const =0
Returns a clone of this AttributeBase.
Constant< T > & addConstant(const std::string &name, Constant< T > &cnst)
Adds an existing constant object to the configuration interface.
Base class for all properties.
#define RTT_API
Definition: rtt-config.h:97
A DataSource which is used to manipulate a reference to an external value.
bool addConstant(base::AttributeBase &c)
Add a Constant with a given value.
A DataSource which is used to read a const reference to an external value.
A container for holding references to properties.
Definition: PropertyBag.hpp:96
virtual DataSourceBase::shared_ptr getDataSource() const =0
Return a internal::DataSource which contains the same contents.
std::vector< base::AttributeBase * > map_t
std::vector< std::string > AttributeNames
A vector containing the names of all attribute objects in this repository.
An attribute is a minimalistic, named placeholder for data.
AttributeObjects const & getValues() const
Returns all attributes in this repository.
base::PropertyBase * getProperty(const std::string &name) const
Get a Property with name name.
A property represents a named value of any type with a description.
Definition: Property.hpp:76
base::AttributeBase * getAttribute(const std::string &name) const
This class is used to return a &#39;default&#39; value when no value is available (&#39;Not Available&#39;).
Definition: NA.hpp:53
A class for keeping track of Attribute, Constant and Property objects of a TaskContext.
void setName(const std::string &name)
Set the name of the property.
Property< T > & addProperty(const std::string &name, T &prop)
Adds a variable of any type as a property to the configuration interface.
bool ready() const
Returns true if the Attribute was correctly initialised.
bool removeProperty(PropertyBag &bag, const std::string &path, const std::string &separator)
Removes a property from a bag given a certain path.
Property< T > & addProperty(const std::string &name, Property< T > &prop)
Adds an existing property object to the configuration interface.
bool addConstant(const std::string &name, const T &cnst)
Adds a variable of any type as read-only attribute to the configuration interface.
std::vector< base::AttributeBase * > AttributeObjects
A vector containing pointers to all attribute objects stored in this repository.
base::AttributeBase * getConstant(const std::string &name) const
std::vector< typename MapT::mapped_type > values(const MapT &map)
Definition: mystd.hpp:140
An Attribute has a name and contains data which can be set and get.
Definition: Attribute.hpp:56
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp: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
void setName(std::string const &new_name)
Get the name of this instance.
Definition: Attribute.cpp:64
Attribute< T > & addAttribute(const std::string &name, Attribute< T > &attr)
Adds an existing attribute object to the configuration interface.
bool addAttribute(const std::string &name, T &attr)
Adds a variable of any type as read/write attribute to the configuration interface.