/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to libssts/types.h

  • Committer: Gustav Hartvigsson
  • Date: 2017-01-24 20:55:19 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20170124205519-gtr18o3dwbunrrnx
* Fixed the tests in the CMake file
* Made doxygen output static declarations.
* Started work on SApplication
* Played with BaseN.c
  * Now it is a lil' bit better
* Spilt defs.h
  * Added types.h
    * Started work on the full typesystem.
      (Still needs testing)
  * Added primes.[c,h]
    * Contains some static array with primes.
      ("Good" primes, and all primes up to 5 000.
    * And helper functions related to Primes (Needs Tests).
* fixed s_dynamic_array_dump_array.
  (The old version did not make much sense)
* removed some functions from DymanicArray.c
* fixed compiler warnings in Mainloop.c
* removed s_map_(de)serialize_json functions.
* Made s_thread_status_get_name be less prone to error
  (This due to the C11 standard not specifing what these
   values should be)
* fixed s_thread_run
* fixed s_threa_stop

  TODO:
* Write tests for the s_prime_* functions
* Write tests for the s_type_* functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
49
typedef int32_t sboolean;
 
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 */
 
195
  S_TYPE_WSTRING, /**< @deprecated  Platform specific wchar_t string */
 
196
  S_TYPE_USTRING, /**< a string of @ref suchar s */
 
197
  S_TYPE_UNUSED_0, /**< */
 
198
  S_TYPE_UNUSED_1, /**< */
 
199
  S_TYPE_UNUSED_2, /**< */
 
200
  S_TYPE_POINTER, /**< @ref spointer */
 
201
  S_TYPE_OBJECT, /**< @ref SObject */
 
202
  S_TYPE_INVALID, /** Invalid type. */
 
203
  S_TYPE_UNUSED_3, /**< */
 
204
  S_TYPE_UNUSED_4, /**< */
 
205
  S_TYPE_LAST_PREDEFINED, /**< One-over-end item in the predefined
 
206
                              * list of types */
 
207
} STypeEnum;
 
208
 
 
209
/** @brief
 
210
 * The names of STypeEnum's items.
 
211
 *
 
212
 * @see STypeEnum
 
213
 * @see SType
 
214
 * @see s_type_get_name
 
215
 */
 
216
S_UNUSED
 
217
static char * STypeName[] = {
 
218
  "NONE",
 
219
  "INT",
 
220
  "LONG",
 
221
  "SHORT",
 
222
  "CHAR",
 
223
  "WCHAR",
 
224
  "UCHAR", /*< char32_t */
 
225
  "UINT",
 
226
  "ULONG",
 
227
  "USHORT"
 
228
  "BOOLEAN",
 
229
  "STRING",
 
230
  "WSTRING",
 
231
  "USTRING",
 
232
  0x0,
 
233
  0x0,
 
234
  0x0,
 
235
  "POINTER",
 
236
  "OBJECT",
 
237
  "INVALID",
 
238
  0x0,
 
239
  0x0,
 
240
};
 
241
 
 
242
 
 
243
/**
 
244
 * Provides type information, includeing what parent an object has. Note that
 
245
 * this iformation should be constant and accessable.
 
246
 */
 
247
typedef struct STypeInfo {
 
248
  SType id; /**< The id of the type. This is set by the system on registration.
 
249
             */
 
250
  SType parent; /**< The parent type's type id. Can be @ref S_TYPE_NONE (0) if
 
251
                 * you are not deriving from any type. */
 
252
  schar * name; /**< The name string. This is used when identifing types in
 
253
                 * debugging but can and will be used in other places. */
 
254
} STypeInfo;
 
255
 
 
256
/**
 
257
 * Free allocaded STypeInfo.
 
258
 * 
 
259
 * @param ti The type information to be freed.
 
260
 */
 
261
S_EXPORTED
 
262
void
 
263
s_type_info_free (STypeInfo * ti);
 
264
 
 
265
/**
 
266
 * 
 
267
 */
 
268
S_EXPORTED
 
269
const STypeInfo *
 
270
s_type_register_return_type_info (schar * name, SType parent);
 
271
 
 
272
/**
 
273
 *
 
274
 */
 
275
S_EXPORTED
 
276
SType
 
277
s_type_register (schar * name, SType parent);
 
278
 
 
279
/**
 
280
 * @brief Get the name of the SType.
 
281
 *
 
282
 * @return the name of the type
 
283
 * @return NULL on invalid id's.
 
284
 *
 
285
 * @note must be freed my caller.
 
286
 */
 
287
S_EXPORTED
 
288
schar *
 
289
s_type_get_name (SType k);
 
290
 
 
291
/**
 
292
 * get type id from name.
 
293
 *
 
294
 * @return the SType of the name.
 
295
 * @return #S_TYPE_NONE on unregisterd name.
 
296
 */
 
297
S_EXPORTED
 
298
SType
 
299
s_type_get_type (schar * name);
 
300
 
 
301
/**
 
302
 * Get the parent type of a type.
 
303
 */
 
304
S_EXPORTED
 
305
SType
 
306
s_type_get_parent (SType type);
 
307
 
 
308
/**
 
309
 * Get a NULL terminated array of all parents of a type.
 
310
 *
 
311
 * @param type The type to check.
 
312
 * @param out_size Where the size of the returned array is stored.
 
313
 */
 
314
S_EXPORTED
 
315
SType *
 
316
s_type_get_array_of_parents (SType type, size_t * out_size);
 
317
 
 
318
/**
 
319
 * Checks if a type is derived from an other type or not.
 
320
 *
 
321
 * @param child_type The type you want to check.
 
322
 * @param parent_type The type you want to check if child_type is a child to.
 
323
 */
 
324
S_EXPORTED
 
325
sboolean
 
326
s_type_is_type (SType child_type, SType parent_type);
 
327
 
 
328
/**
 
329
 * @brief
 
330
 * Teardown the typesystem.
 
331
 *
 
332
 * This is the last thing that should be run in any
 
333
 * applications. Calling this before may cause undefined behaviour.
 
334
 *
 
335
 * @warning Do not call this directly. Use @ref s_teardown() instead.
 
336
 */
 
337
S_HIDDEN
 
338
void
 
339
s_type_system_teardown ();
 
340
 
 
341
S_END_DECLS
 
342
 
 
343
/**
 
344
 * @}
 
345
 * @}
 
346
 */