libzypp  17.35.12
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 
21 namespace 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)... );
34  } else {
35  return expected<Ret, std::exception_ptr>::success(std::invoke(std::forward<F>(f), std::forward<Args>(args)... ));
36  }
37  } catch (...) {
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 ) {
60  std::forward<Fun>(function)
61  };
62  }
63  }
64 
65 
66 }
67 
68 #endif /* !MTRY_H */
auto mtry(Fun &&function)
Definition: mtry.h:58
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:50
Exp mtry(F &&f, Args &&...args)
Definition: mtry.h:28
static expected success(ConsParams &&...params)
Definition: expected.h:115
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition: Exception.h:436