/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
1
#pragma once
2
/*
3
Copyright (c) 2013-2015 Gustav Hartvigsson
4
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
of this software and associated documentation files (the "Software"), to deal
7
in the Software without restriction, including without limitation the rights
8
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
copies of the Software, and to permit persons to whom the Software is
10
furnished to do so, subject to the following conditions:
11
12
The above copyright notice and this permission notice shall be included in
13
all copies or substantial portions of the Software.
14
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
THE SOFTWARE.
22
 */
23
24
#include "defs.h"
25
26
S_BEGIN_DECLS
27
28
/**
29
 * @file
30
 * @addtogroup Definitions
31
 * @{
32
 * @defgroup Types Type declarations
33
 * @addtogroup Types
34
 * @{
35
 * @brief The typedefs that make up the types that are used in libssts.
36
 *
37
 */
38
39
40
/**
41
 * sboolean is the standard definition of a boolean value in SSTS.
42
 *
43
 * @sa TRUE
44
 * @sa FALSE
45
 *
46
 * @note
47
 * This is the way it is done in GLib, so it is done here too.
48
 */
163 by Gustav Hartvigsson
Small cleanup and use _Bool
49
typedef _Bool sboolean;
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
50
51
/** hash type  */
52
typedef size_t hash_t;
53
54
/** spointer is a convinience typedef of void * */
55
typedef void* spointer;
56
57
/** sconstpointer is a convinience typedef of const void * */
58
typedef const void* sconstpointer;
59
60
/**
61
 * To prevent intercomparability problems we def schar and assert its size.
62
 */
63
typedef char schar;
64
static_assert (sizeof(schar) == sizeof (int8_t),
65
              "schar has the wrong size. Is not 8 bit long.");
66
67
/**
68
 * uchar is the representation for use with wide unicode strings and what not.
69
 *
70
 * The rationale for using char32_t as the unicode wide char instead of
71
 * wchar_t is that wchar_t's size depends on the implementation, compiler,
72
 * and system.
73
 *
74
 * To make the wide strings actually work as they should, even over network or
75
 * other communication we set this as the standard for the uchar.
76
 *
77
 * @warning This is not an unsigned char. If you need to store such small
78
 * values use #subyte
79
 */
80
typedef char32_t suchar;
81
82
/**
83
 * To prevent interchangeability problems we def long as int64_t.
84
 */
85
typedef int64_t slong;
86
87
/**
88
 * To prevent interchangeability problems we def int as int32_t.
89
 */
90
typedef int32_t sint;
91
92
/**
93
 * To prevent interchangeability problems we def short as int16_t.
94
 */
95
typedef int16_t sshort;
96
97
/**
98
 * To prevent interchangeability problems we def byte as int8_t.
99
 */
100
typedef int8_t sbyte;
101
102
/* *************************************
103
   ******* UNSIGNED VERSIONS ***********
104
   ************************************* */
105
106
/**
107
 * To prevent interchangeability problems we def ulong as uint64_t.
108
 */
109
typedef uint64_t sulong;
110
111
/**
112
 * To prevent interchangeability problems we def uint as uint32_t.
113
 */
114
typedef uint32_t suint;
115
116
/**
117
 * To prevent interchangeability problems we def ushort as uint16_t.
118
 */
119
typedef uint16_t sushort;
120
121
/**
122
 * To prevent interchangeability problems we def ubyte as uint8_t.
123
 */
124
typedef uint8_t subyte;
125
126
/* *********** FLOATS **************** */
127
128
typedef float sfloat;
129
static_assert (sizeof (sfloat) == sizeof (int8_t) * 4,
130
              "sfloat has the wrong size. Is not 32 bit.");
131
132
133
typedef double sdouble;
134
static_assert (sizeof (sdouble) == sizeof (int8_t) * 8,
135
              "sdouble has the wrong size. Is not 64 bit.");
136
137
138
typedef __float128 squadruple;
139
static_assert (sizeof (squadruple) == sizeof (int8_t) * 16,
140
              "squadruple has the wrong size. Is not 128 bit.");
141
142
/**
143
 * @}
144
 */
145
146
#include "primes.h"
147
148
/**
149
 * @defgroup Typesystem Type System
150
 * @addtogroup Typesystem
151
 * @{
152
 * @brief libssts's very primative type system.
153
 */
154
155
/**
156
 * The maximum amount of types.
157
 * 
158
 * @note Subject to change.
159
 *
160
 */
161
#define S_TYPE_MAX SPrimeListGood[5] // 499
162
163
164
typedef struct STypeSystem STypeSystem;
165
166
167
/**
168
 * The type that represents the type id.
169
 * @see STypeEnum
170
 * @see STypeName
171
 * @see s_type_get_name
172
 */
173
typedef slong SType;
174
175
/**
176
 * Predefined types of objects.
177
 *
178
 * @see STypeName
179
 * @see SType
180
 * @see s_type_get_name
181
 */
