TR_datastructures
0.1.0
A generic C data structures library
Loading...
Searching...
No Matches
include
tr_datastructures
tr_types.h
Go to the documentation of this file.
1
/**
2
* @file tr_types.h
3
* @brief Portable type definitions for C89/C99/C11 compatibility
4
*
5
* Provides fixed width integer types, bool, and NULL for use across
6
* all C standards. On C99 and later the standard headers are used
7
* directly. On C89 portable fallbacks are provided with compile time
8
* size verification.
9
*/
10
11
/*******************************************************************************************************
12
* NAME: tr_types.h
13
*
14
* PURPOSE: Portable type definitions for C89/C99/C11 compatibility
15
*
16
* GLOBAL VARIABLES: None
17
*
18
* DEVELOPMENT HISTORY:
19
*
20
* Date Author Change Id Release Description Of Change
21
* ---------- --------------- --------- ------- -----------------------------------
22
* 24-05-2026 Tiago Rodrigues 1 File preparation
23
*
24
*******************************************************************************************************/
25
#ifndef TR_TYPES_H
26
#define TR_TYPES_H
27
28
/* 0 copyright/licensing */
29
/*******************************************************************************************************
30
*
31
* This is free and unencumbered software released into the public domain (Unlicense).
32
*
33
********************************************************************************************************/
34
35
#ifdef __cplusplus
36
extern
"C"
37
{
38
#endif
39
40
/* 1 includes */
41
/*****************************************************/
42
#include <limits.h>
/* UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX - available in C89 */
43
#include <stddef.h>
/* size_t - available in C89 */
44
/*****************************************************/
45
46
/* 2 defines */
47
/*****************************************************/
48
49
/* ------------------------------------------------------------------
50
* 2.1 Compile-time assert
51
* ------------------------------------------------------------------ */
52
53
/**
54
* @brief Compile time assertion macro
55
*
56
* Triggers a compile error if @p condition is false.
57
* On C11 and later uses @c _Static_assert. On C89/C99 uses
58
* the array size trick.
59
*
60
* @param condition Expression to evaluate at compile time
61
* @param message Token used as the error identifier — no spaces allowed
62
*
63
* Example usage:
64
* @code
65
* TR_STATIC_ASSERT(sizeof(int) == 4, int_must_be_4_bytes);
66
* @endcode
67
*/
68
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
69
/* C11 - native static assert */
70
#define TR_STATIC_ASSERT(condition, message) _Static_assert((condition), #message)
71
#else
72
/* C89/C99 */
73
#define TR_STATIC_ASSERT(condition, message) \
74
typedef char tr_static_assert_##message[(condition) ? 1 : -1]
75
#endif
76
77
/* ------------------------------------------------------------------
78
* 2.2 bool
79
* ------------------------------------------------------------------ */
80
81
/**
82
* @brief Boolean type
83
*
84
* On C++ uses the native bool type.
85
* On C99 and later includes @c stdbool.h.
86
* On C89 provides a portable enum fallback with values
87
* @c false = 0 and @c true = 1, matching C99 exactly.
88
*/
89
#if defined(__cplusplus)
90
/* C++ has native bool */
91
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
92
/* C99 or later - use standard stdbool.h */
93
#include <stdbool.h>
94
#else
95
/* C89 fallback - matches C99 stdbool.h values exactly */
96
typedef
enum
97
{
98
false
= 0,
/**< Boolean false value - evaluates to 0 */
99
true
= 1
/**< Boolean true value - evaluates to 1 */
100
}
bool
;
101
#endif
102
103
/* ------------------------------------------------------------------
104
* 2.3 Fixed width integers
105
* ------------------------------------------------------------------ */
106
107
/**
108
* @defgroup tr_types_integers Fixed Width Integer Types
109
* @{
110
*
111
* @brief Portable fixed width integer types
112
*
113
* On C99 and later these are provided by @c stdint.h directly.
114
* On C89 portable fallbacks are detected at compile time using
115
* @c limits.h constants. A compile time assertion verifies the
116
* correct size on each platform.
117
*
118
* | Type | Size | Range |
119
* |-----------|--------|------------------------------|
120
* | uint8_t | 1 byte | 0 to 255 |
121
* | int8_t | 1 byte | -128 to 127 |
122
* | uint16_t | 2 byte | 0 to 65535 |
123
* | int16_t | 2 byte | -32768 to 32767 |
124
* | uint32_t | 4 byte | 0 to 4294967295 |
125
* | int32_t | 4 byte | -2147483648 to 2147483647 |
126
* | uint64_t | 8 byte | 0 to 18446744073709551615 |
127
* | int64_t | 8 byte | -9223372036854775808 to max |
128
*/
129
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
130
/* C99 or later - use standard stdint.h */
131
#include <stdint.h>
132
#else
133
/* C89 fallback - auto detect correct types using limits.h constants */
134
135
/* uint8_t / int8_t - find a type that is exactly 1 byte */
136
#if defined(UCHAR_MAX) && (UCHAR_MAX == 0xFF)
137
typedef
unsigned
char
uint8_t;
138
typedef
signed
char
int8_t;
139
#else
140
#error "Cannot find a suitable 8-bit type on this platform"
141
#endif
142
143
/* uint16_t / int16_t - find a type that is exactly 2 bytes */
144
#if defined(USHRT_MAX) && (USHRT_MAX == 0xFFFF)
145
typedef
unsigned
short
uint16_t;
146
typedef
signed
short
int16_t;
147
#elif defined(UINT_MAX) && (UINT_MAX == 0xFFFF)
148
typedef
unsigned
int
uint16_t;
149
typedef
signed
int
int16_t;
150
#else
151
#error "Cannot find a suitable 16-bit type on this platform"
152
#endif
153
154
/* uint32_t / int32_t - find a type that is exactly 4 bytes */
155
#if defined(UINT_MAX) && (UINT_MAX == 0xFFFFFFFF)
156
typedef
unsigned
int
uint32_t;
157
typedef
signed
int
int32_t;
158
#elif defined(ULONG_MAX) && (ULONG_MAX == 0xFFFFFFFF)
159
typedef
unsigned
long
uint32_t;
160
typedef
signed
long
int32_t;
161
#else
162
#error "Cannot find a suitable 32-bit type on this platform"
163
#endif
164
165
/* uint64_t / int64_t - find a type that is exactly 8 bytes */
166
#if defined(_MSC_VER)
167
/* Microsoft Visual C++ compiler - use __int64 extension */
168
typedef
unsigned
__int64 uint64_t;
169
typedef
signed
__int64 int64_t;
170
#elif defined(__GNUC__) || defined(__clang__)
171
/* GCC or Clang - long long is supported as an extension in C89 */
172
typedef
unsigned
long
long
uint64_t;
173
typedef
signed
long
long
int64_t;
174
#elif defined(ULONG_MAX) && (ULONG_MAX == 0xFFFFFFFFFFFFFFFF)
175
/* generic fallback - unsigned long happens to be 64 bit */
176
typedef
unsigned
long
uint64_t;
177
typedef
signed
long
int64_t;
178
#else
179
#error "Cannot find a suitable 64-bit type on this platform"
180
#endif
181
182
#endif
/* __STDC_VERSION__ >= 199901L */
183
184
/* ------------------------------------------------------------------
185
* 2.4 NULL
186
* ------------------------------------------------------------------ */
187
188
/**
189
* @brief Null pointer constant
190
*
191
* Defined as @c 0 for C++ (where void* does not implicitly convert)
192
* and as @c ((void*)0) for C. Only defined if not already provided
193
* by an included system header.
194
*/
195
#ifndef NULL
196
#ifdef __cplusplus
197
#define NULL 0
198
#else
199
#define NULL ((void *) 0)
200
#endif
201
#endif
202
203
// clang-format off
204
205
/*****************************************************/
206
207
/* 3 external declarations */
208
/*****************************************************/
209
/*****************************************************/
210
211
/* 4 typedefs */
212
/*****************************************************/
213
/*****************************************************/
214
215
/* 5 global variable declarations */
216
/*****************************************************/
217
/*****************************************************/
218
219
/* 6 function prototypes */
220
/*****************************************************/
221
/*****************************************************/
222
223
// clang-format on
224
225
#ifdef __cplusplus
226
}
227
#endif
228
229
#endif
/* TR_TYPES_H */
bool
bool
Boolean type.
Definition
tr_types.h:97
Generated by
1.9.8