Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef ALIGNOF_REPLACEMENT_HPP_INCLUDED_
00060 #define ALIGNOF_REPLACEMENT_HPP_INCLUDED_
00061
00062
00063
00064 #if defined (__cplusplus)
00065
00066 namespace Alignment
00067 {
00068
00069
00070
00071 namespace ffs
00072 {
00073
00074 template <typename T>
00075 struct alignof
00076 {
00077
00078 enum { s = sizeof (T), value = s ^ (s & (s - 1)) };
00079 };
00080 }
00081
00082 namespace QuantumJump
00083 {
00084
00085
00086 template <typename T> struct alignof;
00087
00088 template <int size_diff>
00089 struct helper
00090 {
00091 template <typename T> struct Val { enum { value = size_diff }; };
00092 };
00093
00094 template <>
00095 struct helper<0>
00096 {
00097 template <typename T> struct Val { enum { value = alignof<T>::value }; };
00098 };
00099
00100 template <typename T>
00101 struct alignof
00102 {
00103 struct Big { T x; char c; };
00104
00105 enum { diff = sizeof (Big) - sizeof (T),
00106 value = helper<diff>::template Val<Big>::value };
00107 };
00108
00109 }
00110
00111 template <typename T>
00112 struct alignof
00113 {
00114 enum { x = QuantumJump::alignof<T>::value,
00115 y = ffs::alignof<T>::value,
00116 value = x < y ? x : y };
00117 };
00118
00119
00120
00121 namespace POD
00122 {
00123
00124
00125
00126
00127
00128
00129 template <typename T, typename U>
00130 struct equally_aligned
00131 {
00132 enum { value = ((int) alignof<T>::value ==
00133 (int) alignof<U>::value) };
00134 };
00135
00136 enum { TYPES_COUNT = 10 };
00137
00138 template <int n> struct types;
00139
00140 struct Unknown;
00141
00142 template <bool POINTERIFY, bool STRUCTIFY>
00143 struct types_helper
00144 {
00145
00146 template <int index> struct Val { typedef char Type; };
00147 };
00148
00149 template <>
00150 struct types_helper<true, false>
00151 {
00152 template <int index>
00153 struct Val
00154 {
00155 typedef typename types<index - (1 * TYPES_COUNT)>::Type BaseType;
00156 typedef BaseType* Type;
00157 };
00158 };
00159
00160 template <>
00161 struct types_helper<false, true>
00162 {
00163 template <int index>
00164 struct Val
00165 {
00166 typedef typename types<index - (2 * TYPES_COUNT)>::Type BaseType;
00167 struct Type { BaseType t; };
00168 };
00169 };
00170
00171 template <int n>
00172 struct types
00173 {
00174 enum { pointerify = ((n >= 1 * TYPES_COUNT) && (n < 2 * TYPES_COUNT)) };
00175 enum { structify = ((n >= 2 * TYPES_COUNT) && (n < 3 * TYPES_COUNT)) };
00176
00177 typedef typename
00178 types_helper<pointerify, structify>::template Val<n>::Type
00179 Type;
00180 };
00181 template <> struct types<0> { typedef char Type; };
00182 template <> struct types<1> { typedef short Type; };
00183 template <> struct types<2> { typedef int Type; };
00184 template <> struct types<3> { typedef long Type; };
00185 template <> struct types<4> { typedef float Type; };
00186 template <> struct types<5> { typedef double Type; };
00187 template <> struct types<6> { typedef long double Type; };
00188 template <> struct types<7> { typedef void* Type; };
00189 template <> struct types<8> { typedef void (*Type) (void); };
00190 template <> struct types<9> { typedef Unknown (Unknown::*Type) (Unknown);};
00191
00192 namespace LinearSearch
00193 {
00194 template <bool EQUALLY_ALIGNED> struct helper;
00195
00196 template <>
00197 struct helper<true>
00198 {
00199 template <typename T, int index>
00200 struct Val
00201 {
00202 typedef typename types<index>::Type Type;
00203 };
00204 };
00205
00206 template <typename T, int index>
00207 struct alignpod
00208 {
00209 typedef typename types<index>::Type Candidate;
00210 enum { EQUALLY_ALIGNED = equally_aligned<T,Candidate>::value };
00211
00212 typedef typename
00213 helper<EQUALLY_ALIGNED>::template Val<T,index>::Type
00214 Type;
00215 };
00216
00217 template <>
00218 struct helper<false>
00219 {
00220 template <typename T, int index>
00221 struct Val
00222 {
00223 typedef typename alignpod<T, index+1>::Type Type;
00224 };
00225 };
00226
00227 }
00228
00229 }
00230
00231 template <typename T>
00232 struct alignpod
00233 {
00234 typedef typename POD::LinearSearch::alignpod<T,0>::Type Type;
00235 };
00236 }
00237
00238 #endif
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 #if defined (__cplusplus)
00261 #define ALIGNOF(type) Alignment::alignof<type>::value
00262 #else
00263
00264
00265
00266
00267
00268
00269 #define ALIGNOF(type) offsetof (struct { char c; type member; }, member)
00270 #endif
00271
00272
00273
00274
00275 #if defined (__cplusplus)
00276 #define ALIGN_POD_TYPE(type) Alignment::alignpod<type>::Type
00277 #else
00278 #define ALIGN_POD_TYPE(type) type
00279 #endif
00280
00281
00282
00283
00284 #endif // Recursive inclusion guard