diff --git a/source/coral.cpp b/source/coral.cpp index 27e163e..3edd14f 100644 --- a/source/coral.cpp +++ b/source/coral.cpp @@ -3,6 +3,7 @@ module; #include #include #include +#include export module coral; @@ -285,6 +286,15 @@ export void operator delete[](void * pointer, coral::allocator & allocator) { // Wrapper types. export namespace coral { + template concept function_pointer = requires (type value, arguments... value_arguments) { + {*value}; + {value(value_arguments...)}; + }; + + template concept functor = requires (type value, arguments... value_arguments) { + {value.operator()(value_arguments...)}; + }; + template struct closure; /** @@ -295,42 +305,32 @@ export namespace coral { * the caller to manage the lifetime of any functor assigned to it. */ template struct closure { - using function = returns(*)(arguments...); - - closure(function callable_function) { - this->dispatch = [](void const * context, arguments... dispatch_arguments) -> returns { - return (reinterpret_cast(context))(dispatch_arguments...); + template closure(callable const & call) requires function_pointer { + this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns { + return (*reinterpret_cast(context))(dispatch_arguments...); }; - this->context = callable_function; + this->context = reinterpret_cast(call); } - template closure(functor * callable_functor) { - this->dispatch = [](void const * context, arguments... dispatch_arguments) -> returns { - return (*reinterpret_cast(context))(dispatch_arguments...); + template closure(callable && call) requires functor { + this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns { + return (*reinterpret_cast(context))(dispatch_arguments...); }; - this->context = callable_functor; + this->context = &call; } closure(closure const &) = delete; - template closure(functor && callable_functor) { - this->dispatch = [](void const * context, arguments... dispatch_arguments) -> returns { - return (*reinterpret_cast(context))(dispatch_arguments...); - }; - - this->context = &callable_functor; - } - returns operator()(arguments const &... call_arguments) const { return this->dispatch(this->context, call_arguments...); } private: - void const * context; + void * context; - returns(* dispatch)(void const *, arguments...); + returns(* dispatch)(void *, arguments...); }; /**