1#ifndef ENTT_SIGNAL_DELEGATE_HPP
2#define ENTT_SIGNAL_DELEGATE_HPP
9#include "../config/config.h"
10#include "../core/type_traits.hpp"
18template<
typename Ret,
typename... Args>
19constexpr auto function_pointer(Ret (*)(Args...)) -> Ret (*)(Args...);
21template<
typename Ret,
typename Type,
typename... Args,
typename Other>
22constexpr auto function_pointer(Ret (*)(Type, Args...), Other &&) -> Ret (*)(Args...);
24template<
typename Class,
typename Ret,
typename... Args,
typename... Other>
25constexpr auto function_pointer(Ret (Class::*)(Args...), Other &&...) -> Ret (*)(Args...);
27template<
typename Class,
typename Ret,
typename... Args,
typename... Other>
28constexpr auto function_pointer(Ret (Class::*)(Args...) const, Other &&...) -> Ret (*)(Args...);
30template<
typename Class,
typename Type,
typename... Other,
typename = std::enable_if_t<std::is_member_object_pointer_v<Type Class::*>>>
31constexpr auto function_pointer(Type Class::*, Other &&...) -> Type (*)();
33template<
typename... Type>
34using function_pointer_t =
decltype(function_pointer(std::declval<Type>()...));
36template<
typename... Class,
typename Ret,
typename... Args>
37[[nodiscard]]
constexpr auto index_sequence_for(Ret (*)(Args...)) {
38 return std::index_sequence_for<Class..., Args...>{};
65template<
typename Ret,
typename...
Args>
67 using return_type = std::remove_const_t<Ret>;
68 using delegate_type = return_type(
const void *,
Args...);
71 [[
nodiscard]]
auto wrap(std::index_sequence<Index...>)
noexcept {
72 return [](
const void *,
Args...
args) -> return_type {
80 [[
nodiscard]]
auto wrap(Type &, std::index_sequence<Index...>)
noexcept {
81 return [](
const void *payload,
Args...
args) -> return_type {
90 [[
nodiscard]]
auto wrap(Type *, std::index_sequence<Index...>)
noexcept {
91 return [](
const void *payload,
Args...
args) -> return_type {
135 template<auto Cand
idate>
140 fn = [](
const void *,
Args...
args) -> return_type {
143 }
else if constexpr(std::is_member_pointer_v<
decltype(
Candidate)>) {
165 template<auto Cand
idate,
typename Type>
169 if constexpr(std::is_invocable_r_v<
Ret,
decltype(
Candidate), Type &,
Args...>) {
170 fn = [](
const void *payload,
Args...
args) -> return_type {
189 template<auto Cand
idate,
typename Type>
193 if constexpr(std::is_invocable_r_v<
Ret,
decltype(
Candidate), Type *,
Args...>) {
194 fn = [](
const void *payload,
Args...
args) -> return_type {
217 ENTT_ASSERT(
function !=
nullptr,
"Uninitialized function pointer");
261 ENTT_ASSERT(
static_cast<bool>(*
this),
"Uninitialized delegate");
262 return fn(instance, std::forward<Args>(
args)...);
271 return !(fn ==
nullptr);
280 return fn ==
other.fn && instance ==
other.instance;
284 const void *instance{};
296template<
typename Ret,
typename...
Args>
305template<auto Cand
idate>
313template<auto Cand
idate,
typename Type>
321template<
typename Ret,
typename...
Args>
bool operator==(const delegate< Ret(Args...)> &other) const noexcept
Compares the contents of two delegates.
Ret operator()(Args... args) const
Triggers a delegate.
void reset() noexcept
Resets a delegate.
Ret(const void *, Args...) function_type
Function type of the contained target.
delegate(function_type *function, const void *payload=nullptr) noexcept
Constructs a delegate and connects an user defined function with optional payload.
void connect(Type &value_or_instance) noexcept
Connects a free function with payload or a bound member to a delegate.
void connect(Type *value_or_instance) noexcept
Connects a free function with payload or a bound member to a delegate.
void connect() noexcept
Connects a free function or an unbound member to a delegate.
Ret result_type
Return type of the delegate.
const void * data() const noexcept
Returns the instance or the payload linked to a delegate, if any.
delegate() noexcept=default
Default constructor.
void connect(function_type *function, const void *payload=nullptr) noexcept
Connects an user defined function with optional payload to a delegate.
function_type * target() const noexcept
Returns a pointer to the stored callable function target, if any.
Ret(Args...) type
Function type of the delegate.
Basic delegate implementation.
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
typename constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
typename type_list_element< Index, List >::type type_list_element_t
Helper type.
delegate(connect_arg_t< Candidate >) -> delegate< std::remove_pointer_t< internal::function_pointer_t< decltype(Candidate)> > >
Deduction guide.
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Disambiguation tag for constructors and the like.
A class to use to push around lists of types, nothing more.