Orocos Real-Time Toolkit  2.5.0
PropertyBag.hpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 19 14:11:20 CET 2004  PropertyBag.hpp
00003 
00004                         PropertyBag.hpp -  description
00005                            -------------------
00006     begin                : Mon January 19 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
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 } // Namespace RTT
00573 #endif