]> git.tdb.fi Git - libs/core.git/blob - source/core/inttypes.h
Exactly-sized integer types, the C++ way
[libs/core.git] / source / core / inttypes.h
1 #ifndef MSP_CORE_INTTYPES_H_
2 #define MSP_CORE_INTTYPES_H_
3
4 #include <climits>
5 #include "typelist.h"
6
7 namespace Msp {
8
9 // Define lists of standard types, both signed and unsigned variants
10 typedef TypeList<signed char, short, int, long>::Type StandardSignedTypes;
11 typedef TypeList<unsigned char, unsigned short, unsigned int, unsigned long>::Type StandardUnsignedTypes;
12
13 // Then add possible nonstandard types
14 #if defined(__GNUC__)
15 #if (__GNUC__>4 || (__GNUC__==4 && __GNUC_MINOR__>=6)) && defined(__LP64__)
16 #pragma GCC diagnostic push
17 #pragma GCC diagnostic ignored "-pedantic"
18 typedef TypeList<StandardSignedTypes, long long, __int128>::Type PlatformSignedTypes;
19 typedef TypeList<StandardUnsignedTypes, unsigned long long, unsigned __int128>::Type PlatformUnsignedTypes;
20 #pragma GCC diagnostic pop
21 #else
22 typedef TypeList<StandardSignedTypes, long long>::Type PlatformSignedTypes;
23 typedef TypeList<StandardUnsignedTypes, unsigned long long>::Type PlatformUnsignedTypes;
24 #endif
25 #elif defined(_MSC_VER)
26 typedef TypeList<StandardSignedTypes, __int64>::Type PlatformSignedTypes;
27 typedef TypeList<StandardUnsignedTypes, unsigned __int64>::Type PlatformUnsignedTypes;
28 #else
29 typedef StandardSignedTypes PlatformSignedTypes;
30 typedef StandardUnsignedTypes PlatformUnsignedTypes;
31 #endif
32
33 /** A helper for choosing an integer type of a specific size.  The size
34 parameter is in bits.  The resulting types can be obtained from the SignedType
35 and UnsignedType typedefs. */
36 template<unsigned size>
37 struct Int
38 {
39         template<typename T>
40         struct Predicate
41         {
42                 enum { value = (sizeof(T)*CHAR_BIT==size) };
43         };
44
45         typedef typename TypeChooser<PlatformSignedTypes, Predicate>::Type SignedType;
46         typedef typename TypeChooser<PlatformUnsignedTypes, Predicate>::Type UnsignedType;
47 };
48
49 // Finally define convenient shorthands for the actual integer types
50 typedef Int<8>::SignedType Int8;
51 typedef Int<8>::UnsignedType UInt8;
52 typedef Int<16>::SignedType Int16;
53 typedef Int<16>::UnsignedType UInt16;
54 typedef Int<32>::SignedType Int32;
55 typedef Int<32>::UnsignedType UInt32;
56 typedef Int<64>::SignedType Int64;
57 typedef Int<64>::UnsignedType UInt64;
58
59 } // namespace Msp
60
61 #endif