libzypp  17.32.5
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 (...) {
38  return expected<Ret, std::exception_ptr>::error(std::current_exception());
39  }
40  }
41 
42 
43  namespace detail
44  {
45  template <typename Callback>
46  struct mtry_helper {
47  Callback function;
48 
49  template <
50  typename ...Args,
51  typename Ret = std::invoke_result_t<Callback, Args...>
52  >
53  auto operator()( Args&& ...args ){
54  return mtry( function, std::forward<Args>(args)... );
55  }
56  };
57  }
58 
59  namespace operators {
60  template <typename Fun>
61  auto mtry ( Fun && function ) {
63  std::forward<Fun>(function)
64  };
65  }
66  }
67 
68 
69 }
70 
71 #endif /* !MTRY_H */
auto operator()(Args &&...args)
Definition: mtry.h:53
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
Exp mtry(F &&f, Args &&...args)
Definition: mtry.h:28
static expected success(ConsParams &&...params)
Definition: expected.h:115