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 "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      * @defgroup BagOperations Recursive Property Bag Operations.
00346      *        These functions operate recursively on the contents of bags,
00347      *        possibly modifying, deleting or creating new Property objects
00348      *        in the bag or in one of its sub-bags.
00349      * @ingroup CoreLibProperties
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 } // Namespace RTT
00480 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3