00001 #ifndef TEMU_OBJSYS_C_H
00002 #define TEMU_OBJSYS_C_H
00003
00004 #include <assert.h>
00005 #include <stdint.h>
00006 #include <stdlib.h>
00007 #include <stddef.h>
00008
00009 #ifdef __cplusplus
00010 extern "C" {
00011 #endif
00012
00013 #ifdef __cplusplus
00014 #define TEMU_PLUGIN_INIT \
00015 extern "C" void temu_pluginInit(void)
00016 #else
00017 #define TEMU_PLUGIN_INIT \
00018 void temu_pluginInit(void)
00019 #endif
00020
00021 typedef void temu_Class;
00022 typedef void temu_MetaIface;
00023
00024
00025 typedef struct temu_Iface {
00026 void *Obj;
00027 void *Iface;
00028 } temu_Iface;
00029
00030
00031
00032
00033
00034 typedef struct temu_IfaceArray {
00035 uint32_t Size;
00036 uint32_t Reserved;
00037 temu_Iface *Ifaces;
00038 } temu_IfaceArray;
00039
00046 temu_IfaceArray temu_ifaceArrayAlloc(unsigned Reserve);
00047
00054 void temu_ifaceArrayPush2(temu_IfaceArray *Arr, void *Obj, void *Iface);
00060 void temu_ifaceArrayPush(temu_IfaceArray *Arr, temu_Iface Iface);
00061
00067 unsigned temu_ifaceArraySize(temu_IfaceArray *Arr);
00068
00069 void temu_ifaceArrayDispose(temu_IfaceArray *Arr);
00070
00071
00072
00073
00074 #define OBJSYS_OBJ_TYPE(N) \
00075 typedef struct { \
00076 void *Obj; \
00077 N ## Iface *Iface; \
00078 } N ## Obj; \
00079 typedef struct { \
00080 uint32_t Size; \
00081 uint32_t Reserved; \
00082 N ## Obj *Ifaces; \
00083 } N ## ObjArray; \
00084 static inline N ## ObjArray \
00085 N ## ObjArrayAlloc(unsigned Reserve) \
00086 { \
00087 temu_IfaceArray Arr = temu_ifaceArrayAlloc(Reserve); \
00088 N ## ObjArray Res; \
00089 Res.Size = Arr.Size; \
00090 Res.Reserved = Arr.Reserved; \
00091 Res.Ifaces = (N##Obj*)Arr.Ifaces; \
00092 return Res; \
00093 } \
00094 static inline void \
00095 N ## ObjArrayDispose(N ## ObjArray *Arr) \
00096 { \
00097 Arr->Size = 0; \
00098 Arr->Reserved = 0; \
00099 temu_ifaceArrayDispose((temu_IfaceArray*)&Arr->Ifaces); \
00100 } \
00101 static inline void \
00102 N ## ObjArrayPush2(N ## ObjArray *Arr, void *Obj, void *Iface) \
00103 { \
00104 temu_ifaceArrayPush2((temu_IfaceArray*)Arr, Obj, Iface); \
00105 } \
00106 static inline void \
00107 N ## ObjArrayPush(N ## ObjArray *Arr, N ## Obj Iface) \
00108 { \
00109 temu_Iface Iface2; \
00110 Iface2.Obj = Iface.Obj; \
00111 Iface2.Iface = (void*)Iface.Iface; \
00112 \
00113 temu_ifaceArrayPush((temu_IfaceArray*)Arr, Iface2); \
00114 } \
00115 static inline unsigned \
00116 N ## ObjArraySize(temu_IfaceArray *Arr) \
00117 { \
00118 return Arr->Size; \
00119 }
00120
00121
00122 #define DYN_ARRAY_TYPE(T, P) \
00123 typedef struct { \
00124 uint32_t Size; \
00125 uint32_t Reserved; \
00126 T *Values; \
00127 } temu_ ## P ## Array; \
00128 static inline temu_ ## P ## Array \
00129 temu_ ## P ## ArrayAlloc(unsigned Reserve) \
00130 { \
00131 temu_ ## P ## Array Arr; \
00132 Arr.Size = 0; \
00133 Arr.Reserved = Reserve; \
00134 Arr.Values = (T*)calloc(Reserve, sizeof(T)); \
00135 assert(Arr.Values); \
00136 return Arr; \
00137 } \
00138 static inline void \
00139 temu_ ## P ## ArrayPush(temu_ ## P ## Array *Arr, T Val) \
00140 { \
00141 if (Arr->Reserved >= Arr->Size) { \
00142 T *NewValues = (T*)realloc(Arr->Values, Arr->Reserved * 2); \
00143 if (NewValues) { \
00144 Arr->Values = NewValues; \
00145 } else { \
00146 abort(); \
00147 } \
00148 } \
00149 Arr->Values[Arr->Size ++] = Val; \
00150 } \
00151 static inline unsigned \
00152 temu_ ## P ## ArraySize(temu_ ## P ## Array *Arr) \
00153 { \
00154 return Arr->Size; \
00155 }
00156
00157 DYN_ARRAY_TYPE(int8_t, i8)
00158 DYN_ARRAY_TYPE(int16_t, i16)
00159 DYN_ARRAY_TYPE(int32_t, i32)
00160 DYN_ARRAY_TYPE(int64_t, i64)
00161
00162 DYN_ARRAY_TYPE(uint8_t, u8)
00163 DYN_ARRAY_TYPE(uint16_t, u16)
00164 DYN_ARRAY_TYPE(uint32_t, u32)
00165 DYN_ARRAY_TYPE(uint64_t, u64)
00166
00167 typedef enum temu_Type {
00168 teTY_Invalid,
00169
00170
00171 teTY_Intptr,
00172 teTY_Uintptr,
00173
00174
00175 teTY_Float,
00176 teTY_Double,
00177
00178
00179 teTY_U8,
00180 teTY_U16,
00181 teTY_U32,
00182 teTY_U64,
00183 teTY_I8,
00184 teTY_I16,
00185 teTY_I32,
00186 teTY_I64,
00187
00188
00189 teTY_Obj,
00190
00191
00192
00193 teTY_InternalPtr,
00194
00195
00196 teTY_Iface,
00197 teTY_IfaceArray,
00198
00199 teTY_String,
00200 } temu_Type;
00201
00208 typedef struct temu_Propref {
00209 temu_Type Typ;
00210 void *Ptr;
00211 } temu_Propref;
00212
00220 typedef struct temu_Propval {
00221 temu_Type Typ;
00222 union {
00223 intptr_t IntPtr;
00224 uintptr_t UIntPtr;
00225
00226 float f;
00227 double d;
00228
00229 uint8_t u8;
00230 uint16_t u16;
00231 uint32_t u32;
00232 uint64_t u64;
00233
00234 int8_t i8;
00235 int16_t i16;
00236 int32_t i32;
00237 int64_t i64;
00238
00239 void *Obj;
00240 temu_Iface Iface;
00241 temu_IfaceArray IfaceArray;
00242 const char *String;
00243 };
00244 } temu_Propval;
00245
00246
00247 #ifdef PROP_ASSERTS_ENABLED
00248 #define PROP_ASSERT(p, t) assert(p.Typ == t && "invalid property type")
00249 #else
00250 #define PROP_ASSERT(p, t)
00251 #endif
00252
00253
00254
00255
00256
00257 #define PROP_VAL_INITIALIZER(typ, suffix, typetag, valtag) \
00258 static inline temu_Propval \
00259 temu_makeProp ## suffix (typ val) \
00260 { \
00261 temu_Propval pv; \
00262 pv.Typ = typetag; \
00263 pv.valtag = val; \
00264 return pv; \
00265 } \
00266 static inline typ \
00267 temu_propValue ## suffix (temu_Propval pv) \
00268 { \
00269 PROP_ASSERT(pv.Typ, typetag); \
00270 typ val = pv.valtag; \
00271 return val; \
00272 }
00273
00274 PROP_VAL_INITIALIZER(intptr_t, IntPtr, teTY_Intptr, IntPtr)
00275 PROP_VAL_INITIALIZER(uintptr_t, UIntPtr, teTY_Uintptr, UIntPtr)
00276
00277 PROP_VAL_INITIALIZER(float, Float, teTY_Float, f)
00278 PROP_VAL_INITIALIZER(double, Double, teTY_Double, d)
00279
00280 PROP_VAL_INITIALIZER(uint8_t, U8, teTY_U8, u8)
00281 PROP_VAL_INITIALIZER(uint16_t, U16, teTY_U16, u16)
00282 PROP_VAL_INITIALIZER(uint32_t, U32, teTY_U32, u32)
00283 PROP_VAL_INITIALIZER(uint64_t, U64, teTY_U64, u64)
00284
00285 PROP_VAL_INITIALIZER(int8_t, I8, teTY_I8, i8)
00286 PROP_VAL_INITIALIZER(int16_t, I16, teTY_I16, i16)
00287 PROP_VAL_INITIALIZER(int32_t, I32, teTY_I32, i32)
00288 PROP_VAL_INITIALIZER(int64_t, I64, teTY_I64, i64)
00289
00290 PROP_VAL_INITIALIZER(void*, Obj, teTY_Obj, Obj)
00291 PROP_VAL_INITIALIZER(temu_Iface, Iface, teTY_Iface, Iface)
00292
00299 typedef void (*temu_PropWriter)(void *Obj, temu_Propval Pv, int Idx);
00300
00307 typedef temu_Propval (*temu_PropReader)(void *Obj, int Idx);
00308
00309
00315 temu_Propref temu_getPropref(const void *Obj, const char *PropName);
00316 int temu_getPropLength(const void *Obj, const char *PropName);
00317
00318
00319
00320
00321
00322
00323 temu_Propval
00324 temu_getValue(void *Obj, const char *PropName, int Idx);
00325
00326
00327 uint8_t temu_getValueU8(void *Obj, const char *PropName, int Idx);
00328 uint16_t temu_getValueU16(void *Obj, const char *PropName, int Idx);
00329 uint32_t temu_getValueU32(void *Obj, const char *PropName, int Idx);
00330 uint64_t temu_getValueU64(void *Obj, const char *PropName, int Idx);
00331
00332 int8_t temu_getValueI8(void *Obj, const char *PropName, int Idx);
00333 int16_t temu_getValueI16(void *Obj, const char *PropName, int Idx);
00334 int32_t temu_getValueI32(void *Obj, const char *PropName, int Idx);
00335 int64_t temu_getValueI64(void *Obj, const char *PropName, int Idx);
00336
00347 temu_Propval
00348 temu_readValue(void *Obj, const char *PropName, int Idx);
00349
00350 uint8_t temu_readValueU8(void *Obj, const char *PropName, int Idx);
00351 uint16_t temu_readValueU16(void *Obj, const char *PropName, int Idx);
00352 uint32_t temu_readValueU32(void *Obj, const char *PropName, int Idx);
00353 uint64_t temu_readValueU64(void *Obj, const char *PropName, int Idx);
00354
00355 int8_t temu_readValueI8(void *Obj, const char *PropName, int Idx);
00356 int16_t temu_readValueI16(void *Obj, const char *PropName, int Idx);
00357 int32_t temu_readValueI32(void *Obj, const char *PropName, int Idx);
00358 int64_t temu_readValueI64(void *Obj, const char *PropName, int Idx);
00359
00368 void temu_setValue(void *Obj, const char *PropName, temu_Propval Val, int Idx);
00369
00370 void temu_setValueU8(void *Obj, const char *PropName, uint8_t Val, int Idx);
00371 void temu_setValueU16(void *Obj, const char *PropName, uint16_t Val, int Idx);
00372 void temu_setValueU32(void *Obj, const char *PropName, uint32_t Val, int Idx);
00373 void temu_setValueU64(void *Obj, const char *PropName, uint64_t Val, int Idx);
00374
00375 void temu_setValueI8(void *Obj, const char *PropName, int8_t Val, int Idx);
00376 void temu_setValueI16(void *Obj, const char *PropName, int16_t Val, int Idx);
00377 void temu_setValueI32(void *Obj, const char *PropName, int32_t Val, int Idx);
00378 void temu_setValueI64(void *Obj, const char *PropName, int64_t Val, int Idx);
00379
00388 void temu_writeValue(void *Obj, const char *PropName, temu_Propval Val, int Idx);
00389
00390 void temu_writeValueU8(void *Obj, const char *PropName, uint8_t Val, int Idx);
00391 void temu_writeValueU16(void *Obj, const char *PropName, uint16_t Val, int Idx);
00392 void temu_writeValueU32(void *Obj, const char *PropName, uint32_t Val, int Idx);
00393 void temu_writeValueU64(void *Obj, const char *PropName, uint64_t Val, int Idx);
00394
00395 void temu_writeValueI8(void *Obj, const char *PropName, int8_t Val, int Idx);
00396 void temu_writeValueI16(void *Obj, const char *PropName, int16_t Val, int Idx);
00397 void temu_writeValueI32(void *Obj, const char *PropName, int32_t Val, int Idx);
00398 void temu_writeValueI64(void *Obj, const char *PropName, int64_t Val, int Idx);
00399
00400
00401
00402 temu_Propval temu_getNamedObjectProp(const char *Obj, const char *PropName,
00403 int Idx);
00404
00405 temu_Propval temu_readNamedObjectProp(const char *Obj, const char *PropName,
00406 int Idx);
00407
00408 void
00409 temu_setNamedObjectProp(const char *Obj, const char *PropName,
00410 temu_Propval Val, int Idx);
00411
00412 void
00413 temu_writeNamedObjectProp(const char *Obj, const char *PropName,
00414 temu_Propval Val, int Idx);
00415
00416
00417
00418 typedef struct temu_CreateArg {
00419 const char *Key;
00420 temu_Propval Val;
00421 } temu_CreateArg;
00422
00423 typedef void* (*temu_ObjectCreateFunc)(const char *Name,
00424 int Argc, const temu_CreateArg *Argv);
00425 typedef void (*temu_ObjectDisposeFunc)(void*);
00426
00439 temu_Class*
00440 temu_registerClass(const char *ClsName,
00441 temu_ObjectCreateFunc Create,
00442 temu_ObjectDisposeFunc Dispose);
00443
00444
00454 temu_Class* temu_registerExternalClass(const char *ClsName);
00455
00456 #ifdef __cplusplus
00457
00475 void
00476 temu_addProperty(temu_Class *Cls, const char *PropName,
00477 int Offset,
00478 temu_Type Typ, int Count,
00479 temu_PropWriter Wr = nullptr,
00480 temu_PropReader Rd = nullptr,
00481 const char *Doc = "");
00482 #else
00483 void
00484 temu_addProperty(temu_Class *Cls, const char *PropName,
00485 int Offset,
00486 temu_Type Typ, int Count,
00487 temu_PropWriter Wr,
00488 temu_PropReader Rd,
00489 const char *Doc);
00490
00491 #endif
00492
00503 #ifdef __cplusplus
00504
00505
00506 void
00507 temu_addInterface(temu_Class *Cls,
00508 const char *IfaceName,
00509 const char *IfaceType,
00510 void *Iface, int Count = 0,
00511 const char *Doc = "");
00512
00513 void*
00514 temu_getInterface(void *Obj,
00515 const char *IfaceName, int Idx = 0);
00516
00517 #else
00518 void
00519 temu_addInterface(temu_Class *Cls,
00520
00521 const char *IfaceName,
00522 const char *IfaceType,
00523 void *Iface, int Count,
00524 const char *Doc);
00525
00526 void*
00527 temu_getInterface(void *Obj,
00528 const char *IfaceName, int Idx);
00529
00530 #endif
00531
00537 void temu_objsysClear(void);
00538
00541 void temu_objsysClearObjects(void);
00542
00550 void* temu_addObject(const char *ClsName, const char *ObjName, void *Obj);
00551
00552
00560 void* temu_createObject(const char *ClsName, const char *ObjName);
00561
00562
00568 void temu_disposeObject(void *Obj);
00569
00573 temu_Class* temu_classForName(const char *ClsName);
00574
00578 temu_Class* temu_classForObject(void *Obj);
00579
00583 temu_Class*
00584 temu_classForObjectName(const char *Obj);
00585
00586
00590 void* temu_objectForName(const char *Name);
00591
00595 const char* temu_nameForObject(const void *Obj);
00596
00610 int temu_loadPlugin(const char *Path);
00611
00615 const char* temu_typeToName(temu_Type Typ);
00616
00617
00631 int temu_connect(void *A, const char *PropName, void *B, const char *IfaceName);
00632
00644 int temu_serialiseJSON(const char *FileName);
00645
00646
00656 int temu_deserialiseJSON(const char *FileName);
00657
00658 void temu_serialiseProp(void *Ctxt, const char *Name, temu_Type Typ,
00659 int Count, void *Data);
00660
00661 void temu_deserialiseProp(void *Ctxt, void *Obj, const char *Name);
00662
00663 int temu_checkpointGetLength(void *Ctxt, const char *Name);
00664 temu_Propval temu_checkpointGetValue(void *Ctxt, const char *Name, int Idx);
00665
00677 int temu_checkSanity(int Report);
00678
00683 typedef struct {
00684 void (*serialise)(void *Obj, const char *BaseName, void *Ctxt);
00685 void (*deserialise)(void *Obj, const char *BaseName, void *Ctxt);
00686
00693 int (*checkSanity)(void *Obj, int Report);
00694 } temu_ObjectIface;
00695
00696 OBJSYS_OBJ_TYPE(temu_Object);
00697
00698 #ifdef __cplusplus
00699 }
00700 #endif
00701
00702
00703 #endif