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

Modules

 Variant Creation
 
 Variant Lifecycle
 
 Variant Type Checking and Access
 
 Variant List Walking
 

Detailed Description

Type-tagged variant containers providing runtime polymorphism and type-safe variadic function arguments without exposing raw va_list semantics.

Concept

The stvar structure combines an stype descriptor with a typed value container (stgeneric), enabling a single variable to hold values of different types while preserving type information at runtime. This is the foundation for:

Lifetime and Scope

Variants created with stvar() use C99 compound literals to create stack-allocated temporaries. These temporaries have automatic storage duration limited to the enclosing block scope:

void processVariants(int count, stvar *vars);
void example() {
// Temporary variants valid until end of function
processVariants(3, (stvar[]){
stvar(int32, 42),
stvar(string, _S"hello"),
stvar(float64, 3.14)
});
// Temporaries destroyed here
}
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
#define stvar(typen, val)
Definition stvar.h:153

For persistent variants, use explicit allocation or embed in structures:

stvar persistent;
stvarCopy(&persistent, stvar(int32, 42));
// ... use persistent ...
stvarDestroy(&persistent);
void stvarDestroy(stvar *stv)
Definition stvar.h:209
void stvarCopy(stvar *dvar, stvar svar)
Definition stvar.h:235

Usage Patterns

Creating variants:

stvar v1 = stvar(int32, 42);
stvar v2 = stvar(string, _S"text");
stvar empty = stvNone;
#define stvNone
Definition stvar.h:169

Type checking and access:

if (stvarIs(&v1, int32)) {
int32 val = v1.data.st_int32;
}
string s = stvarString(&v2); // Returns NULL if not a string
#define stvarIs(svar, type)
Definition stvar.h:267
string stvarString(stvar *svar)
Definition stvar.h:292

Variadic function pattern:

// Internal function taking count and variant array
void _myFunc(int count, stvar *args) {
stvlist list;
stvlInit(&list, count, args);
int32 num;
string str;
if (stvlNext(&list, int32, &num) && stvlNext(&list, string, &str)) {
// Process typed arguments
}
}
// Macro wrapper to automatically fill count and create array
#define myFunc(...) _myFunc(count_macro_args(__VA_ARGS__), (stvar[]){__VA_ARGS__})
// Usage - caller just passes typed arguments directly
myFunc(stvar(int32, 10), stvar(string, _S"data"));
#define stvlNext(list, type, pvar)
Definition stvar.h:516
void stvlInit(stvlist *list, int count, stvar *vars)