Line data Source code
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 :
38 : /// \addtogroup ugbase_common_util
39 : /// \{
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 :
55 : //////////////////////////////
56 : // TypeList
57 : //////////////////////////////
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;
98 : typedef TypeList< T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12 > tail;
99 : enum{length = tail::length+1};
100 : };
101 :
102 : // empty typelist specialization
103 : template<>
104 : struct TypeList< EmptyType, EmptyType, EmptyType, EmptyType,
105 : EmptyType, EmptyType, EmptyType, EmptyType, EmptyType,
106 : EmptyType, EmptyType, EmptyType>
107 : {
108 : enum{length = 0};
109 : };
110 :
111 : //////////////////////////////
112 : // TypeValueList
113 : //////////////////////////////
114 :
115 : // TypeList
116 0 : template <typename TTypeList> struct TypeValueList
117 : {
118 : typedef typename TTypeList::head head;
119 : typedef typename TTypeList::tail tail;
120 :
121 : head hd;
122 : TypeValueList<tail> tl;
123 :
124 : explicit TypeValueList() {
125 : std::cerr << "incomplete TVL\n";
126 : }
127 :
128 0 : TypeValueList(head _hd,
129 : TypeValueList<tail> typValList) :
130 0 : hd(_hd), tl(typValList) {}
131 :
132 : };
133 :
134 : template <>
135 : struct TypeValueList< TypeList<> > {};
136 :
137 :
138 : //////////////////////////////
139 : // Factorial
140 : //////////////////////////////
141 :
142 : /** returns the factorial of n
143 : * The struct value is n!
144 : */
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 :
157 : //////////////////////////////
158 : // Pow
159 : //////////////////////////////
160 :
161 : /** returns the power of n^d
162 : */
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 :
175 : //////////////////////////////
176 : // BinomialCoefficient
177 : //////////////////////////////
178 :
179 : /** returns static value of binomial coefficient
180 : * The struct value is:
181 : *
182 : * n!
183 : * ---------
184 : * k! (n-k)!
185 : */
186 : template <size_t n, int k>
187 : struct BinomialCoefficient
188 : {
189 : enum { value = Factorial<n>::value/
190 : (Factorial<k>::value*Factorial<n-k>::value) };
191 : };
192 :
193 : // end rekursion
194 : template <size_t n>
195 : struct BinomialCoefficient<n,0>
196 : {
197 : enum { value = 1};
198 : };
199 : // end rekursion
200 : template <size_t n>
201 : struct BinomialCoefficient<n,-1>
202 : {
203 : enum { value = 0};
204 : };
205 : // end rekursion
206 : template <size_t n>
207 : struct BinomialCoefficient<n,-2>
208 : {
209 : enum { value = 0};
210 : };
211 : // end rekursion
212 : template <size_t n>
213 : struct BinomialCoefficient<n,-3>
214 : {
215 : enum { value = 0};
216 : };
217 : // end rekursion
218 : template <size_t n>
219 : struct BinomialCoefficient<n,-4>
220 : {
221 : enum { value = 0};
222 : };
223 :
224 : //////////////////////////////
225 : // UniqueTypeID (sreiter)
226 : //////////////////////////////
227 : /// a singleton class that returns a new id for each type
228 : class UniqueTypeIDProvider{
229 : public:
230 4466 : static UniqueTypeIDProvider& inst(){
231 4466 : static UniqueTypeIDProvider utid;
232 4466 : return utid;
233 : }
234 :
235 4466 : size_t new_id() {return ++m_id;}
236 :
237 : private:
238 1 : UniqueTypeIDProvider() : m_id(0) {}
239 : size_t m_id;
240 : };
241 :
242 : /// This method associated a unique unsigned integer value with each type.
243 : template <class TType>
244 7744 : size_t GetUniqueTypeID()
245 : {
246 7744 : static size_t typeID = UniqueTypeIDProvider::inst().new_id();
247 7744 : return typeID;
248 : }
249 :
250 : // end group ugbase_common_util
251 : /// \}
252 :
253 : }
254 :
255 : #endif /* __H__COMMON__METAPROGRAMMING_UTIL__ */
|