00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PI_PROPERTY_BAG
00039 #define PI_PROPERTY_BAG
00040
00041 #include "base/PropertyBase.hpp"
00042
00043 #include <vector>
00044 #include <algorithm>
00045
00046 #ifdef ORO_PRAGMA_INTERFACE
00047 #pragma interface
00048 #endif
00049
00050 namespace RTT
00051 {
00052
00096 class RTT_API PropertyBag
00097 {
00098 public:
00102 typedef std::vector<base::PropertyBase*> Properties;
00106 typedef Properties PropertyContainerType;
00110 typedef Properties::iterator iterator;
00114 typedef Properties::const_iterator const_iterator;
00115
00119 typedef std::vector<std::string> Names;
00123 PropertyBag();
00124
00130 PropertyBag( const std::string& _type);
00131
00137 PropertyBag( const PropertyBag& orig);
00138
00142 ~PropertyBag();
00143
00148 void add(base::PropertyBase *p);
00149
00154 void remove(base::PropertyBase *p);
00155
00165 template<class T>
00166 Property<T>& addProperty( const std::string& name, T& attr) {
00167 typename internal::AssignableDataSource<T>::shared_ptr datasource( new internal::ReferenceDataSource<T>(attr) );
00168 Property<T>* p = new Property<T>(name,"", datasource);
00169 this->ownProperty( p );
00170 return *p;
00171 }
00172
00173
00179 bool addProperty(base::PropertyBase& p);
00180
00185 bool removeProperty(base::PropertyBase *p);
00186
00190 bool ownProperty(base::PropertyBase* p);
00191
00195 bool ownsProperty(base::PropertyBase* p) const;
00196
00201 void clear();
00202
00208 void list(Names &names) const;
00209
00214 Names list() const;
00215
00219 bool empty() const
00220 {
00221 return mproperties.empty();
00222 }
00223
00231 base::PropertyBase* getProperty(const std::string& name) const;
00232
00241 template<class T>
00242 Property<T>* getPropertyType(const std::string& name) const
00243 {
00244 const_iterator i( std::find_if(mproperties.begin(), mproperties.end(), std::bind2nd(FindPropType<T>(), name ) ) );
00245 if ( i != mproperties.end() )
00246 return dynamic_cast<Property<T>* >(*i);
00247 return 0;
00248 }
00249
00254 base::PropertyBase* getItem( int i) const
00255 {
00256 if ( i < 0 || i >= int(mproperties.size()) )
00257 return 0;
00258 return mproperties[i];
00259 }
00260
00264 size_t size() const { return mproperties.size(); }
00265
00270 void identify( base::PropertyIntrospection* pi ) const;
00271
00276 void identify( base::PropertyBagVisitor* pi ) const;
00277
00286 base::PropertyBase* find(const std::string& name) const;
00287
00294 template<class T>
00295 base::PropertyBase* findValue(const T& value) const {
00296 for ( const_iterator i = mproperties.begin();
00297 i != mproperties.end();
00298 i++ )
00299 {
00300 Property<T> p = *i;
00301 if (p.ready() && (p.value() == value))
00302 return *i;
00303 }
00304 return 0;
00305 }
00306
00312 PropertyBag& operator=(const PropertyBag& orig);
00313
00320 PropertyBag& operator<<=(const PropertyBag& source);
00321
00327 PropertyBag& operator<<( base::PropertyBase* item) { this->add(item); return *this; }
00328
00329 const std::string& getType() const { return type;}
00330
00331 void setType(const std::string& newtype) { type = newtype; }
00332
00336 Properties& getProperties() { return mproperties; }
00337
00341 const Properties& getProperties() const { return mproperties; }
00342
00346 Properties getProperties(const std::string& name) const;
00347
00351 Names getPropertyNames() const { return list(); }
00352
00353 iterator begin() { return mproperties.begin(); }
00354 const_iterator begin() const { return mproperties.begin(); }
00355 iterator end() { return mproperties.end(); }
00356 const_iterator end() const { return mproperties.end(); }
00357 protected:
00358 Properties mproperties;
00359 Properties mowned_props;
00360
00364 template<class T>
00365 struct FindPropType : public std::binary_function<const base::PropertyBase*,const std::string, bool>
00366 {
00367 bool operator()(const base::PropertyBase* b1, const std::string& b2) const { return b1->getName() == b2 && dynamic_cast<const Property<T>* >(b1) != 0; }
00368 };
00369
00370 std::string type;
00371 };
00372
00388 RTT_API std::ostream& operator<<(std::ostream& os, const PropertyBag& bag);
00389
00396 RTT_API std::istream& operator>>(std::istream& is, PropertyBag& bag);
00397
00409 RTT_API base::PropertyBase* findProperty(const PropertyBag& bag, const std::string& path, const std::string& separator = std::string(".") );
00410
00421 RTT_API std::vector<std::string> listProperties( const PropertyBag& bag, const std::string& separator = std::string("."));
00422
00432 RTT_API std::vector<std::string> listPropertyDescriptions( const PropertyBag& bag, const std::string& separator = std::string("."));
00433
00442 RTT_API bool storeProperty(PropertyBag& bag, const std::string& path, base::PropertyBase* item, const std::string& separator = std::string(".") );
00443
00452 RTT_API bool removeProperty(PropertyBag& bag, const std::string& path, const std::string& separator = std::string(".") );
00453
00467 RTT_API bool refreshProperties(const PropertyBag& target, const PropertyBag& source, bool strict=false);
00468
00476 RTT_API bool refreshProperty(const PropertyBag& target, const base::PropertyBase& source);
00477
00488 RTT_API bool copyProperties(PropertyBag& target, const PropertyBag& source);
00489
00501 RTT_API bool updateProperties(PropertyBag& target, const PropertyBag& source);
00502
00517 RTT_API bool updateProperty(PropertyBag& target, const PropertyBag& source, const std::string& path, const std::string& separator = ".");
00518
00532 RTT_API bool refreshProperty(PropertyBag& target, const PropertyBag& source, const std::string& path, const std::string& separator = ".");
00533
00544 RTT_API void deleteProperties(PropertyBag& target);
00545
00556 RTT_API void deletePropertyBag(PropertyBag& target);
00557
00567 RTT_API void flattenPropertyBag(PropertyBag& target, const std::string& separator=".");
00568
00572 }
00573 #endif