xoreos  0.0.5
types.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_TYPES_H
26 #define COMMON_TYPES_H
27 
28 #ifdef HAVE_CONFIG_H
29  #include "config.h"
30 #endif
31 
32 #include <cstddef>
33 #include <climits>
34 
35 #ifdef HAVE_INTTYPES_H
36  #include <inttypes.h>
37 #endif // HAVE_INTTYPES_H
38 #ifdef HAVE_STDINT_H
39  #include <stdint.h>
40 #endif // HAVE_STDINT_H
41 #ifdef HAVE_SYS_TYPES_H
42  #include <sys/types.h>
43 #endif // HAVE_SYS_TYPES_H
44 
45 #if defined(HAVE_INTTYPES_H) || defined(HAVE_STDINT_H)
46  /* If either of these is available, the C99 integer types are
47  available. Furthermore <inttypes.h> should even already
48  include <stdint.h>. */
49 #elif defined(HAVE_SYS_TYPES_H)
50  /* <sys/types.h> should define signed integer types just like
51  C99. However, the unsigned ones are usually named a bit
52  differently.
53 
54  TODO: This might be different on some platforms!
55  */
56 
57  typedef u_int8_t uint8_t;
58  typedef u_int16_t uint16_t;
59  typedef u_int32_t uint32_t;
60  typedef u_int64_t uint64_t;
61 #elif defined(SIZEOF_CHAR) && \
62  defined(SIZEOF_SHORT) && \
63  defined(SIZEOF_INT) && \
64  defined(SIZEOF_LONG) && \
65  defined(SIZEOF_LONG_LONG)
66 
67  /* Otherwise, look for candidates among the usual integer
68  types, if we know about their sizes. */
69 
70  #if SIZEOF_CHAR == 1
71  typedef signed char int8_t;
72  typedef unsigned char uint8_t;
73  #else
74  #error Cannot find an 8-bit integer type
75  #endif
76 
77  #if SIZEOF_SHORT == 2
78  typedef short int16_t;
79  typedef unsigned short uint16_t;
80  #elif SIZEOF_INT == 2
81  typedef int int16_t;
82  typedef unsigned int uint16_t;
83  #else
84  #error Cannot find a 16-bit integer type
85  #endif
86 
87  #if SIZEOF_INT == 4
88  typedef int int32_t;
89  typedef unsigned int uint32_t;
90  #elif SIZEOF_LONG == 4
91  typedef long int32_t;
92  typedef unsigned long uint32_t;
93  #else
94  #error Cannot find a 32-bit integer type
95  #endif
96 
97  #if SIZEOF_LONG == 8
98  typedef long int64_t;
99  typedef unsigned long uint64_t;
100  #elif SIZEOF_LONG_LONG == 8
101  typedef long long int64_t;
102  typedef unsigned long long uint64_t;
103  #else
104  #error Cannot find a 64-bit integer type
105  #endif
106 #else
107  #error Cannot find a way to derive fixed-width integer types
108 #endif
109 
110 // intptr_t
111 #ifndef HAVE_INTPTR_T
112  #undef intptr_t
113 
114  #if defined(SIZEOF_VOID_P) && \
115  defined(SIZEOF_SHORT) && \
116  defined(SIZEOF_INT) && \
117  defined(SIZEOF_LONG) && \
118  defined(SIZEOF_LONG_LONG)
119 
120  #if SIZEOF_VOID_P == SIZEOF_SHORT
121  typedef short intptr_t;
122  #define HAVE_INTPTR_T
123  #elif SIZEOF_VOID_P = SIZEOF_INT
124  typedef int intptr_t;
125  #define HAVE_INTPTR_T
126  #elif SIZEOF_VOID_P = SIZEOF_LONG
127  typedef long intptr_t;
128  #define HAVE_INTPTR_T
129  #elif SIZEOF_VOID_P = SIZEOF_LONG_LONG
130  typedef long long intptr_t;
131  #define HAVE_INTPTR_T
132  #endif
133  #endif
134 
135  #ifndef HAVE_INTPTR_T
136  #error Cannot find a way to derive a signed integer type to fit a pointer
137  #endif
138 #endif
139 
140 // uintptr_t
141 #ifndef HAVE_UINTPTR_T
142  #undef uintptr_t
143 
144  #if defined(SIZEOF_VOID_P) && \
145  defined(SIZEOF_SHORT) && \
146  defined(SIZEOF_INT) && \
147  defined(SIZEOF_LONG) && \
148  defined(SIZEOF_LONG_LONG)
149 
150  #if SIZEOF_VOID_P == SIZEOF_SHORT
151  typedef unsigned short uintptr_t;
152  #define HAVE_UINTPTR_T
153  #elif SIZEOF_VOID_P = SIZEOF_INT
154  typedef unsigned int uintptr_t;
155  #define HAVE_UINTPTR_T
156  #elif SIZEOF_VOID_P = SIZEOF_LONG
157  typedef unsigned long uintptr_t;
158  #define HAVE_UINTPTR_T
159  #elif SIZEOF_VOID_P = SIZEOF_LONG_LONG
160  typedef unsigned long long uintptr_t;
161  #define HAVE_UINTPTR_T
162  #endif
163  #endif
164 
165  #ifndef HAVE_UINTPTR_T
166  #error Cannot find a way to derive an unsigned integer type to fit a pointer
167  #endif
168 #endif
169 
170 // The maximum value a size_t can describe
171 #ifndef SIZE_MAX
172  #define SIZE_MAX ((size_t) -1)
173 #endif
174 
175 // Fallback macro to printf an int64. A bit wonky; usage discouraged.
176 #ifndef PRId64
177  #define PRId64 "lld"
178  #define Cd64(x) ((signed long long) (x))
179 #else
180  #define Cd64(x) ((int64) (x))
181 #endif
182 
183 // Fallback macro to printf an uint64. A bit wonky; usage discouraged.
184 #ifndef PRIu64
185  #define PRIu64 "llu"
186  #define Cu64(x) ((unsigned long long) (x))
187 #else
188  #define Cu64(x) ((uint64) (x))
189 #endif
190 
191 /* Shorter type definitions we inherited from ScummVM.
192  Right now, we're keeping them to stay somewhat compatible
193  with ScummVM and ResidualVM, to foster easier code exchange.
194 
195  TODO: We might maybe want to think about completely migrating
196  to just the standard C99 types. Such a change will be a bit
197  messy, though.
198 */
199 typedef int8_t int8;
200 typedef uint8_t uint8;
201 typedef int16_t int16;
202 typedef uint16_t uint16;
203 typedef int32_t int32;
204 typedef uint32_t uint32;
205 typedef int64_t int64;
206 typedef uint64_t uint64;
207 
208 // Type to indicate that this is a raw byte (or an array of raw bytes, ...).
209 typedef uint8 byte;
210 // Convenience type definition for a simple unsigned int.
211 typedef unsigned int uint;
212 
213 // Fallback macro to create an int64 constant.
214 #ifndef INT64_C
215  #define INT64_C(c) (c ## LL)
216 #endif
217 
218 // Fallback macro to create an uint64 constant.
219 #ifndef UINT64_C
220  #define UINT64_C(c) (c ## ULL)
221 #endif
222 
223 // Fallback macros for type limits.
224 #ifndef UINT8_MAX
225  #define UINT8_MAX 0xFF
226 #endif
227 #ifndef UINT16_MAX
228  #define UINT16_MAX 0xFFFF
229 #endif
230 #ifndef UINT32_MAX
231  #define UINT32_MAX 0xFFFFFFFF
232 #endif
233 #ifndef UINT64_MAX
234  #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
235 #endif
236 
237 #ifndef INT8_MAX
238  #define INT8_MAX 0x7F
239 #endif
240 #ifndef INT16_MAX
241  #define INT16_MAX 0x7FFF
242 #endif
243 #ifndef INT32_MAX
244  #define INT32_MAX 0x7FFFFFFF
245 #endif
246 #ifndef INT64_MAX
247  #define INT64_MAX INT64_C(0x7FFFFFFFFFFFFFFF)
248 #endif
249 
250 #ifndef INT8_MIN
251  #define INT8_MIN (-INT8_MAX - 1)
252 #endif
253 #ifndef INT16_MIN
254  #define INT16_MIN (-INT16_MAX - 1)
255 #endif
256 #ifndef INT32_MIN
257  #define INT32_MIN (-INT32_MAX - 1)
258 #endif
259 #ifndef INT64_MIN
260  #define INT64_MIN (-INT64_MAX - INT64_C(1))
261 #endif
262 
263 #endif // COMMON_TYPES_H
uint64_t uint64
Definition: types.h:206
uint8_t uint8
Definition: types.h:200
int16_t int16
Definition: types.h:201
uint16_t uint16
Definition: types.h:202
int64_t int64
Definition: types.h:205
int8_t int8
Definition: types.h:199
uint32_t uint32
Definition: types.h:204
uint8 byte
Definition: types.h:209
unsigned int uint
Definition: types.h:211
int32_t int32
Definition: types.h:203