DataSource.inl

00001 #ifndef CORELIB_DATASOURCE_INL
00002 #define CORELIB_DATASOURCE_INL
00003 
00004 #include "AssignCommand.hpp"
00005 #include "DataSourceTypeInfo.hpp"
00006 #include "DataSourceAdaptor.hpp"
00007 #include "TypeTransporter.hpp"
00008 
00009 #include "rtt-config.h"
00010 
00011 namespace RTT
00012 {
00013 
00014   template<typename T>
00015   DataSource<T>::~DataSource()
00016   {
00017   }
00018 
00019   template< typename T>
00020   std::string DataSource<T>::getType() const
00021   {
00022       return DataSource<T>::GetType();
00023   }
00024 
00025   template< typename T>
00026   std::string DataSource<T>::getTypeName() const
00027   {
00028       return DataSource<T>::GetTypeName();
00029   }
00030 
00031   template< typename T>
00032   std::string DataSource<T>::GetType()
00033   {
00034       return detail::DataSourceTypeInfo< T >::getType() + detail::DataSourceTypeInfo< T >::getQualifier();
00035 
00036   }
00037 
00038   template< typename T>
00039   std::string DataSource<T>::GetTypeName()
00040   {
00041       return detail::DataSourceTypeInfo< T >::getType();
00042 
00043   }
00044 
00045   template< typename T>
00046   const TypeInfo* DataSource<T>::getTypeInfo() const { return GetTypeInfo(); }
00047 
00048   template< typename T>
00049   const TypeInfo* DataSource<T>::GetTypeInfo() { return detail::DataSourceTypeInfo<T>::getTypeInfo(); }
00050 
00051   template< typename T>
00052   bool DataSource<T>::evaluate() const
00053   {
00054       this->get();
00055       return true;
00056   }
00057 
00058   template<>
00059   RTT_API bool DataSource<bool>::evaluate() const;
00060 
00061 
00062     template<typename T>
00063     AssignableDataSource<T>::~AssignableDataSource()
00064     {}
00065 
00066     template<typename T>
00067     bool AssignableDataSource<T>::updateBlob(int protocol, const void* data)
00068     {
00069 #ifndef ORO_EMBEDDED
00070         detail::TypeTransporter* tt = this->getTypeInfo()->getProtocol(protocol);
00071         if ( tt )
00072             return tt->updateBlob( data, DataSourceBase::shared_ptr(this) );
00073 #endif
00074         return false;
00075     }
00076 
00077 
00078     template<typename T>
00079     void* AssignableDataSource<T>::server( int protocol, void* arg )
00080     {
00081 #ifndef ORO_EMBEDDED
00082         detail::TypeTransporter* tt = this->getTypeInfo()->getProtocol(protocol);
00083         if ( tt )
00084             return tt->server( DataSourceBase::shared_ptr(this), true, arg );
00085 #endif
00086         return 0;
00087     }
00088 
00089 
00090     template<class T>
00091     DataSource<T>* DataSource<T>::narrow(DataSourceBase* dsb) {
00092         // first try conventional C++ style cast.
00093         DataSource<T>* ret = dynamic_cast< DataSource<T>* >( dsb );
00094         if (ret) return ret;
00095 #ifndef ORO_EMBEDDED
00096         // if narrowing failed, maybe this DS is a proxy for a remote object.
00097         int p_id = dsb->serverProtocol();
00098         if ( p_id ) {
00099             detail::TypeTransporter* tt = DataSource<T>::GetTypeInfo()->getProtocol( p_id );
00100             if (tt) {
00101                 DataSourceBase* ret2 = tt->narrowDataSource( dsb );
00102                 // it may be that <T> is a 'const &' and the protocol returns a value type.
00103                 return dynamic_cast<DataSource<T>*>(ret2);
00104             }
00105         }
00106 #endif
00107         // all failed:
00108         return 0;
00109     }
00110 
00111     template<class T>
00112     AssignableDataSource<T>* AssignableDataSource<T>::narrow(DataSourceBase* dsb) {
00113         // first try conventional C++ style cast.
00114         AssignableDataSource<T>* ret = dynamic_cast< AssignableDataSource<T>* >( dsb );
00115         if (ret) return ret;
00116 #ifndef ORO_EMBEDDED
00117         int p_id = dsb->serverProtocol();
00118         if ( p_id ) {
00119             detail::TypeTransporter* tt = DataSource<T>::GetTypeInfo()->getProtocol( p_id );
00120             if (tt) {
00121                 DataSourceBase* ret2 = tt->narrowAssignableDataSource( dsb );
00122                 return dynamic_cast<AssignableDataSource<T>*>(ret2);
00123             }
00124         }
00125 #endif
00126         // all failed:
00127         return 0;
00128     }
00129 
00130     template<class T>
00131     bool AssignableDataSource<T>::update( DataSourceBase* other ) {
00132         DataSourceBase::shared_ptr r( other );
00133         typename DataSource<T>::shared_ptr o = AdaptDataSource<T>()( detail::DataSourceTypeInfo<T>::getTypeInfo()->convert(r) );
00134         if (o) {
00135             this->set( o->get() );
00136             return true;
00137         }
00138         return false;
00139     }
00140 
00141     template<class T>
00142     CommandInterface* AssignableDataSource<T>::updateCommand( DataSourceBase* other) {
00143         // Use the same rules of parameter passing as C++, but no const for 'int',...
00144         DataSourceBase::shared_ptr r( other );
00145         typename DataSource<copy_t>::shared_ptr t = AdaptDataSource<copy_t>()( detail::DataSourceTypeInfo<T>::getTypeInfo()->convert(r) );
00146         if ( t )
00147             return new detail::AssignCommand<T,copy_t>( this, t );
00148 
00149 #ifndef ORO_EMBEDDED
00150         throw bad_assignment();
00151 #endif
00152 
00153         return 0;
00154     }
00155 }
00156 
00157 
00158 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3