18 #ifndef NDB_TYPES_UTIL_HPP
19 #define NDB_TYPES_UTIL_HPP
24 #include "my_global.h"
27 #include "ndb_types.h"
63 static bool isBinary();
66 static bool isFixedSized();
69 static Uint32 lengthPrefixSize();
81 template<
typename T >
94 template<
typename T >
112 static Uint32 size();
144 template<
typename T >
184 static Uint32 readLengthPrefix(
const void * a);
188 static void writeLengthPrefix(
void * a, Uint32 l);
207 template<
typename T >
210 typedef typename NumTypeTraits< T >::SignedT SignedT;
211 typedef typename NumTypeTraits< T >::UnsignedT UnsignedT;
214 static SignedT asSigned(T t) {
return static_cast< SignedT
>(t); }
217 static UnsignedT asUnsigned(T t) {
return static_cast< UnsignedT
>(t); }
220 static void load(T * t,
const char * s);
223 static void store(
char * t,
const T * s);
247 template<
typename T >
250 typedef typename NonStdNumTypeTraits< T >::SignedT SignedT;
251 typedef typename NonStdNumTypeTraits< T >::UnsignedT UnsignedT;
254 static SignedT asSigned(T t) {
return static_cast< SignedT
>(t); }
257 static UnsignedT asUnsigned(T t) {
return static_cast< UnsignedT
>(t); }
260 static void load(T * t,
const char * s);
263 static void store(
char * t,
const T * s);
275 #define NDB_SPECIALIZE_ARRAY_TYPE_TRAITS( TR, B, FS, LPS ) \
276 template<> inline bool TR::isBinary() { return B; } \
277 template<> inline bool TR::isFixedSized() { return FS; } \
278 template<> inline Uint32 TR::lengthPrefixSize() { return LPS; }
283 NDB_SPECIALIZE_ARRAY_TYPE_TRAITS(
Tchar,
false,
true, 0)
284 NDB_SPECIALIZE_ARRAY_TYPE_TRAITS(
Tbinary, true, true, 0)
285 NDB_SPECIALIZE_ARRAY_TYPE_TRAITS(
Tvarchar, false, false, 1)
286 NDB_SPECIALIZE_ARRAY_TYPE_TRAITS(
Tvarbinary, true, false, 1)
287 NDB_SPECIALIZE_ARRAY_TYPE_TRAITS(
Tlongvarchar, false, false, 2)
289 #undef NDB_SPECIALIZE_ARRAY_TYPE_TRAITS
292 #define NDB_SPECIALIZE_NUM_TYPE_MAP( DT, ST, UT, I, S ) \
293 template<> struct NumTypeMap< DT > { \
294 typedef DT DomainT; \
295 typedef ST SignedT; \
296 typedef UT UnsignedT; \
297 static bool isIntegral() { return S; }; \
298 static bool isSigned() { return S; }; \
301 NDB_SPECIALIZE_NUM_TYPE_MAP(Int8, Int8, Uint8,
true,
true)
302 NDB_SPECIALIZE_NUM_TYPE_MAP(Uint8, Int8, Uint8, true, false)
303 NDB_SPECIALIZE_NUM_TYPE_MAP(Int16, Int16, Uint16, true, true)
304 NDB_SPECIALIZE_NUM_TYPE_MAP(Uint16, Int16, Uint16, true, false)
305 NDB_SPECIALIZE_NUM_TYPE_MAP(Int32, Int32, Uint32, true, true)
306 NDB_SPECIALIZE_NUM_TYPE_MAP(Uint32, Int32, Uint32, true, false)
307 NDB_SPECIALIZE_NUM_TYPE_MAP(Int64, Int64, Uint64, true, true)
308 NDB_SPECIALIZE_NUM_TYPE_MAP(Uint64, Int64, Uint64, true, false)
310 NDB_SPECIALIZE_NUM_TYPE_MAP(
float,
float,
float, false, true)
311 NDB_SPECIALIZE_NUM_TYPE_MAP(
double,
double,
double, false, true)
312 #undef NDB_SPECIALIZE_NUM_TYPE_MAP
315 #define NDB_SPECIALIZE_NUM_TYPE_TRAITS( TR, T, SZ, LO, HI, SM ) \
316 template<> inline Uint32 TR::size() { return SZ; } \
317 template<> inline T TR::lowest() { return LO; } \
318 template<> inline T TR::highest() { return HI; } \
319 template<> inline T TR::smallest() { return SM; }
321 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tint8, Int8, 1, INT_MIN8, INT_MAX8, 0)
322 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tint16, Int16, 2, INT_MIN16, INT_MAX16, 0)
323 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tint32, Int32, 4, INT_MIN32, INT_MAX32, 0)
324 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tint64, Int64, 8, INT_MIN64, INT_MAX64, 0)
326 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tuint8, Uint8, 1, 0, UINT_MAX8, 0)
327 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tuint16, Uint16, 2, 0, UINT_MAX16, 0)
328 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tuint32, Uint32, 4, 0, UINT_MAX32, 0)
329 NDB_SPECIALIZE_NUM_TYPE_TRAITS(
Tuint64, Uint64, 8, 0, UINT_MAX64, 0)
331 #undef NDB_SPECIALIZE_NUM_TYPE_TRAITS
334 #define NDB_SPECIALIZE_NON_STD_NUM_TYPE_TRAITS( TR, T, LO, HI ) \
335 template<> inline T TR::lowest() { return LO; } \
336 template<> inline T TR::highest() { return HI; }
338 NDB_SPECIALIZE_NON_STD_NUM_TYPE_TRAITS(
Tint24, Int32, INT_MIN24, INT_MAX24)
339 NDB_SPECIALIZE_NON_STD_NUM_TYPE_TRAITS(
Tuint24, Uint32, 0, UINT_MAX24)
340 #undef NDB_SPECIALIZE_NON_STD_NUM_TYPE_TRAITS
347 #define NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS0( H ) \
348 template<> inline Uint32 H::readLengthPrefix(const void * a) { \
353 template<> inline void H::writeLengthPrefix(void * a, Uint32 l) { \
358 NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS0(
Hchar)
359 NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS0(
Hbinary)
360 #undef NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS0
363 #define NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS1( H ) \
364 template<> inline Uint32 H::readLengthPrefix(const void * a) { \
366 const Uint8 * s = static_cast<const Uint8 *>(a); \
369 template<> inline void H::writeLengthPrefix(void * a, Uint32 l) { \
371 assert(l >> (lengthPrefixSize() * 8) == 0); \
372 Uint8 * t = static_cast<Uint8 *>(a); \
373 t[0] = l & 0x000000FF; \
376 NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS1(
Hvarchar)
377 NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS1(
Hvarbinary)
378 #undef NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS1
381 #define NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS2( H ) \
382 template<> inline Uint32 H::readLengthPrefix(const void * a) { \
384 const Uint8 * s = static_cast<const Uint8 *>(a); \
385 return static_cast<Uint32>(s[0] + (s[1] << 8)); \
387 template<> inline void H::writeLengthPrefix(void * a, Uint32 l) { \
389 assert(l >> (lengthPrefixSize() * 8) == 0); \
390 Uint8 * t = static_cast<Uint8 *>(a); \
391 t[0] = static_cast<Uint8>(l & 0x000000FF); \
392 t[1] = static_cast<Uint8>((l & 0x0000FF00) >> 8); \
397 #undef NDB_SPECIALIZE_ARRAY_TYPE_HELPER_LPS2
400 #define NDB_SPECIALIZE_NUM_TYPE_HELPER_BYTE( H, T ) \
401 template<> inline void H::load(T * t, const char * s) { \
402 assert(t); assert(s); assert(t != (const T *)s); \
403 *t = static_cast<T>(*s); \
405 template<> inline void H::store(char * t, const T * s) { \
406 H::load(reinterpret_cast<T *>(t), \
407 reinterpret_cast<const char *>(s)); \
410 NDB_SPECIALIZE_NUM_TYPE_HELPER_BYTE(
Hint8, Int8);
411 NDB_SPECIALIZE_NUM_TYPE_HELPER_BYTE(
Huint8, Uint8);
412 #undef NDB_SPECIALIZE_NUM_TYPE_HELPER_BYTE
415 #define NDB_SPECIALIZE_NUM_TYPE_HELPER( H, T ) \
416 template<> inline void H::load(T * t, const char * s) { \
417 assert(t); assert(s); assert(t != (const T *)s); \
418 memcpy(t, s, H::size()); \
420 template<> inline void H::store(char * t, const T * s) { \
421 H::load(reinterpret_cast<T *>(t), \
422 reinterpret_cast<const char *>(s)); \
425 NDB_SPECIALIZE_NUM_TYPE_HELPER(
Hint16, Int16);
426 NDB_SPECIALIZE_NUM_TYPE_HELPER(
Hint32, Int32);
427 NDB_SPECIALIZE_NUM_TYPE_HELPER(
Hint64, Int64);
429 NDB_SPECIALIZE_NUM_TYPE_HELPER(
Huint16, Uint16);
430 NDB_SPECIALIZE_NUM_TYPE_HELPER(
Huint32, Uint32);
431 NDB_SPECIALIZE_NUM_TYPE_HELPER(
Huint64, Uint64);
433 #undef NDB_SPECIALIZE_NUM_TYPE_HELPER
436 #define NDB_SPECIALIZE_NON_STD_NUM_TYPE_HELPER( H, T, INT3KORR ) \
437 template<> inline void H::load(T * t, const char * s) { \
438 assert(t); assert(s); assert(t != (const T *)s); \
439 *t = (INT3KORR(s)); \
441 template<> inline void H::store(char * t, const T * s) { \
442 assert(t); assert(s); assert((const T *)t != s); \
443 int3store(t, (*s)); \
446 NDB_SPECIALIZE_NON_STD_NUM_TYPE_HELPER(
Hint24, Int32, sint3korr)
447 NDB_SPECIALIZE_NON_STD_NUM_TYPE_HELPER(
Huint24, Uint32, uint3korr)
448 #undef NDB_SPECIALIZE_NON_STD_NUM_TYPE_HELPER