diff --git a/source/coral.cpp b/source/coral.cpp index 7f7f823..0d2289a 100644 --- a/source/coral.cpp +++ b/source/coral.cpp @@ -307,6 +307,13 @@ export namespace coral { * Type-erasing view wrapper for both function and functor types that have a call operator with * a return value matching `return_value` and arguments matching `argument_values`. * + * A closure may be constructed from either of the following inputs: + * + * * A function pointer that uses arguments and returns which are implicitly convertible to `arguments` and + * `returns`. + * + * * An L or R-value functor reference. + * * **Note**: closures take no ownership of allocated memory, making it the responsibility of * the caller to manage the lifetime of any functor assigned to it. */ @@ -327,6 +334,14 @@ export namespace coral { this->context = &call; } + template closure(callable & call) requires functor { + this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns { + return (*reinterpret_cast(context))(dispatch_arguments...); + }; + + this->context = call; + } + closure(closure const &) = delete; returns operator()(arguments const &... call_arguments) const { @@ -340,11 +355,13 @@ export namespace coral { }; /** - * Helpful wrapper utility for using in chainable conditionals like [coral::expected::and_test] to check if a `bool` + * Helpful wrapper utility for using in chainable conditionals like [coral::expected::map] to check if a `bool` * is `true` without writing a lambda. - */ - constexpr bool is_true(bool value) { - return value; + */ + template constexpr auto equality_predicate(value reference_value) { + return [reference_value](value comparing_value) -> bool { + return comparing_value == reference_value; + }; } /** @@ -360,17 +377,6 @@ export namespace coral { (*reinterpret_cast(this->buffer)) = error; } - /** - * Monadic function for calling `predicate` conditionally based on whether the expected is - * ok. If ok, the result of `predicate` is returned, otherwise `false` is always returned. - * - * This function may be used to chain conditional checks that depend on the expected being - * ok without creating a new local variable. - */ - bool and_test(closure const & predicate) const { - return this->is_ok() && predicate(this->value()); - } - /** * Returns `true` if the optional contains a value, otherwise `false` if it holds an error. */