16 #ifndef ZYPP_ZYPPNG_MONADIC_EXPECTED_H 17 #define ZYPP_ZYPPNG_MONADIC_EXPECTED_H 19 #include <zypp-core/zyppng/meta/Functional> 20 #include <zypp-core/zyppng/pipelines/AsyncResult> 21 #include <zypp-core/zyppng/pipelines/Wait> 22 #include <zypp-core/zyppng/pipelines/Transform> 26 template<
typename T,
typename E = std::exception_ptr>
55 : m_isValid(other.m_isValid)
58 new (&m_value) T(other.
m_value);
60 new (&m_error) E(other.
m_error);
65 : m_isValid(other.m_isValid)
68 new (&m_value) T( std::move(other.m_value) );
70 new (&m_error) E( std::move(other.m_error) );
84 if (other.m_isValid) {
86 swap(m_value, other.m_value);
91 auto temp = std::move(other.m_error);
93 new (&other.m_value) T(std::move(m_value));
95 new (&m_error) E(std::move(temp));
96 std::swap(m_isValid, other.m_isValid);
100 if (other.m_isValid) {
108 swap(m_error, other.m_error);
109 std::swap(m_isValid, other.m_isValid);
114 template <
typename... ConsParams>
121 new(&result.
m_value) T(std::forward<ConsParams>(params)...);
125 template <
typename... ConsParams>
132 new(&result.
m_error) E(std::forward<ConsParams>(params)...);
147 # define THROW_MSG_IF_EXCEPTIONS_ARE_ENABLED(WHAT) std::terminate() 149 # define THROW_MSG_IF_EXCEPTIONS_ARE_ENABLED(WHAT) throw std::logic_error(WHAT) 179 if constexpr ( std::is_same_v<E, std::exception_ptr> ) {
180 std::rethrow_exception ( error() );
195 if constexpr ( std::is_same_v<E, std::exception_ptr>() ) {
196 std::rethrow_exception ( error() );
210 const T &operator* ()
const 220 const T *operator-> ()
const 237 #undef THROW_IF_EXCEPTIONS_ARE_ENABLED 239 template <
typename F>
273 : m_isValid(other.m_isValid)
278 new (&m_error) E(other.
m_error);
283 : m_isValid(other.m_isValid)
288 new (&m_error) E(std::move(other.m_error));
302 if (other.m_isValid) {
309 auto temp = std::move(other.m_error);
311 new (&m_error) E(std::move(temp));
312 std::swap(m_isValid, other.m_isValid);
316 if (other.m_isValid) {
324 swap(m_error, other.m_error);
325 std::swap(m_isValid, other.m_isValid);
340 template <
typename... ConsParams>
347 new(&result.
m_error) E(std::forward<ConsParams>(params)...);
362 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) std::terminate() 364 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) throw std::logic_error(WHAT) 385 if constexpr ( std::is_same_v<E, std::exception_ptr> ) {
386 std::rethrow_exception ( error() );
396 template <
typename Type,
typename Err = std::exception_ptr >
408 template <
typename Function,
typename ArgType>
411 template <
typename T>
418 template <
typename T
421 ,
typename ResultType = detail::mbind_cb_result_t<Function, T>
426 if constexpr ( std::is_same_v<T,void> )
432 return ResultType::error(exp.
error());
438 template <
typename T
441 ,
typename ResultType = detail::mbind_cb_result_t<Function, T>
446 if constexpr ( std::is_same_v<T,void> )
449 return std::invoke( std::forward<Function>(f), std::move(exp.get()) );
452 return ResultType::error( std::move(exp.error()) );
458 template <
typename T
461 ,
typename ResultType = detail::mbind_cb_result_t<Function, E>
475 template <
typename T
478 ,
typename ResultType = detail::mbind_cb_result_t<Function, E>
483 return std::invoke( std::forward<Function>(f), std::move(exp.error()) );
497 template <
template<
class,
class... >
class Container,
501 std::
enable_if_t<!std::is_same_v<void, T>, expected<Container<T>,E>>
collect( Container<expected<T, E>, CArgs...>&& in ) {
503 for(
auto &v : in ) {
506 res.push_back( std::move(v.get()) );
515 template <
template<
class,
class... >
class Container,
520 for(
auto &v : in ) {
527 template <
typename T
534 const auto &val = exp.
get();
540 template <
typename T
547 const auto &err = exp.
error();
556 template <
typename Callback>
560 template<
typename T,
typename E >
565 template<
typename T,
typename E >
567 return and_then( std::move(exp),
function );
571 template <
typename Callback>
575 template<
typename T,
typename E >
577 return or_else( exp,
function );
580 template<
typename T,
typename E >
582 return or_else( std::move(exp),
function );
586 template <
typename Callback>
590 template<
typename T,
typename E >
592 return inspect( std::move(exp),
function );
596 template <
typename Callback>
600 template<
typename T,
typename E >
607 template <
typename T >
609 return collect( std::forward<T>(in) );
614 namespace operators {
615 template <
typename Fun>
618 std::forward<Fun>(
function)
622 template <
typename Fun>
625 std::forward<Fun>(
function)
629 template <
typename Fun>
632 std::forward<Fun>(
function)
636 template <
typename Fun>
639 std::forward<Fun>(
function)
643 template <
typename Fun>
646 std::forward<Fun>(
function)
660 template <
template<
class,
class... >
class Container,
662 typename Transformation,
669 if constexpr ( detail::is_async_op_v<Ret> ) {
680 Container<typename Ret::value_type> results;
681 for (
auto &v : in ) {
682 auto res = f(std::move(v));
684 results.push_back( std::move(res.get()) );
694 template <
typename Fun>
697 template <
typename T>
704 namespace operators {
705 template <
typename Transformation>
auto transform(Transformation &&transformation)
static expected success()
auto operator()(expected< T, E > &&exp)
std::enable_if_t<!std::is_same_v< void, T >, expected< Container< T >, E > > collect(Container< expected< T, E >, CArgs... > &&in)
auto inspect_err(Fun &&function)
expected< T, E > inspect(expected< T, E > exp, Function &&f)
static expected error(ConsParams &&...params)
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
auto mbind(Fun &&function)
static expected error(ConsParams &&...params)
auto operator()(expected< T, E > &&exp)
auto inspect(Fun &&function)
std::enable_if< std::is_member_pointer< typename std::decay< Functor >::type >::value, typename std::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
#define THROW_MSG_IF_EXCEPTIONS_ARE_ENABLED(WHAT)
auto operator()(const expected< T, E > &exp)
ResultType or_else(const expected< T, E > &exp, Function &&f)
expected(const expected &other)
void swap(expected &other) noexcept
auto or_else(Fun &&function)
expected(const expected &other)
expected(expected &&other) noexcept
auto operator()(expected< T, E > &&exp)
typename enable_if< B, T >::type enable_if_t
auto transform_collect(Container< Msg, CArgs... > &&in, Transformation &&f)
expected< T, E > inspect_err(expected< T, E > exp, Function &&f)
bool waitForCanContinueExpected(const expected< T > &value)
typename conditional< B, T, F >::type conditional_t
std::conditional_t< isAsync, AsyncOpRef< T >, T > makeReadyResult(T &&result)
static expected success(ConsParams &&...params)
detail::collect_helper collect()
void swap(expected &other) noexcept
typename result_of< T >::type result_of_t
auto transform_collect(Transformation &&f)
auto operator()(expected< T, E > &&exp)
auto and_then(Fun &&function)
auto operator()(const expected< T, E > &exp)
expected(expected &&other) noexcept
ResultType and_then(const expected< T, E > &exp, Function &&f)
typename std::conditional_t< std::is_same_v< ArgType, void >, std::invoke_result< Function >, std::invoke_result< Function, ArgType > >::type mbind_cb_result_t
#define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT)
typename remove_smart_ptr< T >::type remove_smart_ptr_t