CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches

Data Structures

struct  ObjClassInfo
 
struct  ObjInst
 
struct  ObjInst_WeakRef
 

Macros

#define objClassInfoName(cname)   cname##_clsinfo
 
#define Weak(clsname)   clsname##_WeakRef
 
#define ObjInst(inst)   ((ObjInst*)(unused_noeval(&((inst)->_is_ObjInst)), (inst)))
 
#define ObjInst_WeakRef(ref)   ((ObjInst_WeakRef*)(unused_noeval(&((ref)->_is_ObjInst_WeakRef)), (ref)))
 
#define objInstBase(inst)   ObjInst(inst)
 
#define objWeakRefBase(ref)   ObjInst_WeakRef(ref)
 
#define objClsInfo(inst)   (inst->_clsinfo)
 
#define objAcquire(inst)   (_objAcquire(objInstBase(inst)), (inst))
 
#define objRelease(pinst)   (unused_noeval(&((*(pinst))->_is_ObjInst)), _objRelease((ObjInst**)(pinst)))
 
#define objClassIf(clsname, ifname)   ((ifname*)_objClassIf(&objClassInfoName(clsname), objIfBase(&objIfTmplName(ifname))))
 
#define objInstIf(inst, ifname)   ((ifname*)_objInstIf(objInstBase(inst), objIfBase(&objIfTmplName(ifname))))
 
#define objGetWeak(clsname, inst)   ((Weak(clsname)*)_objGetWeak((ObjInst*)clsname(inst)))
 
#define objCloneWeak(ref)   (_objCloneWeak(objWeakRefBase(ref)))
 
#define objDestroyWeak(pref)   (unused_noeval(&((*(pref))->_is_ObjInst_WeakRef)), _objDestroyWeak((ObjInst_WeakRef**)(pref)))
 
#define objAcquireFromWeak(clsname, ref)   ((clsname*)_objAcquireFromWeak((ObjInst_WeakRef*)Weak(clsname)(ref)))
 
#define objDynCast(clsname, inst)   ((clsname*)_objDynCast(objInstBase(inst), &objClassInfoName(clsname)))
 
#define objAcquireFromWeakDyn(clsname, ref)   ((clsname*)_objAcquireFromWeakDyn(objWeakRefBase(ref), &objClassInfoName(clsname)))
 

Typedefs

typedef struct ObjClassInfo ObjClassInfo
 
typedef struct ObjInst ObjInst
 
typedef struct ObjInst_WeakRef ObjInst_WeakRef
 

Functions

void _objDestroy (ObjInst *inst)
 

Detailed Description

Classes are the concrete types in the object system that store per-instance data and implement interfaces. Each class has associated runtime metadata (ObjClassInfo) that describes its structure, inheritance relationships, and interface implementations.

Classes support:

Classes are defined in .cxh files and the build system generates the boilerplate code.

Macro Definition Documentation

◆ objAcquire

#define objAcquire (   inst)    (_objAcquire(objInstBase(inst)), (inst))

ClassType *objAcquire(ClassType *inst)

Increment the reference count of an object

Use this when storing an additional reference to an object. Each objAcquire() must be paired with a corresponding objRelease(). Does nothing if inst is NULL.

Example:

Document *doc = documentCreate(_S"Title"); // refcount = 1
Document *doc2 = objAcquire(doc); // refcount = 2
objRelease(&doc); // refcount = 1
objRelease(&doc2); // refcount = 0, object destroyed
#define objAcquire(inst)
Definition objclass.h:199
#define objRelease(pinst)
Definition objclass.h:223
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
Parameters
instPointer to object instance (may be NULL)
Returns
The same pointer passed in (for convenience)

Definition at line 199 of file objclass.h.

◆ objAcquireFromWeak

#define objAcquireFromWeak (   clsname,
  ref 
)    ((clsname*)_objAcquireFromWeak((ObjInst_WeakRef*)Weak(clsname)(ref)))

ClassName *objAcquireFromWeak(ClassName, Weak(ClassName) *ref)

Attempt to acquire the object referenced by a weak reference

If the object still exists, increments its reference count and returns it. If the object has been destroyed, returns NULL. The caller is responsible for calling objRelease() on the returned object.

Example:

Weak(Document) *weak = objGetWeak(Document, doc);
// ... later, possibly after doc was released ...
Document *doc2 = objAcquireFromWeak(Document, weak);
if (doc2) {
// Object still exists
objRelease(&doc2);
}
#define objGetWeak(clsname, inst)
Definition objclass.h:273
#define Weak(clsname)
Definition objclass.h:128
#define objAcquireFromWeak(clsname, ref)
Definition objclass.h:328
Parameters
clsnameClass name for type casting
refWeak reference (may be NULL)
Returns
Object pointer with incremented refcount, or NULL if object was destroyed

