PropertySequence.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 19 14:11:19 CET 2004  PropertySequence.hpp
00003 
00004                         PropertySequence.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_SEQUENCE_HPP
00039 #define PI_PROPERTY_SEQUENCE_HPP
00040 
00041 #include "PropertyBase.hpp"
00042 #include <vector>
00043 
00044 #ifdef ORO_PRAGMA_INTERFACE
00045 #pragma interface
00046 #endif
00047 
00048 namespace RTT
00049 {
00083     template< class T >
00084     class PropertySequence
00085     {
00086         public:
00087             typedef std::vector<T*> PropertyContainerType;
00088             typedef typename PropertyContainerType::iterator iterator;
00089             typedef typename PropertyContainerType::const_iterator const_iterator;
00090 
00094             PropertySequence( )
00095                 : _properties(), type("type_less")
00096             {}
00097 
00103             PropertySequence( const std::string& _type)
00104                 : _properties(), type(_type)
00105             {}
00106 
00112             PropertySequence( const PropertySequence<T>& orig)
00113                 : _properties( orig.getProperties() ), type( orig.getType() )
00114             {
00115             }
00116 
00121             void add(T *p)
00122             {
00123                 _properties.push_back(p);
00124             }
00125 
00130             void remove(T *p)
00131             {
00132                 iterator i = std::find(_properties.begin(), _properties.end(), p);
00133                 if ( i != _properties.end() )
00134                     _properties.erase(i);
00135             }
00136 
00141             void clear()
00142             {
00143                 _properties.clear();
00144             }
00145 
00146 
00152             void list(std::vector<std::string> &names) const
00153             {
00154                 for (
00155                     const_iterator i = _properties.begin();
00156                     i != _properties.end();
00157                     i++ )
00158                 {
00159                     names.push_back( (*i)->getName() );
00160                 }
00161             }
00162 
00170             T* find(const std::string& name) const
00171             {
00172                 const_iterator i( std::find_if(_properties.begin(), _properties.end(), std::bind2nd(FindProp(), name ) ) );
00173                 if ( i != _properties.end() )
00174                     return ( *i );
00175                 return 0;
00176             }
00177 
00183             PropertySequence<T>& operator=(const PropertySequence<T>& orig)
00184             {
00185                 _properties.clear();
00186 
00187                 const_iterator i = orig.getProperties().begin();
00188                 while (i != orig.getProperties().end() )
00189                 {
00190                     add( (*i) );
00191                     ++i;
00192                 }
00193                 return *this;
00194             }
00195 
00202             PropertySequence<T>& operator<<=(const PropertySequence<T>& source)
00203             {
00204                 //iterate over orig, update or clone Ts
00205                 const_iterator it(source.getProperties().begin());
00206                 while ( it != source.getProperties().end() )
00207                 {
00208                     T* mine = find( (*it)->getName() );
00209                     if (mine != 0)
00210                         remove(mine);
00211                     add( (*it) );
00212                     ++it;
00213                 }
00214                 return *this;
00215             }
00216 
00217             const std::string& getType() const { return type;}
00218 
00219             const PropertyContainerType& getProperties() const { return _properties; }
00220 
00221         protected:
00222             PropertyContainerType _properties;
00223 
00227             struct FindProp : public std::binary_function<const T*,const std::string, bool>
00228             {
00229                 bool operator()(const T* b1, const std::string& b2) const { return b1->getName() == b2; }
00230             };
00231 
00232             const std::string type;
00233     };
00234 
00235 
00249     template< class T>
00250     void refreshProperties(PropertySequence<T>& target, const PropertySequence<T>& source);
00251 
00260     template< class T>
00261     void copyProperties(PropertySequence<T>& target, const PropertySequence<T>& source);
00262 
00267     template< class T>
00268     void deleteProperties(PropertySequence<T>& target);
00269 
00277     template< class T>
00278     inline
00279     void update(PropertySequence<T>& a, const PropertySequence<T>& b)
00280     {
00281         refreshProperties(a,b);
00282     }
00283 
00287     template< class T>
00288     inline
00289     void copy(PropertySequence<T>& a, const PropertySequence<T>& b)
00290     {
00291         copyProperties(a,b);
00292     }
00293 
00294 } // Namespace RTT
00295 #endif

Generated on Tue Aug 25 14:17:23 2009 for Orocos Real-Time Toolkit by  doxygen 1.5.8