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 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 utility {
00028 
00032   /*-----------------.
00033   | remove_reference |
00034   `-----------------*/
00035 
00037 
00038   template<typename T>
00039   struct remove_reference
00040   {
00041     typedef T t;
00042   };
00043 
00044   template<typename T>
00045   struct remove_reference<T&>
00046   {
00047     typedef T t;
00048   };
00049 
00050   template<typename T>
00051   struct remove_reference<const T&>
00052   {
00053     typedef const T t;
00054   };
00057   /*-------------.
00058   | remove_const |
00059   `-------------*/
00060 
00062 
00063   template<typename T>
00064   struct remove_const
00065   {
00066     typedef T t;
00067   };
00068 
00069   template<typename T>
00070   struct remove_const<const T>
00071   {
00072     typedef T t;
00073   };
00076   /*----------.
00077   | static_if |
00078   `----------*/
00079 
00087   template<bool b, typename T, typename U>
00088   struct static_if
00089   {
00090     typedef T t;
00091 
00092     typedef typename remove_const<typename remove_reference<T>::t>::t bare_t1;
00093     typedef typename remove_const<typename remove_reference<U>::t>::t bare_t2;
00094 
00095     static bare_t1&
00096     choose(bare_t1& p1,
00097            bare_t2& )
00098     { return p1; }
00099     static const bare_t1&
00100     choose(const bare_t1& p1,
00101            const bare_t2& )
00102     { return p1; }
00103   };
00104 
00105   template<typename T, typename U>
00106   struct static_if<false, T, U>
00107   {
00108     typedef U t;
00109 
00110     typedef typename remove_const<typename remove_reference<T>::t>::t bare_t1;
00111     typedef typename remove_const<typename remove_reference<U>::t>::t bare_t2;
00112 
00113     static bare_t2&
00114     choose(bare_t1& ,
00115            bare_t2& p2)
00116     { return p2; }
00117     static const bare_t2&
00118     choose(const bare_t1& ,
00119            const bare_t2& p2)
00120     { return p2; }
00121   };
00124   /*-----------------.
00125   | static_if_simple |
00126   `-----------------*/
00127 
00140   template<bool b, typename T, typename U>
00141   struct static_if_simple
00142   {
00143     typedef T t;
00144   };
00145 
00146   template<typename T, typename U>
00147   struct static_if_simple<false, T, U>
00148   {
00149     typedef U t;
00150   };
00153   /*----------.
00154   | static_eq |
00155   `----------*/
00156 
00158 
00159   template<typename T, typename U>
00160   struct static_eq
00161   {
00162     static const bool value = false;
00163   };
00164 
00165   template<typename T>
00166   struct static_eq<T, T>
00167   {
00168     static const bool value = true;
00169   };
00172   /*-----------.
00173   | static_pow |
00174   `-----------*/
00175 
00177 
00178   template<size_t N, size_t P, bool pair_p = false>
00179   struct static_pow_compute
00180   {
00181     static const size_t value =
00182     N * static_pow_compute<N, P-1, (((P-1) % 2) == 0)>::value ;
00183   };
00184 
00185   template<size_t N, size_t P>
00186   struct static_pow_compute<N, P, true>
00187   {
00188     static const size_t temp =
00189     static_pow_compute<N, P/2, (((P/2) % 2) == 0)>::value;
00190     static const size_t value = temp * temp;
00191   };
00192 
00193   template<size_t N>
00194   struct static_pow_compute<N, 0, true>
00195   {
00196     static const size_t value = 1;
00197   };
00204   template<size_t N, size_t P>
00205   struct static_pow
00206   {
00207     static const size_t value =
00208     static_pow_compute<N, P, ((P % 2) == 0)>::value ;
00209   };
00210 
00211   /*---------------------.
00212   | static_pow_minus_one |
00213   `---------------------*/
00214 
00216   template<size_t N, size_t P>
00217   struct static_pow_minus_one
00218   {
00219     static const size_t value =
00220     static_pow_compute<N, P, ((P % 2) == 0)>::value - 1;
00221   };
00222 
00225 } // utility
00226 
00227 #endif // ! VCSN_MISC_STATIC_HH

Generated on Fri Jul 28 12:18:53 2006 for Vaucanson by  doxygen 1.4.6