Definition at line 328 of file objclass.h.

◆ objAcquireFromWeakDyn

#define objAcquireFromWeakDyn (   clsname,
  ref 
)    ((clsname*)_objAcquireFromWeakDyn(objWeakRefBase(ref), &objClassInfoName(clsname)))

ClassName *objAcquireFromWeakDyn(ClassName, Weak(BaseClass) *ref)

Acquire from weak reference with runtime type checking

Combines objAcquireFromWeak() and objDynCast(). Attempts to acquire the object and verify it's compatible with the requested class type. Returns NULL if the object was destroyed or is not compatible with the target class.

Parameters
clsnameClass name to cast to
refWeak reference (may be NULL)
Returns
Object cast to requested type with incremented refcount, or NULL

Definition at line 364 of file objclass.h.

◆ objClassIf

#define objClassIf (   clsname,
  ifname 
)    ((ifname*)_objClassIf(&objClassInfoName(clsname), objIfBase(&objIfTmplName(ifname))))

InterfaceType *objClassIf(ClassName, InterfaceName)

Get interface implementation from a class (without instance)

Queries whether a class implements a specific interface and returns the populated interface structure if it does. Useful for accessing interface methods when you don't have an instance.

Parameters
clsnameClass name (e.g., Document)
ifnameInterface name (e.g., Printable)
Returns
Pointer to populated interface, or NULL if class doesn't implement it

Definition at line 237 of file objclass.h.

◆ objClassInfoName

#define objClassInfoName (   cname)    cname##_clsinfo

Construct class info variable name from class name

Parameters
cnameClass name
Returns
Expands to the class info variable name (e.g., Document_clsinfo)

Definition at line 86 of file objclass.h.

◆ objCloneWeak

#define objCloneWeak (   ref)    (_objCloneWeak(objWeakRefBase(ref)))

WeakRefType *objCloneWeak(WeakRefType *ref)

Increment the reference count of a weak reference

Creates an additional reference to the same weak reference object. Each objCloneWeak() must be paired with objDestroyWeak().

Parameters
refWeak reference to clone (may be NULL)
Returns
The same weak reference with incremented refcount

Definition at line 285 of file objclass.h.

◆ objClsInfo

#define objClsInfo (   inst)    (inst->_clsinfo)

ObjClassInfo *objClsInfo(ClassType *inst)

Get the class metadata for an object instance

Parameters
instPointer to any class instance
Returns
Pointer to the class's ObjClassInfo structure

Definition at line 168 of file objclass.h.

◆ objDestroyWeak

#define objDestroyWeak (   pref)    (unused_noeval(&((*(pref))->_is_ObjInst_WeakRef)), _objDestroyWeak((ObjInst_WeakRef**)(pref)))

void objDestroyWeak(WeakRefType **pref)

Release a weak reference

Decrements the weak reference's own reference count. When the count reaches zero, the weak reference object itself is destroyed. The pointer is set to NULL.

Example:

Weak(Document) *weak = objGetWeak(Document, doc);
// Use weak reference...
objDestroyWeak(&weak); // weak is now NULL
#define objDestroyWeak(pref)
Definition objclass.h:303
Parameters
prefPointer to weak reference pointer (automatically set to NULL)

Definition at line 303 of file objclass.h.

◆ objDynCast

#define objDynCast (   clsname,
  inst 
)    ((clsname*)_objDynCast(objInstBase(inst), &objClassInfoName(clsname)))

ClassName *objDynCast(ClassName, ClassType *inst)

Safely cast an object to a different class type with runtime checking

Checks if the object is an instance of the specified class or any of its subclasses. Returns the object cast to the requested type if compatible, NULL otherwise.

Example:

ObjInst *baseObj = getSomeObject();
Document *doc = objDynCast(Document, baseObj);
if (doc) {
// Safe to use as Document
}
#define objDynCast(clsname, inst)
Definition objclass.h:350
Parameters
clsnameClass name to cast to
instObject instance to cast (may be NULL)
Returns
Object cast to requested type, or NULL if incompatible

Definition at line 350 of file objclass.h.

◆ objGetWeak

#define objGetWeak (   clsname,
  inst 
)    ((Weak(clsname)*)_objGetWeak((ObjInst*)clsname(inst)))

Weak(ClassName) *objGetWeak(ClassName, ClassType *inst)

Get a weak reference to an object

Creates a weak reference if one doesn't exist, or returns the existing weak reference. Weak references allow observing an object without owning it. The returned weak reference has its own reference count and must be destroyed with objDestroyWeak().

Parameters
clsnameClass name for type casting
instObject instance
Returns
Weak reference to the object (never NULL)

Definition at line 273 of file objclass.h.

◆ ObjInst

