File: expected.hpp

package info (click to toggle)
cppgir 2.0%2Bgit20250629.2a7d9ce-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,220 kB
  • sloc: cpp: 16,451; ansic: 355; python: 86; makefile: 13; sh: 9
file content (112 lines) | stat: -rw-r--r-- 2,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef GI_EXPECTED_HPP
#define GI_EXPECTED_HPP

#include "base.hpp"
#include "exception.hpp"

GI_MODULE_EXPORT
namespace gi
{
// alias so we might route to a std type some day ...
template<typename T, typename E>
#ifdef expected_lite_VERSION
using expected = nonstd::expected<T, E>;
#else
using expected = std::expected<T, E>;
#endif

template<typename E>
#ifdef expected_lite_VERSION
using unexpected = nonstd::unexpected_type<E>;
#else
using unexpected = std::unexpected<E>;
#endif

// standardize on glib error
template<typename T>
using result = expected<T, repository::GLib::Error>;

namespace detail
{
// only use nonstd if it does not delegate to std (in incomplete way)
#if defined(expected_lite_VERSION) && !nsel_USES_STD_EXPECTED
inline unexpected<repository::GLib::Error>
make_unexpected(repository::GLib::Error error)
{
  assert(error);
  return nonstd::make_unexpected(std::move(error));
}
#else
inline unexpected<repository::GLib::Error>
make_unexpected(repository::GLib::Error error)
{
  assert(error);
  return std::unexpected<repository::GLib::Error>(std::move(error));
}
#endif

inline unexpected<repository::GLib::Error>
make_unexpected(GError *error)
{
  assert(error);
  return make_unexpected(repository::GLib::Error(error));
}
} // namespace detail

// no forwarding reference; T must be non-reference type
template<typename T>
result<T>
make_result(T t, GError *error)
{
  if (error)
    return detail::make_unexpected(error);
  return t;
}

// rough helpers to unwrap result/expected
// unwrap by move
template<typename T>
T
expect(gi::result<T> &&t)
{
  if (!t) {
    g_critical("error result %s", t.error().what());
    detail::try_throw(std::move(t.error()));
  }
  return std::move(*t);
}

namespace detail
{
template<typename T>
void test_result(const gi::result<T> &);
template<typename T>
int test_result(const T &);
template<typename T>
using is_result = std::is_same<void,
    decltype(test_result(std::forward<T>(std::declval<T>())))>;
} // namespace detail

// should only be used for a non-result
// (e.g. avoid l-value result ending up here)
template<typename T, typename Enable = typename std::enable_if<
                         !detail::is_result<T>::value>::type>
T
expect(T &&t)
{
  return std::forward<T>(t);
}

template<typename T>
struct rv
{
#if GI_DL && GI_EXPECTED
  using type = gi::result<T>;
#else
  using type = T;
#endif
};

} // namespace gi

#endif // GI_EXPECTED_HPP