capydi
Loading...
Searching...
No Matches
StaticEither.hpp
Go to the documentation of this file.
1#ifndef CAPYMETA_STATIC_EITHER_HPP_
2#define CAPYMETA_STATIC_EITHER_HPP_
3
4#include <expected>
5
6namespace capy::meta
7{
8
10{
11 /*
12 We tend to use type tags instead of bools
13 to allow using wrapped_with concept for
14 return values that have StaticEither-ish type
15 */
16 struct ValueTag{};
17 struct ErrorTag{};
18}
19
20template<typename Value, typename Error, typename Tag>
22
23template<typename Value, typename Error>
24class StaticEither<Value, Error, implementation_details_::ValueTag>
25{
26public:
27 using value_type = Value;
28 using error_type = Error;
29
30public:
31 constexpr /* implicit */ StaticEither(Value&& value)
32 : value_ { std::move(value) }
33 {}
34
35public:
36 static consteval bool has_value()
37 {
38 return HAS_VALUE;
39 }
40
41 template<typename Self>
42 constexpr decltype(auto) value(this Self&& self)
43 {
44 return std::forward<Self>(self).value_;
45 }
46
47 template<typename Self>
48 constexpr decltype(auto) error(this Self&& self)
49 {
50 static_assert(
51 false,
52 "Trying to extract an error while the either-object contains value"
53 );
54 }
55
56 constexpr std::expected<Value, Error> as_expected() &&
57 {
58 return std::expected { std::move(this->value_) };
59 }
60
61private:
62 static constexpr bool HAS_VALUE = true;
63
64private:
65 Value value_;
66};
67
68template<typename Value, typename Error>
69class StaticEither<Value, Error, implementation_details_::ErrorTag>
70{
71public:
72 using value_type = Value;
73 using error_type = Error;
74
75public:
76 constexpr /* implicit */ StaticEither(Error&& error)
77 : error_ { std::move(error) }
78 {}
79
80public:
81 static consteval bool has_value()
82 {
83 return HAS_VALUE;
84 }
85
86 template<typename Self>
87 constexpr decltype(auto) value(this Self&& self)
88 {
89 static_assert(false, "Trying to retrieve value from error either-object");
90 }
91
92 template<typename Self>
93 constexpr decltype(auto) error(this Self&& self)
94 {
95 return std::forward<Self>(self).error_;
96 }
97
98 constexpr std::expected<Value, Error> as_expected() &&
99 {
100 return std::unexpected { std::move(this->error_) };
101 }
102
103private:
104 static constexpr bool HAS_VALUE = false;
105
106private:
107 Error error_;
108};
109
110template<typename Value, typename Error>
112
113template<typename Value, typename Error>
115
116}
117
118#endif // !CAPYMETA_STATIC_EITHER_HPP_
constexpr decltype(auto) error(this Self &&self)
Definition StaticEither.hpp:93
constexpr StaticEither(Error &&error)
Definition StaticEither.hpp:76
constexpr decltype(auto) value(this Self &&self)
Definition StaticEither.hpp:87
constexpr std::expected< Value, Error > as_expected() &&
Definition StaticEither.hpp:98
static consteval bool has_value()
Definition StaticEither.hpp:81
static consteval bool has_value()
Definition StaticEither.hpp:36
constexpr decltype(auto) error(this Self &&self)
Definition StaticEither.hpp:48
constexpr std::expected< Value, Error > as_expected() &&
Definition StaticEither.hpp:56
constexpr StaticEither(Value &&value)
Definition StaticEither.hpp:31
constexpr decltype(auto) value(this Self &&self)
Definition StaticEither.hpp:42
Definition StaticEither.hpp:21
Definition Rebind.hpp:7
StaticEither< Value, Error, implementation_details_::ValueTag > StaticOk
Definition StaticEither.hpp:111
StaticEither< Value, Error, implementation_details_::ErrorTag > StaticError
Definition StaticEither.hpp:114
Definition StaticEither.hpp:17
Definition StaticEither.hpp:16