-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta_struct.hpp
More file actions
65 lines (53 loc) · 1.44 KB
/
Copy pathmeta_struct.hpp
File metadata and controls
65 lines (53 loc) · 1.44 KB
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
#pragma once
#include<algorithm> // std::copy_n
#include<cstddef> // std::size_t
namespace mlib
{
template<auto T>
struct c_p { using type = decltype(T); };
template<std::size_t i>
struct fixed_string
{
constexpr fixed_string(const char(&arr)[i + 1])
{
std::copy_n(arr, i + 1, data);
}
auto operator<=>(const fixed_string&) const = default;
char data[i + 1] = {};
};
template<std::size_t i>
fixed_string(const char(&arr)[i])->fixed_string<i - 1>;
template<fixed_string str, typename T>
struct member
{
static constexpr auto tag() { return str; }
T value;
constexpr auto type() { return T{}; }
};
template<fixed_string str>
struct value_
{
template<auto T>
constexpr auto operator=(c_p<T>)
{
return member<str, T>{};
}
};
template<fixed_string T>
inline constexpr auto value = value_<T>{};
template<typename... Members>
struct meta_struct : Members...
{
static constexpr auto members_size = (sizeof(Members) + ... + 0);
};
template<fixed_string tag, typename T>
constexpr decltype(auto) get(member<tag, T>& m)
{
return (m.value);
}
template<member m, typename Meta_struct>
constexpr decltype(auto) get(Meta_struct&& meta)
{
return get<m>(std::forward<Meta_struct>(meta));
}
} // namespace mlib