Orocos Real-Time Toolkit  2.8.3
RealTimeTypekitOperators.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 26 13:25:56 CEST 2006 RealTimeTypekit.cxx
3 
4  RealTimeTypekit.cxx - description
5  -------------------
6  begin : Mon June 26 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@fmtc.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 #include "rtt-typekit-config.h"
41 #include "RealTimeTypekit.hpp"
42 #include "../types/Types.hpp"
43 #include "../types/Operators.hpp"
44 #include "../types/OperatorTypes.hpp"
45 #include "../internal/mystd.hpp"
46 #include "../rtt-fwd.hpp"
47 #include "../FlowStatus.hpp"
48 #include "../SendStatus.hpp"
49 #include "../ConnPolicy.hpp"
50 #include "../typekit/Types.hpp"
51 #include <ostream>
52 #include <sstream>
53 #ifdef OS_RT_MALLOC
54 #include "../rt_string.hpp"
55 #endif
56 
57 namespace RTT
58 {
59  using namespace std;
60  using namespace detail;
61 
62 #ifndef RTT_NO_STD_TYPES
63  template<class T>
64  struct get_capacity
65  : public std::unary_function<T, int>
66  {
67  int operator()(T cont ) const
68  {
69  return cont.capacity();
70  }
71  };
72 
73  template<class T>
74  struct get_size
75  : public std::unary_function<T, int>
76  {
77  int operator()(T cont ) const
78  {
79  return cont.size();
80  }
81  };
82 
86  template <class T>
87  struct string_concatenation : public std::binary_function<const std::string&, T, std::string> {
88  std::string operator()(const std::string& s, T t) const {
89  std::ostringstream oss(s, std::ios_base::ate);
90  oss << std::boolalpha << t;
91  return oss.str();
92  }
93  };
94 #ifdef OS_RT_MALLOC
95  template <class T>
96  struct rt_string_concatenation : public std::binary_function<const rt_string&, T, rt_string> {
97  rt_string operator()(const rt_string& s, T t) const {
98  rt_ostringstream oss(s, std::ios_base::ate);
99  oss << std::boolalpha << t;
100  return oss.str();
101  }
102  };
103 #endif
104 
105 #endif
106 
108  {
110 
111  // boolean stuff:
112  oreg->add( newUnaryOperator( "!", std::logical_not<bool>() ) );
113  oreg->add( newBinaryOperator( "&&", std::logical_and<bool>() ) );
114  oreg->add( newBinaryOperator( "||", std::logical_or<bool>() ) );
115  oreg->add( newBinaryOperator( "==", std::equal_to<bool>() ) );
116  oreg->add( newBinaryOperator( "!=", std::not_equal_to<bool>() ) );
117 
118  // int stuff
119  oreg->add( newUnaryOperator( "-", std::negate<int>() ) );
120  oreg->add( newUnaryOperator( "+", identity<int>() ) );
121  oreg->add( newBinaryOperator( "*", std::multiplies<int>() ) );
122  oreg->add( newBinaryOperator( "/", divides3<int,int,int>() ) ); // use our own divides<> which detects div by zero
123  oreg->add( newBinaryOperator( "%", std::modulus<int>() ) );
124  oreg->add( newBinaryOperator( "+", std::plus<int>() ) );
125  oreg->add( newBinaryOperator( "-", std::minus<int>() ) );
126  oreg->add( newBinaryOperator( "<", std::less<int>() ) );
127  oreg->add( newBinaryOperator( "<=", std::less_equal<int>() ) );
128  oreg->add( newBinaryOperator( ">", std::greater<int>() ) );
129  oreg->add( newBinaryOperator( ">=", std::greater_equal<int>() ) );
130  oreg->add( newBinaryOperator( "==", std::equal_to<int>() ) );
131  oreg->add( newBinaryOperator( "!=", std::not_equal_to<int>() ) );
132 #ifndef ORO_EMBEDDED
133  // uint stuff
134  oreg->add( newUnaryOperator( "+", identity<unsigned int>() ) );
135  oreg->add( newBinaryOperator( "*", std::multiplies<unsigned int>() ) );
136  oreg->add( newBinaryOperator( "/", divides3<unsigned int,unsigned int,unsigned int>() ) ); // use our own divides<> which detects div by zero
137  oreg->add( newBinaryOperator( "%", std::modulus<unsigned int>() ) );
138  oreg->add( newBinaryOperator( "+", std::plus<unsigned int>() ) );
139  oreg->add( newBinaryOperator( "-", std::minus<unsigned int>() ) );
140  oreg->add( newBinaryOperator( "<", std::less<unsigned int>() ) );
141  oreg->add( newBinaryOperator( "<=", std::less_equal<unsigned int>() ) );
142  oreg->add( newBinaryOperator( ">", std::greater<unsigned int>() ) );
143  oreg->add( newBinaryOperator( ">=", std::greater_equal<unsigned int>() ) );
144  oreg->add( newBinaryOperator( "==", std::equal_to<unsigned int>() ) );
145  oreg->add( newBinaryOperator( "!=", std::not_equal_to<unsigned int>() ) );
146 #endif
147  // double stuff..
148  oreg->add( newUnaryOperator( "-", std::negate<double>() ) );
149  oreg->add( newUnaryOperator( "+", identity<double>() ) );
150  oreg->add( newBinaryOperator( "*", std::multiplies<double>() ) );
151  oreg->add( newBinaryOperator( "/", std::divides<double>() ) );
152  oreg->add( newBinaryOperator( "+", std::plus<double>() ) );
153  oreg->add( newBinaryOperator( "-", std::minus<double>() ) );
154  oreg->add( newBinaryOperator( "<", std::less<double>() ) );
155  oreg->add( newBinaryOperator( "<=", std::less_equal<double>() ) );
156  oreg->add( newBinaryOperator( ">", std::greater<double>() ) );
157  oreg->add( newBinaryOperator( ">=", std::greater_equal<double>() ) );
158  oreg->add( newBinaryOperator( "==", std::equal_to<double>() ) );
159  oreg->add( newBinaryOperator( "!=", std::not_equal_to<double>() ) );
160 #ifndef ORO_EMBEDDED
161  // float stuff
162  oreg->add( newUnaryOperator( "-", std::negate<float>() ) );
163  oreg->add( newUnaryOperator( "+", identity<float>() ) );
164  oreg->add( newBinaryOperator( "*", std::multiplies<float>() ) );
165  oreg->add( newBinaryOperator( "/", std::divides<float>() ) );
166  oreg->add( newBinaryOperator( "+", std::plus<float>() ) );
167  oreg->add( newBinaryOperator( "-", std::minus<float>() ) );
168  oreg->add( newBinaryOperator( "<", std::less<float>() ) );
169  oreg->add( newBinaryOperator( "<=", std::less_equal<float>() ) );
170  oreg->add( newBinaryOperator( ">", std::greater<float>() ) );
171  oreg->add( newBinaryOperator( ">=", std::greater_equal<float>() ) );
172  oreg->add( newBinaryOperator( "==", std::equal_to<float>() ) );
173  oreg->add( newBinaryOperator( "!=", std::not_equal_to<float>() ) );
174 #endif
175 #ifndef RTT_NO_STD_TYPES
176  // strings
177  // causes memory allocation....
178  oreg->add( newBinaryOperator( "+", std::plus<std::string>() ) );
179  oreg->add( newBinaryOperator( "+", string_concatenation<int>() ) );
180  oreg->add( newBinaryOperator( "+", string_concatenation<unsigned int>() ) );
181  oreg->add( newBinaryOperator( "+", string_concatenation<double>() ) );
182  oreg->add( newBinaryOperator( "+", string_concatenation<float>() ) );
183  oreg->add( newBinaryOperator( "+", string_concatenation<bool>() ) );
184  oreg->add( newBinaryOperator( "+", string_concatenation<char>() ) );
185  oreg->add( newBinaryOperator( "==", std::equal_to<const std::string&>() ) );
186  oreg->add( newBinaryOperator( "!=", std::not_equal_to< const std::string&>() ) );
187  oreg->add( newBinaryOperator( "<", std::less<const std::string&>() ) );
188  oreg->add( newBinaryOperator( ">", std::greater<const std::string&>() ) );
189  oreg->add( newBinaryOperator( "<=", std::less_equal<std::string>() ) );
190  oreg->add( newBinaryOperator( ">=", std::greater_equal<std::string>() ) );
191 #endif
192 
193 #ifdef OS_RT_MALLOC
194  oreg->add( newBinaryOperator( "+", std::plus<rt_string>() ) );
195  oreg->add( newBinaryOperator( "+", rt_string_concatenation<int>() ) );
196  oreg->add( newBinaryOperator( "+", rt_string_concatenation<unsigned int>() ) );
197  oreg->add( newBinaryOperator( "+", rt_string_concatenation<double>() ) );
198  oreg->add( newBinaryOperator( "+", rt_string_concatenation<float>() ) );
199  oreg->add( newBinaryOperator( "+", rt_string_concatenation<bool>() ) );
200  oreg->add( newBinaryOperator( "+", rt_string_concatenation<char>() ) );
201  oreg->add( newBinaryOperator( "==", std::equal_to<const rt_string&>() ) );
202  oreg->add( newBinaryOperator( "!=", std::not_equal_to< const rt_string&>() ) );
203  oreg->add( newBinaryOperator( "<", std::less<const rt_string&>() ) );
204  oreg->add( newBinaryOperator( ">", std::greater<const rt_string&>() ) );
205  oreg->add( newBinaryOperator( "<=", std::less_equal<rt_string>() ) );
206  oreg->add( newBinaryOperator( ">=", std::greater_equal<rt_string>() ) );
207 #endif
208 
209 #ifndef ORO_EMBEDDED
210  // chars
211  oreg->add( newBinaryOperator( "==", std::equal_to<char>() ) );
212  oreg->add( newBinaryOperator( "!=", std::not_equal_to<char>() ) );
213  oreg->add( newBinaryOperator( "<", std::less<char>() ) );
214  oreg->add( newBinaryOperator( ">", std::greater<char>() ) );
215  oreg->add( newBinaryOperator( "<=", std::less_equal<char>() ) );
216  oreg->add( newBinaryOperator( ">=", std::greater_equal<char>() ) );
217 #if 0
218  // causes memory allocation....
219  oreg->add( newUnaryOperator( "-", std::negate<const std::vector<double>&>() ) );
220  oreg->add( newBinaryOperator( "*", std::multiplies<const std::vector<double>&>() ) );
221  oreg->add( newBinaryOperator( "+", std::plus<const std::vector<double>&>() ) );
222  oreg->add( newBinaryOperator( "-", std::minus<const std::vector<double>&>() ) );
223  oreg->add( newBinaryOperator( "*", multiplies3<const std::vector<double>&, double, const std::vector<double>&>() ) );
224  oreg->add( newBinaryOperator( "*", multiplies3<const std::vector<double>&, const std::vector<double>&, double>() ) );
225  oreg->add( newBinaryOperator( "/", divides3<const std::vector<double>&, const std::vector<double>&, double>() ) );
226 #endif
227 #endif
228 
229  // FlowStatus
230  oreg->add( newBinaryOperator( "==", std::equal_to<FlowStatus>() ) );
231  oreg->add( newBinaryOperator( "!=", std::not_equal_to< FlowStatus>() ) );
232  oreg->add( newBinaryOperator( "<", std::less<FlowStatus>() ) );
233  oreg->add( newBinaryOperator( ">", std::greater<FlowStatus>() ) );
234  oreg->add( newBinaryOperator( "<=", std::less_equal<FlowStatus>() ) );
235  oreg->add( newBinaryOperator( ">=", std::greater_equal<FlowStatus>() ) );
236 
237  // SendStatus
238  oreg->add( newBinaryOperator( "==", std::equal_to<SendStatus>() ) );
239  oreg->add( newBinaryOperator( "!=", std::not_equal_to< SendStatus>() ) );
240  oreg->add( newBinaryOperator( "<", std::less<SendStatus>() ) );
241  oreg->add( newBinaryOperator( ">", std::greater<SendStatus>() ) );
242  oreg->add( newBinaryOperator( "<=", std::less_equal<SendStatus>() ) );
243  oreg->add( newBinaryOperator( ">=", std::greater_equal<SendStatus>() ) );
244 
245  return true;
246  }
247 }
std::basic_ostringstream< char, std::char_traits< char >, RTT::os::rt_allocator< char > > rt_ostringstream
Real-time allocatable, dynamically-size output string stream.
Definition: rt_string.hpp:53
static shared_ptr Instance()
Returns a shared pointer to the singleton of this class.
Definition: Operators.cpp:61
std::basic_string< char, std::char_traits< char >, RTT::os::rt_allocator< char > > rt_string
Real-time allocatable, dynamically-sized string.
Definition: rt_string.hpp:50
STL namespace.
boost::shared_ptr< OperatorRepository > shared_ptr
Definition: Operators.hpp:118
virtual bool loadOperators()
Implement this method to load Scripting operators on types, such as &#39;+&#39;, &#39;*&#39;, ... ...
BinaryOperator< function > * newBinaryOperator(const char *op, function f)
helper function to create a new BinaryOperator
UnaryOperator< function > * newUnaryOperator(const char *op, function f)
helper function to create a new UnaryOperator
int operator()(T cont) const
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
int operator()(T cont) const