T-EMU  2
The Terma Emulator
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
Objsys.h
Go to the documentation of this file.
1 #ifndef TEMU_OBJSYS_C_H
2 #define TEMU_OBJSYS_C_H
3 
4 #include <assert.h>
5 #include <stdint.h>
6 #include <stdlib.h>
7 #include <stddef.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #ifdef __cplusplus
14 #define TEMU_PLUGIN_INIT \
15  extern "C" void temu_pluginInit(void)
16 #else
17 #define TEMU_PLUGIN_INIT \
18  void temu_pluginInit(void)
19 #endif
20 
21 typedef void temu_Class;
22 typedef void temu_MetaIface;
23 
24 // Generic interface
25 typedef struct temu_Iface {
26  void *Obj;
27  void *Iface;
28 } temu_Iface;
29 
30 // In some cases, the number of objects we know about is unknown, so
31 // we need a dynamic array (e.g. std::vector in C++), but we cannot
32 // expose std::vector in a known format to LLVM, so we do this simple
33 // system with a dynamic interface array.
34 typedef struct temu_IfaceArray {
35  uint32_t Size;
36  uint32_t Reserved;
39 
46 temu_IfaceArray temu_ifaceArrayAlloc(unsigned Reserve);
47 
54 void temu_ifaceArrayPush2(temu_IfaceArray *Arr, void *Obj, void *Iface);
61 
67 unsigned temu_ifaceArraySize(temu_IfaceArray *Arr); // Return size
68 
70 
71 
72 // Used to declare typed interfaces, also declares an array of such
73 // and inline wrappers for the iface array functions
74 #define OBJSYS_OBJ_TYPE(N) \
75  typedef struct { \
76  void *Obj; \
77  N ## Iface *Iface; \
78  } N ## Obj; \
79  typedef struct { \
80  uint32_t Size; \
81  uint32_t Reserved; \
82  N ## Obj *Ifaces; \
83  } N ## ObjArray; \
84  static inline N ## ObjArray \
85  N ## ObjArrayAlloc(unsigned Reserve) \
86  { \
87  temu_IfaceArray Arr = temu_ifaceArrayAlloc(Reserve); \
88  N ## ObjArray Res; \
89  Res.Size = Arr.Size; \
90  Res.Reserved = Arr.Reserved; \
91  Res.Ifaces = (N##Obj*)Arr.Ifaces; \
92  return Res; \
93  } \
94  static inline void \
95  N ## ObjArrayDispose(N ## ObjArray *Arr) \
96  { \
97  Arr->Size = 0; \
98  Arr->Reserved = 0; \
99  temu_ifaceArrayDispose((temu_IfaceArray*)&Arr->Ifaces); \
100  } \
101  static inline void \
102  N ## ObjArrayPush2(N ## ObjArray *Arr, void *Obj, void *Iface) \
103  { \
104  temu_ifaceArrayPush2((temu_IfaceArray*)Arr, Obj, Iface); \
105  } \
106  static inline void \
107  N ## ObjArrayPush(N ## ObjArray *Arr, N ## Obj Iface) \
108  { \
109  temu_Iface Iface2; \
110  Iface2.Obj = Iface.Obj; \
111  Iface2.Iface = (void*)Iface.Iface; \
112  \
113  temu_ifaceArrayPush((temu_IfaceArray*)Arr, Iface2); \
114  } \
115  static inline unsigned \
116  N ## ObjArraySize(temu_IfaceArray *Arr) \
117  { \
118  return Arr->Size; \
119  }
120 
121 
122 #define DYN_ARRAY_TYPE(T, P) \
123  typedef struct { \
124  uint32_t Size; \
125  uint32_t Reserved; \
126  T *Values; \
127  } temu_ ## P ## Array; \
128  static inline temu_ ## P ## Array \
129  temu_ ## P ## ArrayAlloc(unsigned Reserve) \
130  { \
131  temu_ ## P ## Array Arr; \
132  Arr.Size = 0; \
133  Arr.Reserved = Reserve; \
134  Arr.Values = (T*)calloc(Reserve, sizeof(T)); \
135  assert(Arr.Values); \
136  return Arr; \
137  } \
138  static inline void \
139  temu_ ## P ## ArrayPush(temu_ ## P ## Array *Arr, T Val) \
140  { \
141  if (Arr->Reserved >= Arr->Size) { \
142  T *NewValues = (T*)realloc(Arr->Values, Arr->Reserved * 2); \
143  if (NewValues) { \
144  Arr->Values = NewValues; \
145  } else { \
146  abort(); \
147  } \
148  } \
149  Arr->Values[Arr->Size ++] = Val; \
150  } \
151  static inline unsigned \
152  temu_ ## P ## ArraySize(temu_ ## P ## Array *Arr) \
153  { \
154  return Arr->Size; \
155  }
156 
157 DYN_ARRAY_TYPE(int8_t, i8)
158 DYN_ARRAY_TYPE(int16_t, i16)
159 DYN_ARRAY_TYPE(int32_t, i32)
160 DYN_ARRAY_TYPE(int64_t, i64)
161 
162 DYN_ARRAY_TYPE(uint8_t, u8)
163 DYN_ARRAY_TYPE(uint16_t, u16)
164 DYN_ARRAY_TYPE(uint32_t, u32)
165 DYN_ARRAY_TYPE(uint64_t, u64)
166 
167 typedef enum temu_Type {
168  teTY_Invalid, // Invalid value
169 
170  // C pointer sized integers
173 
174  // Standard C floating point types
177 
178  // Standard C fixed width integer types
187 
188  // Object pointer, must be saved as an object reference
190 
191  // Internal pointer, points somewhere in the object itself
192  // (e.g. bank resolution arrays) This can be saved as an offset...
194 
195  // Interface (object and interface pointer pair)
197  teTY_IfaceArray, // Dynamic object/interface array
198 
199  teTY_String, // C-string, useful for serialisation
200 } temu_Type;
201 
208 typedef struct temu_Propref {
210  void *Ptr;
211 } temu_Propref;
212 
220 typedef struct temu_Propval {
222  union {
223  intptr_t IntPtr;
224  uintptr_t UIntPtr;
225 
226  float f;
227  double d;
228 
229  uint8_t u8;
230  uint16_t u16;
231  uint32_t u32;
232  uint64_t u64;
233 
234  int8_t i8;
235  int16_t i16;
236  int32_t i32;
237  int64_t i64;
238 
239  void *Obj;
242  const char *String;
243  };
244 } temu_Propval;
245 
246 
247 #ifdef PROP_ASSERTS_ENABLED
248 #define PROP_ASSERT(p, t) assert(p.Typ == t && "invalid property type")
249 #else
250 #define PROP_ASSERT(p, t)
251 #endif
252 
253 // Ugly, but we want to be compatible with C++ and C99, meaning:
254 // cannot use designated initializers (C99, not C++)
255 // cannot use constructors (C++, not C99)
256 
257 #define PROP_VAL_INITIALIZER(typ, suffix, typetag, valtag) \
258  static inline temu_Propval \
259  temu_makeProp ## suffix (typ val) \
260  { \
261  temu_Propval pv; \
262  pv.Typ = typetag; \
263  pv.valtag = val; \
264  return pv; \
265  } \
266  static inline typ \
267  temu_propValue ## suffix (temu_Propval pv) \
268  { \
269  PROP_ASSERT(pv.Typ, typetag); \
270  typ val = pv.valtag; \
271  return val; \
272  }
273 
274 PROP_VAL_INITIALIZER(intptr_t, IntPtr, teTY_Intptr, IntPtr)
275 PROP_VAL_INITIALIZER(uintptr_t, UIntPtr, teTY_Uintptr, UIntPtr)
276 
277 PROP_VAL_INITIALIZER(float, Float, teTY_Float, f)
278 PROP_VAL_INITIALIZER(double, Double, teTY_Double, d)
279 
280 PROP_VAL_INITIALIZER(uint8_t, U8, teTY_U8, u8)
281 PROP_VAL_INITIALIZER(uint16_t, U16, teTY_U16, u16)
282 PROP_VAL_INITIALIZER(uint32_t, U32, teTY_U32, u32)
283 PROP_VAL_INITIALIZER(uint64_t, U64, teTY_U64, u64)
284 
285 PROP_VAL_INITIALIZER(int8_t, I8, teTY_I8, i8)
286 PROP_VAL_INITIALIZER(int16_t, I16, teTY_I16, i16)
287 PROP_VAL_INITIALIZER(int32_t, I32, teTY_I32, i32)
288 PROP_VAL_INITIALIZER(int64_t, I64, teTY_I64, i64)
289 
290 PROP_VAL_INITIALIZER(void*, Obj, teTY_Obj, Obj)
292 
299 typedef void (*temu_PropWriter)(void *Obj, temu_Propval Pv, int Idx);
300 
307 typedef temu_Propval (*temu_PropReader)(void *Obj, int Idx);
308 
309 
315 temu_Propref temu_getPropref(const void *Obj, const char *PropName);
316 int temu_getPropLength(const void *Obj, const char *PropName);
317 
318 /*
319  Get and read value are different
320 
321  Get value has no side effects, while read value may have side effects.
322  */
324 temu_getValue(void *Obj, const char *PropName, int Idx);
325 
326 // Python CTYPES does not like the anonymous union in propval.
327 uint8_t temu_getValueU8(void *Obj, const char *PropName, int Idx);
328 uint16_t temu_getValueU16(void *Obj, const char *PropName, int Idx);
329 uint32_t temu_getValueU32(void *Obj, const char *PropName, int Idx);
330 uint64_t temu_getValueU64(void *Obj, const char *PropName, int Idx);
331 
332 int8_t temu_getValueI8(void *Obj, const char *PropName, int Idx);
333 int16_t temu_getValueI16(void *Obj, const char *PropName, int Idx);
334 int32_t temu_getValueI32(void *Obj, const char *PropName, int Idx);
335 int64_t temu_getValueI64(void *Obj, const char *PropName, int Idx);
336 
348 temu_readValue(void *Obj, const char *PropName, int Idx);
349 
350 uint8_t temu_readValueU8(void *Obj, const char *PropName, int Idx);
351 uint16_t temu_readValueU16(void *Obj, const char *PropName, int Idx);
352 uint32_t temu_readValueU32(void *Obj, const char *PropName, int Idx);
353 uint64_t temu_readValueU64(void *Obj, const char *PropName, int Idx);
354 
355 int8_t temu_readValueI8(void *Obj, const char *PropName, int Idx);
356 int16_t temu_readValueI16(void *Obj, const char *PropName, int Idx);
357 int32_t temu_readValueI32(void *Obj, const char *PropName, int Idx);
358 int64_t temu_readValueI64(void *Obj, const char *PropName, int Idx);
359 
368 void temu_setValue(void *Obj, const char *PropName, temu_Propval Val, int Idx);
369 
370 void temu_setValueU8(void *Obj, const char *PropName, uint8_t Val, int Idx);
371 void temu_setValueU16(void *Obj, const char *PropName, uint16_t Val, int Idx);
372 void temu_setValueU32(void *Obj, const char *PropName, uint32_t Val, int Idx);
373 void temu_setValueU64(void *Obj, const char *PropName, uint64_t Val, int Idx);
374 
375 void temu_setValueI8(void *Obj, const char *PropName, int8_t Val, int Idx);
376 void temu_setValueI16(void *Obj, const char *PropName, int16_t Val, int Idx);
377 void temu_setValueI32(void *Obj, const char *PropName, int32_t Val, int Idx);
378 void temu_setValueI64(void *Obj, const char *PropName, int64_t Val, int Idx);
379 
388 void temu_writeValue(void *Obj, const char *PropName, temu_Propval Val, int Idx);
389 
390 void temu_writeValueU8(void *Obj, const char *PropName, uint8_t Val, int Idx);
391 void temu_writeValueU16(void *Obj, const char *PropName, uint16_t Val, int Idx);
392 void temu_writeValueU32(void *Obj, const char *PropName, uint32_t Val, int Idx);
393 void temu_writeValueU64(void *Obj, const char *PropName, uint64_t Val, int Idx);
394 
395 void temu_writeValueI8(void *Obj, const char *PropName, int8_t Val, int Idx);
396 void temu_writeValueI16(void *Obj, const char *PropName, int16_t Val, int Idx);
397 void temu_writeValueI32(void *Obj, const char *PropName, int32_t Val, int Idx);
398 void temu_writeValueI64(void *Obj, const char *PropName, int64_t Val, int Idx);
399 
400 
401 
402 temu_Propval temu_getNamedObjectProp(const char *Obj, const char *PropName,
403  int Idx);
404 
405 temu_Propval temu_readNamedObjectProp(const char *Obj, const char *PropName,
406  int Idx);
407 
408 void
409 temu_setNamedObjectProp(const char *Obj, const char *PropName,
410  temu_Propval Val, int Idx);
411 
412 void
413 temu_writeNamedObjectProp(const char *Obj, const char *PropName,
414  temu_Propval Val, int Idx);
415 
416 
417 
418 typedef struct temu_CreateArg {
419  const char *Key;
422 
423 typedef void* (*temu_ObjectCreateFunc)(const char *Name,
424  int Argc, const temu_CreateArg *Argv);
425 typedef void (*temu_ObjectDisposeFunc)(void*);
426 
439 temu_Class*
440 temu_registerClass(const char *ClsName,
441  temu_ObjectCreateFunc Create,
442  temu_ObjectDisposeFunc Dispose);
443 
444 
454 temu_Class* temu_registerExternalClass(const char *ClsName);
455 
456 #ifdef __cplusplus
457 
475 void
476 temu_addProperty(temu_Class *Cls, const char *PropName,
477  int Offset,
478  temu_Type Typ, int Count,
479  temu_PropWriter Wr = nullptr,
480  temu_PropReader Rd = nullptr,
481  const char *Doc = "");
482 #else
483 void
484 temu_addProperty(temu_Class *Cls, const char *PropName,
485  int Offset,
486  temu_Type Typ, int Count,
487  temu_PropWriter Wr,
488  temu_PropReader Rd,
489  const char *Doc);
490 
491 #endif
492 
503 #ifdef __cplusplus
504 
505 
506 void
507 temu_addInterface(temu_Class *Cls,
508  const char *IfaceName,
509  const char *IfaceType,
510  void *Iface, int Count = 0,
511  const char *Doc = "");
512 
513 void*
514 temu_getInterface(void *Obj,
515  const char *IfaceName, int Idx = 0);
516 
517 #else
518 void
519 temu_addInterface(temu_Class *Cls,
520 
521  const char *IfaceName,
522  const char *IfaceType,
523  void *Iface, int Count,
524  const char *Doc);
525 
526 void*
527 temu_getInterface(void *Obj,
528  const char *IfaceName, int Idx);
529 
530 #endif
531 
537 void temu_objsysClear(void);
538 
541 void temu_objsysClearObjects(void);
542 
550 void* temu_addObject(const char *ClsName, const char *ObjName, void *Obj);
551 
552 
560 void* temu_createObject(const char *ClsName, const char *ObjName);
561 
562 
568 void temu_disposeObject(void *Obj);
569 
573 temu_Class* temu_classForName(const char *ClsName);
574 
578 temu_Class* temu_classForObject(void *Obj);
579 
583 temu_Class*
584 temu_classForObjectName(const char *Obj);
585 
586 
590 void* temu_objectForName(const char *Name);
591 
595 const char* temu_nameForObject(const void *Obj);
596 
610 int temu_loadPlugin(const char *Path);
611 
615 const char* temu_typeToName(temu_Type Typ);
616 
617 
631 int temu_connect(void *A, const char *PropName, void *B, const char *IfaceName);
632 
644 int temu_serialiseJSON(const char *FileName);
645 
646 
656 int temu_deserialiseJSON(const char *FileName);
657 
658 void temu_serialiseProp(void *Ctxt, const char *Name, temu_Type Typ,
659  int Count, void *Data);
660 
661 void temu_deserialiseProp(void *Ctxt, void *Obj, const char *Name);
662 
663 int temu_checkpointGetLength(void *Ctxt, const char *Name);
664 temu_Propval temu_checkpointGetValue(void *Ctxt, const char *Name, int Idx);
665 
677 int temu_checkSanity(int Report);
678 
683 typedef struct {
684  void (*serialise)(void *Obj, const char *BaseName, void *Ctxt); // Optional
685  void (*deserialise)(void *Obj, const char *BaseName, void *Ctxt); // Optional
686 
693  int (*checkSanity)(void *Obj, int Report); // Optional
695 
696 OBJSYS_OBJ_TYPE(temu_Object);
697 
698 #ifdef __cplusplus
699 }
700 #endif
701 
702 
703 #endif
void * temu_objectForName(const char *Name)
uintptr_t UIntPtr
Definition: Objsys.h:224
int16_t i16
Definition: Objsys.h:235
temu_Type
Definition: Objsys.h:167
void temu_objsysClearObjects(void)
void temu_serialiseProp(void *Ctxt, const char *Name, temu_Type Typ, int Count, void *Data)
void(* temu_PropWriter)(void *Obj, temu_Propval Pv, int Idx)
Definition: Objsys.h:299
void temu_deserialiseProp(void *Ctxt, void *Obj, const char *Name)
temu_Class * temu_registerExternalClass(const char *ClsName)
int temu_getPropLength(const void *Obj, const char *PropName)
void temu_writeValueI32(void *Obj, const char *PropName, int32_t Val, int Idx)
double d
Definition: Objsys.h:227
uint64_t temu_getValueU64(void *Obj, const char *PropName, int Idx)
void temu_writeNamedObjectProp(const char *Obj, const char *PropName, temu_Propval Val, int Idx)
int8_t temu_getValueI8(void *Obj, const char *PropName, int Idx)
void temu_setValueU32(void *Obj, const char *PropName, uint32_t Val, int Idx)
void temu_setValueU8(void *Obj, const char *PropName, uint8_t Val, int Idx)
uint16_t u16
Definition: Objsys.h:230
struct temu_IfaceArray temu_IfaceArray
void temu_setValueU16(void *Obj, const char *PropName, uint16_t Val, int Idx)
temu_IfaceArray temu_ifaceArrayAlloc(unsigned Reserve)
int temu_connect(void *A, const char *PropName, void *B, const char *IfaceName)
int8_t i8
Definition: Objsys.h:234
temu_Class * temu_classForObjectName(const char *Obj)
uint16_t temu_getValueU16(void *Obj, const char *PropName, int Idx)
temu_Iface * Ifaces
Definition: Objsys.h:37
intptr_t IntPtr
Definition: Objsys.h:223
temu_Iface Iface
Definition: Objsys.h:240
#define OBJSYS_OBJ_TYPE(N)
Definition: Objsys.h:74
int32_t i32
Definition: Objsys.h:236
temu_Class * temu_classForObject(void *Obj)
void temu_writeValueU64(void *Obj, const char *PropName, uint64_t Val, int Idx)
int16_t temu_readValueI16(void *Obj, const char *PropName, int Idx)
void * Ptr
Definition: Objsys.h:210
uint8_t temu_readValueU8(void *Obj, const char *PropName, int Idx)
void temu_addProperty(temu_Class *Cls, const char *PropName, int Offset, temu_Type Typ, int Count, temu_PropWriter Wr, temu_PropReader Rd, const char *Doc)
int8_t temu_readValueI8(void *Obj, const char *PropName, int Idx)
#define DYN_ARRAY_TYPE(T, P)
Definition: Objsys.h:122
const char * Key
Definition: Objsys.h:419
temu_Propval temu_getValue(void *Obj, const char *PropName, int Idx)
uint32_t u32
Definition: Objsys.h:231
void temu_addInterface(temu_Class *Cls, const char *IfaceName, const char *IfaceType, void *Iface, int Count, const char *Doc)
void temu_setValueI8(void *Obj, const char *PropName, int8_t Val, int Idx)
uint8_t temu_getValueU8(void *Obj, const char *PropName, int Idx)
void temu_writeValueU16(void *Obj, const char *PropName, uint16_t Val, int Idx)
void temu_setNamedObjectProp(const char *Obj, const char *PropName, temu_Propval Val, int Idx)
void temu_Class
Definition: Objsys.h:21
uint8_t u8
Definition: Objsys.h:229
void temu_ifaceArrayDispose(temu_IfaceArray *Arr)
void temu_MetaIface
Definition: Objsys.h:22
temu_Propval temu_readValue(void *Obj, const char *PropName, int Idx)
int temu_checkpointGetLength(void *Ctxt, const char *Name)
void temu_ifaceArrayPush2(temu_IfaceArray *Arr, void *Obj, void *Iface)
temu_Type Typ
Definition: Objsys.h:221
void * temu_addObject(const char *ClsName, const char *ObjName, void *Obj)
temu_IfaceArray IfaceArray
Definition: Objsys.h:241
void temu_setValueI64(void *Obj, const char *PropName, int64_t Val, int Idx)
void * Iface
Definition: Objsys.h:27
void temu_writeValueI64(void *Obj, const char *PropName, int64_t Val, int Idx)
void temu_writeValueI8(void *Obj, const char *PropName, int8_t Val, int Idx)
uint32_t Reserved
Definition: Objsys.h:36
void * Obj
Definition: Objsys.h:239
temu_Propval Val
Definition: Objsys.h:420
unsigned temu_ifaceArraySize(temu_IfaceArray *Arr)
const char * temu_typeToName(temu_Type Typ)
void *(* temu_ObjectCreateFunc)(const char *Name, int Argc, const temu_CreateArg *Argv)
Definition: Objsys.h:423
int16_t temu_getValueI16(void *Obj, const char *PropName, int Idx)
struct temu_Iface temu_Iface
int32_t temu_getValueI32(void *Obj, const char *PropName, int Idx)
void temu_objsysClear(void)
void(* temu_ObjectDisposeFunc)(void *)
Definition: Objsys.h:425
struct temu_Propref temu_Propref
int temu_loadPlugin(const char *Path)
uint32_t temu_readValueU32(void *Obj, const char *PropName, int Idx)
uint64_t temu_readValueU64(void *Obj, const char *PropName, int Idx)
const char * temu_nameForObject(const void *Obj)
const char * String
Definition: Objsys.h:242
temu_Propval(* temu_PropReader)(void *Obj, int Idx)
Definition: Objsys.h:307
void temu_disposeObject(void *Obj)
temu_Propval temu_readNamedObjectProp(const char *Obj, const char *PropName, int Idx)
void temu_setValueI16(void *Obj, const char *PropName, int16_t Val, int Idx)
int temu_checkSanity(int Report)
int64_t i64
Definition: Objsys.h:237
int64_t temu_getValueI64(void *Obj, const char *PropName, int Idx)
uint32_t Size
Definition: Objsys.h:35
temu_Class * temu_classForName(const char *ClsName)
int32_t temu_readValueI32(void *Obj, const char *PropName, int Idx)
void temu_setValueI32(void *Obj, const char *PropName, int32_t Val, int Idx)
#define PROP_VAL_INITIALIZER(typ, suffix, typetag, valtag)
Definition: Objsys.h:257
void temu_writeValueU32(void *Obj, const char *PropName, uint32_t Val, int Idx)
temu_Class * temu_registerClass(const char *ClsName, temu_ObjectCreateFunc Create, temu_ObjectDisposeFunc Dispose)
temu_Propval temu_getNamedObjectProp(const char *Obj, const char *PropName, int Idx)
uint16_t temu_readValueU16(void *Obj, const char *PropName, int Idx)
temu_Propref temu_getPropref(const void *Obj, const char *PropName)
void * temu_createObject(const char *ClsName, const char *ObjName)
int temu_deserialiseJSON(const char *FileName)
uint64_t u64
Definition: Objsys.h:232
void temu_setValueU64(void *Obj, const char *PropName, uint64_t Val, int Idx)
void temu_setValue(void *Obj, const char *PropName, temu_Propval Val, int Idx)
int64_t temu_readValueI64(void *Obj, const char *PropName, int Idx)
void * Obj
Definition: Objsys.h:26
void temu_writeValue(void *Obj, const char *PropName, temu_Propval Val, int Idx)
float f
Definition: Objsys.h:226
void temu_writeValueI16(void *Obj, const char *PropName, int16_t Val, int Idx)
void * temu_getInterface(void *Obj, const char *IfaceName, int Idx)
void temu_writeValueU8(void *Obj, const char *PropName, uint8_t Val, int Idx)
void temu_ifaceArrayPush(temu_IfaceArray *Arr, temu_Iface Iface)
struct temu_CreateArg temu_CreateArg
struct temu_Propval temu_Propval
int temu_serialiseJSON(const char *FileName)
uint32_t temu_getValueU32(void *Obj, const char *PropName, int Idx)
temu_Type Typ
Definition: Objsys.h:209
temu_Propval temu_checkpointGetValue(void *Ctxt, const char *Name, int Idx)