Attribute.hpp
Go to the documentation of this file.
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 "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                 // by taking a clone(), the DS has a chance to instantiate itself.
00233                 // A clone() of an internal::UnboundDataSource returns the bound type.
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             // 'symbolic' copy, internal::ConstantDataSource returns 'this' on copy...
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


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:45