APM:Libraries
functor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Intel Corporation. All rights reserved.
3  *
4  * This file is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <type_traits>
21 
22 #define FUNCTOR_TYPEDEF(name, rettype, ...) \
23  typedef Functor<rettype, ## __VA_ARGS__> name
24 
25 #define FUNCTOR_DECLARE(name, rettype, ...) \
26  Functor<rettype, ## __VA_ARGS__> name
27 
28 #define FUNCTOR_BIND(obj, func, rettype, ...) \
29  Functor<rettype, ## __VA_ARGS__>::bind<std::remove_reference<decltype(*obj)>::type, func>(obj)
30 
31 #define FUNCTOR_BIND_MEMBER(func, rettype, ...) \
32  Functor<rettype, ## __VA_ARGS__>::bind<std::remove_reference<decltype(*this)>::type, func>(this)
33 
34 template <class RetType, class... Args>
35 class Functor
36 {
37 public:
38  constexpr Functor(void *obj, RetType (*method)(void *obj, Args...))
39  : _obj(obj)
40  , _method(method)
41  {
42  }
43 
44  // Allow to construct an empty Functor
45  constexpr Functor(decltype(nullptr))
46  : Functor(nullptr, nullptr) { }
47 
49  : Functor(nullptr, nullptr) { }
50 
51  // Call the method on the obj this Functor is bound to
52  RetType operator()(Args... args) const
53  {
54  return _method(_obj, args...);
55  }
56 
57  // Compare if the two Functors are calling the same method in the same
58  // object
59  inline bool operator==(const Functor<RetType, Args...>& rhs)
60  {
61  return _obj == rhs._obj && _method == rhs._method;
62  }
63 
64  // Allow to check if there's a method set in the Functor
65  explicit operator bool() const
66  {
67  return _method != nullptr;
68  }
69 
70  template<class T, RetType (T::*method)(Args...)>
71  static constexpr Functor bind(T *obj)
72  {
73  return { obj, method_wrapper<T, method> };
74  }
75 
76 private:
77  void *_obj;
78  RetType (*_method)(void *obj, Args...);
79 
80  template<class T, RetType (T::*method)(Args...)>
81  static RetType method_wrapper(void *obj, Args... args)
82  {
83  T *t = static_cast<T*>(obj);
84  return (t->*method)(args...);
85  }
86 };
static constexpr Functor bind(T *obj)
Definition: functor.h:71
constexpr Functor()
Definition: functor.h:48
static RetType method_wrapper(void *obj, Args... args)
Definition: functor.h:81
RetType(* _method)(void *obj, Args...)
Definition: functor.h:78
RetType operator()(Args... args) const
Definition: functor.h:52
bool operator==(const Functor< RetType, Args... > &rhs)
Definition: functor.h:59
#define constexpr
Definition: AP_HAL_Macros.h:16
constexpr Functor(void *obj, RetType(*method)(void *obj, Args...))
Definition: functor.h:38
void * _obj
Definition: functor.h:77
constexpr Functor(decltype(nullptr))
Definition: functor.h:45