OperatorTypes.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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 );
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
00100 if ( op != mop || a->getTypeInfo() != DataSourceTypeInfo<arg1_t>::getTypeInfo() ) return 0;
00101
00102 typename DataSource<arg1_t>::shared_ptr arg1 =
00103 AdaptDataSource<arg1_t>()( a );
00104 typename DataSource<arg2_t>::shared_ptr arg2 =
00105 AdaptDataSource<arg2_t>()( DataSourceTypeInfo<arg2_t>::getTypeInfo()->convert(b) );
00106
00107
00108 if ( !arg1 || ! arg2 ) return 0;
00109
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
00137 typename DataSource<arg1_t>::shared_ptr arg1 =
00138 AdaptDataSource<arg1_t>()( a );
00139 if ( !arg1 ) return 0;
00140
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