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

Core Concept

The stype system provides runtime type information through 32-bit type descriptors that encode type identity, size, and behavioral flags. This enables generic programming patterns similar to C++ templates while maintaining C compatibility and avoiding code bloat from template instantiation.

Type descriptors are created at compile-time through macro expansion, allowing the compiler to optimize away most overhead when types are known statically. When types must be determined at runtime (e.g., heterogeneous containers, variant types), the compact 32-bit descriptor provides efficient type identification and dispatch to appropriate operations.

Type Descriptor Format

A stype is a 32-bit value with the following layout:

Bits 0-7: Type ID (identifies the specific type)
Bits 8-15: Flags (behavioral attributes)
Bits 16-31: Size (storage size in bytes)

Type IDs are grouped into classes (integer, unsigned, float, pointer, CX types) to enable efficient categorization and default operation selection.

Supported Types

Primitive Types:

CX Framework Types:

Usage Patterns

Generic function parameters use macros that expand to type-value pairs:

htInsert(&ht, string, _S"key", int32, 42);
// Expands to type descriptors + type-checked values
#define htInsert(htbl, ktype, key, vtype, val,...)
Definition hashtable.h:443
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392

Stored values in containers use stStored() to extract values from memory:

stgeneric val = stStored(elemType, &array[idx]);
#define stStored(st, storage)
Definition stype.h:1319

Custom types via opaque(RealType) provide type-safe handling of structs:

saInit(&arr, opaque(MyStruct), 16);
#define saInit(out, type, capacity,...)
Definition sarray.h:318

Type Operations

The system provides function pointers for standard operations on types:

Default implementations exist for built-in types. Custom types can override these through the STypeOps structure.

Type Safety

Compile-time checking is enforced through macro expansion:

Runtime checking occurs when:

Variant Types (stvar)

The stvar structure combines a value with its type descriptor for runtime polymorphism:

stvar v = stvar(int32, 42);
if (v.type == stType(int32)) {
int32 val = v.data.st_int32;
}
#define stvar(typen, val)
Definition stvar.h:153
#define stType(name)
Definition stype.h:822

This is the foundation for type-safe variadic arguments and heterogeneous collections throughout the framework.