Orocos Real-Time Toolkit  2.9.0
PropertyBag.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:20 CET 2004 PropertyBag.hpp
3 
4  PropertyBag.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 PI_PROPERTY_BAG
39 #define PI_PROPERTY_BAG
40 
41 #include "base/PropertyBase.hpp"
42 
43 #include <vector>
44 #include <algorithm>
45 
46 #ifdef ORO_PRAGMA_INTERFACE
47 #pragma interface
48 #endif
49 
50 namespace RTT
51 {
52 
97  {
98  public:
102  typedef std::vector<base::PropertyBase*> Properties;
106  typedef Properties PropertyContainerType;
110  typedef Properties::iterator iterator;
114  typedef Properties::const_iterator const_iterator;
115 
119  typedef std::vector<std::string> Names;
123  PropertyBag();
124 
130  PropertyBag( const std::string& _type);
131 
137  PropertyBag( const PropertyBag& orig);
138 
142  ~PropertyBag();
143 
148  void add(base::PropertyBase *p);
149 
154  void remove(base::PropertyBase *p);
155 
165  template<class T>
166  Property<T>& addProperty( const std::string& name, T& attr) {
168  Property<T>* p = new Property<T>(name,"", datasource);
169  this->ownProperty( p );
170  return *p;
171  }
172 
173 
179  bool addProperty(base::PropertyBase& p);
180 
186 
190  bool ownProperty(base::PropertyBase* p);
191 
195  bool ownsProperty(base::PropertyBase* p) const;
196 
201  void clear();
202 
208  void list(Names &names) const;
209 
214  Names list() const;
215 
219  bool empty() const
220  {
221  return mproperties.empty();
222  }
223 
231  base::PropertyBase* getProperty(const std::string& name) const;
232 
241  template<class T>
242  Property<T>* getPropertyType(const std::string& name) const
243  {
244  const_iterator i( std::find_if(mproperties.begin(), mproperties.end(), std::bind2nd(FindPropType<T>(), name ) ) );
245  if ( i != mproperties.end() )
246  return dynamic_cast<Property<T>* >(*i);
247  return 0;
248  }
249 
255  {
256  if ( i < 0 || i >= int(mproperties.size()) )
257  return 0;
258  return mproperties[i];
259  }
260 
264  size_t size() const { return mproperties.size(); }
265 
270  void identify( base::PropertyIntrospection* pi ) const;
271 
276  void identify( base::PropertyBagVisitor* pi ) const;
277 
286  base::PropertyBase* find(const std::string& name) const;
287 
294  template<class T>
295  base::PropertyBase* findValue(const T& value) const {
296  for ( const_iterator i = mproperties.begin();
297  i != mproperties.end();
298  i++ )
299  {
300  Property<T> p = *i;
301  if (p.ready() && (p.value() == value))
302  return *i;
303  }
304  return 0;
305  }
306 
312  PropertyBag& operator=(const PropertyBag& orig);
313 
320  PropertyBag& operator<<=(const PropertyBag& source);
321 
327  PropertyBag& operator<<( base::PropertyBase* item) { this->add(item); return *this; }
328 
329  const std::string& getType() const { return type;}
330 
331  void setType(const std::string& newtype) { type = newtype; }
332 
336  Properties& getProperties() { return mproperties; }
337 
341  const Properties& getProperties() const { return mproperties; }
342 
346  Properties getProperties(const std::string& name) const;
347 
351  Names getPropertyNames() const { return list(); }
352 
353  iterator begin() { return mproperties.begin(); }
354  const_iterator begin() const { return mproperties.begin(); }
355  iterator end() { return mproperties.end(); }
356  const_iterator end() const { return mproperties.end(); }
357  protected:
358  Properties mproperties;
359  Properties mowned_props;
360 
364  template<class T>
365  struct FindPropType : public std::binary_function<const base::PropertyBase*,const std::string, bool>
366  {
367  bool operator()(const base::PropertyBase* b1, const std::string& b2) const { return b1->getName() == b2 && dynamic_cast<const Property<T>* >(b1) != 0; }
368  };
369 
370  std::string type;
371  };
372 
388  RTT_API std::ostream& operator<<(std::ostream& os, const PropertyBag& bag);
389 
396  RTT_API std::istream& operator>>(std::istream& is, PropertyBag& bag);
397 
409  RTT_API base::PropertyBase* findProperty(const PropertyBag& bag, const std::string& path, const std::string& separator = std::string(".") );
410 
421  RTT_API std::vector<std::string> listProperties( const PropertyBag& bag, const std::string& separator = std::string("."));
422 
432  RTT_API std::vector<std::string> listPropertyDescriptions( const PropertyBag& bag, const std::string& separator = std::string("."));
433 
442  RTT_API bool storeProperty(PropertyBag& bag, const std::string& path, base::PropertyBase* item, const std::string& separator = std::string(".") );
443 
452  RTT_API bool removeProperty(PropertyBag& bag, const std::string& path, const std::string& separator = std::string(".") );
453 
467  RTT_API bool refreshProperties(const PropertyBag& target, const PropertyBag& source, bool strict=false);
468 
476  RTT_API bool refreshProperty(const PropertyBag& target, const base::PropertyBase& source);
477 
488  RTT_API bool copyProperties(PropertyBag& target, const PropertyBag& source);
489 
501  RTT_API bool updateProperties(PropertyBag& target, const PropertyBag& source);
502 
517  RTT_API bool updateProperty(PropertyBag& target, const PropertyBag& source, const std::string& path, const std::string& separator = ".");
518 
532  RTT_API bool refreshProperty(PropertyBag& target, const PropertyBag& source, const std::string& path, const std::string& separator = ".");
533 
544  RTT_API void deleteProperties(PropertyBag& target);
545 
556  RTT_API void deletePropertyBag(PropertyBag& target);
557 
567  RTT_API void flattenPropertyBag(PropertyBag& target, const std::string& separator=".");
568 
572 } // Namespace RTT
573 #endif
bool updateProperty(PropertyBag &target, const PropertyBag &source, const std::string &name, const std::string &separator)
This function updates (recursively) the values of a single Property object of one Bag with the proper...
base::PropertyBase * findValue(const T &value) const
Finds the base::PropertyBase by value.
Property< T > * getPropertyType(const std::string &name) const
Get the first Property with name name of a given type T.
bool updateProperties(PropertyBag &target, const PropertyBag &source)
This function updates (recursively) the values of Property objects of one Bag with the values of Prop...
bool ready() const
Inspect if this Property is correctly initialised and ready for usage.
bool storeProperty(PropertyBag &bag, const std::string &path, base::PropertyBase *item, const std::string &separator)
Stores a property in a bag given a certain path with transfer of ownership.
vector< string > listProperties(const PropertyBag &source, const std::string &separator)
List all properties in a PropertyBag in a single list.
PropertyBag & operator<<(base::PropertyBase *item)
The &#39;add&#39; operator.
std::istream & operator>>(std::istream &is, BufferPolicy &bp)
const std::string & getType() const
bool operator()(const base::PropertyBase *b1, const std::string &b2) const
Base class for all properties.
bool copyProperties(PropertyBag &target, const PropertyBag &source)
This function copies (recursively) the Properties of one Bag into another Bag.
void setType(const std::string &newtype)
#define RTT_API
Definition: rtt-config.h:97
A DataSource which is used to manipulate a reference to an external value.
A container for holding references to properties.
Definition: PropertyBag.hpp:96
Names getPropertyNames() const
Returns a list of all the names of the properties in this bag.
reference_t value()
Access to the value of the Property.
Definition: Property.hpp:277
PropertyBase * findProperty(const PropertyBag &bag, const std::string &nameSequence, const std::string &separator)
This function locates a Property in nested PropertyBags.
void deletePropertyBag(PropertyBag &target)
This function iterates over a PropertyBag and recursively deletes all Property objects.
Property< T > & addProperty(const std::string &name, T &attr)
Adds a variable of any type as a property to this bag.
Properties PropertyContainerType
Deprecated, use Properties.
A simple introspection interface to visit PropertyBags.
vector< string > listPropertyDescriptions(const PropertyBag &source, const std::string &separator)
List all descriptions of properties in a PropertyBag in a single list.
const_iterator begin() const
A property represents a named value of any type with a description.
Definition: Property.hpp:76
bool refreshProperty(const PropertyBag &target, const PropertyBase &source)
Refresh one Property in the target bag with the new value.
Properties mowned_props
std::string type
Properties & getProperties()
Returns a list of all the property objects in this bag.
Properties::const_iterator const_iterator
A const iterator over the Properties.
bool empty() const
Return true if no properties are present in this bag.
void flattenPropertyBag(PropertyBag &target, const std::string &separator)
This function flattens a PropertyBag recursively.
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
const Properties & getProperties() const
Returns a list of all the property objects in this bag.
std::ostream & operator<<(std::ostream &os, const BufferPolicy &bp)
bool removeProperty(PropertyBag &bag, const std::string &path, const std::string &separator)
Removes a property from a bag given a certain path.
An interface which all classes which wish to visit a Property should implement.
base::PropertyBase * getItem(int i) const
Returns the i&#39;th Property, starting from 0.
void deleteProperties(PropertyBag &target)
This function iterates over a PropertyBag and deletes all Property objects in it without recursion...
std::vector< std::string > Names
The container to hold names of Properties.
const std::string & getName() const
Get the name of the property.
Properties::iterator iterator
An iterator over the Properties.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
size_t size() const
Get the number of Properties in this bag.
bool refreshProperties(const PropertyBag &target, const PropertyBag &source, bool allprops)
This function refreshes the values of the properties in one PropertyBag with the values of the proper...
Properties mproperties
const_iterator end() const
A function object for finding a Property by name and type.
std::vector< base::PropertyBase * > Properties
The container in which the properties are stored.
iterator begin()