76 lines
2.3 KiB
C++
76 lines
2.3 KiB
C++
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: auTryCallUtils.hpp
|
||
|
Date: 2022-11-16
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#pragma once
|
||
|
|
||
|
#define _AU_TRYCALL_CHECKCODE(X) AU_TEMPLATE_ENABLE_WHEN(AuIsBaseOf_v<AuCallErrorTag, CtorCode_t> && !AuIsSame_v<X, CtorCode_t>)
|
||
|
|
||
|
template <class CtorCode_t = AuErrorCode_t, class Callable_t, typename ... Args, _AU_TRYCALL_CHECKCODE(Callable_t)>
|
||
|
inline auto AuTryCall(Callable_t callable, CtorCode_t &status, Args&& ... args)
|
||
|
{
|
||
|
if constexpr (AuIsCallable_v<Callable_t, Args&& ..., CtorCode_t &>)
|
||
|
{
|
||
|
status = CtorCode_t::Failed();
|
||
|
return callable(AuForward<Args&&>(args)..., AuReference(status));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
static_assert(AuIsCallable_v<Callable_t, Args&& ...>, "bad try-call prototype");
|
||
|
|
||
|
try
|
||
|
{
|
||
|
status = CtorCode_t::Success();
|
||
|
return callable(AuForward<Args &&>(args)...);
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
status = CtorCode_t::Failed();
|
||
|
return AuResultOf_t<Callable_t, Args...>();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
template <class CtorCode_t = AuErrorCode_t, class Callable_t, typename ... Args, class T, _AU_TRYCALL_CHECKCODE(T)>
|
||
|
inline CtorCode_t AuTryCall(Callable_t callable, T &ref, Args&& ... args)
|
||
|
{
|
||
|
CtorCode_t status;
|
||
|
if constexpr (AuIsCallable_v<Callable_t, Args ..., CtorCode_t &>)
|
||
|
{
|
||
|
ref = callable(AuForward<Args>(args)..., AuReference(status));
|
||
|
return status;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
ref = callable(AuForward<Args>(args)...);
|
||
|
return CtorCode_t::Success();
|
||
|
}
|
||
|
catch (...)
|
||
|
{
|
||
|
ref = AuMove(T());
|
||
|
if constexpr (AuIsSame_v<CtorCode_t, AuErrorCode_t>)
|
||
|
{
|
||
|
return CtorCode_t::Catch();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return CtorCode_t::Failed();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
template <class CtorCode_t = AuErrorCode_t, class Callable_t, typename ... Args, _AU_TRYCALL_CHECKCODE(Callable_t)>
|
||
|
inline auto AuTryCallPair(Callable_t callable, Args&& ... args)
|
||
|
{
|
||
|
CtorCode_t code;
|
||
|
auto a = AuTryCall<CtorCode_t, Callable_t, Args...>(callable, AuReference(code), AuForward<Args &&>(args)...);
|
||
|
return AuMakePair(AuMove(code), a);
|
||
|
}
|
||
|
|
||
|
#undef _AU_TRYCALL_CHECKCODE
|