L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
type_traits.h
1// vi:ft=cpp
2/*
3 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>,
4 * Torsten Frenzel <frenzel@os.inf.tu-dresden.de>
5 * economic rights: Technische Universität Dresden (Germany)
6 *
7 * License: see LICENSE.spdx (in this directory or the directories above)
8 */
9
10#pragma once
11
12namespace cxx {
13
14class Null_type;
15
16template< bool flag, typename T, typename F >
17class Select
18{
19public:
20 typedef T Type;
21};
22
23template< typename T, typename F >
24class Select< false, T, F >
25{
26public:
27 typedef F Type;
28};
29
30
31
32template< typename T, typename U >
33class Conversion
34{
35 typedef char S;
36 class B { char dummy[2]; };
37 static S test(U);
38 static B test(...);
39 static T make_T();
40public:
41 enum
42 {
43 exists = sizeof(test(make_T())) == sizeof(S),
44 two_way = exists && Conversion<U,T>::exists,
45 exists_2_way = two_way,
46 same_type = false
47 };
48};
49
50template< >
51class Conversion<void, void>
52{
53public:
54 enum { exists = 1, two_way = 1, exists_2_way = two_way, same_type = 1 };
55};
56
57template< typename T >
58class Conversion<T, T>
59{
60public:
61 enum { exists = 1, two_way = 1, exists_2_way = two_way, same_type = 1 };
62};
63
64template< typename T >
65class Conversion<void, T>
66{
67public:
68 enum { exists = 0, two_way = 0, exists_2_way = two_way, same_type = 0 };
69};
70
71template< typename T >
72class Conversion<T, void>
73{
74public:
75 enum { exists = 0, two_way = 0, exists_2_way = two_way, same_type = 0 };
76};
77
78template< int I >
79class Int_to_type
80{
81public:
82 enum { i = I };
83};
84
85namespace TT
86{
87 template< typename U > class Pointer_traits
88 {
89 public:
90 typedef Null_type Pointee;
91 enum { value = false };
92 };
93
94 template< typename U > class Pointer_traits< U* >
95 {
96 public:
97 typedef U Pointee;
98 enum { value = true };
99 };
100
101 template< typename U > struct Ref_traits
102 {
103 enum { value = false };
104 typedef U Referee;
105 };
106
107 template< typename U > struct Ref_traits<U&>
108 {
109 enum { value = true };
110 typedef U Referee;
111 };
112
113
114 template< typename U > struct Add_ref { typedef U &Type; };
115 template< typename U > struct Add_ref<U&> { typedef U Type; };
116
117 template< typename U > struct PMF_traits { enum { value = false }; };
118 template< typename U, typename F > struct PMF_traits<U F::*>
119 { enum { value = true }; };
120
121
122 template< typename U > class Is_unsigned { public: enum { value = false }; };
123 template<> class Is_unsigned<unsigned> { public: enum { value = true }; };
124 template<> class Is_unsigned<unsigned char> {
125 public: enum { value = true };
126 };
127 template<> class Is_unsigned<unsigned short> {
128 public: enum { value = true };
129 };
130 template<> class Is_unsigned<unsigned long> {
131 public: enum { value = true };
132 };
133 template<> class Is_unsigned<unsigned long long> {
134 public: enum { value = true };
135 };
136
137 template< typename U > class Is_signed { public: enum { value = false }; };
138 template<> class Is_signed<signed char> { public: enum { value = true }; };
139 template<> class Is_signed<signed short> { public: enum { value = true }; };
140 template<> class Is_signed<signed> { public: enum { value = true }; };
141 template<> class Is_signed<signed long> { public: enum { value = true }; };
142 template<> class Is_signed<signed long long> {
143 public: enum { value = true };
144 };
145
146 template< typename U > class Is_int { public: enum { value = false }; };
147 template<> class Is_int< char > { public: enum { value = true }; };
148 template<> class Is_int< bool > { public: enum { value = true }; };
149 template<> class Is_int< wchar_t > { public: enum { value = true }; };
150
151 template< typename U > class Is_float { public: enum { value = false }; };
152 template<> class Is_float< float > { public: enum { value = true }; };
153 template<> class Is_float< double > { public: enum { value = true }; };
154 template<> class Is_float< long double > { public: enum { value = true }; };
155
156 template<typename T> class Const_traits
157 {
158 public:
159 enum { value = false };
160 typedef T Type;
161 typedef const T Const_type;
162 };
163
164 template<typename T> class Const_traits<const T>
165 {
166 public:
167 enum { value = true };
168 typedef T Type;
169 typedef const T Const_type;
170 };
171};
172
173template< typename T >
174class Type_traits
175{
176public:
177
178 enum
179 {
180 is_unsigned = TT::Is_unsigned<T>::value,
181 is_signed = TT::Is_signed<T>::value,
182 is_int = TT::Is_int<T>::value,
183 is_float = TT::Is_float<T>::value,
184 is_pointer = TT::Pointer_traits<T>::value,
185 is_pointer_to_member = TT::PMF_traits<T>::value,
186 is_reference = TT::Ref_traits<T>::value,
187 is_scalar = is_unsigned || is_signed || is_int || is_pointer
188 || is_pointer_to_member || is_reference,
189 is_fundamental = is_unsigned || is_signed || is_float
190 || Conversion<T, void>::same_type,
191 is_const = TT::Const_traits<T>::value,
192
193 alignment =
194 (sizeof(T) >= sizeof(unsigned long)
195 ? sizeof(unsigned long)
196 : (sizeof(T) >= sizeof(unsigned)
197 ? sizeof(unsigned)
198 : (sizeof(T) >= sizeof(short)
199 ? sizeof(short)
200 : 1)))
201 };
202
203 typedef typename Select<is_scalar, T, typename TT::Add_ref<typename TT::Const_traits<T>::Const_type>::Type>::Type Param_type;
204 typedef typename TT::Pointer_traits<T>::Pointee Pointee_type;
205 typedef typename TT::Ref_traits<T>::Referee Referee_type;
206 typedef typename TT::Const_traits<T>::Type Non_const_type;
207 typedef typename TT::Const_traits<T>::Const_type Const_type;
208
209 static unsigned long align(unsigned long a)
210 {
211 return (a + static_cast<unsigned long>(alignment) - 1UL)
212 & ~(static_cast<unsigned long>(alignment) - 1UL);
213 }
214};
215
216
217};
218
219
220
Our C++ library.
Definition arith:11