Orocos Real-Time Toolkit  2.6.0
PrimitiveTypeInfo.hpp
00001 #ifndef ORO_PRIMITIVE_TYPEINFO_HPP
00002 #define ORO_PRIMITIVE_TYPEINFO_HPP
00003 
00004 #include "Types.hpp"
00005 #include "../Property.hpp"
00006 #include "../Attribute.hpp"
00007 #include "../Logger.hpp"
00008 #include "TypeStreamSelector.hpp"
00009 #include "TypeInfoGenerator.hpp"
00010 #include "StreamFactory.hpp"
00011 #include "TemplateValueFactory.hpp"
00012 #include "../rtt-config.h"
00013 
00014 namespace RTT
00015 {
00016     namespace types {
00017 
00031     template<typename T, bool use_ostream = false>
00032     class PrimitiveTypeInfo :
00033             public TypeInfoGenerator,
00034             public TemplateValueFactory<T>,
00035             public StreamFactory
00036     {
00037     protected:
00038         const std::string tname;
00039         boost::shared_ptr<PrimitiveTypeInfo<T, use_ostream> > mshared;
00040     public:
00044         typedef T DataType;
00045 
00053         PrimitiveTypeInfo(std::string name)
00054             : tname(name)
00055         {
00056         }
00057 
00058         virtual ~PrimitiveTypeInfo()
00059         {
00060         }
00061 
00062         boost::shared_ptr<PrimitiveTypeInfo<T, use_ostream> > getSharedPtr() {
00063             if (!mshared)
00064                 mshared.reset(this);
00065             return mshared;
00066         }
00067 
00068         bool installTypeInfoObject(TypeInfo* ti) {
00069             // Install the factories for primitive types
00070             ti->setValueFactory( this->getSharedPtr() );
00071             ti->setStreamFactory( this->getSharedPtr() );
00072 
00073             // Install the type info object for T
00074             internal::DataSourceTypeInfo<T>::value_type_info::TypeInfoObject = ti;
00075             ti->setTypeId( &typeid(T) );
00076 
00077             // Clean up reference to ourselves.
00078             mshared.reset();
00079             // Don't delete us, we're memory-managed.
00080             return false;
00081         }
00082 
00083         TypeInfo* getTypeInfoObject() const {
00084             return TypeInfoRepository::Instance()->getTypeInfo<T>();
00085         }
00086 
00087         virtual const std::string& getTypeName() const { return tname; }
00088 
00089         virtual std::ostream& write( std::ostream& os, base::DataSourceBase::shared_ptr in ) const {
00090             typename internal::DataSource<T>::shared_ptr d = boost::dynamic_pointer_cast< internal::DataSource<T> >( in );
00091             if ( d && use_ostream )
00092                 types::TypeStreamSelector<T, use_ostream>::write( os, d->rvalue() );
00093             else {
00094 #ifdef OS_HAVE_STREAMS
00095                 std::string output = std::string("(")+ in->getTypeName() +")";
00096                 os << output;
00097 #endif
00098             }
00099             return os;
00100             //return os << "("<< tname <<")"
00101         }
00102 
00103         virtual std::istream& read( std::istream& os, base::DataSourceBase::shared_ptr out ) const {
00104             typename internal::AssignableDataSource<T>::shared_ptr d = boost::dynamic_pointer_cast< internal::AssignableDataSource<T> >( out );
00105             if ( d && use_ostream ) {
00106                 types::TypeStreamSelector<T, use_ostream>::read( os, d->set() );
00107                 d->updated(); // because use of set().
00108             }
00109             return os;
00110         }
00111 
00112         virtual bool isStreamable() const {
00113             return use_ostream;
00114         }
00115 
00116         virtual bool composeType( base::DataSourceBase::shared_ptr source, base::DataSourceBase::shared_ptr result) const {
00117             return false;
00118         }
00119 
00123         virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
00124         {
00125             return source;
00126         }
00127 
00128         virtual bool decomposeType( base::DataSourceBase::shared_ptr source, PropertyBag& targetbag ) const {
00129             return false;
00130         }
00131     };
00132 }}
00133 
00134 #endif