PropertySequence.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_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
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 }
00295 #endif