clingo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
type_manip.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2010, Benjamin Kaufmann
3 //
4 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
5 //
6 // Clasp is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Clasp is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Clasp; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 #ifndef BK_LIB_TYPE_MANIP_H_INCLUDED
21 #define BK_LIB_TYPE_MANIP_H_INCLUDED
22 namespace bk_lib { namespace detail {
23 #if (_MSC_VER >= 1300)
24 #define ALIGNOF(PARAM) (__alignof(PARAM))
25 #elif defined(__GNUC__)
26 #define ALIGNOF(PARAM) (__alignof__(PARAM))
27 #else
28 template <class T>
29 struct align_helper { char x; T y; };
30 #define ALIGNOF(T) (sizeof(align_helper<T>)-sizeof(T))
31 #endif
32 
33 
34 // if b then if_type else else_type
35 template <bool b, class if_type, class else_type>
36 struct if_then_else;
37 
38 template <class if_type, class else_type>
39 struct if_then_else<true, if_type, else_type> { typedef if_type type; };
40 template <class if_type, class else_type>
41 struct if_then_else<false, if_type, else_type> { typedef else_type type; };
42 
43 // 1 if T == U, else 0
44 template <class T, class U> struct same_type { enum { value = 0 }; };
45 template <class T> struct same_type<T,T> { enum { value = 1 }; };
46 
47 template <bool> struct disable_if { typedef bool type; };
48 template <> struct disable_if<true> { };
49 
50 // not in list - marks end of type list
51 struct nil_type {};
52 
53 // list of types - terminated by nil_type
54 template <class head, class tail>
55 struct type_list {
56  typedef head head_type;
57  typedef tail tail_type;
58 };
59 
60 // generates a type lits with up to 18 elements
61 template <
62  typename T1 = nil_type, typename T2 = nil_type, typename T3 = nil_type,
63  typename T4 = nil_type, typename T5 = nil_type, typename T6 = nil_type,
64  typename T7 = nil_type, typename T8 = nil_type, typename T9 = nil_type,
65  typename T10 = nil_type, typename T11 = nil_type, typename T12 = nil_type,
66  typename T13 = nil_type, typename T14 = nil_type, typename T15 = nil_type,
67  typename T16 = nil_type, typename T17 = nil_type, typename T18 = nil_type
68 >
72 };
73 
74 template <>
75 struct generate_type_list<> { typedef nil_type type; };
76 
77 // maps an integer constant to a type
78 template <int i>
79 struct int2type { enum { value = i }; };
82 
83 // declared but not defined
84 struct unknown_type;
85 
86 // finds the element in the type list TList that
87 // has the same alignment as X or X if no such element exists
88 template <class X, class TList>
89 struct max_align;
90 
91 // IF ALIGNOF(X) == ALIGNOF(H) then H
92 // ELSE max_align<X, Tail>
93 template <bool, class X, class H, class Tail>
95 
96 // Base case: ALIGNOF(X) == ALIGNOF(H)
97 template <class X, class H, class T>
98 struct max_align_aux<true, X, H, T> {
99  typedef H type;
100 };
101 
102 // Recursive case
103 template <bool, class X, class H, class Tail>
104 struct max_align_aux {
105  typedef typename max_align<X, Tail>::type type;
106 };
107 
108 template <class X>
109 struct max_align<X, nil_type> {
110  typedef X type;
111 };
112 
113 template <class X, class H, class T>
114 struct max_align<X, type_list<H, T> > {
115 private:
116  enum { x_align = ALIGNOF(X) };
117  enum { h_align = ALIGNOF(H) };
118 public:
119  typedef typename max_align_aux<static_cast<int>(x_align) == static_cast<int>(h_align), X, H, T>::type type;
120  enum { value = sizeof(type) };
121 };
122 
123 // computes alignment size (::value) and type (::type) of T
124 template <class T>
125 struct align_of {
126  typedef generate_type_list<bool, char, short, int, long, float, double, long double, void*,
127  unknown_type(*)(unknown_type), unknown_type* unknown_type::*,
128  unknown_type (unknown_type::*)(unknown_type)>::type align_list;
131 };
132 
133 #undef ALIGNOF
134 
135 }}
136 #endif
137 
BinderType type
Definition: statements.cc:1283
bool type
Definition: type_manip.h:47
int2type< 0 > false_type
Definition: type_manip.h:80
Definition: type_manip.h:69
max_align< X, Tail >::type type
Definition: type_manip.h:105
type_list< T1, tail_type > type
Definition: type_manip.h:71
X type
Definition: type_manip.h:110
max_align< T, align_list >::type type
Definition: type_manip.h:129
Definition: type_manip.h:55
T y
Definition: type_manip.h:29
char x
Definition: type_manip.h:29
nil_type type
Definition: type_manip.h:75
Definition: type_manip.h:29
Definition: type_manip.h:44
#define ALIGNOF(T)
Definition: type_manip.h:30
int2type< 1 > true_type
Definition: type_manip.h:81
Definition: type_manip.h:79
generate_type_list< T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18 >::type tail_type
Definition: type_manip.h:70
Definition: type_manip.h:89
max_align_aux< static_cast< int >x_align)==static_cast< int >h_align), X, H, T >::type type
Definition: type_manip.h:119
Definition: type_manip.h:79
Definition: type_manip.h:94
head head_type
Definition: type_manip.h:56
Definition: type_manip.h:36
Definition: type_manip.h:51
Definition: type_manip.h:125
tail tail_type
Definition: type_manip.h:57
Definition: type_manip.h:47
Definition: type_manip.h:44