OperatorTypes.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:56 CEST 2006  OperatorTypes.hpp
00003 
00004                         OperatorTypes.hpp -  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 #ifndef ORO_CORELIB_OPERATOP_TYPES_HPP
00040 #define ORO_CORELIB_OPERATOP_TYPES_HPP
00041 
00042 #include "Operators.hpp"
00043 #include "DataSources.hpp"
00044 #include "DataSourceAdaptor.hpp"
00045 
00046 namespace RTT
00047 {
00048     namespace detail {
00049 
00054         template<typename function>
00055         class UnaryOperator
00056             : public UnaryOp
00057         {
00058             typedef typename function::argument_type arg_t;
00059             typedef typename function::result_type result_t;
00060             const char* mop;
00061             function fun;
00062         public:
00063             UnaryOperator( const char* op, function f )
00064                 : mop( op ), fun( f )
00065             {
00066             }
00067             DataSource<result_t>* build( const std::string& op, DataSourceBase* a )
00068             {
00069                 if ( op != mop ) return 0;
00070                 typename DataSource<arg_t>::shared_ptr arg =
00071                     AdaptDataSource<arg_t>()( a ); // do not call convert(a) here ! Would always succeed.
00072                 if ( ! arg ) return 0;
00073                 return new UnaryDataSource<function>( arg, fun );
00074             }
00075         };
00076 
00077 
00082         template<typename function>
00083         class BinaryOperator
00084             : public BinaryOp
00085         {
00086             typedef typename function::first_argument_type arg1_t;
00087             typedef typename function::second_argument_type arg2_t;
00088             typedef typename function::result_type result_t;
00089             const char* mop;
00090             function fun;
00091         public:
00092             BinaryOperator( const char* op, function f )
00093                 : mop( op ), fun( f )
00094             {
00095             }
00096             DataSource<result_t>* build( const std::string& op, DataSourceBase* a,
00097                                          DataSourceBase* b )
00098             {
00099                 // operation (+,-,...) and first argument type must match.
00100                 if ( op != mop || a->getTypeInfo() != DataSourceTypeInfo<arg1_t>::getTypeInfo() ) return 0;
00101                 //         Logger::log() << Logger::Debug << "BinaryOperator: "<< op << Logger::nl;
00102                 typename DataSource<arg1_t>::shared_ptr arg1 =
00103                     AdaptDataSource<arg1_t>()( a ); // first argument must be exact match.
00104                 typename DataSource<arg2_t>::shared_ptr arg2 =
00105                     AdaptDataSource<arg2_t>()( DataSourceTypeInfo<arg2_t>::getTypeInfo()->convert(b) );
00106                 //         Logger::log() << "arg1 : "<< arg1 <<" second arg: "<<arg2<<"..." << Logger::endl;
00107                 //         Logger::log() << "arg1 was: "<< typeid(arg1).name()  <<" a was: "<<typeid(a).name()<<"..." << Logger::endl;
00108                 if ( !arg1 || ! arg2 ) return 0;
00109                 //         Logger::log() << "success !"<< Logger::endl;
00110                 return new BinaryDataSource<function>( arg1, arg2, fun );
00111             }
00112         };
00113 
00117         template<typename function>
00118         class DotOperator
00119             : public DotOp
00120         {
00121             typedef typename function::argument_type arg1_t;
00122             typedef typename function::result_type result_t;
00123             const char* memb;
00124             function fun;
00125         public:
00129             DotOperator( const char* m, function f )
00130                 : memb( m ), fun( f )
00131             {
00132             }
00133             DataSource<result_t>* build( const std::string& member, DataSourceBase* a)
00134             {
00135                 if ( member != memb ) return 0;
00136                 //         Logger::log() << Logger::Debug << "DotOperator: "<< op << Logger::nl;
00137                 typename DataSource<arg1_t>::shared_ptr arg1 =
00138                     AdaptDataSource<arg1_t>()( a );
00139                 if ( !arg1 ) return 0;
00140                 //         Logger::log() << "success !"<< Logger::endl;
00141                 return new UnaryDataSource<function>( arg1, fun );
00142             }
00143         };
00144 
00145     }
00146 
00150     template<typename function>
00151     detail::UnaryOperator<function>*
00152     newUnaryOperator( const char* op, function f )
00153     {
00154         return new detail::UnaryOperator<function>( op, f );
00155     }
00156 
00160     template<typename function>
00161     detail::BinaryOperator<function>*
00162     newBinaryOperator( const char* op, function f )
00163     {
00164         return new detail::BinaryOperator<function>( op, f );
00165     }
00166 
00170     template<typename function>
00171     detail::DotOperator<function>*
00172     newDotOperator( const char* member, function f )
00173     {
00174         return new detail::DotOperator<function>( member, f );
00175     }
00176 }
00177 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3