54 #include <type_traits> 72 template<
class FUN,
typename...ARGS>
76 ,ARGS&& ...args) noexcept
78 using Res = std::invoke_result_t<FUN,ARGS...>;
80 capturedFailure =
nullptr;
81 if constexpr (std::is_void_v<Res>)
82 std::invoke (std::forward<FUN>(callable), std::forward<ARGS>(args)...);
84 return std::invoke (std::forward<FUN>(callable), std::forward<ARGS>(args)...);
88 capturedFailure = std::current_exception();
89 if constexpr (not std::is_void_v<Res>)
105 template<
typename RES>
117 std::exception_ptr failure_;
122 : failure_{success?
nullptr: std::make_exception_ptr (
error::State{
"operation failed"})}
127 : failure_{std::make_exception_ptr (reason)}
131 template<
class FUN,
typename...ARGS,
typename=
lib::meta::enable_if<std::is_invocable<FUN,ARGS...>>>
132 Result (FUN&& callable, ARGS&& ...args) noexcept
136 ,std::forward<FUN> (callable)
137 ,std::forward<ARGS>(args)...);
141 operator bool()
const {
return isValid(); }
142 bool isValid()
const {
return not failure_; }
148 std::rethrow_exception(failure_);
162 template<
typename RES>
175 template<
typename=lib::meta::disable_if<std::is_invocable<RES>>>
178 , value_{std::forward<RES> (value)}
182 template<
class FUN,
typename...ARGS,
typename=
lib::meta::enable_if<std::is_invocable<FUN,ARGS...>>>
183 Result (FUN&& callable, ARGS&& ...args) noexcept
186 ,std::forward<FUN> (callable)
187 ,std::forward<ARGS>(args)...)}
199 template<
typename TY =RES>
204 return static_cast<TY
> (*value_);
209 value_or (O&& defaultVal)
211 return isValid()? *value_ : std::forward<O> (defaultVal);
214 template<
typename MAKE,
typename...ARGS>
216 or_else (MAKE&& producer, ARGS ...args)
221 return std::invoke(std::forward<MAKE> (producer), std::forward<ARGS> (args)...);
226 template<
typename VAL,
typename=lib::meta::disable_if<std::is_invocable<VAL>>>
230 template<
typename FUN,
typename...ARGS>
231 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.