File: expected.hpp

package info (click to toggle)
cppgir 2.0%2Bgit20240928.c8bb1c6%2Breally2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,228 kB
  • sloc: cpp: 14,307; ansic: 339; makefile: 11; sh: 9
file content (107 lines) | stat: -rw-r--r-- 2,214 bytes parent folder | download | duplicates (2)
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
#ifndef GI_EXPECTED_HPP
#define GI_EXPECTED_HPP

#if __has_include("nonstd/expected.hpp")
#include "nonstd/expected.hpp"
#else
#include <expected>
#endif

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

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

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

namespace detail
{
#ifdef expected_lite_VERSION
inline nonstd::unexpected_type<repository::GLib::Error>
make_unexpected(GError *error)
{
  assert(error);
  return nonstd::make_unexpected(repository::GLib::Error(error));
}

inline nonstd::unexpected_type<repository::GLib::Error>
make_unexpected(repository::GLib::Error error)
{
  assert(error);
  return nonstd::make_unexpected(std::move(error));
}
#else
inline std::unexpected<repository::GLib::Error>
make_unexpected(GError *error)
{
  assert(error);
  return std::unexpected<repository::GLib::Error>(error);
}
#endif
} // 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)
    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