siplasplas
A library for C++ reflection and introspection
memberfunctor.hpp
1 #ifndef SIPLASPLAS_UTILITY_MEMBERFUNCTOR_HPP
2 #define SIPALSPLAS_UTILITY_MEMBERFUNCTOR_HPP
3 
4 #include "meta.hpp"
5 #include <utility>
6 
7 namespace cpp
8 {
9 
23 template<typename T>
24 using IsEboCandidate = ::cpp::meta::bool_<
25  (sizeof(::cpp::meta::inherit<T, ::cpp::meta::aggregate<int>>) == sizeof(int))
26 >;
27 
47 template<typename F, bool Ebo = IsEboCandidate<F>::value>
48 class MemberFunctor : private F
49 {
50 protected:
54  template<typename... Args>
55  constexpr MemberFunctor(Args&&... args) :
56  F{std::forward<Args>(args)...}
57  {}
58 
63  template<typename... Args>
64  constexpr decltype(auto) invoke(Args&&... args) const
65  {
66  return F::operator()(std::forward<Args>(args)...);
67  }
68 
73  template<typename... Args>
74  constexpr decltype(auto) invoke(Args&&... args)
75  {
76  return F::operator()(std::forward<Args>(args)...);
77  }
78 
82  constexpr const F& get() const
83  {
84  return *static_cast<const F*>(this);
85  }
86 
90  constexpr F& get()
91  {
92  return *static_cast<F*>(this);
93  }
94 };
95 
96 template<typename F>
97 class MemberFunctor<F, false>
98 {
99 protected:
100  template<typename... Args>
101  constexpr MemberFunctor(Args&&... args) :
102  _functor{std::forward<Args>(args)...}
103  {}
104 
105  template<typename... Args>
106  constexpr decltype(auto) invoke(Args&&... args) const
107  {
108  return _functor(std::forward<Args>(args)...);
109  }
110 
111  template<typename... Args>
112  constexpr decltype(auto) invoke(Args&&... args)
113  {
114  return _functor(std::forward<Args>(args)...);
115  }
116 
117  constexpr const F& get() const
118  {
119  return _functor;
120  }
121 
122  constexpr F& get()
123  {
124  return _functor;
125  }
126 
127 private:
128  F _functor;
129 };
130 
131 }
132 
133 #endif // SIPLASPLAS_UTILITY_MEMBERFUNCTOR_HPP
Definition: canary_allocator.hpp:7
::cpp::meta::bool_<(sizeof(::cpp::meta::inherit< T, ::cpp::meta::aggregate< int >>)==sizeof(int)) > IsEboCandidate
Checks if a type is a candidate for the Empty Base Optimization (EBO)
Definition: memberfunctor.hpp:26
Implements optimal-storage for a functor that will be used as member of a class.
Definition: memberfunctor.hpp:48
constexpr MemberFunctor(Args &&... args)
Initializes a member functor with the given arguments.
Definition: memberfunctor.hpp:55
decltype(auto) constexpr invoke(Args &&... args) const
Invokes the member functor with the given arguments.
Definition: memberfunctor.hpp:64