182
typedef enum STypeEnum {
183
  S_TYPE_NONE = 0, /**< Not a type. */
184
  S_TYPE_INT, /**< @ref sint */
185
  S_TYPE_LONG, /**< @ref slong */
186
  S_TYPE_SHORT, /**< @ref sshort */
187
  S_TYPE_CHAR, /**< @ref schar */
188
  S_TYPE_WCHAR, /**< @deprecated wide char (wchar_t) */
189
  S_TYPE_UCHAR, /**< @ref suchar */
190
  S_TYPE_UINT, /**< @ref suint */
191
  S_TYPE_ULONG, /**< @ref sulong */
192
  S_TYPE_USHORT, /**< @ref sushort */
193
  S_TYPE_BOOLEAN, /**< @ref sboolean */
194
  S_TYPE_STRING, /**< a string of @ref schar s */
151.1.2 by Gustav Hartvigsson
* Removed wchar.
195
#if 0
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
196
  S_TYPE_WSTRING, /**< @deprecated  Platform specific wchar_t string */
151.1.2 by Gustav Hartvigsson
* Removed wchar.
197
#endif
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
198
  S_TYPE_USTRING, /**< a string of @ref suchar s */
199
  S_TYPE_UNUSED_0, /**< */
200
  S_TYPE_UNUSED_1, /**< */
201
  S_TYPE_UNUSED_2, /**< */
202
  S_TYPE_POINTER, /**< @ref spointer */
203
  S_TYPE_OBJECT, /**< @ref SObject */
204
  S_TYPE_INVALID, /** Invalid type. */
205
  S_TYPE_UNUSED_3, /**< */
206
  S_TYPE_UNUSED_4, /**< */
207
  S_TYPE_LAST_PREDEFINED, /**< One-over-end item in the predefined
208
                              * list of types */
209
} STypeEnum;
210
211
/** @brief
212
 * The names of STypeEnum's items.
213
 *
214
 * @see STypeEnum
215
 * @see SType
216
 * @see s_type_get_name
217
 */
218
S_UNUSED
219
static char * STypeName[] = {
220
  "NONE",
221
  "INT",
222
  "LONG",
223
  "SHORT",
224
  "CHAR",
225
  "WCHAR",
226
  "UCHAR", /*< char32_t */
227
  "UINT",
228
  "ULONG",
229
  "USHORT"
230
  "BOOLEAN",
231
  "STRING",
232
  "WSTRING",
233
  "USTRING",
234
  0x0,
235
  0x0,
236
  0x0,
237
  "POINTER",
238
  "OBJECT",
239
  "INVALID",
240
  0x0,
241
  0x0,
242
};
243
244
245
/**
246
 * Provides type information, includeing what parent an object has. Note that
247
 * this iformation should be constant and accessable.
248
 */
249
typedef struct STypeInfo {
250
  SType id; /**< The id of the type. This is set by the system on registration.
251
             */
252
  SType parent; /**< The parent type's type id. Can be @ref S_TYPE_NONE (0) if
253
                 * you are not deriving from any type. */
254
  schar * name; /**< The name string. This is used when identifing types in
255
                 * debugging but can and will be used in other places. */
256
} STypeInfo;
257
258
/**
259
 * Free allocaded STypeInfo.
260
 * 
261
 * @param ti The type information to be freed.
262
 */
263
S_EXPORTED
264
void
265
s_type_info_free (STypeInfo * ti);
266
267
/**
268
 * 
269
 */
270
S_EXPORTED
271
const STypeInfo *
272
s_type_register_return_type_info (schar * name, SType parent);
273
274
/**
275
 *
276
 */
277
S_EXPORTED
278
SType
279
s_type_register (schar * name, SType parent);
280
281
/**
282
 * @brief Get the name of the SType.
283
 *
284
 * @return the name of the type
285
 * @return NULL on invalid id's.
286
 *
287
 * @note must be freed my caller.
288
 */
289
S_EXPORTED
290
schar *
291
s_type_get_name (SType k);
292
293
/**
294
 * get type id from name.
295
 *
296
 * @return the SType of the name.
297
 * @return #S_TYPE_NONE on unregisterd name.
298
 */
299
S_EXPORTED
300
SType
301
s_type_get_type (schar * name);
302
303
/**
304
 * Get the parent type of a type.
305
 */
306
S_EXPORTED
307
SType
308
s_type_get_parent (SType type);
309
310
/**
311
 * Get a NULL terminated array of all parents of a type.
312
 *
313
 * @param type The type to check.
314
 * @param out_size Where the size of the returned array is stored.
315
 */
316
S_EXPORTED
317
SType *
318
s_type_get_array_of_parents (SType type, size_t * out_size);
319
320
/**
321
 * Checks if a type is derived from an other type or not.
322
 *
323
 * @param child_type The type you want to check.
152 by Gustav Hartvigsson
* Made SRandom compile
324
 * @param parent_type The type you want to check if child_type is a child to
325
 * parent_type.
150 by Gustav Hartvigsson
* Fixed the tests in the CMake file
326
 */
327
S_EXPORTED
328
sboolean
329
s_type_is_type (SType child_type, SType parent_type);
330
331
/**
332
 * @brief
333
 * Teardown the typesystem.
334
 *
335
 * This is the last thing that should be run in any
336
 * applications. Calling this before may cause undefined behaviour.
337
 *
338
 * @warning Do not call this directly. Use @ref s_teardown() instead.
339
 */
340
S_HIDDEN
341
void
342
s_type_system_teardown ();
343
344
S_END_DECLS
345
346
/**
347
 * @}
348
 * @}
349
 */