TrueReality  v0.1.1912
TypeList.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * The Base of this class has been adopted from the Delta3D engine
6 *
7 * This library is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the Free
9 * Software Foundation; either version 3.0 of the License, or (at your option)
10 * any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15 * details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * @author Maxim Serebrennik
22 */
23 
24 #pragma once
25 
26 namespace trUtil
27 {
28  /*
29  * This file contains small collection of generic programming templates etc
30  * gathered from various sources (Loki library is the main one). Some of them
31  * has been modified, others have remained intact.
32  * Copyright Aleksei Trunov 2005
33  * Use, copy, modify, distribute and sell it for free.
34  */
35 
36  template <bool flag, typename T, typename U>
37  struct Select
38  {
39  using Result = T;
40  };
41  template <typename T, typename U>
42  struct Select<false, T, U>
43  {
44  using Result = U;
45  };
46 
47  template <int v> struct Int2Type { enum { value = v }; };
48 
49  template<class T, int i> struct IsIntType { enum { value = false }; };
50  template<int i> struct IsIntType<Int2Type<i>, i> { enum { value = true }; };
51  template<class T, int i> struct NotIntType { enum { value = !IsIntType<T, i>::value }; };
52 
58  class NullType {};
59  struct EmptyType {};
60 
69  template <class T, class U>
70  struct TypeList
71  {
72  using Head = T;
73  using Tail = U;
74  };
75 
76  template <
77  class T1 = NullType
78  , class T2 = NullType
79  , class T3 = NullType
80  , class T4 = NullType
81  , class T5 = NullType
82  , class T6 = NullType
83  , class T7 = NullType
84  , class T8 = NullType
85  > struct CreateTL
86  {
88  };
90  {
91  using Type = NullType;
92  };
93 
94  template <
95  int i1 = -1
96  , int i2 = -1
97  , int i3 = -1
98  , int i4 = -1
99  , int i5 = -1
100  , int i6 = -1
101  , int i7 = -1
102  , int i8 = -1
103  > struct CreateIdsTL
104  {
105  using Type = TypeList<Int2Type<i1>, typename CreateIdsTL<i2, i3, i4, i5, i6, i7, i8, -1>::Type>;
106  };
107  template <> struct CreateIdsTL<-1, -1, -1, -1, -1, -1, -1, -1> { using Type = NullType; };
108 
109  template <class TL, int i = 0> struct IdsFromTL
110  {
112  };
113  template <int i> struct IdsFromTL<NullType, i> { using Type = NullType; };
114 
115  template <class TList, class T>
116  struct AppendTL
117  {
119  };
120  template <class T>
121  struct AppendTL<NullType, T>
122  {
124  };
125 
129 #define TYPELIST_0() trUtil::NullType
130 #define TYPELIST_1(T1) trUtil::TypeList<T1, TYPELIST_0()>
131 #define TYPELIST_2(T1, T2) trUtil::TypeList<T1, TYPELIST_1(T2)>
132 #define TYPELIST_3(T1, T2, T3) trUtil::TypeList<T1, TYPELIST_2(T2, T3)>
133 #define TYPELIST_4(T1, T2, T3, T4) trUtil::TypeList<T1, TYPELIST_3(T2, T3, T4)>
134 #define TYPELIST_5(T1, T2, T3, T4, T5) trUtil::TypeList<T1, TYPELIST_4(T2, T3, T4, T5)>
135 #define TYPELIST_6(T1, T2, T3, T4, T5, T6) trUtil::TypeList<T1, TYPELIST_5(T2, T3, T4, T5, T6)>
136 #define TYPELIST_7(T1, T2, T3, T4, T5, T6, T7) trUtil::TypeList<T1, TYPELIST_6(T2, T3, T4, T5, T6, T7)>
137 #define TYPELIST_8(T1, T2, T3, T4, T5, T6, T7, T8) trUtil::TypeList<T1, TYPELIST_7(T2, T3, T4, T5, T6, T7, T8)>
138 
139  template <class TList> struct Length;
140  template <> struct Length<NullType>
141  {
142  enum { value = 0 };
143  };
144  template <class T, class U>
145  struct Length< TypeList<T, U> >
146  {
147  enum { value = 1 + Length<U>::value };
148  };
149 
150  template <class TList, unsigned int i> struct TypeAt;
151  template <class T, class U>
152  struct TypeAt< TypeList<T, U>, 0 >
153  {
154  using Result = T;
155  };
156  template <class T, class U, unsigned int i>
157  struct TypeAt< TypeList<T, U>, i >
158  {
159  using Result = typename TypeAt<U, i - 1>::Result;
160  };
161 
162  template <class TList, unsigned int i, typename DefType = NullType>
164  {
165  using Result = DefType;
166  };
167  template <class T, class U, typename DefType>
168  struct TypeAtNonStrict< TypeList<T, U>, 0, DefType >
169  {
170  using Result = T;
171  };
172  template <class T, class U, unsigned int i, typename DefType>
173  struct TypeAtNonStrict< TypeList<T, U>, i, DefType >
174  {
175  using Result = typename TypeAtNonStrict<U, i - 1, DefType>::Result;
176  };
177 
178  // Tuples-related
179 
180  template <typename T, unsigned int i = 0> struct TupleHolder
181  {
182  using Type = T;
183  using StoredType = T;
186  TupleHolder(Type t) : value(t) {}
187  TupleHolder& operator=(TupleHolder const& v) { value = v.value; }
188  };
189 
190  template <typename TList, template <class, unsigned int> class Holder, unsigned int i = 0> struct InstantiateH;
191  template <typename T, typename U, template <class, unsigned int> class Holder, unsigned int i>
192  struct InstantiateH<TypeList<T, U>, Holder, i>
193  : public Holder<typename TypeList<T, U>::Head, i>
194  , public InstantiateH<typename TypeList<T, U>::Tail, Holder, i + 1>
195  {
196  enum { ordern = i };
197  using LeftBase = Holder<typename TypeList<T, U>::Head, i>;
203  };
204  template <template <class, unsigned int> class Holder, unsigned int i>
205  struct InstantiateH<NullType, Holder, i>
206  {
208  };
209 
210  template <typename InstH, unsigned int j, unsigned int i = 0> struct TailAt;
211  template <typename T, typename U, template <class, unsigned int> class Holder, unsigned int i>
212  struct TailAt<InstantiateH<TypeList<T, U>, Holder, i>, 0, i>
213  {
215  };
216  template <typename T, typename U, template <class, unsigned int> class Holder, unsigned int j, unsigned int i>
217  struct TailAt<InstantiateH<TypeList<T, U>, Holder, i>, j, i>
218  {
219  using Result = typename TailAt<InstantiateH<typename TypeList<T, U>::Tail, Holder, i + 1>, j - 1, i + 1>::Result;
220  };
221 
222  template <unsigned int j, typename InstH, unsigned int i = 0> struct InstantiateHAccessor;
223  template <typename T, typename U, template <class, unsigned int> class Holder, unsigned int i>
224  struct InstantiateHAccessor<0, InstantiateH<TypeList<T, U>, Holder, i>, i>
225  {
228  static inline TargetHolder& Get(Instance& h) { return static_cast<TargetHolder&>(h); }
229  static inline TargetHolder const& Get(Instance const& h) { return static_cast<TargetHolder const&>(h); }
230  };
231  template <unsigned int j, typename T, typename U, template <class, unsigned int> class Holder, unsigned int i>
232  struct InstantiateHAccessor<j, InstantiateH<TypeList<T, U>, Holder, i>, i>
233  {
235  using TargetHolder = Holder<typename TypeAt<TypeList<T, U>, j>::Result, j + i>;
236  using RightBase = typename Instance::RightBase;
237  static inline TargetHolder& Get(Instance& h) { return InstantiateHAccessor<j - 1, RightBase, i + 1>::Get(static_cast<RightBase&>(h)); }
238  static inline TargetHolder const& Get(Instance const& h) { return InstantiateHAccessor<j - 1, RightBase, i + 1>::Get(static_cast<RightBase const&>(h)); }
239  };
240  template <unsigned int j, class Instantiated> inline
242  GetH(Instantiated& h)
243  {
245  }
246  template <unsigned int j, class Instantiated> inline
248  GetH(Instantiated const& h)
249  {
251  }
252 }
typename TypeAt< U, i - 1 >::Result Result
Definition: TypeList.h:159
A null type class.
Definition: TypeList.h:58
StoredType value
Definition: TypeList.h:184
InstantiateH(typename TypeList< T, U >::Head h, NullType)
Definition: TypeList.h:200
typename TypeAtNonStrict< U, i - 1, DefType >::Result Result
Definition: TypeList.h:175
List of types structure.
Definition: TypeList.h:70
InstantiateH(typename TypeList< T, U >::Head h)
Definition: TypeList.h:201
InstantiateH(typename TypeList< T, U >::Head h, RightBase const &t)
Definition: TypeList.h:199
InstantiateHAccessor< j, Instantiated, Instantiated::ordern >::TargetHolder & GetH(Instantiated &h)
Definition: TypeList.h:242
TupleHolder(Type t)
Definition: TypeList.h:186
Holder< typename TypeAt< TypeList< T, U >, j >::Result, j+i > TargetHolder
Definition: TypeList.h:235
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
Holder< typename TypeList< T, U >::Head, i > LeftBase
Definition: TypeList.h:197
TupleHolder & operator=(TupleHolder const &v)
Definition: TypeList.h:187
typename TailAt< InstantiateH< typename TypeList< T, U >::Tail, Holder, i+1 >, j - 1, i+1 >::Result Result
Definition: TypeList.h:219