ug4
metaprogramming_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Andreas Vogel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef __H__COMMON__METAPROGRAMMING_UTIL__
34 #define __H__COMMON__METAPROGRAMMING_UTIL__
35 
36 namespace ug {
37 
40 
41 template <int N>
42 struct Int2Type {
43  enum{ value = N};
44  typedef int value_type;
45 };
46 
47 template <class T>
48 struct Pointer2Value{};
49 
50 template <class T>
51 struct Pointer2Value<T*>{
52  typedef T type;
53 };
54 
56 // TypeList
58 
59 // empty type
60 struct EmptyType {};
61 
62 // TypeList
63 template
64 <
65  typename T1=EmptyType,
66  typename T2=EmptyType,
67  typename T3=EmptyType,
68  typename T4=EmptyType,
69  typename T5=EmptyType,
70  typename T6=EmptyType,
71  typename T7=EmptyType,
72  typename T8=EmptyType,
73  typename T9=EmptyType,
74  typename T10=EmptyType,
75  typename T11=EmptyType,
76  typename T12=EmptyType
77 > struct TypeList;
78 
79 // implementation of TypeList
80 template
81 <
82  typename T1,
83  typename T2,
84  typename T3,
85  typename T4,
86  typename T5,
87  typename T6,
88  typename T7,
89  typename T8,
90  typename T9,
91  typename T10,
92  typename T11,
93  typename T12
94 >
95 struct TypeList
96 {
97  typedef T1 head;
99  enum{length = tail::length+1};
100 };
101 
102 // empty typelist specialization
103 template<>
107 {
108  enum{length = 0};
109 };
110 
112 // TypeValueList
114 
115 // TypeList
116 template <typename TTypeList> struct TypeValueList
117 {
118  typedef typename TTypeList::head head;
119  typedef typename TTypeList::tail tail;
120 
123 
124  explicit TypeValueList() {
125  std::cerr << "incomplete TVL\n";
126  }
127 
129  TypeValueList<tail> typValList) :
130  hd(_hd), tl(typValList) {}
131 
132 };
133 
134 template <>
135 struct TypeValueList< TypeList<> > {};
136 
137 
139 // Factorial
141 
145 template <size_t n>
146 struct Factorial
147 {
148  enum{value = n*Factorial<n-1>::value};
149 };
150 
151 template <>
152 struct Factorial<1>
153 {
154  enum {value = 1};
155 };
156 
158 // Pow
160 
163 template <int n, size_t d>
164 struct Pow
165 {
166  enum{value = n*Pow<n, d-1>::value};
167 };
168 
169 template <int n>
170 struct Pow<n, 0>
171 {
172  enum {value = 1};
173 };
174 
176 // BinomialCoefficient
178 
186 template <size_t n, int k>
188 {
189  enum { value = Factorial<n>::value/
191 };
192 
193 // end rekursion
194 template <size_t n>
196 {
197  enum { value = 1};
198 };
199 // end rekursion
200 template <size_t n>
202 {
203  enum { value = 0};
204 };
205 // end rekursion
206 template <size_t n>
208 {
209  enum { value = 0};
210 };
211 // end rekursion
212 template <size_t n>
214 {
215  enum { value = 0};
216 };
217 // end rekursion
218 template <size_t n>
220 {
221  enum { value = 0};
222 };
223 
225 // UniqueTypeID (sreiter)
229  public:
231  static UniqueTypeIDProvider utid;
232  return utid;
233  }
234 
235  size_t new_id() {return ++m_id;}
236 
237  private:
239  size_t m_id;
240 };
241 
243 template <class TType>
245 {
246  static size_t typeID = UniqueTypeIDProvider::inst().new_id();
247  return typeID;
248 }
249 
250 // end group ugbase_common_util
252 
253 }
254 
255 #endif /* __H__COMMON__METAPROGRAMMING_UTIL__ */
a singleton class that returns a new id for each type
Definition: metaprogramming_util.h:228
UniqueTypeIDProvider()
Definition: metaprogramming_util.h:238
size_t new_id()
Definition: metaprogramming_util.h:235
size_t m_id
Definition: metaprogramming_util.h:239
static UniqueTypeIDProvider & inst()
Definition: metaprogramming_util.h:230
function common tail(list, first)
function common head(list, last)
size_t GetUniqueTypeID()
This method associated a unique unsigned integer value with each type.
Definition: metaprogramming_util.h:244
the ug namespace
Definition: metaprogramming_util.h:188
@ value
Definition: metaprogramming_util.h:189
Definition: metaprogramming_util.h:60
Definition: metaprogramming_util.h:147
@ value
Definition: metaprogramming_util.h:148
Definition: metaprogramming_util.h:42
@ value
Definition: metaprogramming_util.h:43
int value_type
Definition: metaprogramming_util.h:44
T type
Definition: metaprogramming_util.h:52
Definition: metaprogramming_util.h:48
Definition: metaprogramming_util.h:165
@ value
Definition: metaprogramming_util.h:166
Definition: metaprogramming_util.h:96
TypeList< T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 > tail
Definition: metaprogramming_util.h:98
T1 head
Definition: metaprogramming_util.h:97
@ length
Definition: metaprogramming_util.h:99
Definition: metaprogramming_util.h:117
TypeValueList< tail > tl
Definition: metaprogramming_util.h:122
TTypeList::head head
Definition: metaprogramming_util.h:118
TypeValueList(head _hd, TypeValueList< tail > typValList)
Definition: metaprogramming_util.h:128
TTypeList::tail tail
Definition: metaprogramming_util.h:119
head hd
Definition: metaprogramming_util.h:121
TypeValueList()
Definition: metaprogramming_util.h:124