Orocos Real-Time Toolkit
2.6.0
|
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 "../internal/DataSources.hpp" 00044 #include <boost/shared_ptr.hpp> 00045 00046 namespace RTT 00047 { 00048 namespace types { 00049 00054 template<typename function> 00055 class UnaryOperator 00056 : public UnaryOp 00057 { 00058 typedef typename internal::remove_cr<typename function::argument_type>::type arg_t; 00059 typedef typename internal::remove_cr<typename function::result_type>::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 internal::DataSource<result_t>* build( const std::string& op, base::DataSourceBase* a ) 00068 { 00069 if ( op != mop ) return 0; 00070 base::DataSourceBase::shared_ptr dsb = a; 00071 typename internal::DataSource<arg_t>::shared_ptr arg = 00072 boost::dynamic_pointer_cast< internal::DataSource<arg_t> >( dsb ); // do not call convert(a) here ! Would always succeed. 00073 if ( ! arg ) return 0; 00074 return new internal::UnaryDataSource<function>( arg, fun ); 00075 } 00076 }; 00077 00078 00083 template<typename function> 00084 class BinaryOperator 00085 : public BinaryOp 00086 { 00087 typedef typename internal::remove_cr<typename function::first_argument_type>::type arg1_t; 00088 typedef typename internal::remove_cr<typename function::second_argument_type>::type arg2_t; 00089 typedef typename internal::remove_cr<typename function::result_type>::type result_t; 00090 const char* mop; 00091 function fun; 00092 public: 00093 BinaryOperator( const char* op, function f ) 00094 : mop( op ), fun( f ) 00095 { 00096 } 00097 internal::DataSource<result_t>* build( const std::string& op, base::DataSourceBase* a, 00098 base::DataSourceBase* b ) 00099 { 00100 // operation (+,-,...) and first argument type must match. 00101 if ( op != mop || a->getTypeInfo() != internal::DataSourceTypeInfo<arg1_t>::getTypeInfo() ) return 0; 00102 // Logger::log() << Logger::Debug << "BinaryOperator: "<< op << Logger::nl; 00103 base::DataSourceBase::shared_ptr dsb = a; 00104 typename internal::DataSource<arg1_t>::shared_ptr arg1 = 00105 boost::dynamic_pointer_cast< internal::DataSource<arg1_t> >( dsb ); // first argument must be exact match. 00106 typename internal::DataSource<arg2_t>::shared_ptr arg2 = 00107 boost::dynamic_pointer_cast< internal::DataSource<arg2_t> >( internal::DataSourceTypeInfo<arg2_t>::getTypeInfo()->convert(b) ); 00108 // Logger::log() << "arg1 : "<< arg1 <<" second arg: "<<arg2<<"..." << Logger::endl; 00109 // Logger::log() << "arg1 was: "<< typeid(arg1).name() <<" a was: "<<typeid(a).name()<<"..." << Logger::endl; 00110 if ( !arg1 || ! arg2 ) return 0; 00111 // Logger::log() << "success !"<< Logger::endl; 00112 return new internal::BinaryDataSource<function>( arg1, arg2, fun ); 00113 } 00114 00115 bool isExactMatch(const std::string& op, base::DataSourceBase* a, 00116 base::DataSourceBase* b ) { 00117 return op == mop 00118 && a->getTypeInfo() == internal::DataSourceTypeInfo<arg1_t>::getTypeInfo() 00119 && b->getTypeInfo() == internal::DataSourceTypeInfo<arg2_t>::getTypeInfo(); 00120 } 00121 }; 00122 00126 template<typename function> 00127 UnaryOperator<function>* 00128 newUnaryOperator( const char* op, function f ) 00129 { 00130 return new UnaryOperator<function>( op, f ); 00131 } 00132 00136 template<typename function> 00137 BinaryOperator<function>* 00138 newBinaryOperator( const char* op, function f ) 00139 { 00140 return new BinaryOperator<function>( op, f ); 00141 } 00142 00143 }} 00144 #endif