Attribute.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Tue Dec 21 22:43:08 CET 2004  Attribute.hpp
00003 
00004                         Attribute.hpp -  description
00005                            -------------------
00006     begin                : Tue December 21 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 
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                 // by taking a clone(), the DS has a chance to instantiate itself.
00201                 // A clone() of an UnboundDataSource returns the bound type.
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             // 'symbolic' copy, ConstantDataSource returns 'this' on copy...
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             // instantiate does not apply.
00336             return new Alias<T>( mname, data->copy( replacements ) );
00337         }
00338     };
00339 }
00340 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3