/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
*/

#ifndef __H_DEFS__
#define __H_DEFS__

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stddef.h>
#include <uchar.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>

#include "config.h"

/** @file
 * @defgroup Definitions Definitions
 * @addtogroup Definitions
 * @{
 * @brief Declarations of the types and macros that are used extensively
 * throughout libssts.
 */

/**
 * @def S_BEGIN_DECLS
 * For interoperability with C++ we have to have this.
 *
 * Put at the beginning of header files.
 * @see S_END_DECLS
 */

/**
 * @def S_END_DECLS
 *
 * Put at end of header files.
 * @see S_BEGIN_DECLS
 */

#ifdef __cplusplus
#define S_BEGIN_DECLS extern "C" {
#define S_END_DECLS }
#else
#define S_BEGIN_DECLS
#define S_END_DECLS
#endif /*__cplusplus*/

S_BEGIN_DECLS

/* ***************************** GENERIC MACROS ***************************** */

/** @def
 */

/**
 * @def S_DEPRECATED
 * Mark function as deprecated.
 */
/**
 * @def S_UNUSED
 * Supress compiler warning that variable is unused.
 */
/**
 * @def S_ALIGNED_8
 * Macro to align memory to 8 bit.
 */
/**
 * @def S_ALIGNED_16
 * Macro to align memory to 16 bit.
 */
#if defined(__GNUC__)
#define S_DEPRECATED __attribute__((deprecated))
#define S_UNUSED __attribute__((unused))
#define S_ALIGNED_8 __attribute__((aligned (8)))
#define S_ALIGNED_16 __attribute__((aligned (16)))
#elif defined(__MSC_VER)
#define S_DEPRECATED __declspec(deprecated)
// TODO, check if this rely works in MSVC.
#define S_UNUSED __pragma(warning(suppress:4100))
#define S_ALIGNED_8 __declspec(align(8))
#define S_ALIGNED_16 __declspec(align(16))
#else
#define S_DEPRECATED
#define S_UNUSED
#define S_ALIGNED_8
#define S_ALIGNED_16
#endif

/**
 * @def __S_ATOMIC__
 * The stupid atomic value.
 */
#if __STDC_NO_ATOMICS__
#define __S_ATOMIC__ volatile
#else
#define __S_ATOMIC__ __Atomic
#endif

/**
 * @def S_EXPORTED
 * Mark function as exported. This makes the function available from outside a
 * library on some platforms.
 *
 * @see S_HIDDEN
 */
/**
 * @def S_HIDDEN
 * Mark function as hidden. This hides a function so it is not exposed to the
 * outside of the library.
 *
 * @see S_EXPORTED
 */

/*
 * Copied from: https://gcc.gnu.org/wiki/Visibility .. with modifications.
 */
#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_DLL
    #ifdef __GNUC__
      #define S_EXPORTED __attribute__ ((dllexport))
    #else
      #define S_EXPORTED __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #else
    #ifdef __GNUC__
      #define S_EXPORTED __attribute__ ((dllimport))
    #else
      #define S_EXPORTED __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
    #endif
  #endif
  #define S_HIDDEN
#else
  #if __GNUC__ >= 4
    #define S_EXPORTED __attribute__ ((visibility ("default")))
    #define S_HIDDEN  __attribute__ ((visibility ("hidden")))
  #else
    #define S_EXPORTED
    #define S_HIDDEN
  #endif
#endif

/**
 * FALSE has the absolute value of 0, it is used in as a way to represent a
 * false value. A "not" value.
 *
 * @see sboolean
 */
#define FALSE 0

 /**
  * TRUE represents a value that is true. A value that is "not false".
  *
  * @see sboolean
  */
#define TRUE !FALSE

/**
 * Stupid macro to create a hash of a pointer.
 */
#define S_POINTER_TO_HASH_T(p) ((hash_t)(unsigned long) p)

/**
 * @defgroup Types Type declarations
 * @addtogroup Types
 * @{
 * @brief The typedefs that make up the types that are used in libssts.
 * 
 */

/**
 * sboolean is the standard definition of a boolean value in SSTS.
 *
 * @sa TRUE
 * @sa FALSE
 *
 * @note
 * This is the way it is done in GLib, so it is done here too.
 */
typedef int32_t sboolean;

/** hash type  */
typedef size_t hash_t;

/** spointer is a convinience typedef of void * */
typedef void* spointer;

/** sconstpointer is a convinience typedef of const void * */
typedef const void* sconstpointer;

/**
 * To prevent intercomparability problems we def schar and assert its size.
 */
typedef char schar;
static_assert (sizeof(schar) == sizeof (int8_t),
              "schar has the wrong size. Is not 8 bit long.");

/**
 * uchar is the representation for use with wide unicode strings and what not.
 *
 * The rationale for using char32_t as the unicode wide char instead of
 * wchar_t is that wchar_t's size depends on the implementation, compiler,
 * and system.
 *
 * To make the wide strings actually work as they should, even over network or
 * other communication we set this as the standard for the uchar.
 *
 * @warning This is not an unsigned char. If you need to store such small
 * values use #subyte
 */
typedef char32_t suchar;

/**
 * To prevent interchangeability problems we def long as int64_t.
 */
typedef int64_t slong;

/**
 * To prevent interchangeability problems we def int as int32_t.
 */
typedef int32_t sint;

/**
 * To prevent interchangeability problems we def short as int16_t.
 */
typedef int16_t sshort;

/**
 * To prevent interchangeability problems we def byte as int8_t.
 */
typedef int8_t sbyte;

