Go to the documentation of this file.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
00039 #ifndef ORO_CORELIB_ATTRIBUTE_HPP
00040 #define ORO_CORELIB_ATTRIBUTE_HPP
00041
00042 #include "internal/DataSource.hpp"
00043 #include "internal/DataSources.hpp"
00044 #include "base/AttributeBase.hpp"
00045
00046 namespace RTT
00047 {
00055 template<typename T>
00056 class Attribute
00057 : public base::AttributeBase
00058 {
00059 typename internal::AssignableDataSource<T>::shared_ptr data;
00060 public:
00061
00065 Attribute()
00066 {
00067 }
00068
00074 explicit Attribute(const std::string& name)
00075 : base::AttributeBase(name),
00076 data( new internal::ValueDataSource<T>( T() ) )
00077 {}
00078
00085 Attribute(const std::string& name, T t)
00086 : base::AttributeBase(name),
00087 data( new internal::ValueDataSource<T>( t ) )
00088 {
00089 }
00090
00100 template<class Owner>
00101 Attribute(const std::string& name, T t, Owner o)
00102 : base::AttributeBase(name),
00103 data( new internal::ValueDataSource<T>( t ) )
00104 {
00105 o->addAttribute(this);
00106 }
00107
00114 Attribute( const std::string& name, internal::AssignableDataSource<T>* d)
00115 : base::AttributeBase(name),
00116 data( d )
00117 {
00118 }
00119
00123 Attribute( const Attribute<T>& a)
00124 : base::AttributeBase( a.mname ),
00125 data( a.data->clone() )
00126 {
00127 }
00128
00132 Attribute<T>& operator=( const Attribute<T>& a)
00133 {
00134 if ( this == &a )
00135 return *this;
00136 mname = a.mname;
00137 data = a.data->clone();
00138 return *this;
00139 }
00140
00150 Attribute( base::AttributeBase* ab)
00151 : base::AttributeBase( ab ? ab->getName() : "" ),
00152 data( ab ? internal::AssignableDataSource<T>::narrow( ab->getDataSource().get() ) : 0 )
00153 {
00154 }
00155
00164 Attribute<T>& operator=(base::AttributeBase* ab)
00165 {
00166 if ( ab == this )
00167 return *this;
00168
00169 if (!ab) {
00170 data = 0;
00171 mname.clear();
00172 return *this;
00173 }
00174 typename internal::AssignableDataSource<T>::shared_ptr a
00175 = boost::dynamic_pointer_cast<internal::AssignableDataSource<T> >( ab->getDataSource() );
00176 if (a) {
00177 data = a;
00178 mname = ab->getName();
00179 } else {
00180 data = 0;
00181 }
00182 return *this;
00183 }
00184
00188 T const& get() const
00189 {
00190 data->evaluate();
00191 return data->rvalue();
00192 }
00193
00199 void set( T const& t )
00200 {
00201 data->set(t);
00202 }
00203
00210 typename internal::AssignableDataSource<T>::reference_t set() {
00211 return data->set();
00212 }
00213
00214 base::DataSourceBase::shared_ptr getDataSource() const
00215 {
00216 return data;
00217 }
00218
00219 typename internal::AssignableDataSource<T>::shared_ptr getAssignableDataSource() const
00220 {
00221 return data;
00222 }
00223
00224 Attribute<T>* clone() const
00225 {
00226 return new Attribute<T>( mname, data.get() );
00227 }
00228
00229 Attribute<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replacements, bool instantiate )
00230 {
00231 if ( instantiate ) {
00232
00233
00234 internal::AssignableDataSource<T>* instds = data->clone();
00235 replacements[ data.get() ] = instds;
00236 return new Attribute<T>( mname, instds );
00237 }
00238 else {
00239 return new Attribute<T>( mname, data->copy( replacements ) );
00240 }
00241 }
00242 };
00243
00248 template<typename T>
00249 class Constant
00250 : public base::AttributeBase
00251 {
00252 public:
00253 typename internal::DataSource<T>::shared_ptr data;
00254
00258 Constant()
00259 {
00260 }
00261
00267 Constant(const std::string& name, T t)
00268 : base::AttributeBase(name),
00269 data( new internal::ConstantDataSource<T>( t ) )
00270 {
00271 }
00272
00282 template<class Owner>
00283 Constant(const std::string& name, T t, Owner owner)
00284 : base::AttributeBase(name),
00285 data( new internal::ConstantDataSource<T>( t ) )
00286 {
00287 owner->addAttribute( this );
00288 }
00289
00293 Constant(const std::string& name, internal::DataSource<T>* d )
00294 : base::AttributeBase(name),
00295 data( d )
00296 {
00297 }
00298
00307 Constant( base::AttributeBase* ab )
00308 : base::AttributeBase( ab ? ab->getName() : ""),
00309 data( ab ? internal::DataSource<T>::narrow( ab->getDataSource().get() ) : 0 )
00310 {
00311 }
00312
00321 Constant<T>& operator=(base::AttributeBase* ab)
00322 {
00323 if ( ab == this)
00324 return *this;
00325 if (!ab) {
00326 data = 0;
00327 mname.clear();
00328 return *this;
00329 }
00330 typename internal::DataSource<T>::shared_ptr a
00331 = boost::dynamic_pointer_cast<internal::DataSource<T> >( ab->getDataSource() );
00332 if (a) {
00333 data = a;
00334 mname = ab->getName();
00335 } else {
00336 data = 0;
00337 }
00338 return *this;
00339 }
00340
00344 T get() const
00345 {
00346 return data->get();
00347 }
00348
00349 base::DataSourceBase::shared_ptr getDataSource() const
00350 {
00351 return data;
00352 }
00353
00354 Constant<T>* clone() const
00355 {
00356 return new Constant<T>( mname, data.get() );
00357 }
00358
00359 Constant<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replacements, bool instantiate )
00360 {
00361
00362 Constant<T>* ret = new Constant<T>( mname, data.get() );
00363 return ret;
00364 }
00365 };
00366
00375 class RTT_API Alias
00376 : public base::AttributeBase
00377 {
00378 base::DataSourceBase::shared_ptr data;
00379 public:
00380 Alias(const std::string& name, base::DataSourceBase::shared_ptr d );
00381
00391 template<class Owner>
00392 Alias(const std::string& name, base::DataSourceBase::shared_ptr d, Owner owner)
00393 : base::AttributeBase(name),
00394 data( d )
00395 {
00396 owner->addAttribute( this );
00397 }
00398
00399 base::DataSourceBase::shared_ptr getDataSource() const;
00400
00401 Alias* clone() const;
00402
00403 Alias* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replacements, bool );
00404 };
00405 }
00406 #endif