Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
functioncall.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_FUNCTIONCALL_H_
19 #define ION_BASE_FUNCTIONCALL_H_
20 
21 #include <tuple>
22 
23 #include "base/macros.h"
24 #include "ion/base/allocatable.h"
25 
26 namespace ion {
27 namespace base {
28 
31 class FunctionCallBase : public Allocatable {
32  public:
33  virtual void operator()() const = 0;
34  ~FunctionCallBase() override {}
35 };
36 
52  3.14);
58 template <typename T>
60 
62 template <typename ReturnType, typename... Types>
63 class FunctionCall<ReturnType(Types...)> : public FunctionCallBase {
64  public:
66  FunctionCall(ReturnType (*func)(Types...),
67  Types&&... args) // NOLINT
68  : free_func_(func),
69  args_(std::make_tuple(std::forward<Types>(args)...)) {}
70 
72  FunctionCall(const std::function<ReturnType(Types...)>& func,
73  Types&&... args) // NOLINT
74  : free_func_(NULL),
75  std_func_(func),
76  args_(std::make_tuple(std::forward<Types>(args)...)) {}
77 
78  ~FunctionCall() override {}
79 
81  void operator()() const override {
83  if (free_func_)
84  this->ExpandArgsAndCall(free_func_,
85  SequenceGenerator<sizeof...(Types)>{});
86  else
87  this->ExpandArgsAndCall(std_func_,
88  SequenceGenerator<sizeof...(Types)>{});
89  }
90 
92  template <size_t I>
93  const typename std::tuple_element<I, std::tuple<Types...> >::type& GetArg()
94  const {
95  return std::get<I>(args_);
96  }
97 
99  template <size_t I>
100  void SetArg(const typename std::tuple_element<I, std::tuple<Types...> >::type&
101  value) {
102  std::get<I>(args_) = value;
103  }
104 
105  private:
108  template <size_t... Indicies> struct Sequence {};
109 
111  template <size_t N, size_t... Indices>
112  struct SequenceGenerator : SequenceGenerator<N - 1U, N - 1U, Indices...> {};
113 
115  template <size_t... Indices>
116  struct SequenceGenerator<0U, Indices...> : Sequence<Indices...> {};
117 
122  template <typename Func, size_t... Indices>
123  void ExpandArgsAndCall(const Func& func, Sequence<Indices...> seq) const {
124  func(std::get<Indices>(args_)...);
125  }
126 
129  ReturnType (*free_func_)(Types...);
130  std::function<ReturnType(Types...)> std_func_;
131 
133  std::tuple<Types...> args_;
134 };
135 
136 } // namespace base
137 } // namespace ion
138 
139 #endif // ION_BASE_FUNCTIONCALL_H_
FunctionCall wraps an arbitrary call to a function, including its arguments.
Definition: functioncall.h:59
void SetArg(const typename std::tuple_element< I, std::tuple< Types...> >::type &value)
Sets the Ith argument of the stored arguments to the passed value.
Definition: functioncall.h:100
std::string type
Definition: printer.cc:353
FunctionCall(ReturnType(*func)(Types...), Types &&...args)
Constructor for a free function (general non-member function).
Definition: functioncall.h:66
double value
void operator()() const override
Calls the wrapped function with the stored arguments.
Definition: functioncall.h:81
FunctionCallBase does nothing but define a virtual operator() to allow containers of arbitrary Functi...
Definition: functioncall.h:31
Allocatable is an abstract base class for classes whose memory is managed by an Allocator.
Definition: allocatable.h:60
virtual void operator()() const =0
FunctionCall(const std::function< ReturnType(Types...)> &func, Types &&...args)
Constructor for a std::function (returned by std::bind()).
Definition: functioncall.h:72
const std::tuple_element< I, std::tuple< Types...> >::type & GetArg() const
Returns a const reference to the Ith argument of the function call.
Definition: functioncall.h:93