type_manip.h
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2010, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #ifndef BK_LIB_TYPE_MANIP_H_INCLUDED
00021 #define BK_LIB_TYPE_MANIP_H_INCLUDED
00022 namespace bk_lib { namespace detail {
00023 #if (_MSC_VER >= 1300)
00024 #define ALIGNOF(PARAM) (__alignof(PARAM))
00025 #elif defined(__GNUC__)
00026 #define ALIGNOF(PARAM) (__alignof__(PARAM))
00027 #else
00028 template <class T>
00029 struct align_helper { char x; T  y; };
00030 #define ALIGNOF(T) (sizeof(align_helper<T>)-sizeof(T))
00031 #endif
00032 
00033 
00034 // if b then if_type else else_type
00035 template <bool b, class if_type, class else_type>
00036 struct if_then_else;
00037 
00038 template <class if_type, class else_type>
00039 struct if_then_else<true, if_type, else_type>  {  typedef if_type type;  };
00040 template <class if_type, class else_type>
00041 struct if_then_else<false, if_type, else_type> { typedef else_type type; };
00042 
00043 // 1 if T == U, else 0
00044 template <class T, class U>  struct same_type        { enum { value = 0 }; };
00045 template <class T>           struct same_type<T,T>   { enum { value = 1 }; };
00046 
00047 template <bool>              struct disable_if       { typedef bool type; };
00048 template <>                  struct disable_if<true> {  };
00049 
00050 // not in list - marks end of type list
00051 struct nil_type {};
00052 
00053 // list of types - terminated by nil_type
00054 template <class head, class tail>
00055 struct type_list {
00056         typedef head head_type;
00057         typedef tail tail_type;
00058 };
00059 
00060 // generates a type lits with up to 18 elements
00061 template <
00062         typename T1  = nil_type, typename T2  = nil_type, typename T3  = nil_type,
00063         typename T4  = nil_type, typename T5  = nil_type, typename T6  = nil_type,
00064         typename T7  = nil_type, typename T8  = nil_type, typename T9  = nil_type,
00065         typename T10 = nil_type, typename T11 = nil_type, typename T12 = nil_type,
00066         typename T13 = nil_type, typename T14 = nil_type, typename T15 = nil_type,
00067         typename T16 = nil_type, typename T17 = nil_type, typename T18 = nil_type
00068 > 
00069 struct generate_type_list {
00070         typedef typename generate_type_list<T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18>::type tail_type;
00071         typedef type_list<T1, tail_type> type;
00072 };
00073 
00074 template <> 
00075 struct generate_type_list<> { typedef nil_type type; };
00076 
00077 // maps an integer constant to a type
00078 template <int i>
00079 struct int2type { enum { value = i }; };
00080 typedef int2type<0> false_type;
00081 typedef int2type<1> true_type;
00082 
00083 // declared but not defined
00084 struct unknown_type;
00085 
00086 // finds the element in the type list TList that 
00087 // has the same alignment as X or X if no such element exists
00088 template <class X, class TList>
00089 struct max_align;
00090 
00091 // IF ALIGNOF(X) == ALIGNOF(H) then H 
00092 // ELSE max_align<X, Tail>
00093 template <bool, class X, class H, class Tail>
00094 struct max_align_aux;
00095 
00096 // Base case: ALIGNOF(X) == ALIGNOF(H)
00097 template <class X, class H, class T>
00098 struct max_align_aux<true, X, H, T> {
00099         typedef H type;
00100 };
00101 
00102 // Recursive case
00103 template <bool, class X, class H, class Tail>
00104 struct max_align_aux {
00105         typedef typename max_align<X, Tail>::type type;
00106 };
00107 
00108 template <class X>
00109 struct max_align<X, nil_type> {
00110         typedef X type;
00111 };
00112 
00113 template <class X, class H, class T>
00114 struct max_align<X, type_list<H, T> > {
00115 private:
00116         enum { x_align = ALIGNOF(X) };
00117         enum { h_align = ALIGNOF(H) };
00118 public:
00119         typedef typename max_align_aux<static_cast<int>(x_align) == static_cast<int>(h_align), X, H, T>::type type;
00120         enum { value = sizeof(type) };
00121 };
00122 
00123 // computes alignment size (::value) and type (::type)  of T
00124 template <class T>
00125 struct align_of {
00126         typedef generate_type_list<bool, char, short, int, long, float, double, long double, void*,
00127                 unknown_type(*)(unknown_type), unknown_type* unknown_type::*,
00128                 unknown_type (unknown_type::*)(unknown_type)>::type align_list;
00129         typedef typename max_align<T, align_list>::type type;
00130         enum { value = max_align<T, align_list>::value };
00131 };
00132 
00133 #undef ALIGNOF
00134 
00135 }}
00136 #endif
00137 


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:40