45 #include <type_traits> 63 template<
class FUN,
typename...ARGS>
67 ,ARGS&& ...args) noexcept
69 using Res = std::invoke_result_t<FUN,ARGS...>;
71 capturedFailure =
nullptr;
72 if constexpr (std::is_void_v<Res>)
73 std::invoke (std::forward<FUN>(callable), std::forward<ARGS>(args)...);
75 return std::invoke (std::forward<FUN>(callable), std::forward<ARGS>(args)...);
79 capturedFailure = std::current_exception();
80 if constexpr (not std::is_void_v<Res>)
96 template<
typename RES>
108 std::exception_ptr failure_;
113 : failure_{success?
nullptr: std::make_exception_ptr (
error::State{
"operation failed"})}
118 : failure_{std::make_exception_ptr (reason)}
122 template<
class FUN,
typename...ARGS,
typename=
lib::meta::enable_if<std::is_invocable<FUN,ARGS...>>>
123 Result (FUN&& callable, ARGS&& ...args) noexcept
127 ,std::forward<FUN> (callable)
128 ,std::forward<ARGS>(args)...);
132 operator bool()
const {
return isValid(); }
133 bool isValid()
const {
return not failure_; }
139 std::rethrow_exception(failure_);
153 template<
typename RES>
166 template<
typename=lib::meta::disable_if<std::is_invocable<RES>>>
169 , value_{std::forward<RES> (value)}
173 template<
class FUN,
typename...ARGS,
typename=
lib::meta::enable_if<std::is_invocable<FUN,ARGS...>>>
174 Result (FUN&& callable, ARGS&& ...args) noexcept
177 ,std::forward<FUN> (callable)
178 ,std::forward<ARGS>(args)...)}
190 template<
typename TY =RES>
195 return static_cast<TY
> (*value_);
200 value_or (O&& defaultVal)
202 return isValid()? *value_ : std::forward<O> (defaultVal);
205 template<
typename MAKE,
typename...ARGS>
207 or_else (MAKE&& producer, ARGS ...args)
212 return std::invoke(std::forward<MAKE> (producer), std::forward<ARGS> (args)...);
217 template<
typename VAL,
typename=lib::meta::disable_if<std::is_invocable<VAL>>>
221 template<
typename FUN,
typename...ARGS>
222 Result (FUN&&, ARGS&&...) ->
Result<std::invoke_result_t<FUN,ARGS...>>;
The base case is just to capture success or failure, without returning any value result.
Representation of the result of some operation, EITHER a value or a failure.
Result(lumiera::Error const &reason)
mark failed result, with reason given.
Singleton holder for NIL or default value objects.
Result(lumiera::Error const &reason)
failed result, with reason given.
Singleton-style holder for NIL or default values.
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera's exception hierarchy.
void maybeThrow(string description="")
Check the lumiera error state and throw a specific exception in case a non-cleared errorflag is detec...
Lumiera error handling (C++ interface).
Result(bool success=false)
mark either failure (default) or success
auto failsafeInvoke(std::exception_ptr &capturedFailure, FUN &&callable, ARGS &&...args) noexcept
Helper to invoke an arbitrary callable in a failsafe way.
Result(FUN &&callable, ARGS &&...args) noexcept
invoke a callable and mark success or failure
Library implementation: smart-pointer variations, wrappers and managing holders.
Interface and Base definition for all Lumiera Exceptions.