libzypp  17.38.7
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"
21 #include <zypp-core/ng/pipelines/operators.h>
22 
23 namespace 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)... );
35  } else {
36  return expected<Ret, std::exception_ptr>::success(std::invoke(std::forward<F>(f), std::forward<Args>(args)... ));
37  }
38  } catch (...) {
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 */
static expected< Ret > execute(F &&f, Args &&...args)
Definition: mtry.h:29
auto mtry(Fun &&function)
Definition: mtry.h:71
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)
Definition: functional.h:32
auto operator()(Args &&...args)
Definition: mtry.h:63
static expected success(ConsParams &&...params)
Definition: expected.h:178
auto mtry(F &&f, Args &&...args)
Definition: mtry.h:50
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition: Exception.h:471