Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Meta.hxx
Go to the documentation of this file.
1 #ifndef META_HXX_
2 #define META_HXX_
3 #include <iostream>
4 #include <type_traits>
5 #include <variant>
6 #include <tuple>
7 
9 
10 template <bool value, typename ReturnType = void> using EnableIf = std::enable_if_t<value, ReturnType>;
11 
17 template <typename... Types> struct TypeList;
18 
19 template <typename T> struct TypeList<T>
20 {
21  using current = T;
22  using next = nullptr_t;
23 };
24 
25 template <typename T, typename... Ts> struct TypeList<T, Ts...>
26 {
27  using current = T;
28  using next = TypeList<Ts...>;
29 };
30 
31 template <size_t i, typename List, typename = void> struct GetType_t
32 {
33  using type = nullptr_t;
34 };
35 
36 template <size_t i, template <typename...> typename List, typename T, typename... Ts>
37 struct GetType_t<i, List<T, Ts...>, EnableIf<i == 0>>
38 {
39  using type = T;
40 };
41 
42 template <size_t i, template <typename...> typename List, typename T1, typename T2, typename... Ts>
43 struct GetType_t<i, List<T1, T2, Ts...>, EnableIf<i != 0>>
44 {
45  using type = typename GetType_t<i - 1, List<T2, Ts...>>::type;
46 };
47 
55 template <auto i, typename List> using GetType = typename GetType_t<i, List>::type;
56 
62 template <class List> struct VariantType;
63 
64 template <template <typename...> typename List, typename... Ts> struct VariantType<List<Ts...>>
65 {
66  using type = std::variant<Ts...>;
67 };
68 
74 template <class List> struct TupleType;
75 
76 template <template <typename...> typename List, typename... Ts> struct TupleType<List<Ts...>>
77 {
78  using type = std::tuple<Ts...>;
79 };
80 
81 template <typename List, typename Type> constexpr bool ContainsType = false; // lgtm [cpp/use-in-own-initializer]
82 
83 template <template <typename...> typename List, typename T1, typename T2, typename... Ts>
84 constexpr bool ContainsType<List<T1, Ts...>, T2> = std::is_same_v<T1, T2> ? true : ContainsType<List<Ts...>, T2>;
85 
92 template <auto val, typename Type = decltype(val)> using Constant = std::integral_constant<Type, val>;
93 
94 template <typename> struct GetMemberType
95 {
96 };
97 
98 template <typename Type, typename Member> struct GetMemberType<Member Type::*>
99 {
100  using type = Member;
101 };
102 
103 template <typename WeakType, typename> struct StrongType
104 {
105  StrongType() = default;
106  StrongType(const StrongType &) = default;
107  StrongType(StrongType &&) = default;
108  StrongType &operator=(const StrongType &) = default;
109  StrongType &operator=(StrongType &&) = default;
110  StrongType &operator=(const WeakType &weak)
111  {
112  m_Data = weak;
113  return *this;
114  }
115  StrongType &operator=(WeakType &&weak)
116  {
117  m_Data = weak;
118  return *this;
119  }
120 
121  template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<WeakType, Args...>>>
122  explicit inline StrongType(Args &&...args) : m_Data(std::forward<Args>(args)...)
123  {
124  }
125 
126  WeakType &get() noexcept { return m_Data; }
127  const WeakType &get() const noexcept { return m_Data; }
128 
129  friend bool operator==(const StrongType &s1, const StrongType &s2) noexcept { return s1.m_Data == s2.m_Data; }
130 
131  friend std::ostream &operator<<(std::ostream &os, const StrongType &obj) { return os << obj.m_Data; }
132 
133  friend std::istream &operator>>(std::istream &is, StrongType &obj) { return is >> obj.m_Data; }
134 
135 private:
136  WeakType m_Data;
137 };
138 
139 template <typename WeakType, typename Tag> struct std::hash<StrongType<WeakType, Tag>>
140 {
141  std::size_t operator()(const StrongType<WeakType, Tag> &key) const { return std::hash<WeakType>{}(key.get()); }
142 };
143 
144 #endif
TypeList< T >::next
nullptr_t next
Definition: Meta.hxx:22
StrongType
Definition: Meta.hxx:103
Constant
Constant::value returns the value.
StrongType::operator=
StrongType & operator=(const WeakType &weak)
Definition: Meta.hxx:110
GetType_t::type
nullptr_t type
Definition: Meta.hxx:33
GetType_t
Definition: Meta.hxx:31
StrongType::operator=
StrongType & operator=(WeakType &&weak)
Definition: Meta.hxx:115
TupleType< List< Ts... > >::type
std::tuple< Ts... > type
Definition: Meta.hxx:78
VariantType
VariantType::type returns std::variant<All fields inside the TypeList>
Definition: Meta.hxx:62
EnableIf
std::enable_if_t< value, ReturnType > EnableIf
Definition: Meta.hxx:10
TypeList
Represent a list of types.
Definition: Meta.hxx:17
std::hash< StrongType< WeakType, Tag > >::operator()
std::size_t operator()(const StrongType< WeakType, Tag > &key) const
Definition: Meta.hxx:141
StrongType::StrongType
StrongType(Args &&...args)
Definition: Meta.hxx:122
StrongType::m_Data
WeakType m_Data
Definition: Meta.hxx:136
StrongType::operator<<
friend std::ostream & operator<<(std::ostream &os, const StrongType &obj)
Definition: Meta.hxx:131
List
std::list< Type > List
Definition: AudioMixer.hxx:27
nullptr_t
std::nullptr_t nullptr_t
Definition: Meta.hxx:8
TypeList< T, Ts... >::current
T current
Definition: Meta.hxx:27
TupleType
TupleType::type returns std::tuple<All fields inside the TypeList>
Definition: Meta.hxx:74
GetMemberType
Definition: Meta.hxx:94
StrongType::operator==
friend bool operator==(const StrongType &s1, const StrongType &s2) noexcept
Definition: Meta.hxx:129
TypeList< T >::current
T current
Definition: Meta.hxx:21
StrongType::operator>>
friend std::istream & operator>>(std::istream &is, StrongType &obj)
Definition: Meta.hxx:133
StrongType::operator=
StrongType & operator=(const StrongType &)=default
GetType_t< i, List< T1, T2, Ts... >, EnableIf< i !=0 > >::type
typename GetType_t< i - 1, List< T2, Ts... > >::type type
Definition: Meta.hxx:45
GetMemberType< Member Type::* >::type
Member type
Definition: Meta.hxx:100
std
Definition: Point.hxx:83
ContainsType
constexpr bool ContainsType
Definition: Meta.hxx:81
StrongType::get
const WeakType & get() const noexcept
Definition: Meta.hxx:127
GetType
GetType is the ith type in TypeList if within range, otherwise nullptr_t.
StrongType::StrongType
StrongType()=default
GetType_t< i, List< T, Ts... >, EnableIf< i==0 > >::type
T type
Definition: Meta.hxx:39
StrongType::get
WeakType & get() noexcept
Definition: Meta.hxx:126
VariantType< List< Ts... > >::type
std::variant< Ts... > type
Definition: Meta.hxx:66