Orocos Real-Time Toolkit  2.6.0
RealTimeTypekitConstructors.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:56 CEST 2006  RealTimeTypekit.cxx
00003 
00004                         RealTimeTypekit.cxx -  description
00005                            -------------------
00006     begin                : Mon June 26 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@fmtc.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 
00040 #include "rtt-typekit-config.h"
00041 #include "RealTimeTypekit.hpp"
00042 #include "../types/Types.hpp"
00043 #include "../FlowStatus.hpp"
00044 #include "../ConnPolicy.hpp"
00045 #include "../internal/DataSources.hpp"
00046 #include "../typekit/Types.hpp"
00047 #include "../rtt-fwd.hpp"
00048 #include "../internal/mystd.hpp"
00049 #include "../types/TemplateConstructor.hpp"
00050 #ifdef OS_RT_MALLOC
00051 #include "../rt_string.hpp"
00052 #endif
00053 
00054 namespace RTT
00055 {
00056     using namespace std;
00057     using namespace detail;
00058 
00059     namespace {
00060 #ifndef ORO_EMBEDDED
00061         // CONSTRUCTORS
00062         struct array_ctor
00063             : public std::unary_function<int, const std::vector<double>&>
00064         {
00065             typedef const std::vector<double>& (Signature)( int );
00066             mutable boost::shared_ptr< std::vector<double> > ptr;
00067             array_ctor()
00068                 : ptr( new std::vector<double>() ) {}
00069             const std::vector<double>& operator()( int size ) const
00070             {
00071                 ptr->resize( size );
00072                 return *(ptr);
00073             }
00074         };
00075 
00080         struct array_varargs_ctor
00081         {
00082             typedef const std::vector<double>& result_type;
00083             typedef double argument_type;
00084             result_type operator()( const std::vector<double>& args ) const
00085             {
00086                 return args;
00087             }
00088         };
00089 
00094         typedef NArityDataSource<array_varargs_ctor> ArrayDataSource;
00095 
00100         struct ArrayBuilder
00101             : public TypeConstructor
00102         {
00103             virtual DataSourceBase::shared_ptr build(const std::vector<DataSourceBase::shared_ptr>& args) const {
00104                 if (args.size() == 0 )
00105                     return DataSourceBase::shared_ptr();
00106                 ArrayDataSource::shared_ptr vds = new ArrayDataSource();
00107                 for(unsigned int i=0; i != args.size(); ++i) {
00108                     DataSource<double>::shared_ptr dsd = boost::dynamic_pointer_cast< DataSource<double> >( args[i] );
00109                     if (dsd)
00110                         vds->add( dsd );
00111                     else
00112                         return DataSourceBase::shared_ptr();
00113                 }
00114                 return vds;
00115             }
00116 
00117         };
00118 
00119         struct array_ctor2
00120             : public std::binary_function<int, double, const std::vector<double>&>
00121         {
00122             typedef const std::vector<double>& (Signature)( int, double );
00123             mutable boost::shared_ptr< std::vector<double> > ptr;
00124             array_ctor2()
00125                 : ptr( new std::vector<double>() ) {}
00126             const std::vector<double>& operator()( int size, double value ) const
00127             {
00128                 ptr->resize( size );
00129                 ptr->assign( size, value );
00130                 return *(ptr);
00131             }
00132         };
00133 
00134         double float_to_double( float val ) {return double(val);}
00135         float double_to_float( double val ) {return float(val);}
00136 
00137 
00138         int float_to_int(float f) { return int(f); }
00139         float int_to_float(int i) { return float(i); }
00140         int double_to_int(double f) { return int(f); }
00141         double int_to_double(int i) { return double(i); }
00142         unsigned int int_to_uint(int i) { return (unsigned int)(i); }
00143         int uint_to_int(unsigned int ui) { return int(ui); }
00144 #endif
00145         bool flow_to_bool(FlowStatus fs) { return fs != NoData ; }
00146         bool send_to_bool(SendStatus ss) { return ss == SendSuccess; }
00147         bool int_to_bool(int i) { return i != 0; }
00148         int bool_to_int(bool b) { return int(b); }
00149 
00150         struct string_ctor
00151             : public std::unary_function<int, const std::string&>
00152         {
00153             mutable boost::shared_ptr< std::string > ptr;
00154             typedef const std::string& (Signature)( int );
00155             string_ctor()
00156                 : ptr( new std::string() ) {}
00157             const std::string& operator()( int size ) const
00158             {
00159                 ptr->resize( size );
00160                 return *(ptr);
00161             }
00162         };
00163 
00164 #ifdef OS_RT_MALLOC
00165         struct rt_string_ctor_int
00166             : public std::unary_function<int, const RTT::rt_string&>
00167         {
00168             mutable boost::shared_ptr< rt_string > ptr;
00169             typedef const rt_string& (Signature)( int );
00170             rt_string_ctor_int()
00171                 : ptr( new rt_string() ) {}
00172             const rt_string& operator()( int size ) const
00173             {
00174                 ptr->resize( size );
00175                 return *(ptr);
00176             }
00177         };
00178 
00179             struct rt_string_ctor_string
00180                 : public std::unary_function<const std::string&, const RTT::rt_string&>
00181             {
00182                 mutable boost::shared_ptr< rt_string > ptr;
00183                 typedef const rt_string& (Signature)( std::string const& );
00184                 rt_string_ctor_string()
00185                     : ptr( new rt_string() ) {}
00186                 const rt_string& operator()( std::string const& arg ) const
00187                 {
00188                     *ptr = arg.c_str();
00189                     return *(ptr);
00190                 }
00191             };
00192 
00193         struct string_ctor_rt_string
00194             : public std::unary_function<const rt_string&, const string&>
00195         {
00196             mutable boost::shared_ptr< string > ptr;
00197             typedef const string& (Signature)( rt_string const& );
00198             string_ctor_rt_string()
00199             : ptr( new string() ) {}
00200             const string& operator()( rt_string const& arg ) const
00201             {
00202                 *ptr = arg.c_str();
00203                 return *(ptr);
00204             }
00205         };
00206 
00207 #endif
00208     }
00209 
00210     bool RealTimeTypekitPlugin::loadConstructors()
00211     {
00212         TypeInfoRepository::shared_ptr ti = TypeInfoRepository::Instance();
00213 #ifndef ORO_EMBEDDED
00214         ti->type("double")->addConstructor( newConstructor( &float_to_double, true ));
00215         ti->type("double")->addConstructor( newConstructor( &int_to_double, true ));
00216         ti->type("float")->addConstructor( newConstructor( &int_to_float, true ));
00217         ti->type("float")->addConstructor( newConstructor( &double_to_float, true ));
00218         ti->type("int")->addConstructor( newConstructor( &float_to_int, false ));
00219         ti->type("int")->addConstructor( newConstructor( &double_to_int, false ));
00220         ti->type("int")->addConstructor( newConstructor( &uint_to_int, true ));
00221         ti->type("int")->addConstructor( newConstructor( &bool_to_int, true ));
00222         ti->type("uint")->addConstructor( newConstructor( &int_to_uint, true ));
00223         ti->type("string")->addConstructor( newConstructor( string_ctor() ) );
00224 #ifdef OS_RT_MALLOC
00225         ti->type("rt_string")->addConstructor( newConstructor( rt_string_ctor_int() ) );
00226         ti->type("rt_string")->addConstructor( newConstructor( rt_string_ctor_string() ) );
00227         ti->type("string")->addConstructor( newConstructor( string_ctor_rt_string() ) );
00228 #endif
00229         ti->type("bool")->addConstructor( newConstructor( &flow_to_bool, true ) );
00230         ti->type("bool")->addConstructor( newConstructor( &send_to_bool, true ) );
00231         ti->type("bool")->addConstructor( newConstructor( &int_to_bool, true ) );
00232 #endif
00233         return true;
00234     }
00235 }