Orocos Real-Time Toolkit  2.9.0
mystd.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Wed Jul 28 09:08:56 CEST 2004 mystd.hpp
3 
4  mystd.hpp - description
5  -------------------
6  begin : Wed July 28 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.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 #ifndef ORO_MYSTD_HPP
39 #define ORO_MYSTD_HPP
40 
41 #include <boost/type_traits/add_reference.hpp>
42 #include <boost/type_traits/add_const.hpp>
43 #include <boost/type_traits/is_same.hpp>
44 #include <boost/type_traits/is_void.hpp>
45 #include <boost/type_traits/remove_reference.hpp>
46 #include <boost/type_traits/remove_const.hpp>
47 #include <boost/mpl/bool.hpp>
48 #include <boost/mpl/logical.hpp>
49 #include <boost/utility.hpp>
50 #include <functional>
51 #include <algorithm>
52 #include <vector>
53 
54 // here we define some generally useful template stuff that is missing
55 // from the STL..
56 namespace RTT { namespace internal {
57 
58  // combines remove_reference and remove_const
59  template<typename T>
60  struct remove_cr
61  {
62  typedef typename boost::remove_const<
63  typename boost::remove_reference<T>::type>::type type;
64  };
65 
66  template<typename T>
68  :public boost::mpl::false_
69  {};
70 
71  template<typename T>
72  struct is_pure_reference<T&>
73  :public boost::mpl::true_
74  {};
75 
76  template<typename T>
77  struct is_pure_reference<T const&>
78  :public boost::mpl::false_
79  {};
80 
81  template<typename iter>
82  static void delete_all( iter a, iter b )
83  {
84  for ( ; a < b; a++ )
85  delete *a;
86  }
87 
88 
89  // SGI extension, does not seem present in current GNU STL
90  // implementation...
91 
92  template<typename T>
93  struct select1st
94  : public std::unary_function<T, typename T::first_type>
95  {
96  typename T::first_type operator()( const T& p ) {
97  return p.first;
98  }
99  };
100 
101  template<typename T>
102  struct select2nd
103  : public std::unary_function<T, typename T::second_type>
104  {
105  typename T::second_type operator()( const T& p ) {
106  return p.second;
107  }
108  };
109 
110 #if 0
111  // Alternative implementations, return const ref.
112  template<typename PairT>
113  class select1st
114  : public std::unary_function<PairT, typename PairT::first_type>
115  {
116  typedef typename PairT::first_type ResultT;
117  public:
118  const ResultT& operator()( const PairT& p )
119  {
120  return p.first;
121  };
122  };
123 
124  template<typename PairT>
125  class select2nd
126  : public std::unary_function<PairT, typename PairT::second_type>
127  {
128  typedef typename PairT::second_type ResultT;
129  public:
130  const ResultT& operator()( const PairT& p )
131  {
132  return p.second;
133  };
134  };
135 #endif
136 
137 
138  // my own handy little extension..
139  template<typename MapT>
140  std::vector<typename MapT::mapped_type> values( const MapT& map )
141  {
142  std::vector<typename MapT::mapped_type> ret;
143  ret.reserve( map.size() );
144  std::transform( map.begin(), map.end(),
145  std::back_inserter( ret ),
147  return ret;
148  }
149 
150  template<typename MapT>
151  std::vector<typename MapT::key_type> keys( const MapT& map )
152  {
153  std::vector<typename MapT::key_type> ret;
154  ret.reserve( map.size() );
155  std::transform( map.begin(), map.end(),
156  std::back_inserter( ret ),
158  return ret;
159  }
160 }
161 }
162 
163 namespace std
164 {
165  // must be in std namespace.
166  // STL specialisations for const references : Add others if necessary.
167  template <class _Tp>
168  struct equal_to< const _Tp& >
169  : public binary_function<const _Tp&, const _Tp& ,bool>
170  {
171  bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
172  };
173 
175  template <class _Tp>
176  struct not_equal_to<const _Tp&>
177  : public binary_function<const _Tp&, const _Tp&, bool>
178  {
179  bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
180  };
181 
183  template <class _Tp>
184  struct greater<const _Tp&>
185  : public binary_function<const _Tp&,const _Tp&,bool>
186  {
187  bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
188  };
189 
191  template <class _Tp>
192  struct less<const _Tp&>
193  : public binary_function<const _Tp&,const _Tp&,bool>
194  {
195  bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
196  };
197 
198  // Ternary functions.
199  template<class Arg1T, class Arg2T, class Arg3T, class ResultT >
201  {
202  typedef ResultT result_type;
203  typedef Arg1T first_argument_type;
204  typedef Arg2T second_argument_type;
205  typedef Arg3T third_argument_type;
206  };
207 }
208 
209 // STL extensions, some are SGI extensions, others are my own..
210 namespace RTT
211 {namespace internal {
212 
213  template<typename T>
214  struct identity
215  : public std::unary_function<T, T>
216  {
217  const T& operator()( const T& t ) const
218  {
219  return t;
220  }
221  };
222 
223  // ternary
224  template<typename ResultT, typename Arg1T, typename Arg2T, typename Arg3T>
226  {
227  typedef ResultT (Signature)( Arg1T, Arg2T, Arg3T );
228 
229  ResultT (*fun)( Arg1T, Arg2T, Arg3T );
230 
231  typedef ResultT result_type;
232  typedef Arg1T first_argument_type;
233  typedef Arg2T second_argument_type;
234  typedef Arg3T third_argument_type;
235  pointer_to_ternary_function( ResultT (*f)(Arg1T, Arg2T, Arg3T ) )
236  : fun( f )
237  {
238  }
239  ResultT operator()( Arg1T a, Arg2T b, Arg3T c ) const
240  {
241  return (*fun)( a, b, c );
242  }
243  };
244 
245  template<typename ResultT, typename Arg1T, typename Arg2T, typename Arg3T>
247  ptr_fun( ResultT (*fun)( Arg1T, Arg2T, Arg3T ) )
248  {
250  }
251 
252 
253  // sixary
254  template<typename ResultT, typename Arg1T, typename Arg2T, typename Arg3T,
255  typename Arg4T, typename Arg5T, typename Arg6T >
257  {
258  typedef ResultT (Signature)( Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T );
259  ResultT (*fun)( Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T );
260  typedef ResultT result_type;
261  typedef Arg1T first_argument_type;
262  typedef Arg2T second_argument_type;
263  typedef Arg3T third_argument_type;
264  typedef Arg4T fourth_argument_type;
265  typedef Arg5T fifth_argument_type;
266  typedef Arg6T sixth_argument_type;
267  pointer_to_sixary_function( ResultT (*f)(Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T ) )
268  : fun( f )
269  {
270  }
271  ResultT operator()( Arg1T a, Arg2T b, Arg3T c, Arg4T d, Arg5T e, Arg6T f ) const
272  {
273  return (*fun)( a, b, c, d, e, f );
274  }
275  };
276 
277  template<typename ResultT, typename Arg1T, typename Arg2T, typename Arg3T,
278  typename Arg4T, typename Arg5T, typename Arg6T >
280  ptr_fun( ResultT (*fun)( Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T ) )
281  {
283  }
284 
285 
286 #if 0
287 
288  // the STL lacks a functor multiplying two objects of distinct
289  // types.. multiplies<T> requires that a and b are both of type
290  // T when calling operator()(a,b). So I wrote my own replacement.
291  // This relies on the GCC typeof C++ extension
292  template<typename A, typename B>
293  struct multiplies
294  {
295  typedef typeof( A() * B() ) result_type;
296  typedef A first_argument_type;
297  typedef B second_argument_type;
298 
299  result_type operator()( A a, B b ) const
300  {
301  return a*b;
302  }
303  };
304  template<typename A, typename B>
305  struct divides
306  {
307  typedef typeof( A() / B() ) result_type;
308  typedef A first_argument_type;
309  typedef B second_argument_type;
310 
311  result_type operator()( A a, B b ) const
312  {
313  return a/b;
314  }
315  };
316 #else
317  template<typename R, typename A, typename B>
318  struct multiplies3
319  {
320  typedef R result_type;
323 
324  result_type operator()( A a, B b ) const
325  {
326  return a*b;
327  }
328  };
329  template<typename R, typename A, typename B>
330  struct divides3
331  {
332  typedef R result_type;
335 
336  result_type operator()( A a, B b ) const
337  {
338  return a/b;
339  }
340  };
341  template<>
342  struct divides3<int, int, int>
343  {
344  typedef int result_type;
345  typedef int first_argument_type;
346  typedef int second_argument_type;
347 
348  result_type operator()( int a, int b ) const
349  {
350  // integer division by zero will throw a fatal
351  // exception, aborting the program (SIGFPE). This is
352  // unacceptable, the problem is however that
353  // we can not signal an erronous expression in Orocos.
354  // we propagate zero instead.
355  return b == 0 ? 0 : a/b;
356  }
357  };
358  template<>
359  struct divides3<unsigned int, unsigned int, unsigned int>
360  {
361  typedef unsigned int result_type;
362  typedef unsigned int first_argument_type;
363  typedef unsigned int second_argument_type;
364 
365  result_type operator()( unsigned int a, unsigned int b ) const
366  {
367  // integer division by zero will throw a fatal
368  // exception, aborting the program (SIGFPE). This is
369  // unacceptable, the problem is however that
370  // we can not signal an erronous expression in Orocos.
371  // we propagate zero instead.
372  return b == 0 ? 0 : a/b;
373  }
374  };
375  template<>
376  struct divides3<long long, long long, long long>
377  {
378  typedef long long result_type;
379  typedef long long first_argument_type;
380  typedef long long second_argument_type;
381 
382  result_type operator()( long long a, long long b ) const
383  {
384  // integer division by zero will throw a fatal
385  // exception, aborting the program (SIGFPE). This is
386  // unacceptable, the problem is however that
387  // we can not signal an erronous expression in Orocos.
388  // we propagate zero instead.
389  return b == 0 ? 0 : a/b;
390  }
391  };
392  template<>
393  struct divides3<unsigned long long, unsigned long long, unsigned long long>
394  {
395  typedef unsigned long long result_type;
396  typedef unsigned long long first_argument_type;
397  typedef unsigned long long second_argument_type;
398 
399  result_type operator()( unsigned long long a, unsigned long long b ) const
400  {
401  // integer division by zero will throw a fatal
402  // exception, aborting the program (SIGFPE). This is
403  // unacceptable, the problem is however that
404  // we can not signal an erronous expression in Orocos.
405  // we propagate zero instead.
406  return b == 0 ? 0 : a/b;
407  }
408  };
409  template<typename R, typename A, typename B>
410  struct adds3
411  {
412  typedef R result_type;
415 
416  result_type operator()( A a, B b ) const
417  {
418  return a+b;
419  }
420  };
421  template<typename R, typename A, typename B>
422  struct subs3
423  {
424  typedef R result_type;
427 
428  result_type operator()( A a, B b ) const
429  {
430  return a-b;
431  }
432  };
433 #endif
434 }} // namespace RTT
435 
436 
437 
438 #endif
result_type operator()(A a, B b) const
Definition: mystd.hpp:324
T::first_type operator()(const T &p)
Definition: mystd.hpp:96
pointer_to_ternary_function< ResultT, Arg1T, Arg2T, Arg3T > ptr_fun(ResultT(*fun)(Arg1T, Arg2T, Arg3T))
Definition: mystd.hpp:247
result_type operator()(unsigned int a, unsigned int b) const
Definition: mystd.hpp:365
Arg1T first_argument_type
Definition: mystd.hpp:203
STL namespace.
bool operator()(const _Tp &__x, const _Tp &__y) const
Definition: mystd.hpp:187
ResultT operator()(Arg1T a, Arg2T b, Arg3T c, Arg4T d, Arg5T e, Arg6T f) const
Definition: mystd.hpp:271
Arg3T third_argument_type
Definition: mystd.hpp:205
result_type operator()(A a, B b) const
Definition: mystd.hpp:428
result_type operator()(int a, int b) const
Definition: mystd.hpp:348
pointer_to_ternary_function(ResultT(*f)(Arg1T, Arg2T, Arg3T))
Definition: mystd.hpp:235
T::second_type operator()(const T &p)
Definition: mystd.hpp:105
Arg2T second_argument_type
Definition: mystd.hpp:204
bool operator()(const _Tp &__x, const _Tp &__y) const
Definition: mystd.hpp:179
result_type operator()(A a, B b) const
Definition: mystd.hpp:336
result_type operator()(unsigned long long a, unsigned long long b) const
Definition: mystd.hpp:399
bool operator()(const _Tp &__x, const _Tp &__y) const
Definition: mystd.hpp:195
bool operator()(const _Tp &__x, const _Tp &__y) const
Definition: mystd.hpp:171
std::vector< typename MapT::key_type > keys(const MapT &map)
Definition: mystd.hpp:151
result_type operator()(long long a, long long b) const
Definition: mystd.hpp:382
const T & operator()(const T &t) const
Definition: mystd.hpp:217
pointer_to_sixary_function(ResultT(*f)(Arg1T, Arg2T, Arg3T, Arg4T, Arg5T, Arg6T))
Definition: mystd.hpp:267
std::vector< typename MapT::mapped_type > values(const MapT &map)
Definition: mystd.hpp:140
boost::remove_const< typename boost::remove_reference< typename function::result_type >::type >::type type
Definition: mystd.hpp:63
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
result_type operator()(A a, B b) const
Definition: mystd.hpp:416
ResultT operator()(Arg1T a, Arg2T b, Arg3T c) const
Definition: mystd.hpp:239