PropertyBag.hpp
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 "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 template< class T>
00053 class Property;
00054
00098 class RTT_API PropertyBag
00099 {
00100 public:
00104 typedef std::vector<PropertyBase*> Properties;
00108 typedef Properties PropertyContainerType;
00112 typedef Properties::iterator iterator;
00116 typedef Properties::const_iterator const_iterator;
00117
00121 typedef std::vector<std::string> Names;
00125 PropertyBag();
00126
00132 PropertyBag( const std::string& _type);
00133
00139 PropertyBag( const PropertyBag& orig);
00140
00144 ~PropertyBag();
00145
00150 void add(PropertyBase *p);
00151
00156 void remove(PropertyBase *p);
00157
00163 bool addProperty(PropertyBase *p);
00164
00169 bool removeProperty(PropertyBase *p);
00170
00174 bool ownProperty(PropertyBase* p);
00175
00179 bool ownsProperty(PropertyBase* p);
00180
00185 void clear();
00186
00192 void list(Names &names) const;
00193
00198 Names list() const;
00199
00203 bool empty() const
00204 {
00205 return mproperties.empty();
00206 }
00207
00216 template<class T>
00217 Property<T>* getProperty(const std::string& name) const
00218 {
00219 const_iterator i( std::find_if(mproperties.begin(), mproperties.end(), std::bind2nd(FindProp(), name ) ) );
00220 if ( i != mproperties.end() )
00221 return dynamic_cast< Property<T>* >( *i );
00222 return 0;
00223 }
00224
00229 PropertyBase* getItem( int i) const
00230 {
00231 if ( i < 0 || i >= int(mproperties.size()) )
00232 return 0;
00233 return mproperties[i];
00234 }
00235
00239 size_t size() const { return mproperties.size(); }
00240
00244 void identify( PropertyIntrospection* pi ) const;
00245
00249 void identify( PropertyBagVisitor* pi ) const;
00250
00258 PropertyBase* find(const std::string& name) const;
00259
00266 template<class T>
00267 PropertyBase* findValue(const T& value) const {
00268 for ( const_iterator i = mproperties.begin();
00269 i != mproperties.end();
00270 i++ )
00271 {
00272 Property<T> p = *i;
00273 if (p.ready() && (p.value() == value))
00274 return *i;
00275 }
00276 return 0;
00277 }
00278
00284 PropertyBag& operator=(const PropertyBag& orig);
00285
00292 PropertyBag& operator<<=(const PropertyBag& source);
00293
00299 PropertyBag& operator<<( PropertyBase* item) { this->add(item); return *this; }
00300
00301 const std::string& getType() const { return type;}
00302
00303 void setType(const std::string& newtype) { type = newtype; }
00304
00308 Properties& getProperties() { return mproperties; }
00309
00313 const Properties& getProperties() const { return mproperties; }
00314
00318 Properties getProperties(const std::string& name) const;
00319
00323 Names getPropertyNames() const { return list(); }
00324
00325 iterator begin() { return mproperties.begin(); }
00326 const_iterator begin() const { return mproperties.begin(); }
00327 iterator end() { return mproperties.end(); }
00328 const_iterator end() const { return mproperties.end(); }
00329 protected:
00330 Properties mproperties;
00331 Properties mowned_props;
00332
00336 struct FindProp : public std::binary_function<const PropertyBase*,const std::string, bool>
00337 {
00338 bool operator()(const PropertyBase* b1, const std::string& b2) const { return b1->getName() == b2; }
00339 };
00340
00341 std::string type;
00342 };
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00364 RTT_API PropertyBase* findProperty(const PropertyBag& bag, const std::string& path, const std::string& separator = std::string(".") );
00365
00379 RTT_API bool refreshProperties(const PropertyBag& target, const PropertyBag& source, bool strict=false);
00380
00388 RTT_API bool refreshProperty(const PropertyBag& target, const PropertyBase& source);
00389
00400 RTT_API bool copyProperties(PropertyBag& target, const PropertyBag& source);
00401
00413 RTT_API bool updateProperties(PropertyBag& target, const PropertyBag& source);
00414
00429 RTT_API bool updateProperty(PropertyBag& target, const PropertyBag& source, const std::string& path, const std::string& separator = ".");
00430
00444 RTT_API bool refreshProperty(PropertyBag& target, const PropertyBag& source, const std::string& path, const std::string& separator = ".");
00445
00454 RTT_API void deleteProperties(PropertyBag& target);
00455
00463 RTT_API void deletePropertyBag(PropertyBag& target);
00464
00474 RTT_API void flattenPropertyBag(PropertyBag& target, const std::string& separator=".");
00475
00479 }
00480 #endif