Go to the documentation of this file.
9 #ifndef EAGINE_CALLABLE_REF_HPP
10 #define EAGINE_CALLABLE_REF_HPP
23 template <
typename FuncSig,
bool NoExcept>
34 template <
typename RV,
typename... P,
bool NE>
39 std::tuple<std::remove_cv_t<std::remove_reference_t<P>>...>;
52 : _data{std::exchange(temp._data,
nullptr)}
53 , _func{std::exchange(temp._func,
nullptr)} {}
56 constexpr basic_callable_ref(
const basic_callable_ref&) noexcept =
default;
61 std::swap(temp._data, _data);
62 std::swap(temp._func, _func);
74 : _func{
reinterpret_cast<_func_t
>(func)} {}
79 : _data{
static_cast<void*
>(data)}
80 , _func{
reinterpret_cast<_func_t
>(func)} {
81 EAGINE_ASSERT(_data !=
nullptr);
87 : _data(
static_cast<void*
>(&data))
88 , _func(
reinterpret_cast<_func_t
>(func)) {}
93 typename = std::enable_if_t<!std::is_same_v<C, basic_callable_ref>>>
95 : _data(
static_cast<void*
>(&obj))
96 , _func(
reinterpret_cast<_func_t
>(&_cls_fn_call_op<C>)) {}
101 typename = std::enable_if_t<!std::is_same_v<C, basic_callable_ref>>>
103 : _data(
static_cast<void*
>(
const_cast<C*
>(&obj)))
104 , _func(
reinterpret_cast<_func_t
>(&_cls_fn_call_op_c<C>)) {}
107 template <
typename C, RV (C::*Ptr)(P...) noexcept(NE)>
111 : _data(
static_cast<void*
>(obj))
112 , _func(
reinterpret_cast<_func_t
>(mfc.make_free())) {
113 EAGINE_ASSERT(_data !=
nullptr);
114 EAGINE_ASSERT(_func !=
nullptr);
118 template <
typename C, RV (C::*Ptr)(P...)
const noexcept(NE)>
123 : _data(
static_cast<void*
>(
const_cast<C*
>(obj)))
124 , _func(
reinterpret_cast<_func_t
>(mfc.make_free())) {
125 EAGINE_ASSERT(_data !=
nullptr);
126 EAGINE_ASSERT(_func !=
nullptr);
131 return _func !=
nullptr;
136 explicit constexpr
operator bool() const noexcept {
142 template <
typename... A>
144 EAGINE_ASSERT(is_valid());
145 if(_data ==
nullptr) {
146 return (
reinterpret_cast<_func_pt
>(_func))(std::forward<A>(a)...);
148 return (
reinterpret_cast<_func_vpt
>(_func))(
149 _data, std::forward<A>(a)...);
154 void* _data{
nullptr};
155 void (*_func)() noexcept(NE){
nullptr};
157 using _func_t = void (*)() noexcept(NE);
158 using _func_pt = RV (*)(P...) noexcept(NE);
159 using _func_vpt = RV (*)(
void*, P...) noexcept(NE);
161 template <
typename C>
162 static auto _cls_fn_call_op(
void* that, P... p) noexcept(NE) -> RV {
164 C& obj = *(
static_cast<C*
>(that));
167 if constexpr(std::is_void_v<RV>) {
168 obj(std::forward<P>(p)...);
170 return obj(std::forward<P>(p)...);
174 template <
typename C>
175 static auto _cls_fn_call_op_c(
void* that, P... p) noexcept(NE) -> RV {
177 const C& obj = *(
static_cast<const C*
>(that));
180 if constexpr(std::is_void_v<RV>) {
181 obj(std::forward<P>(p)...);
183 return obj(std::forward<P>(p)...);
190 template <
typename Sig>
195 #endif // EAGINE_CALLABLE_REF_HPP
Declaration of class template storing a reference to a callable object.
Definition: callable_ref.hpp:24
basic_callable_ref(C *obj, member_function_constant< RV(C::*)(P...) noexcept(NE), Ptr > mfc) noexcept
Construction a pointer to object and member function constant.
Definition: callable_ref.hpp:108
constexpr auto is_valid() const noexcept
Indicates if this object stores a valid callable reference.
Definition: callable_ref.hpp:130
Common code is placed in this namespace.
Definition: eagine.hpp:21
basic_callable_ref(const C *obj, member_function_constant< RV(C::*)(P...) const noexcept(NE), Ptr > mfc) noexcept
Construction a pointer to const object and member function constant.
Definition: callable_ref.hpp:119
std::tuple< std::remove_cv_t< std::remove_reference_t< P > >... > argument_tuple_type
Alias for the callable's argument type tuple.
Definition: callable_ref.hpp:39
basic_callable_ref(construct_from_t, C &obj) noexcept
Construction a reference to object with a call operator.
Definition: callable_ref.hpp:94
Template used to construct tag-types used mostly in tag-dispatching.
Definition: selector.hpp:21
constexpr auto operator=(basic_callable_ref &&temp) noexcept -> basic_callable_ref &
Move assignment operator.
Definition: callable_ref.hpp:59
basic_callable_ref(construct_from_t, const C &obj) noexcept
Construction a const reference to object with a call operator.
Definition: callable_ref.hpp:102
basic_callable_ref(T *data, RV(*func)(T *, P...) noexcept(NE)) noexcept
Construction from pointers to an object and a function.
Definition: callable_ref.hpp:78
static auto argument_tuple() noexcept -> argument_tuple_type
Creates a default-constructed instance of argument_type_tuple.
Definition: callable_ref.hpp:42
Declaration of compile-time member function pointer wrappers.
Definition: mem_func_const.hpp:19
basic_callable_ref(T &data, RV(*func)(T *, P...) noexcept(NE)) noexcept
Construction from a reference to an object and a pointer to function.
Definition: callable_ref.hpp:86
auto operator()(A &&... a) const -> RV
Call operator.
Definition: callable_ref.hpp:143