Orocos Real-Time Toolkit  2.6.0
TemplateValueFactory.hpp
00001 #ifndef RTT_TEMPLATE_TYPE_FACTORY
00002 #define RTT_TEMPLATE_TYPE_FACTORY
00003 
00004 #include "ValueFactory.hpp"
00005 #include "../Property.hpp"
00006 #include "../Attribute.hpp"
00007 #include "../Logger.hpp"
00008 #include "../rtt-config.h"
00009 
00010 namespace RTT
00011 {
00012     namespace types {
00013 
00014         template<class T>
00015         class TemplateValueFactory 
00016             : public ValueFactory
00017         {
00018         public:
00019             typedef T DataType;
00020             base::AttributeBase* buildConstant(std::string name, base::DataSourceBase::shared_ptr dsb) const
00021             {
00022                 typename internal::DataSource<DataType>::shared_ptr res =
00023                     boost::dynamic_pointer_cast< internal::DataSource<DataType> >( internal::DataSourceTypeInfo<DataType>::getTypeInfo()->convert(dsb));
00024                 if ( res ) {
00025                     res->get();
00026                     //Logger::log() << Logger::Info << "Building "<<tname<<" Constant '"<<name<<"' with value "<< dsb->getTypeInfo()->toString(dsb) <<Logger::endl;
00027                     return new Constant<DataType>( name, res->rvalue() );
00028                 }
00029                 else
00030                     return 0;
00031             }
00032 
00033             base::AttributeBase* buildVariable(std::string name) const
00034             {
00035                 // A variable starts its life as unbounded.
00036                 //Logger::log() << Logger::Debug << "Building variable '"<<name <<"' of type " << tname <<Logger::endl;
00037                 return new Attribute<T>( name, new internal::UnboundDataSource<internal::ValueDataSource<T> >() );
00038             }
00039 
00040             base::AttributeBase* buildAttribute( std::string name, base::DataSourceBase::shared_ptr in) const
00041             {
00042                 typename internal::AssignableDataSource<DataType>::shared_ptr ds;
00043                 if ( !in )
00044                     ds = new internal::ValueDataSource<DataType>();
00045                 else
00046                     ds = internal::AssignableDataSource<DataType>::narrow( in.get() );
00047                 if (!ds)
00048                     return 0;
00049                 // An attribute is always bounded.
00050                 //Logger::log() << Logger::Debug << "Building Attribute '"<< name <<"' of type " << tname <<Logger::endl;
00051                 return new Attribute<DataType>( name, ds.get() );
00052             }
00053 
00054             base::AttributeBase* buildAlias(std::string name, base::DataSourceBase::shared_ptr in ) const
00055             {
00056                 typename internal::DataSource<T>::shared_ptr ds = boost::dynamic_pointer_cast< internal::DataSource<T> >( internal::DataSourceTypeInfo<T>::getTypeInfo()->convert(in) );
00057                 if ( ! ds )
00058                     return 0;
00059                 return new Alias( name, ds );
00060             }
00061 
00062             base::DataSourceBase::shared_ptr buildActionAlias(base::ActionInterface* action, base::DataSourceBase::shared_ptr in) const
00063             {
00064                 typename internal::AssignableDataSource<T>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( in ); // no type conversion is done.
00065                 if ( ads )
00066                     return new internal::ActionAliasAssignableDataSource<T>(action, ads.get());
00067 
00068                 typename internal::DataSource<T>::shared_ptr ds = boost::dynamic_pointer_cast< internal::DataSource<T> >( in ); // no type conversion is done.
00069                 if ( ! ds )
00070                     return 0;
00071                 return new internal::ActionAliasDataSource<T>(action, ds.get());
00072             }
00073 
00074             virtual base::PropertyBase* buildProperty(const std::string& name, const std::string& desc, base::DataSourceBase::shared_ptr source = 0) const {
00075                 if (source) {
00076                     typename internal::AssignableDataSource<DataType>::shared_ptr ad
00077                         = boost::dynamic_pointer_cast< internal::AssignableDataSource<DataType> >( source );
00078                     if (ad)
00079                         return new Property<DataType>(name, desc, ad );
00080                     else {
00081                         //log(Error) <<"Failed to build 'Property<"<< this->tname <<"> "<<name<<"' from given DataSourceBase. Returning default."<<endlog();
00082                     }
00083                 }
00084                 return new Property<DataType>(name, desc, DataType());
00085             }
00086 
00087             virtual base::DataSourceBase::shared_ptr buildValue() const {
00088                 return new internal::ValueDataSource<DataType>();
00089             }
00090             virtual base::DataSourceBase::shared_ptr buildReference(void* ptr) const {
00091                 return new internal::ReferenceDataSource<DataType>(*static_cast<DataType*>(ptr));
00092             }
00093 
00094         };
00095 
00096     }
00097 }
00098 
00099 #endif