/* *************************************
   ******* UNSIGNED VERSIONS ***********
   ************************************* */

/**
 * To prevent interchangeability problems we def ulong as uint64_t.
 */
typedef uint64_t sulong;

/**
 * To prevent interchangeability problems we def uint as uint32_t.
 */
typedef uint32_t suint;

/**
 * To prevent interchangeability problems we def ushort as uint16_t.
 */
typedef uint16_t sushort;

/**
 * To prevent interchangeability problems we def ubyte as uint8_t.
 */
typedef uint8_t subyte;

/* *********** FLOATS **************** */

typedef float sfloat;
static_assert (sizeof (sfloat) == sizeof (int8_t) * 4,
              "sfloat has the wrong size. Is not 32 bit.");


typedef double sdouble;
static_assert (sizeof (sdouble) == sizeof (int8_t) * 8,
              "sdouble has the wrong size. Is not 64 bit.");


typedef __float128 squadruple;
static_assert (sizeof (squadruple) == sizeof (int8_t) * 16,
              "squadruple has the wrong size. Is not 128 bit.");

/**
 * @}
 */

/**
 * Represents different types of objects.
 *
 * @se STypeName
 */
typedef enum {
  S_TYPE_NONE = 0, /**< Not a type. */
  S_TYPE_INT, /**< @ref sint */
  S_TYPE_LONG, /**< @ref slong */
  S_TYPE_SHORT, /**< @ref sshort */
  S_TYPE_CHAR, /**< @ref schar */
  S_TYPE_WCHAR, /**< @deprecated wide char (wchar_t) */
  S_TYPE_UCHAR, /**< @ref suchar */
  S_TYPE_UINT, /**< @ref suint */
  S_TYPE_ULONG, /**< @ref sulong */
  S_TYPE_USHORT, /**< @ref sushort */
  S_TYPE_BOOLEAN, /**< @ref sboolean */
  S_TYPE_STRING, /**< a string of @ref schar s */
  S_TYPE_WSTRING, /**< @deprecated  Platform specific wchar_t string */
  S_TYPE_USTRING, /**< a string of @ref suchar s */
  S_TYPE_UNUSED_0, /**< */
  S_TYPE_UNUSED_1, /**< */
  S_TYPE_UNUSED_2, /**< */
  S_TYPE_POINTER, /**< @ref spointer */
  S_TYPE_OBJECT, /**< @ref SObject */
  S_TYPE_INVALID /** Invalid type. */
} SType;

/** @brief
 * The names of the SType's
 *
 *
 */
static char * STypeName[] S_UNUSED = {
  "NONE",
  "INT",
  "LONG",
  "SHORT",
  "CHAR",
  "WCHAR",
  "UCHAR", /*< char32_t */
  "UINT",
  "ULONG",
  "USHORT"
  "BOOLEAN",
  "STRING",
  "WSTRING",
  "USTRING",
  "UNUSED_0/INVALID",
  "UNUSED_1/INVALID",
  "UNUSED_2/INVALID",
  "POINTER",
  "OBJECT",
  "INVALID",
  0x0,
  0x0,
};

/**
 * @brief Get the name of the SType.
 *
 * For use in bindings.
 */
char *
s_type_get_name (SType k);

/**
 * @defgroup Colours Colours
 * @addtogroup Colours
 * @{
 * @brief Printing colours for terminal/console printing.
 * 
 * Here is a list of macros for use when printing to the console.
 * @ref S_COLOR_RESET is used to reset the colours back to their original
 *      colour.
 * 
 * An example on how to use them can be found in utils.h:
 * @code{.c}
#define s_err_print(p, ...)\
  fprintf (stderr, S_COLOR_RED "[ERR] " p S_COLOR_RESET "\n", ##__VA_ARGS__)
 * @endcode
 */
 
#define S_COLOR_RESET   "\033[0m"       /**< Reset */
#define S_COLOR_BLACK   "\033[30m"      /**< Black */
#define S_COLOR_RED     "\033[31m"      /**< Red */
#define S_COLOR_GREEN   "\033[32m"      /**< Green */
#define S_COLOR_YELLOW  "\033[33m"      /**< Yellow */
#define S_COLOR_BLUE    "\033[34m"      /**< Blue */
#define S_COLOR_MAGENTA "\033[35m"      /**< Magenta */
#define S_COLOR_CYAN    "\033[36m"      /**< Cyan */
#define S_COLOR_WHITE   "\033[37m"      /**< White */
#define S_COLOR_BOLDBLACK   "\033[1m\033[30m"      /**< Bold Black */
#define S_COLOR_BOLDRED     "\033[1m\033[31m"      /**< Bold Red */
#define S_COLOR_BOLDGREEN   "\033[1m\033[32m"      /**< Bold Green */
#define S_COLOR_BOLDYELLOW  "\033[1m\033[33m"      /**< Bold Yellow */
#define S_COLOR_BOLDBLUE    "\033[1m\033[34m"      /**< Bold Blue */
#define S_COLOR_BOLDMAGENTA "\033[1m\033[35m"      /**< Bold Magenta */
#define S_COLOR_BOLDCYAN    "\033[1m\033[36m"      /**< Bold Cyan */
#define S_COLOR_BOLDWHITE   "\033[1m\033[37m"      /**< Bold White */

/** @} */

/**
 * @}
 */

/* We include this last so it does not create any problems.
 * We want people only to have to include defs.h and not mm.h anyway.
 *
 * It should be considered a continuation of defs.h.
 */
#include "mm.h"

S_END_DECLS

#endif