Replace coral::is_true with coral::equality_predicate

This commit is contained in:
kayomn 2023-02-26 00:32:24 +00:00
parent dc39fedf0c
commit 3f84b0ce0b

View File

@ -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<typename callable> closure(callable & call) requires functor<callable, arguments...> {
this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns {
return (*reinterpret_cast<callable *>(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<typename value> 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<error_element *>(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<bool(value_element const &)> 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.
*/