capydi
Loading...
Searching...
No Matches
MetaMap.hpp
Go to the documentation of this file.
8
9namespace capy::meta
10{
11
12template<typename Key, typename Value>
13class KVPair
14{
15public:
16 constexpr KVPair(Unit<Key>&&, Value&& value)
17 : value_ { std::move(value) }
18 {}
19
20public:
21 constexpr RuntimeRef<Value>
23 {
24 return value_;
25 }
26
28 inner_search(Unit<Key>&&) const noexcept
29 {
30 return value_;
31 }
32
33private:
34 Value value_;
35};
36
37template<wrapped_with<Pack> KeysPack, typename Value>
38 requires (pack_size_v<KeysPack> > 0)
40{
41public:
42 constexpr MultyKVPair(KeysPack&&, Value&& value)
43 : value_ { std::move(value) }
44 {}
45
46public:
47 template<typename Key>
49 constexpr RuntimeRef<Value>
50 inner_search(Unit<Key>&& key) noexcept
51 {
52 return [this, &key]<typename... Keys>(Pack<Keys...>) -> Value& {
53 return Overload {
54 [this](Unit<Keys>&&) -> Value& {
55 return this->value_;
56 }...
57 }(std::move(key));
58 }(KeysPack{});
59 }
60
61 template<typename Key>
64 inner_search(Unit<Key>&& key) const noexcept
65 {
66 return [this, &key]<typename... Keys>(Pack<Keys...>) -> const Value& {
67 return Overload {
68 [this](Unit<Keys>&&) -> const Value& {
69 return this->value_;
70 }...
71 }(std::move(key));
72 }(KeysPack{});
73 }
74
75private:
76 Value value_;
77};
78
79template <typename... KVPairs>
80class MetaMap : private KVPairs...
81{
82private:
83 using KVPairs::inner_search...;
84
85public:
86 constexpr explicit MetaMap(KVPairs&&... pairs)
87 : KVPairs(std::move(pairs))...
88 {}
89
90public:
91 template<typename Self, typename Key>
92 constexpr wrapped_with<std::optional> auto
93 find(this Self&& self, Unit<Key>&& key) noexcept
94 {
95 return std::forward<Self>(self)
96 .static_find(std::move(key))
97 .as_optional();
98 }
99
100 template<typename Self, typename Key>
101 constexpr wrapped_with<StaticMaybe> auto
102 static_find(this Self&& self, Unit<Key>&& key) noexcept
103 {
104 #define RETRIEVAL_CALL_ \
105 std::forward<Self>(self).inner_search(std::move(key))
106
107 if constexpr (requires { RETRIEVAL_CALL_; })
108 {
109 return StaticMaybe {
111 };
112 }
113 else
114 {
115 return StaticNone<None>{};
116 }
117
118 #undef RETRIEVAL_CALL_
119 }
120};
121
122}
#define RETRIEVAL_CALL_
Compile-time type pack utilities and metaprogramming foundations.
constexpr RuntimeRef< const Value > inner_search(Unit< Key > &&) const noexcept
Definition MetaMap.hpp:28
constexpr RuntimeRef< Value > inner_search(Unit< Key > &&) noexcept
Definition MetaMap.hpp:22
constexpr KVPair(Unit< Key > &&, Value &&value)
Definition MetaMap.hpp:16
constexpr MetaMap(KVPairs &&... pairs)
Definition MetaMap.hpp:86
constexpr wrapped_with< std::optional > auto find(this Self &&self, Unit< Key > &&key) noexcept
Definition MetaMap.hpp:93
constexpr wrapped_with< StaticMaybe > auto static_find(this Self &&self, Unit< Key > &&key) noexcept
Definition MetaMap.hpp:102
constexpr MultyKVPair(KeysPack &&, Value &&value)
Definition MetaMap.hpp:42
constexpr RuntimeRef< const Value > inner_search(Unit< Key > &&key) const noexcept
Definition MetaMap.hpp:64
constexpr RuntimeRef< Value > inner_search(Unit< Key > &&key) noexcept
Definition MetaMap.hpp:50
Definition RuntimeRef.hpp:13
Definition StaticMaybe.hpp:23
Definition WrappedWIth.hpp:30
Definition Rebind.hpp:7
StaticMaybe< Value, implementation_details_::NoneTag > StaticNone
Definition StaticMaybe.hpp:85
constexpr bool pack_contains_t
Definition Contains.hpp:13
constexpr std::size_t pack_size_v
Definition Pack.hpp:73
Definition Overload.hpp:9
A compile-time heterogeneous type list.
Definition Pack.hpp:70
A zero-cost wrapper for forwarding compile-time type information.
Definition Pack.hpp:45