]> git.tdb.fi Git - libs/core.git/blob - source/core/inttypes.h
Use a different diagnostic pragma for recent GCC versions
[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 #if __GNUC__>=5
18 #pragma GCC diagnostic ignored "-Wpedantic"
19 #else
20 #pragma GCC diagnostic ignored "-pedantic"
21 #endif
22 typedef TypeList<StandardSignedTypes, long long, __int128>::Type PlatformSignedTypes;
23 typedef TypeList<StandardUnsignedTypes, unsigned long long, unsigned __int128>::Type PlatformUnsignedTypes;
24 #pragma GCC diagnostic pop
25 #else
26 typedef TypeList<StandardSignedTypes, long long>::Type PlatformSignedTypes;
27 typedef TypeList<StandardUnsignedTypes, unsigned long long>::Type PlatformUnsignedTypes;
28 #endif
29 #elif defined(_MSC_VER)
30 typedef TypeList<StandardSignedTypes, __int64>::Type PlatformSignedTypes;
31 typedef TypeList<StandardUnsignedTypes, unsigned __int64>::Type PlatformUnsignedTypes;
32 #else
33 typedef StandardSignedTypes PlatformSignedTypes;
34 typedef StandardUnsignedTypes PlatformUnsignedTypes;
35 #endif
36
37 /** A helper for choosing an integer type of a specific size.  The size
38 parameter is in bits.  The resulting types can be obtained from the SignedType
39 and UnsignedType typedefs. */
40 template<unsigned size>
41 struct Int
42 {
43         template<typename T>
44         struct Predicate
45         {
46                 enum { value = (sizeof(T)*CHAR_BIT==size) };
47         };
48
49         typedef typename TypeChooser<PlatformSignedTypes, Predicate>::Type SignedType;
50         typedef typename TypeChooser<PlatformUnsignedTypes, Predicate>::Type UnsignedType;
51 };
52
53 /** A helper for choosing an integer that's the same size as another type. */
54 template<typename T>
55 struct MatchingInt: Int<sizeof(T)*CHAR_BIT>
56 { };
57
58 // Finally define convenient shorthands for the actual integer types
59 typedef Int<8>::SignedType Int8;
60 typedef Int<8>::UnsignedType UInt8;
61 typedef Int<16>::SignedType Int16;
62 typedef Int<16>::UnsignedType UInt16;
63 typedef Int<32>::SignedType Int32;
64 typedef Int<32>::UnsignedType UInt32;
65 typedef Int<64>::SignedType Int64;
66 typedef Int<64>::UnsignedType UInt64;
67 typedef MatchingInt<void *>::UnsignedType IntPtr;
68
69 } // namespace Msp
70
71 #endif