STXXL  1.4-dev
 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 
24 
25 //! IF template metaprogramming statement.
26 //!
27 //! If \c Flag is \c true then \c IF<>::result is of type Type1
28 //! otherwise of \c IF<>::result is of type Type2
29 template <bool Flag, class Type1, class Type2>
30 struct IF
31 {
32  typedef Type1 result;
33 };
34 
35 template <class Type1, class Type2>
36 struct IF<false, Type1, Type2>
37 {
38  typedef Type2 result;
39 };
40 
41 //! If \c Flag is \c true then \c IF<>::result is Num1
42 //! otherwise of \c IF<>::result is Num2
43 template <bool Flag, unsigned Num1, unsigned Num2>
44 struct IF_N
45 {
46  enum
47  {
48  result = Num1
49  };
50 };
51 
52 template <unsigned Num1, unsigned Num2>
53 struct IF_N<false, Num1, Num2>
54 {
55  enum
56  {
57  result = Num2
58  };
59 };
60 
61 const int DEFAULT = ~(~0u >> 1); // initialize with the smallest int
62 
63 struct NilCase { };
64 
65 template <int tag_, class Type_, class Next_ = NilCase>
66 struct CASE
67 {
68  enum { tag = tag_ };
69  typedef Type_ Type;
70  typedef Next_ Next;
71 };
72 
73 template <int tag, class Case>
74 class SWITCH
75 {
76  typedef typename Case::Next NextCase;
77  enum
78  {
79  caseTag = Case::tag,
80  found = (caseTag == tag || caseTag == DEFAULT)
81  };
82 
83 public:
84  typedef typename IF<found,
85  typename Case::Type,
88 };
89 
90 template <int tag>
91 class SWITCH<tag, NilCase>
92 {
93 public:
94  typedef NilCase result;
95 };
96 
97 //! \internal, use LOG2 instead
98 template <unsigned_type Input>
100 {
101 public:
102  enum
103  {
104  value = LOG2_floor<Input / 2>::value + 1
105  };
106 };
107 
108 template <>
109 class LOG2_floor<1>
110 {
111 public:
112  enum
113  {
114  value = 0
115  };
116 };
117 
118 template <>
119 class LOG2_floor<0>
120 {
121 public:
122  enum
123  {
124  value = 0
125  };
126 };
127 
128 template <unsigned_type Input>
129 class LOG2
130 {
131 public:
132  enum
133  {
135  ceil = LOG2_floor<Input - 1>::value + 1
136  };
137 };
138 
139 template <>
140 class LOG2<1>
141 {
142 public:
143  enum
144  {
145  floor = 0,
146  ceil = 0
147  };
148 };
149 
150 template <>
151 class LOG2<0>
152 {
153 public:
154  enum
155  {
156  floor = 0,
157  ceil = 0
158  };
159 };
160 
162 
163 #endif // !STXXL_COMMON_TMETA_HEADER
const int DEFAULT
Definition: tmeta.h:61
IF< found, typename Case::Type, typename SWITCH< tag, NextCase >::result >::result result
Definition: tmeta.h:87
Next_ Next
Definition: tmeta.h:70
If Flag is true then IF&lt;&gt;::result is Num1 otherwise of IF&lt;&gt;::result is Num2.
Definition: tmeta.h:44
Type_ Type
Definition: tmeta.h:69
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Case::Next NextCase
Definition: tmeta.h:76
Type1 result
Definition: tmeta.h:32
IF template metaprogramming statement.
Definition: tmeta.h:30
#define STXXL_END_NAMESPACE
Definition: namespace.h:17