Main MRPT website > C++ reference for MRPT 1.5.9
metaprogramming.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef metaprogramming_H
10 #define metaprogramming_H
11 
12 #include <mrpt/utils/CObject.h>
14 
15 namespace mrpt
16 {
17  namespace utils
18  {
19  class CStream;
20 
21  /** A set of utility objects for metaprogramming with STL algorithms.
22  * \ingroup stlext_grp
23  */
24  namespace metaprogramming
25  {
26  /** \addtogroup stlext_grp
27  * @{ */
28 
29  /** An object for deleting pointers (intended for STL algorithms) */
30  struct ObjectDelete {
31  template<typename T>
32  inline void operator()(const T *ptr) {
33  delete ptr;
34  }
35  };
36 
37  /** A function which deletes a container of pointers. */
38  template<typename T> inline void DeleteContainer(T &container) {
39  for_each(container.begin(),container.end(),ObjectDelete());
40  }
41 
42  //NOTE: when using this algorithm with for_each, replace the whole line with:
43  // for_each(bypassPointer(<beginning iterator>),bypassPointer(<ending iterator>),mem_fun_ref(&<NonPointerBaseClass>::clear)).
44  /** An object for clearing an object (invokes its method "->clear()") given a pointer or smart-pointer, intended for being used in STL algorithms. */
45  struct ObjectClear {
46  template<typename T>
47  inline void operator()(T &ptr) {
48  if (ptr) ptr->clear();
49  }
50  };
51 
52  //NOTE: replace this with mem_fun_ref(&<BaseClass>::clear).
53  /** An object for clearing an object (invokes its method ".clear()") given a pointer or smart-pointer, intended for being used in STL algorithms. */
54  struct ObjectClear2 {
55  template<typename T>
56  inline void operator()(T &ptr){
57  ptr.clear();
58  }
59  };
60 
61  /** An object for clearing an object->second (invokes its method "clear()") given a pointer or smart-pointer, intended for being used in STL algorithms. */
63  template<typename T>
64  inline void operator()(T obj) {
65  obj.second.clear();
66  }
67  };
68 
69  /** An object for transforming between types/classes, intended for being used in STL algorithms.
70  * Example of usage:
71  * \code
72  * vector<int> v1(10); // Input
73  * vector<double> v2(10); // Output
74  * std::transform(v1.begin(),v1.end(), v2.begin(), ObjectConvert<double> );
75  * \endcode
76  */
77  template <typename TARGET_TYPE>
78  struct ObjectConvert {
79  template<typename T>
80  inline TARGET_TYPE operator()(const T &val) {
81  return TARGET_TYPE(val);
82  }
83  };
84 
85  //NOTE: replace with mem_fun_ref(&CSerializable::make_unique)
86  /** An object for making smart pointers unique (ie, making copies if necessary), intended for being used in STL algorithms. */
89  typedef void result_type;
90  inline void operator()(mrpt::utils::CObjectPtr &ptr) {
91  ptr.make_unique();
92  }
93  };
94 
95  /** An object for making smart pointers unique (ie, making copies if necessary), intended for being used in STL algorithms. */
97  template <typename T>
98  inline void operator()(T &ptr) {
99  ptr.first.make_unique();
100  ptr.second.make_unique();
101  }
102  };
103 
104  //NOTE: replace with mem_fun_ref(&CSerializable::clear_unique)
105  /** An object for making smart pointers unique (ie, making copies if necessary), intended for being used in STL algorithms. */
106  template <typename T>
108  typedef T argument_type;
109  typedef void result_type;
110  inline void operator()(T &ptr)
111  {
112  ptr.clear_unique();
113  }
114  };
115 
116  /** Behaves like std::copy but allows the source and target iterators to be of different types through static typecasting.
117  * \note As in std::copy, the target iterator must point to the first "slot" where to put the first transformed element, and sufficient space must be allocated in advance.
118  * \sa copy_container_typecasting
119  */
120  template<typename it_src, typename it_dst>
121  inline void copy_typecasting(it_src first, it_src last,it_dst target)
122  {
123  for (it_src i=first; i!=last ; ++i,++target)
124  *target = static_cast<typename it_dst::value_type>(*i);
125  }
126 
127  /** Copy all the elements in a container (vector, deque, list) into a different one performing the appropriate typecasting.
128  * The target container is automatically resized to the appropriate size, and previous contents are lost.
129  * This can be used to assign std::vector's of different types:
130  * \code
131  * std::vector<int> vi(10);
132  * std::vector<float> vf;
133  * vf = vi; // Compiler error
134  * mrpt::utils::metaprogramming::copy_container_typecasting(v1,vf); // Ok
135  * \endcode
136  */
137  template<typename src_container, typename dst_container>
138  inline void copy_container_typecasting(const src_container &src, dst_container &trg)
139  {
140  trg.resize( src.size() );
141  typename src_container::const_iterator i = src.begin();
142  typename src_container::const_iterator last = src.end();
143  typename dst_container::iterator target = trg.begin();
144  for ( ; i!=last ; ++i,++target)
145  *target = static_cast<typename dst_container::value_type>(*i);
146  }
147 
148  /** This class bypasses pointer access in iterators to pointers, thus allowing
149  * the use of algorithms that expect an object of class T with containers of T*.
150  * Although it may be used directly, use the bypassPointer function for better
151  * results and readability (since it most probably won't require template
152  * arguments).
153  */
154  template<typename T,typename U> class MemoryBypasserIterator {
155  private:
157  public:
158  typedef typename T::iterator_category iterator_category;
159  typedef U value_type;
160  typedef typename T::difference_type difference_type;
161  typedef U *pointer;
162  typedef U &reference;
163  inline MemoryBypasserIterator(const T &bi):baseIterator(bi) {}
164  inline reference operator*() {
165  return *(*baseIterator);
166  }
168  ++baseIterator;
169  return *this;
170  }
172  MemoryBypasserIterator it=*this;
173  ++baseIterator;
174  return it;
175  }
177  --baseIterator;
178  return *this;
179  }
181  MemoryBypasserIterator it=*this;
182  --baseIterator;
183  return *this;
184  }
186  baseIterator+=off;
187  return *this;
188  }
190  return (MemoryBypasserIterator<T,U>(*this))+=off;
191  }
193  baseIterator-=off;
194  return *this;
195  }
197  return (MemoryBypasserIterator<T,U>(*this))-=off;
198  }
200  return baseIterator-it.baseIterator;
201  }
203  return *(this+off);
204  }
205  inline bool operator==(const MemoryBypasserIterator<T,U> &i) const {
206  return baseIterator==i.baseIterator;
207  }
208  inline bool operator!=(const MemoryBypasserIterator<T,U> &i) const {
209  return baseIterator!=i.baseIterator;
210  }
211  inline bool operator<(const MemoryBypasserIterator<T,U> &i) const {
212  return baseIterator<i.baseIterator;
213  }
214  };
215 
216  /** Sintactic sugar for MemoryBypasserIterator.
217  * For example, having the following declarations:
218  * vector<double *> vec;
219  * void modifyVal(double &v);
220  * The following sentence is not legal:
221  * for_each(vec.begin(),vec.end(),&modifyVal)
222  * But this one is:
223  * for_each(bypassPointer(vec.begin()),bypassPointer(vec.end()),&modifyVal)
224  */
225  template<typename U,typename T> inline MemoryBypasserIterator<T,U> bypassPointer(const T &baseIterator) {
226  return MemoryBypasserIterator<T,U>(baseIterator);
227  }
228 
229  /** This template encapsulates a binary member function and a single object into a
230  * function expecting the two parameters of the member function.
231  * Don't use directly. Use the wrapMember function instead to avoid explicit
232  * template instantiation.
233  */
234  template<typename T,typename U1,typename U2,typename V> class BinaryMemberFunctionWrapper {
235  private:
236  typedef T (V::*MemberFunction)(U1,U2);
237  V &obj;
239  public:
242  typedef T result_type;
244  inline T operator()(U1 p1,U2 p2) {
245  return (obj.*func)(p1,p2);
246  }
247  };
248  /** This template encapsulates an unary member function and a single object into a
249  * function expecting the parameter of the member function.
250  * Don't use directly. Use the wrapMember function instead.
251  */
252  template<typename T,typename U,typename V> class UnaryMemberFunctionWrapper {
253  private:
254  typedef T (V::*MemberFunction)(U);
255  V &obj;
257  public:
258  typedef U argument_type;
259  typedef T result_type;
261  inline T operator()(U p) {
262  return (obj.*func)(p);
263  }
264  };
265  /** This template encapsulates a member function without arguments and a single
266  * object into a function.
267  * Don't use directly. Use the wrapMember function instead.
268  */
269  template<typename T,typename V> class MemberFunctionWrapper {
270  private:
271  typedef T (V::*MemberFunction)(void);
272  V &obj;
274  public:
276  inline T operator()() {
277  return (obj.*func)();
278  }
279  };
280  /** This function creates a function from an object and a member function.
281  * It has three overloads, for zero, one and two parameters in the function.
282  */
283  template<typename T,typename U1,typename U2,typename V> inline BinaryMemberFunctionWrapper<T,U1,U2,V> wrapMember(V &obj,T (V::*fun)(U1,U2)) {
285  }
286  template<typename T,typename U,typename V> inline UnaryMemberFunctionWrapper<T,U,V> wrapMember(V &obj,T (V::*fun)(U)) {
288  }
289  template<typename T,typename V> inline MemberFunctionWrapper<T,V> wrapMember(V &obj,T (V::*fun)(void)) {
290  return MemberFunctionWrapper<T,V>(obj,fun);
291  }
292 
293  /** Equivalent of std::bind1st for functions with non-const arguments.
294  */
295  template<typename Op> class NonConstBind1st {
296  private:
297  Op &op;
298  typename Op::first_argument_type &val;
299  public:
300  typedef typename Op::second_argument_type argument_type;
301  typedef typename Op::result_type result_type;
302  NonConstBind1st(Op &o,typename Op::first_argument_type &t):op(o),val(t) {}
304  return op(val,s);
305  }
306  };
307  /** Use this function instead of directly calling NonConstBind1st.
308  */
309  template<typename Op> inline NonConstBind1st<Op> nonConstBind1st(Op &o,typename Op::first_argument_type &t) {
310  return NonConstBind1st<Op>(o,t);
311  }
312  /** Equivalent of std::bind2nd for functions with non-const arguments.
313  */
314  template<typename Op> class NonConstBind2nd {
315  private:
316  Op &op;
317  typename Op::second_argument_type &val;
318  public:
319  typedef typename Op::first_argument_type argument_type;
320  typedef typename Op::result_type result_type;
321  NonConstBind2nd(Op &o,typename Op::second_argument_type &t):op(o),val(t) {}
323  return op(f,val);
324  }
325  };
326  /** Do not directly use the NonConstBind2nd class directly. Use this function.
327  */
328  template<typename Op> inline NonConstBind2nd<Op> nonConstBind2nd(Op &o,typename Op::second_argument_type &t) {
329  return NonConstBind2nd<Op>(o,t);
330  }
331 
332  /** @} */ // end of grouping
333 
334  } // end metaprogramming
335  } // End of namespace
336 } // end of namespace
337 #endif
NonConstBind2nd< Op > nonConstBind2nd(Op &o, typename Op::second_argument_type &t)
Do not directly use the NonConstBind2nd class directly.
MemoryBypasserIterator< T, U > operator++(int)
GLdouble GLdouble t
Definition: glext.h:3610
An object for making smart pointers unique (ie, making copies if necessary), intended for being used ...
An object for making smart pointers unique (ie, making copies if necessary), intended for being used ...
MemoryBypasserIterator< T, U > & operator++()
This class bypasses pointer access in iterators to pointers, thus allowing the use of algorithms that...
MemoryBypasserIterator< T, U > & operator+=(difference_type off)
Equivalent of std::bind1st for functions with non-const arguments.
GLint * first
Definition: glext.h:3703
Scalar * iterator
Definition: eigen_plugins.h:23
bool operator!=(const MemoryBypasserIterator< T, U > &i) const
const Scalar * const_iterator
Definition: eigen_plugins.h:24
result_type operator()(argument_type &f)
GLdouble s
Definition: glext.h:3602
GLuint src
Definition: glext.h:6303
NonConstBind1st< Op > nonConstBind1st(Op &o, typename Op::first_argument_type &t)
Use this function instead of directly calling NonConstBind1st.
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
void DeleteContainer(T &container)
A function which deletes a container of pointers.
An object for clearing an object (invokes its method ".clear()") given a pointer or smart-pointer...
MemoryBypasserIterator< T, U > & operator-=(difference_type off)
bool operator==(const MemoryBypasserIterator< T, U > &i) const
BinaryMemberFunctionWrapper< T, U1, U2, V > wrapMember(V &obj, T(V::*fun)(U1, U2))
This function creates a function from an object and a member function.
This template encapsulates a member function without arguments and a single object into a function...
reference operator[](difference_type off) const
MemoryBypasserIterator< T, U > operator--(int)
MemoryBypasserIterator< T, U > bypassPointer(const T &baseIterator)
Sintactic sugar for MemoryBypasserIterator.
int val
Definition: mrpt_jpeglib.h:953
NonConstBind2nd(Op &o, typename Op::second_argument_type &t)
MemoryBypasserIterator< T, U > operator+(difference_type off) const
MemoryBypasserIterator< T, U > operator-(difference_type off) const
difference_type operator-(const MemoryBypasserIterator< T, U > &it) const
An object for clearing an object (invokes its method "->clear()") given a pointer or smart-pointer...
Equivalent of std::bind2nd for functions with non-const arguments.
An object for making smart pointers unique (ie, making copies if necessary), intended for being used ...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void copy_typecasting(it_src first, it_src last, it_dst target)
Behaves like std::copy but allows the source and target iterators to be of different types through st...
An object for clearing an object->second (invokes its method "clear()") given a pointer or smart-poin...
void operator()(mrpt::utils::CObjectPtr &ptr)
An object for transforming between types/classes, intended for being used in STL algorithms.
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
NonConstBind1st(Op &o, typename Op::first_argument_type &t)
MemoryBypasserIterator< T, U > & operator--()
An object for deleting pointers (intended for STL algorithms)
This template encapsulates an unary member function and a single object into a function expecting the...
GLfloat GLfloat p
Definition: glext.h:5587
This template encapsulates a binary member function and a single object into a function expecting the...
void copy_container_typecasting(const src_container &src, dst_container &trg)
Copy all the elements in a container (vector, deque, list) into a different one performing the approp...
result_type operator()(argument_type &s)



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020