Orocos Real-Time Toolkit  2.5.0
TemplateTypeInfo.hpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 19 14:11:26 CET 2004  Types.hpp
00003 
00004                         Types.hpp -  description
00005                            -------------------
00006     begin                : Mon January 19 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 #ifndef ORO_TEMPLATE_TYPEINFO_HPP
00039 #define ORO_TEMPLATE_TYPEINFO_HPP
00040 
00041 #include "Types.hpp"
00042 #include "../Property.hpp"
00043 #include "../Attribute.hpp"
00044 #include "../Logger.hpp"
00045 #include "../InputPort.hpp"
00046 #include "../OutputPort.hpp"
00047 #include "PrimitiveTypeInfo.hpp"
00048 
00049 #include "../rtt-config.h"
00050 
00051 namespace RTT
00052 {
00053     namespace types {
00054 
00071     template<typename T, bool use_ostream = false>
00072     class TemplateTypeInfo
00073         : public PrimitiveTypeInfo<T, use_ostream>
00074     {
00075     public:
00076         using TypeInfo::buildConstant;
00077         using TypeInfo::buildVariable;
00078 
00082         typedef T UserType;
00086         typedef typename Property<T>::DataSourceType PropertyType;
00087 
00095         TemplateTypeInfo(std::string name)
00096             : PrimitiveTypeInfo<T,use_ostream>(name)
00097         {
00098         }
00099 
00100         virtual ~TemplateTypeInfo()
00101         {
00102         }
00103 
00104         virtual bool composeType( base::DataSourceBase::shared_ptr source, base::DataSourceBase::shared_ptr result) const {
00105             const internal::DataSource<PropertyBag>* pb = dynamic_cast< const internal::DataSource<PropertyBag>* > (source.get() );
00106             if ( !pb )
00107                 return false;
00108             typename internal::AssignableDataSource<PropertyType>::shared_ptr ads = boost::dynamic_pointer_cast< internal::AssignableDataSource<PropertyType> >( result );
00109             if ( !ads )
00110                 return false;
00111 
00112             // last fall-back: use user supplied function:
00113             if ( composeTypeImpl( pb->rvalue(), ads->set() ) )
00114                 ads->updated();
00115             else {
00116                 Logger::log() <<Logger::Debug<<"Failed to compose from "<< source->getTypeName() <<Logger::endl;
00117                 return false;
00118             }
00119             Logger::log() <<Logger::Debug<<"Successfuly composed type from "<< source->getTypeName() <<Logger::endl;
00120             return true;
00121         }
00122 
00129         virtual base::DataSourceBase::shared_ptr decomposeType(base::DataSourceBase::shared_ptr source) const
00130         {
00131             // Extract typed values
00132             typename internal::DataSource<PropertyType>::shared_ptr ds = boost::dynamic_pointer_cast< internal::DataSource<PropertyType> >( source );
00133             if ( !ds )
00134                 return base::DataSourceBase::shared_ptr(); // happens in the case of 'unknown type'
00135             Property<PropertyBag> targetbag_p("targetbag");
00136             if (decomposeTypeImpl( ds->rvalue(), targetbag_p.value() ))
00137                 return targetbag_p.getDataSource();
00138             return base::DataSourceBase::shared_ptr();
00139         }
00140 
00144         virtual bool composeTypeImpl(const PropertyBag& source,  typename internal::AssignableDataSource<T>::reference_t result) const {
00145             return false;
00146         }
00147 
00152         virtual bool decomposeTypeImpl( typename internal::AssignableDataSource<T>::const_reference_t source, PropertyBag& targetbag ) const {
00153             return false;
00154         }
00155 
00156         base::InputPortInterface*  inputPort(std::string const& name) const { return new InputPort<T>(name); }
00157         base::OutputPortInterface* outputPort(std::string const& name) const { return new OutputPort<T>(name); }
00158 
00159         base::ChannelElementBase::shared_ptr buildDataStorage(ConnPolicy const& policy) const {
00160             return internal::ConnFactory::buildDataStorage<T>(policy);
00161         }
00162 
00163         base::ChannelElementBase::shared_ptr buildChannelOutput(base::InputPortInterface& port) const
00164         {
00165             return internal::ConnFactory::buildChannelOutput(
00166                     static_cast<RTT::InputPort<T>&>(port), new internal::SimpleConnID());
00167         }
00168 
00169         base::ChannelElementBase::shared_ptr buildChannelInput(base::OutputPortInterface& port) const
00170         {
00171             return internal::ConnFactory::buildChannelInput(
00172                     static_cast<RTT::OutputPort<T>&>(port), new internal::SimpleConnID(), 0 );
00173         }
00174     };
00175 }}
00176 
00177 #endif