capydi
Loading...
Searching...
No Matches
StaticMaybe.hpp
Go to the documentation of this file.
1#ifndef CAPYMETA_STATIC_MAYBE_HPP_
2#define CAPYMETA_STATIC_MAYBE_HPP_
3
4#include "Maybe.hpp"
5
6#include <optional>
7
8namespace capy::meta
9{
10
12{
13 /*
14 We tend to use type tags instead of bools
15 to allow using wrapped_with concept for
16 return values that have StaticMaybe-ish type
17 */
18 struct SomeTag{};
19 struct NoneTag{};
20}
21
22template<typename Value, typename Tag>
24
25template<typename Value>
27{
28public:
29 constexpr explicit StaticMaybe(Value&& value)
30 : value_ { std::move(value) }
31 {}
32
33public:
34 static consteval bool has_value()
35 {
36 return HAS_VALUE;
37 }
38
39 template<typename Self>
40 constexpr decltype(auto) value(this Self&& self)
41 {
42 return std::forward<Self>(self).value_;
43 }
44
45 constexpr std::optional<Value> as_optional() &&
46 {
47 return std::optional { std::move(this->value_) };
48 }
49
50private:
51 static constexpr bool HAS_VALUE = true;
52
53private:
54 Value value_;
55};
56
57template<typename Value>
59{
60public:
61 static consteval bool has_value()
62 {
63 return HAS_VALUE;
64 }
65
66 template<typename Self>
67 constexpr decltype(auto) value(this Self&& self)
68 {
69 static_assert(false, "Trying to retrieve value from null optional");
70 }
71
72 constexpr std::optional<Value> as_optional() &&
73 {
74 return std::nullopt;
75 }
76
77private:
78 static constexpr bool HAS_VALUE = false;
79};
80
81template<typename Value>
83
84template<typename Value>
86
87}
88
89#endif // CAPYMETA_STATIC_MAYBE_HPP_
constexpr decltype(auto) value(this Self &&self)
Definition StaticMaybe.hpp:67
static consteval bool has_value()
Definition StaticMaybe.hpp:61
constexpr std::optional< Value > as_optional() &&
Definition StaticMaybe.hpp:72
static consteval bool has_value()
Definition StaticMaybe.hpp:34
constexpr std::optional< Value > as_optional() &&
Definition StaticMaybe.hpp:45
constexpr StaticMaybe(Value &&value)
Definition StaticMaybe.hpp:29
constexpr decltype(auto) value(this Self &&self)
Definition StaticMaybe.hpp:40
Definition StaticMaybe.hpp:23
Definition Rebind.hpp:7
StaticMaybe< Value, implementation_details_::NoneTag > StaticNone
Definition StaticMaybe.hpp:85
StaticMaybe(Value &&) -> StaticMaybe< Value, implementation_details_::SomeTag >