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_IF_EXCEPTIONS_ARE_ENABLED(WHAT) std::terminate()
149 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) throw std::logic_error(WHAT)
169 const T &operator* ()
const
179 const T *operator-> ()
const
196 #undef THROW_IF_EXCEPTIONS_ARE_ENABLED
198 template <
typename F>
232 : m_isValid(other.m_isValid)
237 new (&m_error) E(other.
m_error);
242 : m_isValid(other.m_isValid)
247 new (&m_error) E(std::move(other.m_error));
261 if (other.m_isValid) {
268 auto temp = std::move(other.m_error);
270 new (&m_error) E(std::move(temp));
271 std::swap(m_isValid, other.m_isValid);
275 if (other.m_isValid) {
283 swap(m_error, other.m_error);
284 std::swap(m_isValid, other.m_isValid);
299 template <
typename... ConsParams>
306 new(&result.
m_error) E(std::forward<ConsParams>(params)...);
321 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) std::terminate()
323 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) throw std::logic_error(WHAT)
340 template <
typename Type,
typename Err = std::exception_ptr >
352 template <
typename Function,
typename ArgType>
355 template <
typename T>
362 template <
typename T
370 if constexpr ( std::is_same_v<T,void> )
376 return ResultType::error(exp.
error());
382 template <
typename T
390 if constexpr ( std::is_same_v<T,void> )
393 return std::invoke( std::forward<Function>(f), std::move(exp.get()) );
396 return ResultType::error( std::move(exp.error()) );
402 template <
typename T
410 if constexpr ( std::is_same_v<T,void> )
419 template <
typename T
427 if constexpr ( std::is_same_v<T,void> )
430 return std::invoke( std::forward<Function>(f), std::move(exp.error()) );
444 template <
template<
class,
class... >
class Container,
450 for(
auto &v : in ) {
453 res.push_back( std::move(v.get()) );
458 template <
typename T
465 const auto &val = exp.
get();
471 template <
typename T
478 const auto &err = exp.
error();
487 template <
typename Callback>
491 template<
typename T,
typename E >
496 template<
typename T,
typename E >
498 return and_then( std::move(exp), function );
502 template <
typename Callback>
506 template<
typename T,
typename E >
508 return or_else( exp, function );
511 template<
typename T,
typename E >
513 return or_else( std::move(exp), function );
517 template <
typename Callback>
521 template<
typename T,
typename E >
523 return inspect( std::move(exp), function );
527 template <
typename Callback>
531 template<
typename T,
typename E >
538 template <
typename T >
540 return collect( std::forward<T>(in) );
545 namespace operators {
546 template <
typename Fun>
549 std::forward<Fun>(function)
553 template <
typename Fun>
556 std::forward<Fun>(function)
560 template <
typename Fun>
563 std::forward<Fun>(function)
567 template <
typename Fun>
570 std::forward<Fun>(function)
574 template <
typename Fun>
577 std::forward<Fun>(function)
591 template <
template<
class,
class... >
class Container,
593 typename Transformation,
611 Container<typename Ret::value_type> results;
612 for (
auto &v : in ) {
613 auto res = f(std::move(v));
615 results.push_back( std::move(res.get()) );
625 template <
typename Fun>
628 template <
typename T>
635 namespace operators {
636 template <
typename Transformation>
static expected success()
expected(expected &&other) noexcept
expected(const expected &other)
static expected error(ConsParams &&...params)
void swap(expected &other) noexcept
expected(expected &&other) noexcept
static expected success(ConsParams &&...params)
static expected error(ConsParams &&...params)
expected(const expected &other)
void swap(expected &other) noexcept
#define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT)
typename conditional< B, T, F >::type conditional_t
typename result_of< T >::type result_of_t
std::enable_if< std::is_member_pointer< typenamestd::decay< Functor >::type >::value, typenamestd::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
constexpr bool is_async_op_v
bool waitForCanContinueExpected(const expected< T > &value)
typename std::conditional_t< std::is_same_v< ArgType, void >, std::invoke_result< Function >, std::invoke_result< Function, ArgType > >::type mbind_cb_result_t
auto or_else(Fun &&function)
auto transform_collect(Transformation &&f)
auto and_then(Fun &&function)
auto inspect_err(Fun &&function)
detail::collect_helper collect()
auto inspect(Fun &&function)
auto mbind(Fun &&function)
auto transform_collect(Container< Msg, CArgs... > &&in, Transformation &&f)
std::conditional_t< isAsync, AsyncOpRef< T >, T > makeReadyResult(T &&result)
typename remove_smart_ptr< T >::type remove_smart_ptr_t
static expected< std::decay_t< Type >, Err > make_expected_success(Type &&t)
ResultType or_else(const expected< T, E > &exp, Function &&f)
ResultType and_then(const expected< T, E > &exp, Function &&f)
Container< Ret > transform(Container< Msg, CArgs... > &&val, Transformation &&transformation)
expected< T, E > inspect(expected< T, E > exp, Function &&f)
expected< T, E > inspect_err(expected< T, E > exp, Function &&f)
auto operator()(expected< T, E > &&exp)
auto operator()(const expected< T, E > &exp)
auto operator()(expected< T, E > &&exp)
auto operator()(expected< T, E > &&exp)
auto operator()(const expected< T, E > &exp)
auto operator()(expected< T, E > &&exp)