16 #ifndef ZYPP_ZYPPNG_MONADIC_EXPECTED_H 17 #define ZYPP_ZYPPNG_MONADIC_EXPECTED_H 21 #include <zypp-core/ng/meta/Functional> 23 #include <zypp-core/ng/pipelines/operators.h> 31 template <
typename E = T>
55 template<
typename T,
typename E = std::exception_ptr>
85 new (&m_value) T( v );
90 new (&m_value) T( std::move(v) );
95 new(&m_error) E(err.error());
100 new(&m_error) E(std::move(err.error()));
104 : m_isValid(other.m_isValid)
107 new (&m_value) T(other.
m_value);
109 new (&m_error) E(other.
m_error);
114 : m_isValid(other.m_isValid)
117 new (&m_value) T( std::move(other.m_value) );
119 new (&m_error) E( std::move(other.m_error) );
123 template <
class G = E >
136 template<
class U = std::remove_cv_t<T> >
147 if (other.m_isValid) {
149 swap(m_value, other.m_value);
154 auto temp = std::move(other.m_error);
156 new (&other.m_value) T(std::move(m_value));
158 new (&m_error) E(std::move(temp));
159 std::swap(m_isValid, other.m_isValid);
163 if (other.m_isValid) {
171 swap(m_error, other.m_error);
172 std::swap(m_isValid, other.m_isValid);
177 template <
typename... ConsParams>
184 new(&result.
m_value) T(std::forward<ConsParams>(params)...);
188 template <
typename... ConsParams>
195 new(&result.
m_error) E(std::forward<ConsParams>(params)...);
210 # define THROW_MSG_IF_EXCEPTIONS_ARE_ENABLED(WHAT) std::terminate() 212 # define THROW_MSG_IF_EXCEPTIONS_ARE_ENABLED(WHAT) throw std::logic_error(WHAT) 242 if constexpr ( std::is_same_v<E, std::exception_ptr> ) {
243 std::rethrow_exception ( error() );
258 if constexpr ( std::is_same_v<E, std::exception_ptr>() ) {
259 std::rethrow_exception ( error() );
273 const T &operator* ()
const 283 const T *operator-> ()
const 300 #undef THROW_IF_EXCEPTIONS_ARE_ENABLED 302 template <
typename F>
330 new(&m_error) E(std::move(err.error()));
343 : m_isValid(other.m_isValid)
348 new (&m_error) E(other.
m_error);
353 : m_isValid(other.m_isValid)
358 new (&m_error) E(std::move(other.m_error));
368 template <
class G = E >
380 if (other.m_isValid) {
387 auto temp = std::move(other.m_error);
389 new (&m_error) E(std::move(temp));
390 std::swap(m_isValid, other.m_isValid);
394 if (other.m_isValid) {
402 swap(m_error, other.m_error);
403 std::swap(m_isValid, other.m_isValid);
413 template <
typename... ConsParams>
420 new(&result.
m_error) E(std::forward<ConsParams>(params)...);
435 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) std::terminate() 437 # define THROW_IF_EXCEPTIONS_ARE_ENABLED(WHAT) throw std::logic_error(WHAT) 458 if constexpr ( std::is_same_v<E, std::exception_ptr> ) {
459 std::rethrow_exception ( error() );
469 template <
typename Type,
typename Err = std::exception_ptr >
481 template <
typename Function,
typename ArgType>
484 template <
typename T>
489 template<
typename ResultType>
491 template <
typename E>
492 static ResultType
error ( E &&err ) {
493 return ResultType::error( std::forward<E>(err) );
496 template <
typename Res>
498 return std::forward<Res>(exp);
502 template <
typename ResultType,
typename E>
507 template <
typename ResultType>
515 template <
typename T
518 ,
typename ResultType = detail::mbind_cb_result_t<Function, T>
523 if constexpr ( std::is_same_v<T,void> )
528 return detail::expected_make_error<ResultType>(exp.
error());
532 template <
typename T
535 ,
typename ResultType = detail::mbind_cb_result_t<Function, T>
540 if constexpr ( std::is_same_v<T,void> )
543 return std::invoke( std::forward<Function>(f), std::move(exp.get()) );
545 return detail::expected_make_error<ResultType>(exp.error());
549 template <
typename T
552 ,
typename ResultType = detail::mbind_cb_result_t<Function, E>
563 template <
typename T
566 ,
typename ResultType = detail::mbind_cb_result_t<Function, E>
571 return std::invoke( std::forward<Function>(f), std::move(exp.error()) );
582 template <
template<
class,
class... >
class Container,
586 std::
enable_if_t<!std::is_same_v<void, T>, expected<Container<T>,E>>
collect( Container<expected<T, E>, CArgs...>&& in ) {
588 for(
auto &v : in ) {
591 res.push_back( std::move(v.get()) );
600 template <
template<
class,
class... >
class Container,
605 for(
auto &v : in ) {
612 template <
typename T
619 const auto &val = exp.
get();
625 template <
typename T
632 const auto &err = exp.
error();
641 template <
typename Callback>
645 template<
typename T,
typename E >
650 template<
typename T,
typename E >
652 return and_then( std::move(exp),
function );
656 template <
typename Callback>
660 template<
typename T,
typename E >
662 return or_else( exp,
function );
665 template<
typename T,
typename E >
667 return or_else( std::move(exp),
function );
671 template <
typename Callback>
675 template<
typename T,
typename E >
677 return inspect( std::move(exp),
function );
681 template <
typename Callback>
685 template<
typename T,
typename E >
692 template <
typename T >
694 return collect( std::forward<T>(in) );
699 namespace operators {
700 template <
typename Fun>
703 std::forward<Fun>(
function)
707 template <
typename Fun>
710 std::forward<Fun>(
function)
714 template <
typename Fun>
717 std::forward<Fun>(
function)
721 template <
typename Fun>
724 std::forward<Fun>(
function)
728 template <
typename Fun>
731 std::forward<Fun>(
function)
745 template <
template<
class,
class... >
class Container,
747 typename Transformation,
753 Container<typename Ret::value_type> results;
754 for (
auto &v : in ) {
755 auto res = f(std::move(v));
757 results.push_back( std::move(res.get()) );
766 template <
typename Fun>
769 template <
typename T>
776 namespace operators {
777 template <
typename Transformation>
784 #ifdef ZYPP_ENABLE_ASYNC 785 #include <zypp-core/ng/async/pipelines/expected.hpp>
ResultType expected_make_error(E &&error)
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)
expected & operator=(U &&v)
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)
static ResultType error(E &&err)
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
expected< T, E > inspect_err(expected< T, E > exp, Function &&f)
unexpected & operator=(const unexpected &)=default
std::enable_if_t< is_instance_of_v< expected, Ret >, expected< Container< typename Ret::value_type > > > transform_collect(Container< Msg, CArgs... > &&in, Transformation &&f)
bool waitForCanContinueExpected(const expected< T > &value)
typename conditional< B, T, F >::type conditional_t
unexpected(E) -> unexpected< E >
expected(unexpected< E > &&err) noexcept
static expected success(ConsParams &&...params)
detail::collect_helper collect()
ResultType expected_forward(ResultType &&res)
expected(unexpected< G > err) noexcept
static ResultType forward(Res &&exp)
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(const unexpected< E > &err) noexcept
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)