Vaucanson 1.4
static.hh
Go to the documentation of this file.
00001 // static.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_MISC_STATIC_HH
00018 # define VCSN_MISC_STATIC_HH
00019 
00025 # include <cstddef>
00026 
00027 namespace vcsn {
00028   namespace misc {
00029 
00033   /*-----------------.
00034   | remove_reference |
00035   `-----------------*/
00036 
00038 
00039     template <typename T>
00040     struct remove_reference
00041     {
00042         typedef T t;
00043     };
00044 
00045     template <typename T>
00046     struct remove_reference<T&>
00047     {
00048         typedef T t;
00049     };
00050 
00051     template <typename T>
00052     struct remove_reference<const T&>
00053     {
00054         typedef const T t;
00055     };
00058   /*-------------.
00059   | remove_const |
00060   `-------------*/
00061 
00063 
00064     template <typename T>
00065     struct remove_const
00066     {
00067         typedef T t;
00068     };
00069 
00070     template <typename T>
00071     struct remove_const<const T>
00072     {
00073         typedef T t;
00074     };
00077   /*----------.
00078   | static_if |
00079   `----------*/
00080 
00088     template <bool b, typename T, typename U>
00089     struct static_if
00090     {
00091         typedef T t;
00092 
00093         typedef typename remove_const<typename remove_reference<T>::t>::t bare_t1;
00094         typedef typename remove_const<typename remove_reference<U>::t>::t bare_t2;
00095 
00096         static bare_t1&
00097         choose (bare_t1& p1,
00098                 bare_t2& )
00099         { return p1; }
00100         static const bare_t1&
00101         choose (const bare_t1& p1,
00102                 const bare_t2& )
00103         { return p1; }
00104     };
00105 
00106     template <typename T, typename U>
00107     struct static_if<false, T, U>
00108     {
00109         typedef U t;
00110 
00111         typedef typename remove_const<typename remove_reference<T>::t>::t bare_t1;
00112         typedef typename remove_const<typename remove_reference<U>::t>::t bare_t2;
00113 
00114         static bare_t2&
00115         choose (bare_t1& ,
00116                 bare_t2& p2)
00117         { return p2; }
00118         static const bare_t2&
00119         choose (const bare_t1& ,
00120                 const bare_t2& p2)
00121         { return p2; }
00122     };
00125   /*----------------.
00126   | Static Booleans |
00127   `----------------*/
00131     struct true_t {};
00132     struct false_t {};
00133 
00134     template <bool B>
00135     struct bool_to_type_helper
00136     {
00137         typedef true_t ret;
00138     };
00139 
00140     template <>
00141     struct bool_to_type_helper<false>
00142     {
00143         typedef false_t ret;
00144     };
00145 
00146 # define BOOL_TO_TYPE(Bool) typename vcsn::misc::bool_to_type_helper< (Bool)>::ret
00147 
00148   /*-----------------.
00149   | static_if_simple |
00150   `-----------------*/
00151 
00164     template <bool b, typename T, typename U>
00165     struct static_if_simple
00166     {
00167         typedef T t;
00168     };
00169 
00170     template <typename T, typename U>
00171     struct static_if_simple<false, T, U>
00172     {
00173         typedef U t;
00174     };
00177   /*----------.
00178   | static_eq |
00179   `----------*/
00180 
00182 
00183     template <typename T, typename U>
00184     struct static_eq
00185     {
00186         static const bool value = false;
00187     };
00188 
00189     template <typename T>
00190     struct static_eq<T, T>
00191     {
00192         static const bool value = true;
00193     };
00196   /*-----------.
00197   | static_pow |
00198   `-----------*/
00199 
00201 
00202     template <size_t N, size_t P, bool pair_p = false>
00203     struct static_pow_compute
00204     {
00205         static const size_t value =
00206           N * static_pow_compute<N, P-1, (((P-1) % 2) == 0)>::value ;
00207     };
00208 
00209     template <size_t N, size_t P>
00210     struct static_pow_compute<N, P, true>
00211     {
00212         static const size_t temp =
00213           static_pow_compute<N, P/2, (((P/2) % 2) == 0)>::value;
00214         static const size_t value = temp * temp;
00215     };
00216 
00217     template <size_t N>
00218     struct static_pow_compute<N, 0, true>
00219     {
00220         static const size_t value = 1;
00221     };
00228     template <size_t N, size_t P>
00229     struct static_pow
00230     {
00231         static const size_t value =
00232           static_pow_compute<N, P, ((P % 2) == 0)>::value ;
00233     };
00234 
00235   /*---------------------.
00236   | static_pow_minus_one |
00237   `---------------------*/
00238 
00240     template <size_t N, size_t P>
00241     struct static_pow_minus_one
00242     {
00243         static const size_t value =
00244           static_pow_compute<N, P, ((P % 2) == 0)>::value - 1;
00245     };
00246 
00249   } // misc
00250 } // vcsn
00251 
00252 #endif // ! VCSN_MISC_STATIC_HH