#define ObjInst (   inst)    ((ObjInst*)(unused_noeval(&((inst)->_is_ObjInst)), (inst)))

ObjInst *ObjInst(ClassType *inst)

Cast a class instance pointer to base ObjInst type

Parameters
instPointer to any class instance
Returns
Base ObjInst pointer (with compile-time type validation)

Definition at line 136 of file objclass.h.

◆ ObjInst_WeakRef

#define ObjInst_WeakRef (   ref)    ((ObjInst_WeakRef*)(unused_noeval(&((ref)->_is_ObjInst_WeakRef)), (ref)))

ObjInst_WeakRef *ObjInst_WeakRef(WeakRefType *ref)

Cast a weak reference pointer to base ObjInst_WeakRef type

Parameters
refPointer to any weak reference
Returns
Base ObjInst_WeakRef pointer (with compile-time type validation)

Definition at line 144 of file objclass.h.

◆ objInstBase

#define objInstBase (   inst)    ObjInst(inst)

ObjInst *objInstBase(ClassType *inst)

Alias for ObjInst() cast

Parameters
instPointer to any class instance
Returns
Base ObjInst pointer

Definition at line 152 of file objclass.h.

◆ objInstIf

#define objInstIf (   inst,
  ifname 
)    ((ifname*)_objInstIf(objInstBase(inst), objIfBase(&objIfTmplName(ifname))))

InterfaceType *objInstIf(ClassType *inst, InterfaceName)

Query an object for a specific interface implementation

Returns the populated interface if the object implements it, NULL otherwise. Always check the return value before using the interface to ensure the object supports it.

Example:

ObjInst *obj = getSomeObject();
Printable *printIf = objInstIf(obj, Printable);
if (printIf) {
printIf->print(obj);
}
#define objInstIf(inst, ifname)
Definition objclass.h:259
Parameters
instObject instance (may be NULL)
ifnameInterface name
Returns
Pointer to populated interface, or NULL if not implemented

Definition at line 259 of file objclass.h.

◆ objRelease

#define objRelease (   pinst)    (unused_noeval(&((*(pinst))->_is_ObjInst)), _objRelease((ObjInst**)(pinst)))

void objRelease(ClassType **pinst)

Decrement reference count and destroy object if it reaches zero

This is the primary way to release ownership of an object. The pointer is automatically set to NULL after the call. If the reference count reaches zero, the object's destroy() callback (if any) is called, followed by memory deallocation. Does nothing if *pinst is NULL.

CRITICAL: Pass a pointer-to-pointer, not a pointer. The object pointer will be set to NULL.

Example:

Document *doc = documentCreate(_S"Title");
// Use doc...
objRelease(&doc); // Note: &doc, not doc
// doc is now NULL
Parameters
pinstPointer to object pointer (automatically set to NULL)

Definition at line 223 of file objclass.h.

◆ objWeakRefBase

#define objWeakRefBase (   ref)    ObjInst_WeakRef(ref)

ObjInst_WeakRef *objWeakRefBase(WeakRefType *ref)

Alias for ObjInst_WeakRef() cast

Parameters
refPointer to any weak reference
Returns
Base ObjInst_WeakRef pointer

Definition at line 160 of file objclass.h.

◆ Weak

#define Weak (   clsname)    clsname##_WeakRef

Construct weak reference type name from class name

Parameters
clsnameClass name
Returns
Expands to the weak reference type (e.g., Document_WeakRef)

Definition at line 128 of file objclass.h.

Typedef Documentation

◆ ObjClassInfo

typedef struct ObjClassInfo ObjClassInfo

Runtime metadata for a class

This structure contains all the information needed to manage a class at runtime, including its size, inheritance relationships, interface implementations, and lifecycle callbacks. One instance exists per class for the lifetime of the program.

The structure is divided into two sections:

  • Static fields: Set by the class implementer during declaration
  • Runtime fields: Populated automatically during first instantiation

Definition at line 31 of file objclass.h.

◆ ObjInst

typedef struct ObjInst ObjInst

Base structure for all object instances

ObjInst is the implicit base class for all objects in the CX object system. Every class instance begins with these fields, allowing generic handling of objects regardless of their specific class type.

The structure uses a union trick with _is_ObjInst to enable compile-time type checking in macros while maintaining C compatibility.

Definition at line 32 of file objclass.h.

◆ ObjInst_WeakRef

Weak reference structure

Allows observing an object without owning it. When the object is destroyed, the weak reference's _inst pointer is set to NULL, allowing safe detection of destruction. Multiple weak references can point to the same object.

Function Documentation

◆ _objDestroy()

void _objDestroy ( ObjInst inst)

Internal object destruction function - DO NOT CALL DIRECTLY!

This function is called internally when an object's reference count reaches zero. Always use objRelease() instead, which properly manages reference counting.