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

Macros

#define stDeclare(name)   extern const STypeInfo _sti_##name
 
#define stDefine(name)   const STypeInfo _sti_##name =
 

Detailed Description

Macros for defining user types that integrate fully with the stype system.

A custom type is defined with stDefine followed by a brace-enclosed STypeInfo initializer, paired with a standard block of harness #defines that hook the type name into stType, stArg, stCheckedArg, and the container APIs (saInit, saPush, htInsert, stCmp, etc.).

File-local type (no cross-TU sharing needed):

// In myfile.c:
static stDefine(mykey) {
.id = STypeId_opaque,
.size = sizeof(MyKey),
.flags = stFlag(PassPtr),
.ops = { .dtor = myKeyDtor, .cmp = myKeyCmp, .hash = myKeyHash },
};
#define SType_mykey MyKey*
#define STStorageType_mykey MyKey
#define STypeArg_mykey(type, val) stgeneric(opaque, &(val))
#define STypeArgPtr_mykey(type, val) &stgeneric(opaque, (val))
#define STypeCheckedArg_mykey(type, val) stType(type), stArg(type, val)
#define STypeCheckedPtrArg_mykey(type, val) stType(type), stArgPtr(type, val)
hashtable tbl;
htInit(&tbl, mykey, int32, 32);
htInsert(&tbl, mykey, k, int32, 500);
#define htInit(out, keytype, valtype, initsz,...)
Definition hashtable.h:344
#define htInsert(htbl, ktype, key, vtype, val,...)
Definition hashtable.h:443
#define stDefine(name)
Definition stype.h:1928

Shared type (visible across translation units):

// mylib.h:
stDeclare(vec3);
saDeclare(vec3);
#define SType_vec3 Vec3*
#define STStorageType_vec3 Vec3
#define STypeArg_vec3(type, val) stgeneric(opaque, &(val))
#define STypeArgPtr_vec3(type, val) &stgeneric(opaque, (val))
#define STypeCheckedArg_vec3(type, val) stType(type), stArg(type, val)
#define STypeCheckedPtrArg_vec3(type, val) stType(type), stArgPtr(type, val)
// mylib.c:
stDefine(vec3) {
.id = STypeId_opaque,
.size = sizeof(Vec3),
.flags = stFlag(PassPtr),
.ops = { .cmp = vec3Cmp, .hash = vec3Hash },
};
// any .c file that includes mylib.h:
sa_Vec3 points;
saInit(&points, vec3, 64);
Vec3 v = { 1.0f, 2.0f, 3.0f };
saPush(&points, vec3, v);
htInsert(&ht, string, _SL("origin"), vec3, v);
#define saDeclare(name)
Definition sarray.h:93
#define saInit(out, type, capacity,...)
Definition sarray.h:315
#define saPush(handle, type, elem,...)
Definition sarray.h:460
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
#define stDeclare(name)
Definition stype.h:1908

stType(vec3) resolves to &_sti_vec3 — a static, canonical pointer with no STypeFlag_Temporary set, so no type registry lookup occurs at init or push time.

Macro Definition Documentation

◆ stDeclare

#define stDeclare (   name)    extern const STypeInfo _sti_##name

stDeclare(name)

Declare a custom type descriptor defined in another translation unit.

Places extern const STypeInfo _sti_##name in the current scope. Typically used in a header file alongside a #define stType_name (&_sti_name) so that stType(name) resolves cleanly.

Parameters
nameType name (token)

Definition at line 1908 of file stype.h.

◆ stDefine

#define stDefine (   name)    const STypeInfo _sti_##name =

stDefine(name)

Begin the definition of a custom type descriptor.

Must be followed immediately by a brace-enclosed STypeInfo struct initializer. Place at file scope in a .c file; use static storage class for file-local types.

Parameters
nameType name (token)

Example:

stDefine(vec3) {
.id = STypeId_opaque,
.size = sizeof(Vec3),
.flags = stFlag(PassPtr),
.ops = { .cmp = vec3Cmp, .hash = vec3Hash },
};

Definition at line 1928 of file stype.h.