MIRA
GetterSetter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_GETTERSETTER_H_
49 #define _MIRA_GETTERSETTER_H_
50 
51 #include <type_traits>
52 
53 #ifndef Q_MOC_RUN
54 #include <boost/function.hpp>
55 #endif
56 
57 #include <utils/Bind.h>
60 
61 namespace mira {
62 
64 
86 template<typename T>
87 class Getter
88 {
89 public:
90 
91  typedef T value_type;
92 
93 public:
94 
95  Getter(boost::function<value_type ()> f) : fn(f) {}
96 
98  value_type get() const { return fn(); }
99 
104  operator value_type() const { return get(); }
105 
106  template<typename Reflector>
107  void reflect(Reflector& r)
108  {
109  value_type tmp = fn();
110  r.delegate(tmp, REFLECT_CTRLFLAG_TEMP_TRACKING);
111  }
112 
113 private:
114  boost::function<value_type ()> fn;
115 };
116 
118 
119 template <typename T, typename SerializerTag>
120 class IsTransparentSerializable<Getter<T>,SerializerTag> : public std::true_type {};
121 
135 template<typename T>
136 Getter<T> getter(T (*f)())
137 {
138  return Getter<T>(f);
139 }
140 
154 template<typename T>
155 Getter<T> getter(const T& (*f)())
156 {
157  return Getter<T>(f);
158 }
159 
175 template<typename T, typename Class>
176 Getter<T> getter( T (Class::*f) (), Class* obj)
177 {
178  return Getter<T>( boost::bind( f, boost::ref(*obj) ) );
179 }
180 
196 template<typename T, typename Class>
197 Getter<T> getter( const T& (Class::*f) (), Class* obj)
198 {
199  return Getter<T>( boost::bind( f, boost::ref(*obj) ) );
200 }
201 
202 
218 template<typename T, typename Class>
219 Getter<T> getter( T (Class::*f) () const, Class* obj)
220 {
221  return Getter<T>( boost::bind( f, boost::ref(*obj) ) );
222 }
223 
239 template<typename T, typename Class>
240 Getter<T> getter( const T& (Class::*f) () const, Class* obj)
241 {
242  return Getter<T>( boost::bind( f, boost::ref(*obj) ) );
243 }
244 
254 template<typename T, typename LambdaFn>
255 Getter<T> getter(LambdaFn fn)
256 {
257  return Getter<T>(fn);
258 }
259 
270 template<typename T>
271 Getter<T> getter(boost::function<T ()> f)
272 {
273  return Getter<T>(f);
274 }
275 
277 
279 template <typename T, typename TObject>
280 class GetterWithObject
281 {
282 public:
283 
284  GetterWithObject(boost::function<T (const TObject&)> f, const TObject& obj) :
285  fn(f), object(obj) {}
286 
287 public:
288 
289  T operator()() { return fn(object); }
290 
291 private:
292 
293  boost::function<T (const TObject&)> fn;
294  const TObject& object;
295 };
296 
298 
299 
327 template<typename T, typename TObject>
328 Getter<T> getter(T (*f)(const TObject&), const TObject& object)
329 {
330  return Getter<T>(GetterWithObject<T,TObject>(f,object));
331 }
332 
340 template<typename T, typename TObject>
341 Getter<T> getter(T (*f)(TObject), const TObject& object)
342 {
343  return Getter<T>(GetterWithObject<T,TObject>(f,object));
344 }
345 
353 template<typename T, typename TObject>
354 Getter<T> getter(boost::function<T(const TObject&)> f, const TObject& object)
355 {
356  return Getter<T>(GetterWithObject<T,TObject>(f,object));
357 }
358 
366 template<typename T, typename TObject>
367 Getter<T> getter(boost::function<T(TObject)> f, const TObject& object)
368 {
369  return Getter<T>(GetterWithObject<T,TObject>(f,object));
370 }
371 
372 
394 template<typename T>
395 class Setter
396 {
397 public:
398  typedef T value_type;
399 
400 public:
401  Setter(boost::function<void (const value_type&)> f) : fn(f) {}
402 
404  void set(const value_type& value) { fn(value); }
405 
407  Setter& operator=(const value_type& value)
408  {
409  set(value);
410  return *this;
411  }
412 
413  template<typename Reflector>
414  void reflect(Reflector& r)
415  {
416  value_type tmp;
417  r.delegate(tmp, REFLECT_CTRLFLAG_TEMP_TRACKING);
418  set(tmp);
419  }
420 
421 private:
422 
423  boost::function<void (const value_type&)> fn;
424 };
425 
426 template <typename T, typename SerializerTag>
427 class IsTransparentSerializable<Setter<T>,SerializerTag> : public std::true_type {};
428 
442 template<typename T>
443 Setter<T> setter(void (*f)(const T&))
444 {
445  return Setter<T>(f);
446 }
447 
461 template<typename T>
462 Setter<T> setter(void (*f)(T))
463 {
464  return Setter<T>(f);
465 }
466 
476 template<typename T, typename LambdaFn>
477 Setter<T> setter(LambdaFn fn)
478 {
479  return Setter<T>(fn);
480 }
481 
495 template<typename T, typename Class>
496 Setter<T> setter( void (Class::*f) (const T&), Class* obj)
497 {
498  return Setter<T>( boost::bind( f, boost::ref(*obj), _1 ) );
499 }
500 
516 template<typename T, typename Class>
517 Setter<T> setter( void (Class::*f) (T), Class* obj)
518 {
519  return Setter<T>( boost::bind( f, boost::ref(*obj), _1 ) );
520 }
521 
522 
533 template<typename T>
534 Setter<T> setter(boost::function<void (const T&)> f)
535 {
536  return Setter<T>(f);
537 }
538 
540 template <typename T, typename TObject>
541 class SetterWithObject
542 {
543 public:
544 
545  SetterWithObject(boost::function<TObject (const T&)> f, TObject& obj) :
546  fn(f), object(obj) {}
547 
548 public:
549 
550  void operator()(const T& value) { object = fn(value); }
551 
552 private:
553 
554  boost::function<TObject (const T&)> fn;
555  TObject& object;
556 };
558 
586 template<typename T, typename TObject>
587 Setter<T> setter(TObject (*f)(const T&), TObject& object)
588 {
589  return Setter<T>(SetterWithObject<T,TObject>(f,object));
590 }
591 
592 /*
593  * @ingroup SerializationModule
594  *
595  * Creates a Setter for a global function or static class
596  * method that additionally is assigned with a certain object.
597  * Same as above.
598  */
599 template<typename T, typename TObject>
600 Setter<T> setter(TObject (*f)(T), TObject& object)
601 {
602  return Setter<T>(SetterWithObject<T,TObject>(f,object));
603 }
604 
605 /*
606  * @ingroup SerializationModule
607  *
608  * Creates a Setter for a boost bind function
609  * that additionally is assigned with a certain object.
610  * Same as above.
611  */
612 template<typename T, typename TObject>
613 Setter<T> setter(boost::function<TObject(const T&)> f, TObject& object)
614 {
615  return Setter<T>(SetterWithObject<T,TObject>(f,object));
616 }
617 
618 /*
619  * @ingroup SerializationModule
620  *
621  * Creates a Setter for a boost bind function
622  * that additionally is assigned with a certain object.
623  * Same as above.
624  */
625 template<typename T, typename TObject>
626 Setter<T> setter(boost::function<TObject(T)> f, TObject& object)
627 {
628  return Setter<T>(SetterWithObject<T,TObject>(f,object));
629 }
630 
632 
633 } // namespace
634 
635 #endif
This object can use object tracking internally, but the object tracking system&#39;s state remains unchan...
Definition: ReflectControlFlags.h:82
Type trait that indicates whether a type should be serialized "transparently", i.e.
Definition: IsTransparentSerializable.h:81
Setter & operator=(const value_type &value)
mimic the assignment behavior of underlying type, e.g. mysetter = 1234;
Definition: GetterSetter.h:407
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Getter(boost::function< value_type()> f)
Definition: GetterSetter.h:95
Class object which supports some kind of class reflection.
Definition: Class.h:97
Setter< T > setter(void(*f)(const T &))
Creates a Setter for global or static class methods taking the argument by const reference.
Definition: GetterSetter.h:443
Holds a boost::function object to a special setter function that must meet the signature "void method...
Definition: GetterSetter.h:395
Provides type trait that indicates whether a type should be serialized "transparently".
Flags controlling reflector behaviour.
Getter< T > getter(T(*f)())
Creates a Getter for global or static class methods returning the result by value.
Definition: GetterSetter.h:136
Setter(boost::function< void(const value_type &)> f)
Definition: GetterSetter.h:401
Wrapper for boost/bind.
Holds a boost::function object to a special getter function that must meet the signature "T method()"...
Definition: GetterSetter.h:87
T value_type
Definition: GetterSetter.h:91
void reflect(Reflector &r)
Definition: GetterSetter.h:414
T value_type
Definition: GetterSetter.h:398
void reflect(Reflector &r)
Definition: GetterSetter.h:107