Vaucanson  1.4.1
static.hh
Go to the documentation of this file.
1 // static.hh: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_MISC_STATIC_HH
18 # define VCSN_MISC_STATIC_HH
19 
25 # include <cstddef>
26 
27 namespace vcsn {
28  namespace misc {
29 
33  /*-----------------.
34  | remove_reference |
35  `-----------------*/
36 
38 
39  template <typename T>
41  {
42  typedef T t;
43  };
44 
45  template <typename T>
46  struct remove_reference<T&>
47  {
48  typedef T t;
49  };
50 
51  template <typename T>
52  struct remove_reference<const T&>
53  {
54  typedef const T t;
55  };
58  /*-------------.
59  | remove_const |
60  `-------------*/
61 
63 
64  template <typename T>
65  struct remove_const
66  {
67  typedef T t;
68  };
69 
70  template <typename T>
71  struct remove_const<const T>
72  {
73  typedef T t;
74  };
77  /*----------.
78  | static_if |
79  `----------*/
80 
88  template <bool b, typename T, typename U>
89  struct static_if
90  {
91  typedef T t;
92 
93  typedef typename remove_const<typename remove_reference<T>::t>::t bare_t1;
94  typedef typename remove_const<typename remove_reference<U>::t>::t bare_t2;
95 
96  static bare_t1&
97  choose (bare_t1& p1,
98  bare_t2& )
99  { return p1; }
100  static const bare_t1&
101  choose (const bare_t1& p1,
102  const bare_t2& )
103  { return p1; }
104  };
105 
106  template <typename T, typename U>
107  struct static_if<false, T, U>
108  {
109  typedef U t;
110 
111  typedef typename remove_const<typename remove_reference<T>::t>::t bare_t1;
112  typedef typename remove_const<typename remove_reference<U>::t>::t bare_t2;
113 
114  static bare_t2&
115  choose (bare_t1& ,
116  bare_t2& p2)
117  { return p2; }
118  static const bare_t2&
119  choose (const bare_t1& ,
120  const bare_t2& p2)
121  { return p2; }
122  };
125  /*----------------.
126  | Static Booleans |
127  `----------------*/
131  struct true_t {};
132  struct false_t {};
133 
134  template <bool B>
135  struct bool_to_type_helper
136  {
137  typedef true_t ret;
138  };
139 
140  template <>
141  struct bool_to_type_helper<false>
142  {
143  typedef false_t ret;
144  };
145 
146 # define BOOL_TO_TYPE(Bool) typename vcsn::misc::bool_to_type_helper< (Bool)>::ret
147 
148  /*-----------------.
149  | static_if_simple |
150  `-----------------*/
151 
164  template <bool b, typename T, typename U>
166  {
167  typedef T t;
168  };
169 
170  template <typename T, typename U>
171  struct static_if_simple<false, T, U>
172  {
173  typedef U t;
174  };
177  /*----------.
178  | static_eq |
179  `----------*/
180 
182 
183  template <typename T, typename U>
184  struct static_eq
185  {
186  static const bool value = false;
187  };
188 
189  template <typename T>
190  struct static_eq<T, T>
191  {
192  static const bool value = true;
193  };
196  /*-----------.
197  | static_pow |
198  `-----------*/
199 
201 
202  template <size_t N, size_t P, bool pair_p = false>
204  {
205  static const size_t value =
206  N * static_pow_compute<N, P-1, (((P-1) % 2) == 0)>::value ;
207  };
208 
209  template <size_t N, size_t P>
210  struct static_pow_compute<N, P, true>
211  {
212  static const size_t temp =
213  static_pow_compute<N, P/2, (((P/2) % 2) == 0)>::value;
214  static const size_t value = temp * temp;
215  };
216 
217  template <size_t N>
218  struct static_pow_compute<N, 0, true>
219  {
220  static const size_t value = 1;
221  };
228  template <size_t N, size_t P>
229  struct static_pow
230  {
231  static const size_t value =
233  };
234 
235  /*---------------------.
236  | static_pow_minus_one |
237  `---------------------*/
238 
240  template <size_t N, size_t P>
242  {
243  static const size_t value =
245  };
246 
249  } // misc
250 } // vcsn
251 
252 #endif // ! VCSN_MISC_STATIC_HH