MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
meta/base.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_META_BASE_HPP
20#define MTCORE_META_BASE_HPP
21
25
30namespace mtcore::meta {
38 template<bool Cond, typename L, typename R>
39 struct If;
40
41 template<typename L, typename R>
42 struct If<true, L, R> {
43 using T = L;
44 static constexpr auto value = true;
45 };
46
47 template<typename L, typename R>
48 struct If<false, L, R> {
49 using T = R;
50 static constexpr auto value = false;
51 };
52
58 template<bool... Conds>
59 struct And {
60 static constexpr auto value = (Conds && ...);
61 };
62
68 template<bool... Conds>
69 struct Or {
70 static constexpr auto value = (Conds || ...);
71 };
72
78 template<int... T>
79 struct IntList;
80
81 template<>
82 struct IntList<> {
83 static constexpr size_t size = 0;
84 static constexpr bool empty = true;
85 template<int>
86 static constexpr bool has = false;
87 };
88
89 template<int Head, int... Rest>
90 struct IntList<Head, Rest...> {
91 static constexpr auto head = Head;
92 using Next = IntList<Rest...>;
93 static constexpr size_t size = 1 + Next::size;
94 static constexpr bool empty = false;
95
96 template<int Needle>
97 static constexpr bool has = Needle == Head || Next::template has<Needle>;
98 };
99
100 static_assert(IntList<1, 2, 3>::has<3>);
101 static_assert(IntList<1, 2, 3>::has<2>);
102 static_assert(!IntList<1, 2, 3>::has<6>);
103
109 template<typename... T>
110 struct TypeList;
111
112 template<>
113 struct TypeList<> {
114 static constexpr size_t size = 0;
115 static constexpr bool empty = 0;
116 };
117
118 template<typename Head, typename... Rest>
119 struct TypeList<Head, Rest...> {
120 using T = Head;
121 using Next = TypeList<Rest...>;
122 static constexpr size_t size = 1 + Next::size;
123 static constexpr bool empty = false;
124 };
125
131 template<typename... T>
133
134 template<typename L>
135 struct ConcatIntList<L> {
136 using T = L;
137 };
138
139 template<int... L1, int... L2, typename... LRest>
140 struct ConcatIntList<IntList<L1...>, IntList<L2...>, LRest...> {
141 using T = typename ConcatIntList<IntList<L1..., L2...>, LRest...>::T;
142 };
143
144 static_assert(ConcatIntList<IntList<1, 2>, IntList<3>>::T::has<3>);
145 static_assert(!ConcatIntList<IntList<1, 2>, IntList<3>>::T::has<6>);
146} // namespace mtcore::meta
147
148#endif // MTCORE_META_BASE_HPP
Meta is a meta programming utility helper Mostly focused on templates and constexpr.
Definition meta/base.hpp:30
Checks that all conditions hold true.
Definition meta/base.hpp:59
static constexpr auto value
Definition meta/base.hpp:60
typename ConcatIntList< IntList< L1..., L2... >, LRest... >::T T
Concatenates two or more IntLists.
static constexpr auto value
Definition meta/base.hpp:50
static constexpr auto value
Definition meta/base.hpp:44
Switches types based on a condition.
Definition meta/base.hpp:39
A list of integers, often used for indexing into template lists.
Definition meta/base.hpp:79
Checks that at least one condition hold true.
Definition meta/base.hpp:69
static constexpr auto value
Definition meta/base.hpp:70
static constexpr size_t size
static constexpr bool empty
A list of types, often used as a container when there isn't a more specialized container.