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

libzypp 17.36.6

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
21namespace zyppng {
22
23 template < typename F
24 , typename ...Args
25 , typename Ret = std::invoke_result_t<F, Args...>
26 , typename Exp = expected<Ret, std::exception_ptr>
27 >
28 Exp mtry(F &&f, Args&& ...args)
29 {
30 try {
31 if constexpr ( std::is_same_v<void, Ret> ) {
32 std::invoke(std::forward<F>(f), std::forward<Args>(args)... );
33 return expected<Ret, std::exception_ptr>::success();
34 } else {
35 return expected<Ret, std::exception_ptr>::success(std::invoke(std::forward<F>(f), std::forward<Args>(args)... ));
36 }
37 } catch (...) {
38 return expected<Ret, std::exception_ptr>::error(ZYPP_FWD_CURRENT_EXCPT());
39 }
40 }
41
42
43 namespace detail
44 {
45 template <typename Callback>
46 struct mtry_helper {
47 Callback function;
48
49 template < typename ...Args >
50 auto operator()( Args&& ...args ){
51 return mtry( function, std::forward<Args>(args)... );
52 }
53 };
54 }
55
56 namespace operators {
57 template <typename Fun>
58 auto mtry ( Fun && function ) {
59 return detail::mtry_helper<Fun> {
60 std::forward<Fun>(function)
61 };
62 }
63 }
64
65
66}
67
68#endif /* !MTRY_H */
zyppng::expected
Definition expected.h:27
zyppng::expected::success
static expected success(ConsParams &&...params)
Definition expected.h:115
zyppng::expected::error
E & error()
Definition expected.h:225
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::operators::mtry
auto mtry(Fun &&function)
Definition mtry.h:58
zyppng
Definition MediaCurl2.h:22
zyppng::mtry
Exp mtry(F &&f, Args &&...args)
Definition mtry.h:28
zyppng::detail::mtry_helper
Definition mtry.h:46
zyppng::detail::mtry_helper::function
Callback function
Definition mtry.h:47
zyppng::detail::mtry_helper::operator()
auto operator()(Args &&...args)
Definition mtry.h:50
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:436