STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tmeta.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/tmeta.h
3  *
4  * Template Metaprogramming Tools
5  * (from the Generative Programming book Krysztof Czarnecki, Ulrich Eisenecker)
6  *
7  * Part of the STXXL. See http://stxxl.sourceforge.net
8  *
9  * Copyright (C) 2003 Roman Dementiev <[email protected]>
10  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
11  *
12  * Distributed under the Boost Software License, Version 1.0.
13  * (See accompanying file LICENSE_1_0.txt or copy at
14  * http://www.boost.org/LICENSE_1_0.txt)
15  **************************************************************************/
16 
17 #ifndef STXXL_COMMON_TMETA_HEADER
18 #define STXXL_COMMON_TMETA_HEADER
19 
20 #include <stxxl/bits/namespace.h>
22 
23 
25 
26 //! IF template metaprogramming statement.
27 //!
28 //! If \c Flag is \c true then \c IF<>::result is of type Type1
29 //! otherwise of \c IF<>::result is of type Type2
30 template <bool Flag, class Type1, class Type2>
31 struct IF
32 {
33  typedef Type1 result;
34 };
35 
36 template <class Type1, class Type2>
37 struct IF<false, Type1, Type2>
38 {
39  typedef Type2 result;
40 };
41 
42 
43 //! If \c Flag is \c true then \c IF<>::result is Num1
44 //! otherwise of \c IF<>::result is Num2
45 template <bool Flag, unsigned Num1, unsigned Num2>
46 struct IF_N
47 {
48  enum
49  {
50  result = Num1
51  };
52 };
53 
54 template <unsigned Num1, unsigned Num2>
55 struct IF_N<false, Num1, Num2>
56 {
57  enum
58  {
59  result = Num2
60  };
61 };
62 
63 const int DEFAULT = ~(~0u >> 1); // initialize with the smallest int
64 
65 struct NilCase { };
66 
67 template <int tag_, class Type_, class Next_ = NilCase>
68 struct CASE
69 {
70  enum { tag = tag_ };
71  typedef Type_ Type;
72  typedef Next_ Next;
73 };
74 
75 template <int tag, class Case>
76 class SWITCH
77 {
78  typedef typename Case::Next NextCase;
79  enum
80  {
81  caseTag = Case::tag,
82  found = (caseTag == tag || caseTag == DEFAULT)
83  };
84 
85 public:
86  typedef typename IF<found,
87  typename Case::Type,
90 };
91 
92 template <int tag>
93 class SWITCH<tag, NilCase>
94 {
95 public:
96  typedef NilCase result;
97 };
98 
99 //! \internal, use LOG2 instead
100 template <unsigned_type Input>
102 {
103 public:
104  enum
105  {
106  value = LOG2_floor<Input / 2>::value + 1
107  };
108 };
109 
110 template <>
111 class LOG2_floor<1>
112 {
113 public:
114  enum
115  {
116  value = 0
117  };
118 };
119 
120 template <>
121 class LOG2_floor<0>
122 {
123 public:
124  enum
125  {
126  value = 0
127  };
128 };
129 
130 template <unsigned_type Input>
131 class LOG2
132 {
133 public:
134  enum
135  {
137  ceil = LOG2_floor<Input - 1>::value + 1
138  };
139 };
140 
141 template <>
142 class LOG2<1>
143 {
144 public:
145  enum
146  {
147  floor = 0,
148  ceil = 0
149  };
150 };
151 
152 template <>
153 class LOG2<0>
154 {
155 public:
156  enum
157  {
158  floor = 0,
159  ceil = 0
160  };
161 };
162 
164 
165 #endif // !STXXL_COMMON_TMETA_HEADER
const int DEFAULT
Definition: tmeta.h:63
IF< found, typename Case::Type, typename SWITCH< tag, NextCase >::result >::result result
Definition: tmeta.h:89
Next_ Next
Definition: tmeta.h:72
If Flag is true then IF&lt;&gt;::result is Num1 otherwise of IF&lt;&gt;::result is Num2.
Definition: tmeta.h:46
Type_ Type
Definition: tmeta.h:71
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Case::Next NextCase
Definition: tmeta.h:78
Type1 result
Definition: tmeta.h:33
IF template metaprogramming statement.
Definition: tmeta.h:31
#define STXXL_END_NAMESPACE
Definition: namespace.h:17