Orocos Real-Time Toolkit  2.9.0
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  // llong stuff
147  oreg->add( newUnaryOperator( "-", std::negate<long long>() ) );
148  oreg->add( newUnaryOperator( "+", identity<long long>() ) );
149  oreg->add( newBinaryOperator( "*", std::multiplies<long long>() ) );
150  oreg->add( newBinaryOperator( "/", divides3<long long,long long,long long>() ) ); // use our own divides<> which detects div by zero
151  oreg->add( newBinaryOperator( "%", std::modulus<long long>() ) );
152  oreg->add( newBinaryOperator( "+", std::plus<long long>() ) );
153  oreg->add( newBinaryOperator( "-", std::minus<long long>() ) );
154  oreg->add( newBinaryOperator( "<", std::less<long long>() ) );
155  oreg->add( newBinaryOperator( "<=", std::less_equal<long long>() ) );
156  oreg->add( newBinaryOperator( ">", std::greater<long long>() ) );
157  oreg->add( newBinaryOperator( ">=", std::greater_equal<long long>() ) );
158  oreg->add( newBinaryOperator( "==", std::equal_to<long long>() ) );
159  oreg->add( newBinaryOperator( "!=", std::not_equal_to<long long>() ) );
160  // ullong stuff
161  oreg->add( newUnaryOperator( "+", identity<unsigned long long>() ) );
162  oreg->add( newBinaryOperator( "*", std::multiplies<unsigned long long>() ) );
163  oreg->add( newBinaryOperator( "/", divides3<unsigned long long,unsigned long long,unsigned long long>() ) ); // use our own divides<> which detects div by zero
164  oreg->add( newBinaryOperator( "%", std::modulus<unsigned long long>() ) );
165  oreg->add( newBinaryOperator( "+", std::plus<unsigned long long>() ) );
166  oreg->add( newBinaryOperator( "-", std::minus<unsigned long long>() ) );
167  oreg->add( newBinaryOperator( "<", std::less<unsigned long long>() ) );
168  oreg->add( newBinaryOperator( "<=", std::less_equal<unsigned long long>() ) );
169  oreg->add( newBinaryOperator( ">", std::greater<unsigned long long>() ) );
170  oreg->add( newBinaryOperator( ">=", std::greater_equal<unsigned long long>() ) );
171  oreg->add( newBinaryOperator( "==", std::equal_to<unsigned long long>() ) );
172  oreg->add( newBinaryOperator( "!=", std::not_equal_to<unsigned long long>() ) );
173 #endif
174  // double stuff..
175  oreg->add( newUnaryOperator( "-", std::negate<double>() ) );
176  oreg->add( newUnaryOperator( "+", identity<double>() ) );
177  oreg->add( newBinaryOperator( "*", std::multiplies<double>() ) );
178  oreg->add( newBinaryOperator( "/", std::divides<double>() ) );
179  oreg->add( newBinaryOperator( "+", std::plus<double>() ) );
180  oreg->add( newBinaryOperator( "-", std::minus<double>() ) );
181  oreg->add( newBinaryOperator( "<", std::less<double>() ) );
182  oreg->add( newBinaryOperator( "<=", std::less_equal<double>() ) );
183  oreg->add( newBinaryOperator( ">", std::greater<double>() ) );
184  oreg->add( newBinaryOperator( ">=", std::greater_equal<double>() ) );
185  oreg->add( newBinaryOperator( "==", std::equal_to<double>() ) );
186  oreg->add( newBinaryOperator( "!=", std::not_equal_to<double>() ) );
187 #ifndef ORO_EMBEDDED
188  // float stuff
189  oreg->add( newUnaryOperator( "-", std::negate<float>() ) );
190  oreg->add( newUnaryOperator( "+", identity<float>() ) );
191  oreg->add( newBinaryOperator( "*", std::multiplies<float>() ) );
192  oreg->add( newBinaryOperator( "/", std::divides<float>() ) );
193  oreg->add( newBinaryOperator( "+", std::plus<float>() ) );
194  oreg->add( newBinaryOperator( "-", std::minus<float>() ) );
195  oreg->add( newBinaryOperator( "<", std::less<float>() ) );
196  oreg->add( newBinaryOperator( "<=", std::less_equal<float>() ) );
197  oreg->add( newBinaryOperator( ">", std::greater<float>() ) );
198  oreg->add( newBinaryOperator( ">=", std::greater_equal<float>() ) );
199  oreg->add( newBinaryOperator( "==", std::equal_to<float>() ) );
200  oreg->add( newBinaryOperator( "!=", std::not_equal_to<float>() ) );
201 #endif
202 #ifndef RTT_NO_STD_TYPES
203  // strings
204  // causes memory allocation....
205  oreg->add( newBinaryOperator( "+", std::plus<std::string>() ) );
206  oreg->add( newBinaryOperator( "+", string_concatenation<int>() ) );
207  oreg->add( newBinaryOperator( "+", string_concatenation<unsigned int>() ) );
208  oreg->add( newBinaryOperator( "+", string_concatenation<double>() ) );
209  oreg->add( newBinaryOperator( "+", string_concatenation<float>() ) );
210  oreg->add( newBinaryOperator( "+", string_concatenation<bool>() ) );
211  oreg->add( newBinaryOperator( "+", string_concatenation<char>() ) );
212  oreg->add( newBinaryOperator( "==", std::equal_to<const std::string&>() ) );
213  oreg->add( newBinaryOperator( "!=", std::not_equal_to< const std::string&>() ) );
214  oreg->add( newBinaryOperator( "<", std::less<const std::string&>() ) );
215  oreg->add( newBinaryOperator( ">", std::greater<const std::string&>() ) );
216  oreg->add( newBinaryOperator( "<=", std::less_equal<std::string>() ) );
217  oreg->add( newBinaryOperator( ">=", std::greater_equal<std::string>() ) );
218 #endif
219 
220 #ifdef OS_RT_MALLOC
221  oreg->add( newBinaryOperator( "+", std::plus<rt_string>() ) );
222  oreg->add( newBinaryOperator( "+", rt_string_concatenation<int>() ) );
223  oreg->add( newBinaryOperator( "+", rt_string_concatenation<unsigned int>() ) );
224  oreg->add( newBinaryOperator( "+", rt_string_concatenation<double>() ) );
225  oreg->add( newBinaryOperator( "+", rt_string_concatenation<float>() ) );
226  oreg->add( newBinaryOperator( "+", rt_string_concatenation<bool>() ) );
227  oreg->add( newBinaryOperator( "+", rt_string_concatenation<char>() ) );
228  oreg->add( newBinaryOperator( "==", std::equal_to<const rt_string&>() ) );
229  oreg->add( newBinaryOperator( "!=", std::not_equal_to< const rt_string&>() ) );
230  oreg->add( newBinaryOperator( "<", std::less<const rt_string&>() ) );
231  oreg->add( newBinaryOperator( ">", std::greater<const rt_string&>() ) );
232  oreg->add( newBinaryOperator( "<=", std::less_equal<rt_string>() ) );
233  oreg->add( newBinaryOperator( ">=", std::greater_equal<rt_string>() ) );
234 #endif
235 
236 #ifndef ORO_EMBEDDED
237  // chars
238  oreg->add( newBinaryOperator( "==", std::equal_to<char>() ) );
239  oreg->add( newBinaryOperator( "!=", std::not_equal_to<char>() ) );
240  oreg->add( newBinaryOperator( "<", std::less<char>() ) );
241  oreg->add( newBinaryOperator( ">", std::greater<char>() ) );
242  oreg->add( newBinaryOperator( "<=", std::less_equal<char>() ) );
243  oreg->add( newBinaryOperator( ">=", std::greater_equal<char>() ) );
244 #if 0
245  // causes memory allocation....
246  oreg->add( newUnaryOperator( "-", std::negate<const std::vector<double>&>() ) );
247  oreg->add( newBinaryOperator( "*", std::multiplies<const std::vector<double>&>() ) );
248  oreg->add( newBinaryOperator( "+", std::plus<const std::vector<double>&>() ) );
249  oreg->add( newBinaryOperator( "-", std::minus<const std::vector<double>&>() ) );
250  oreg->add( newBinaryOperator( "*", multiplies3<const std::vector<double>&, double, const std::vector<double>&>() ) );
251  oreg->add( newBinaryOperator( "*", multiplies3<const std::vector<double>&, const std::vector<double>&, double>() ) );
252  oreg->add( newBinaryOperator( "/", divides3<const std::vector<double>&, const std::vector<double>&, double>() ) );
253 #endif
254 #endif
255 
256  // FlowStatus
257  oreg->add( newBinaryOperator( "==", std::equal_to<FlowStatus>() ) );
258  oreg->add( newBinaryOperator( "!=", std::not_equal_to< FlowStatus>() ) );
259  oreg->add( newBinaryOperator( "<", std::less<FlowStatus>() ) );
260  oreg->add( newBinaryOperator( ">", std::greater<FlowStatus>() ) );
261  oreg->add( newBinaryOperator( "<=", std::less_equal<FlowStatus>() ) );
262  oreg->add( newBinaryOperator( ">=", std::greater_equal<FlowStatus>() ) );
263 
264  // WriteStatus
265  oreg->add( newBinaryOperator( "==", std::equal_to<WriteStatus>() ) );
266  oreg->add( newBinaryOperator( "!=", std::not_equal_to< WriteStatus>() ) );
267  oreg->add( newBinaryOperator( "<", std::less<WriteStatus>() ) );
268  oreg->add( newBinaryOperator( ">", std::greater<WriteStatus>() ) );
269  oreg->add( newBinaryOperator( "<=", std::less_equal<WriteStatus>() ) );
270  oreg->add( newBinaryOperator( ">=", std::greater_equal<WriteStatus>() ) );
271 
272  // SendStatus
273  oreg->add( newBinaryOperator( "==", std::equal_to<SendStatus>() ) );
274  oreg->add( newBinaryOperator( "!=", std::not_equal_to< SendStatus>() ) );
275  oreg->add( newBinaryOperator( "<", std::less<SendStatus>() ) );
276  oreg->add( newBinaryOperator( ">", std::greater<SendStatus>() ) );
277  oreg->add( newBinaryOperator( "<=", std::less_equal<SendStatus>() ) );
278  oreg->add( newBinaryOperator( ">=", std::greater_equal<SendStatus>() ) );
279 
280  return true;
281  }
282 }
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:52
int operator()(T cont) const