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                 if ( &orig == this )
00186                     return *this;
00187                 _properties.clear();
00188 
00189                 const_iterator i = orig.getProperties().begin();
00190                 while (i != orig.getProperties().end() )
00191                 {
00192                     add( (*i) );
00193                     ++i;
00194                 }
00195                 return *this;
00196             }
00197 
00204             PropertySequence<T>& operator<<=(const PropertySequence<T>& source)
00205             {
00206                 //iterate over orig, update or clone Ts
00207                 const_iterator it(source.getProperties().begin());
00208                 while ( it != source.getProperties().end() )
00209                 {
00210                     T* mine = find( (*it)->getName() );
00211                     if (mine != 0)
00212                         remove(mine);
00213                     add( (*it) );
00214                     ++it;
00215                 }
00216                 return *this;
00217             }
00218 
00219             const std::string& getType() const { return type;}
00220 
00221             const PropertyContainerType& getProperties() const { return _properties; }
00222 
00223         protected:
00224             PropertyContainerType _properties;
00225 
00229             struct FindProp : public std::binary_function<const T*,const std::string, bool>
00230             {
00231                 bool operator()(const T* b1, const std::string& b2) const { return b1->getName() == b2; }
00232             };
00233 
00234             const std::string type;
00235     };
00236 
00237 
00251     template< class T>
00252     void refreshProperties(PropertySequence<T>& target, const PropertySequence<T>& source);
00253 
00262     template< class T>
00263     void copyProperties(PropertySequence<T>& target, const PropertySequence<T>& source);
00264 
00269     template< class T>
00270     void deleteProperties(PropertySequence<T>& target);
00271 
00279     template< class T>
00280     inline
00281     void update(PropertySequence<T>& a, const PropertySequence<T>& b)
00282     {
00283         refreshProperties(a,b);
00284     }
00285 
00289     template< class T>
00290     inline
00291     void copy(PropertySequence<T>& a, const PropertySequence<T>& b)
00292     {
00293         copyProperties(a,b);
00294     }
00295 
00296 } // Namespace RTT
00297 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3