Attribute.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
00039 #ifndef ORO_CORELIB_ATTRIBUTE_HPP
00040 #define ORO_CORELIB_ATTRIBUTE_HPP
00041
00042 #include "DataSource.hpp"
00043 #include "DataSources.hpp"
00044 #include "AttributeBase.hpp"
00045 #include "BuildType.hpp"
00046
00047 namespace RTT
00048 {
00054 template<typename T>
00055 class Attribute
00056 : public AttributeBase
00057 {
00058 typename AssignableDataSource<T>::shared_ptr data;
00059 public:
00060
00064 Attribute()
00065 {
00066 }
00067
00073 Attribute(const std::string& name)
00074 : AttributeBase(name),
00075 data( detail::BuildType<T>::Value( T() ) )
00076 {}
00077
00084 explicit Attribute(const std::string& name, T t)
00085 : AttributeBase(name),
00086 data( detail::BuildType<T>::Value( t ) )
00087 {
00088 }
00089
00096 Attribute( const std::string& name, AssignableDataSource<T>* d)
00097 : AttributeBase(name),
00098 data( d )
00099 {
00100 }
00101
00105 Attribute( const Attribute<T>& a)
00106 : AttributeBase( a.mname ),
00107 data( a.data->clone() )
00108 {
00109 }
00110
00114 Attribute<T>& operator=( const Attribute<T>& a)
00115 {
00116 if ( this == &a )
00117 return *this;
00118 mname = a.mname;
00119 data = a.data->clone();
00120 return *this;
00121 }
00122
00131 Attribute( AttributeBase* ab)
00132 : AttributeBase( ab ? ab->getName() : "" ),
00133 data( ab ? AssignableDataSource<T>::narrow( ab->getDataSource().get() ) : 0 )
00134 {
00135 }
00136
00143 Attribute<T>& operator=(AttributeBase* ab)
00144 {
00145 if ( ab == this )
00146 return *this;
00147
00148 if (!ab) {
00149 data = 0;
00150 return *this;
00151 }
00152 mname = ab->getName();
00153 Attribute<T>* a = dynamic_cast<Attribute<T>*>(ab);
00154 if (a)
00155 data = a->getAssignableDataSource();
00156 return *this;
00157 }
00158
00162 T get() const
00163 {
00164 return data->get();
00165 }
00166
00170 void set( T t )
00171 {
00172 data->set(t);
00173 }
00174
00178 typename AssignableDataSource<T>::reference_t set() {
00179 return data->set();
00180 }
00181
00182 DataSourceBase::shared_ptr getDataSource() const
00183 {
00184 return data;
00185 }
00186
00187 typename AssignableDataSource<T>::shared_ptr getAssignableDataSource() const
00188 {
00189 return data;
00190 }
00191
00192 Attribute<T>* clone() const
00193 {
00194 return new Attribute<T>( mname, data.get() );
00195 }
00196
00197 Attribute<T>* copy( std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool instantiate )
00198 {
00199 if ( instantiate ) {
00200
00201
00202 AssignableDataSource<T>* instds = data->clone();
00203 replacements[ data.get() ] = instds;
00204 return new Attribute<T>( mname, instds );
00205 }
00206 else {
00207 return new Attribute<T>( mname, data->copy( replacements ) );
00208 }
00209 }
00210 };
00211
00217 template<typename T>
00218 class Constant
00219 : public AttributeBase
00220 {
00221 public:
00222 typename DataSource<T>::shared_ptr data;
00223
00227 Constant(const std::string& name, T t)
00228 : AttributeBase(name),
00229 data( new ConstantDataSource<T>( t ) )
00230 {
00231 }
00232
00236 Constant(const std::string& name, DataSource<T>* d )
00237 : AttributeBase(name),
00238 data( d )
00239 {
00240 }
00241
00249 Constant( AttributeBase* ab )
00250 : AttributeBase( ab ? ab->getName() : "")
00251 {
00252 if ( !ab )
00253 return;
00254 Constant<T>* c = dynamic_cast<Constant<T>*>(ab);
00255 if (c)
00256 data = c->getDataSource();
00257 }
00258
00266 Constant<T>& operator=(AttributeBase* ab)
00267 {
00268 if (!ab) {
00269 data = 0;
00270 return *this;
00271 }
00272 mname = ab->getName();
00273 Constant<T>* c = dynamic_cast<Constant<T>*>(ab);
00274 if (c)
00275 data = c->getDataSource();
00276 }
00277
00281 T get() const
00282 {
00283 return data->get();
00284 }
00285
00286 DataSourceBase::shared_ptr getDataSource() const
00287 {
00288 return data;
00289 }
00290
00291 Constant<T>* clone() const
00292 {
00293 return new Constant<T>( mname, data.get() );
00294 }
00295
00296 Constant<T>* copy( std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool instantiate )
00297 {
00298
00299 Constant<T>* ret = new Constant<T>( mname, data.get() );
00300 return ret;
00301 }
00302 };
00303
00312 template<typename T>
00313 class Alias
00314 : public AttributeBase
00315 {
00316 typename DataSource<T>::shared_ptr data;
00317 public:
00318 Alias(const std::string& name, typename DataSource<T>::shared_ptr d )
00319 : AttributeBase(name),
00320 data( d )
00321 {
00322 }
00323
00324 DataSourceBase::shared_ptr getDataSource() const
00325 {
00326 return data;
00327 }
00328
00329 Alias<T>* clone() const
00330 {
00331 return new Alias<T>( mname, data.get() );
00332 }
00333 Alias<T>* copy( std::map<const DataSourceBase*, DataSourceBase*>& replacements, bool )
00334 {
00335
00336 return new Alias<T>( mname, data->copy( replacements ) );
00337 }
00338 };
00339 }
00340 #endif