Stxxl  1.3.2
tmeta.h
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_META_TEMPLATE_PROGRAMMING
18 #define STXXL_META_TEMPLATE_PROGRAMMING
19 
20 #include <stxxl/bits/namespace.h>
21 #include <stxxl/bits/common/types.h>
22 
23 
24 __STXXL_BEGIN_NAMESPACE
25 
27 
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 
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,
88  typename SWITCH<tag, NextCase>::result
89  >::result result;
90 };
91 
92 template <int tag>
93 class SWITCH<tag, NilCase>
94 {
95 public:
96  typedef NilCase result;
97 };
98 
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  {
136  floor = LOG2_floor<Input>::value,
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 
163 __STXXL_END_NAMESPACE
164 
165 #endif // !STXXL_META_TEMPLATE_PROGRAMMING
Definition: tmeta.h:101
Definition: tmeta.h:46
IF template metaprogramming statement.
Definition: tmeta.h:31