CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
objclass.h
Go to the documentation of this file.
1#pragma once
2
5
7#include <cx/obj/objiface.h>
8#include <cx/thread/atomic.h>
9#include <cx/thread/rwlock.h>
11
28
29CX_C_BEGIN
30
32typedef struct ObjInst ObjInst;
33typedef struct StructMemberDesc StructMemberDesc;
34typedef struct SerWriter SerWriter;
35typedef struct SerReader SerReader;
36
46typedef struct ObjClassInfo {
47 size_t instsize;
51
58 bool (*init)(void* _self);
59
65 void (*destroy)(void* _self);
66
67 // ----- Statically defined in class info instance by class implementer -----
68
70 strref name;
71
75 const StructMemberDesc* sermembers;
77
78 bool _abstract;
79
80 // ----- Runtime use only, do not set manually! -----
81
83 intptr (*_cmp)(void* self, void* other, uint32 flags);
84
86 uint32 (*_hash)(void* self, uint32 flags);
87
88 sa_ObjIface _impl;
89 hashtable _tmpl;
91
96#define objClassInfoName(cname) cname##_clsinfo
97
108typedef struct ClassSet {
109 int nentries;
112
122_Ret_maybenull_ ObjClassInfo* classSetFind(_In_ const ClassSet* cs, _In_opt_ strref name);
123
132typedef struct ObjInst {
133 union {
136 };
138 atomic(uintptr) _ref;
139 atomic(ptr) _weakref;
140
141 // Class-specific data members follow in derived classes
142} ObjInst;
143
149typedef struct ObjInst_WeakRef {
150 union {
153 };
154 atomic(uintptr) _ref;
157
162#define Weak(clsname) clsname##_WeakRef
163
170#define ObjInst(inst) ((ObjInst*)(unused_noeval(&((inst)->_is_ObjInst)), (inst)))
171
178#define ObjInst_WeakRef(ref) \
179 ((ObjInst_WeakRef*)(unused_noeval(&((ref)->_is_ObjInst_WeakRef)), (ref)))
180
187#define objInstBase(inst) ObjInst(inst)
188
195#define objWeakRefBase(ref) ObjInst_WeakRef(ref)
196
203#define objClsInfo(inst) (inst->_clsinfo)
204
209void _objDestroy(_Pre_notnull_ _Post_invalid_ ObjInst* inst);
210
211_meta_inline void _objAcquire(_In_opt_ ObjInst* inst)
212{
213 if (inst)
214 atomicFetchAdd(uintptr, &inst->_ref, 1, Relaxed);
215}
216
234#define objAcquire(inst) (_objAcquire(objInstBase(inst)), (inst))
235
236_At_(*instp, _Pre_maybenull_ _Post_null_) void _objRelease(_Inout_ ObjInst** instp);
237
257#define objRelease(pinst) \
258 (unused_noeval(&((*(pinst))->_is_ObjInst)), _objRelease((ObjInst**)(pinst)))
259
260_Ret_maybenull_ ObjIface* _objClassIf(_In_ ObjClassInfo* cls, _In_ ObjIface* iftmpl);
272#define objClassIf(clsname, ifname) \
273 ((ifname*)_objClassIf(&objClassInfoName(clsname), objIfBase(&objIfTmplName(ifname))))
274
275_Ret_maybenull_ ObjIface* _objInstIf(_In_opt_ ObjInst* inst, _In_ ObjIface* iftmpl);
295#define objInstIf(inst, ifname) \
296 ((ifname*)_objInstIf(objInstBase(inst), objIfBase(&objIfTmplName(ifname))))
297
298_Ret_valid_ ObjInst_WeakRef* _objGetWeak(_In_ ObjInst* inst);
310#define objGetWeak(clsname, inst) ((Weak(clsname)*)_objGetWeak((ObjInst*)clsname(inst)))
311
312_Ret_maybenull_ ObjInst_WeakRef* _objCloneWeak(_In_opt_ ObjInst_WeakRef* ref);
322#define objCloneWeak(ref) (_objCloneWeak(objWeakRefBase(ref)))
323
324void _objDestroyWeak(_Inout_ ObjInst_WeakRef** refp);
340#define objDestroyWeak(pref) \
341 (unused_noeval(&((*(pref))->_is_ObjInst_WeakRef)), _objDestroyWeak((ObjInst_WeakRef**)(pref)))
342
343_Ret_maybenull_ ObjInst* _objAcquireFromWeak(_In_opt_ ObjInst_WeakRef* ref);
366#define objAcquireFromWeak(clsname, ref) \
367 ((clsname*)_objAcquireFromWeak((ObjInst_WeakRef*)Weak(clsname)(ref)))
368
369_Ret_maybenull_ ObjInst* _objDynCast(_In_opt_ ObjInst* inst, _In_ ObjClassInfo* cls);
389#define objDynCast(clsname, inst) \
390 ((clsname*)_objDynCast(objInstBase(inst), &objClassInfoName(clsname)))
391
392_Ret_maybenull_ ObjInst*
393_objAcquireFromWeakDyn(_In_opt_ ObjInst_WeakRef* ref, _In_ ObjClassInfo* cls);
405#define objAcquireFromWeakDyn(clsname, ref) \
406 ((clsname*)_objAcquireFromWeakDyn(objWeakRefBase(ref), &objClassInfoName(clsname)))
407
409
410CX_C_END
Atomic operations.
void _objDestroy(ObjInst *inst)
ObjClassInfo * classSetFind(const ClassSet *cs, strref name)
#define ObjInst(inst)
Definition objclass.h:170
#define ObjInst_WeakRef(ref)
Definition objclass.h:178
Hash table container with type-safe generic key-value storage.
Interface definitions and management for the object system.
Reader-writer lock synchronization primitive.
ObjClassInfo * entries[]
sorted by name for binary search
Definition objclass.h:110
bool(* init)(void *_self)
Definition objclass.h:58
bool _abstract
True if this is an abstract class (cannot be instantiated)
Definition objclass.h:78
uint32(* _hash)(void *self, uint32 flags)
Cached function pointer for Hashable.hash interface (NULL if not implemented)
Definition objclass.h:86
hashtable _tmpl
Maps interface templates to populated implementations.
Definition objclass.h:89
ObjClassInfo * parent
Parent class (NULL if no parent)
Definition objclass.h:48
ObjIface * classif
Class interface (methods specific to this class, optional)
Definition objclass.h:49
int32 nsermembers
Number of entries in sermembers.
Definition objclass.h:76
ObjIface ** ifimpl
NULL-terminated array of interface implementations.
Definition objclass.h:50
void(* destroy)(void *_self)
Definition objclass.h:65
const StructMemberDesc * sermembers
Definition objclass.h:75
size_t instsize
Size in bytes of a class instance.
Definition objclass.h:47
intptr(* _cmp)(void *self, void *other, uint32 flags)
Cached function pointer for Sortable.cmp interface (NULL if not implemented)
Definition objclass.h:83
sa_ObjIface _impl
Storage for hydrated interface implementations.
Definition objclass.h:88
strref name
Wire name for serialization, only if the class is serializable.
Definition objclass.h:70
RWLock _lock
Protects access during object destruction.
Definition objclass.h:155
ObjInst * _inst
Pointer to the referenced object (NULL if destroyed)
Definition objclass.h:151
atomic(uintptr) _ref
Reference count for the weak reference itself.
void * _is_ObjInst_WeakRef
Type marker for compile-time validation.
Definition objclass.h:152
void * _is_ObjInst
Type marker for compile-time validation.
Definition objclass.h:135
ObjIface * _classif
Class interface (vtable) - called _ in generated class code.
Definition objclass.h:134
atomic(ptr) _weakref
Associated weak reference object (NULL if none exist)
atomic(uintptr) _ref
Reference count for memory management.
ObjClassInfo * _clsinfo
Pointer to class metadata.
Definition objclass.h:137
Macros for suppressing compiler warnings.