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
6#include <cx/obj/objiface.h>
8#include <cx/thread/atomic.h>
10#include <cx/thread/rwlock.h>
11
28
29CX_C_BEGIN
30
32typedef struct ObjInst ObjInst;
33typedef struct ObjClassInfo ObjClassInfo;
34typedef struct ObjInst ObjInst;
35
45typedef struct ObjClassInfo {
46 // ----- Statically defined in class info instance by class implementer -----
47
48 size_t instsize;
52
59 bool(*init)(void *_self);
60
66 void(*destroy)(void *_self);
67
68 bool _abstract;
69
70 // ----- Runtime use only, do not set manually! -----
71
73 intptr (*_cmp)(void *self, void *other, uint32 flags);
74
76 uint32 (*_hash)(void *self, uint32 flags);
77
78 sa_ObjIface _impl;
79 hashtable _tmpl;
81
86#define objClassInfoName(cname) cname##_clsinfo
87
96typedef struct ObjInst {
97 union {
100 };
102 atomic(uintptr) _ref;
103 atomic(ptr) _weakref;
104
105 // Class-specific data members follow in derived classes
106} ObjInst;
107
113typedef struct ObjInst_WeakRef
114{
115 union
116 {
119 };
120 atomic(uintptr) _ref;
123
128#define Weak(clsname) clsname##_WeakRef
129
136#define ObjInst(inst) ((ObjInst*)(unused_noeval(&((inst)->_is_ObjInst)), (inst)))
137
144#define ObjInst_WeakRef(ref) ((ObjInst_WeakRef*)(unused_noeval(&((ref)->_is_ObjInst_WeakRef)), (ref)))
145
152#define objInstBase(inst) ObjInst(inst)
153
160#define objWeakRefBase(ref) ObjInst_WeakRef(ref)
161
168#define objClsInfo(inst) (inst->_clsinfo)
169
174void _objDestroy(_Pre_notnull_ _Post_invalid_ ObjInst *inst);
175
176_meta_inline void _objAcquire(_In_opt_ ObjInst *inst)
177{
178 if (inst)
179 atomicFetchAdd(uintptr, &inst->_ref, 1, Relaxed);
180}
181
199#define objAcquire(inst) (_objAcquire(objInstBase(inst)), (inst))
200
201_At_(*instp, _Pre_maybenull_ _Post_null_)
202void _objRelease(_Inout_ ObjInst **instp);
203
223#define objRelease(pinst) (unused_noeval(&((*(pinst))->_is_ObjInst)), _objRelease((ObjInst**)(pinst)))
224
225_Ret_maybenull_ ObjIface *_objClassIf(_In_ ObjClassInfo *cls, _In_ ObjIface *iftmpl);
237#define objClassIf(clsname, ifname) ((ifname*)_objClassIf(&objClassInfoName(clsname), objIfBase(&objIfTmplName(ifname))))
238
239_Ret_maybenull_ ObjIface *_objInstIf(_In_opt_ ObjInst *inst, _In_ ObjIface *iftmpl);
259#define objInstIf(inst, ifname) ((ifname*)_objInstIf(objInstBase(inst), objIfBase(&objIfTmplName(ifname))))
260
261_Ret_valid_ ObjInst_WeakRef *_objGetWeak(_In_ ObjInst *inst);
273#define objGetWeak(clsname, inst) ((Weak(clsname)*)_objGetWeak((ObjInst*)clsname(inst)))
274
275_Ret_maybenull_ ObjInst_WeakRef *_objCloneWeak(_In_opt_ ObjInst_WeakRef *ref);
285#define objCloneWeak(ref) (_objCloneWeak(objWeakRefBase(ref)))
286
287void _objDestroyWeak(_Inout_ ObjInst_WeakRef **refp);
303#define objDestroyWeak(pref) (unused_noeval(&((*(pref))->_is_ObjInst_WeakRef)), _objDestroyWeak((ObjInst_WeakRef**)(pref)))
304
305_Ret_maybenull_ ObjInst *_objAcquireFromWeak(_In_opt_ ObjInst_WeakRef *ref);
328#define objAcquireFromWeak(clsname, ref) ((clsname*)_objAcquireFromWeak((ObjInst_WeakRef*)Weak(clsname)(ref)))
329
330_Ret_maybenull_ ObjInst *_objDynCast(_In_opt_ ObjInst *inst, _In_ ObjClassInfo *cls);
350#define objDynCast(clsname, inst) ((clsname*)_objDynCast(objInstBase(inst), &objClassInfoName(clsname)))
351
352_Ret_maybenull_ ObjInst *_objAcquireFromWeakDyn(_In_opt_ ObjInst_WeakRef *ref, _In_ ObjClassInfo *cls);
364#define objAcquireFromWeakDyn(clsname, ref) ((clsname*)_objAcquireFromWeakDyn(objWeakRefBase(ref), &objClassInfoName(clsname)))
365
367
368CX_C_END
void _objDestroy(ObjInst *inst)
#define ObjInst(inst)
Definition objclass.h:136
#define ObjInst_WeakRef(ref)
Definition objclass.h:144
Hash table container with type-safe generic key-value storage.
Interface definitions and management for the object system.
Reader-writer lock synchronization primitive.
bool(* init)(void *_self)
Definition objclass.h:59
bool _abstract
True if this is an abstract class (cannot be instantiated)
Definition objclass.h:68
uint32(* _hash)(void *self, uint32 flags)
Cached function pointer for Hashable.hash interface (NULL if not implemented)
Definition objclass.h:76
hashtable _tmpl
Maps interface templates to populated implementations.
Definition objclass.h:79
ObjClassInfo * parent
Parent class (NULL if no parent)
Definition objclass.h:49
ObjIface * classif
Class interface (methods specific to this class, optional)
Definition objclass.h:50
ObjIface ** ifimpl
NULL-terminated array of interface implementations.
Definition objclass.h:51
void(* destroy)(void *_self)
Definition objclass.h:66
size_t instsize
Size in bytes of a class instance.
Definition objclass.h:48
intptr(* _cmp)(void *self, void *other, uint32 flags)
Cached function pointer for Sortable.cmp interface (NULL if not implemented)
Definition objclass.h:73
sa_ObjIface _impl
Storage for hydrated interface implementations.
Definition objclass.h:78
RWLock _lock
Protects access during object destruction.
Definition objclass.h:121
ObjInst * _inst
Pointer to the referenced object (NULL if destroyed)
Definition objclass.h:117
atomic(uintptr) _ref
Reference count for the weak reference itself.
void * _is_ObjInst_WeakRef
Type marker for compile-time validation.
Definition objclass.h:118
void * _is_ObjInst
Type marker for compile-time validation.
Definition objclass.h:99
ObjIface * _classif
Class interface (vtable) - called _ in generated class code.
Definition objclass.h:98
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:101
Macros for suppressing compiler warnings.