Go to the documentation of this file.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 "../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 );
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
00101 if ( op != mop || a->getTypeInfo() != internal::DataSourceTypeInfo<arg1_t>::getTypeInfo() ) return 0;
00102
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 );
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
00109
00110 if ( !arg1 || ! arg2 ) return 0;
00111
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