24#ifndef MYSQL_UTILS_CALL_AND_CATCH_H
25#define MYSQL_UTILS_CALL_AND_CATCH_H
45using Call_and_catch_type = std::conditional_t<
46 std::same_as<Type, void>,
48 std::optional<std::conditional_t<
49 std::is_reference_v<Type>,
51 std::reference_wrapper<std::remove_reference_t<Type>>,
73template <
class Function_t,
class... Args_t>
74 requires std::invocable<Function_t, Args_t...>
75[[nodiscard]]
decltype(
auto) call_and_catch(
const Function_t &
function,
76 Args_t &&...args)
noexcept {
77 auto call_function = [&]() ->
decltype(
auto) {
78 return function(std::forward<Args_t>(args)...);
81 if constexpr (
noexcept(
function(std::forward<Args_t>(args)...))) {
82 return call_function();
83 }
else if constexpr (std::same_as<Return_t, void>) {
92 if constexpr (std::is_reference_v<Return_t>)
93 return std::make_optional(
std::ref(call_function()));
95 return std::make_optional(call_function());
97 return Call_and_catch_type<Return_t>();
104enum class Shall_catch {
no,
yes };
111template <Shall_catch shall_catch,
class Function_t,
class... Args_t>
113 const Function_t &
function,
115 noexcept(
noexcept(
function(std::forward<Args_t>(args)...)) ||
116 shall_catch == Shall_catch::yes) {
117 if constexpr (shall_catch == Shall_catch::yes) {
118 return call_and_catch(
function, std::forward<Args_t>(args)...);
120 return function(std::forward<Args_t>(args)...);
151#define DEDUCED_NOEXCEPT_FUNCTION(X) \
152 noexcept(noexcept(X)) { return (X); }
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
MediaType
Definition: media_type.h:33
Definition: gtid_format.h:47
decltype(call_function()) Return_t
Definition: call_and_catch.h:80
Return_status
Simple, strongly-typed enumeration to indicate internal status: ok, error.
Definition: return_status.h:40
decltype(auto) conditional_call_and_catch(const Function_t &function, Args_t &&...args) noexcept(noexcept(function(std::forward< Args_t >(args)...))||shall_catch==Shall_catch::yes)
Call function, and if shall_catch is true, catch exceptions and wrap them in the return value.
Definition: call_and_catch.h:112