libzypp: zypp-core/ng/pipelines/mtry.h Source File

libzypp 17.38.8

mtry.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13* Based on code by Ivan Čukić (BSD/MIT licensed) from the functional cpp book
14*/
15
16#ifndef ZYPP_ZYPPNG_MONADIC_MTRY_H
17#define ZYPP_ZYPPNG_MONADIC_MTRY_H
18
19#include "expected.h"
20#include <zypp-core/base/Exception.h>
21#include <zypp-core/ng/pipelines/operators.h>
22
23namespace zyppng {
24
25 namespace detail {
26
27 template <typename Ret, typename F, typename ...Args >
28 struct MtryImpl {
29 static expected<Ret> execute( F &&f, Args&& ...args )
30 {
31 try {
32 if constexpr ( std::is_same_v<void, Ret> ) {
33 std::invoke(std::forward<F>(f), std::forward<Args>(args)... );
34 return expected<Ret, std::exception_ptr>::success();
35 } else {
36 return expected<Ret, std::exception_ptr>::success(std::invoke(std::forward<F>(f), std::forward<Args>(args)... ));
37 }
38 } catch (...) {
39 return expected<Ret, std::exception_ptr>::error(ZYPP_FWD_CURRENT_EXCPT());
40 }
41 }
42 };
43 }
44
45 template < typename F
46 , typename ...Args
47 , typename Ret = std::invoke_result_t<F, Args...>
48 , typename Exp = expected<Ret, std::exception_ptr>
49 >
50 auto mtry(F &&f, Args&& ...args)
51 {
52 return detail::MtryImpl<Ret, F, Args...>::execute( std::forward<F>(f), std::forward<Args>(args)... );
53 }
54
55
56 namespace detail
57 {
58 template <typename Callback>
59 struct mtry_helper {
60 Callback function;
61
62 template < typename ...Args >
63 auto operator()( Args&& ...args ){
64 return mtry( function, std::forward<Args>(args)... );
65 }
66 };
67 }
68
69 namespace operators {
70 template <typename Fun>
71 auto mtry ( Fun && function ) {
72 return ::zyppng::detail::mtry_helper<Fun> {
73 std::forward<Fun>(function)
74 };
75 }
76 }
77}
78
79#ifdef ZYPP_ENABLE_ASYNC
80#include <zypp-core/ng/async/pipelines/mtry.hpp>
81#endif
82
83#endif /* !MTRY_H */
Exception.h
ZYPP_FWD_CURRENT_EXCPT
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition Exception.h:471
zyppng::expected
Definition expected.h:56
zyppng::expected::success
static expected success(ConsParams &&...params)
Definition expected.h:178
zyppng::expected::error
E & error()
Definition expected.h:288
expected.h
std::invoke
std::enable_if< std::is_member_pointer< typenamestd::decay< Functor >::type >::value, typenamestd::result_of< Functor &&(Args &&...)>::type >::type invoke(Functor &&f, Args &&... args)
Definition functional.h:32
zyppng::detail
Definition reporthelper.h:31
zyppng::operators::mtry
auto mtry(Fun &&function)
Definition mtry.h:71
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::mtry
auto mtry(F &&f, Args &&...args)
Definition mtry.h:50
zyppng::detail::MtryImpl
Definition mtry.h:28
zyppng::detail::MtryImpl::execute
static expected< Ret > execute(F &&f, Args &&...args)
Definition mtry.h:29
zyppng::detail::mtry_helper
Definition mtry.h:59
zyppng::detail::mtry_helper::function
Callback function
Definition mtry.h:60
zyppng::detail::mtry_helper::operator()
auto operator()(Args &&...args)
Definition mtry.h:63