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

Macros

#define stTypeCast(name, v)   ((SType_##name)(v))
 
#define stPtrCast(name, v)   ((SType_##name*)(v))
 
#define stvarTypeId(v)   (stvarType(v)->id)
 
#define stTypeId(name)   STypeId_##name
 
#define stTypeSize(name)   STypeSize_##name
 
#define stHasFlag(st, fname)   (((st)->flags & stFlag(fname)) != 0)
 
#define stGetSize(st)   ((st)->size)
 
#define stCheck(type, val)   STypeCheck_##type(type, val)
 
#define stCheckPtr(type, ptr)   STypeCheckPtr_##type(type, ptr)
 
#define stRvalAddr(type, rval)   ((stStorageType(type)[1]) { rval })
 

Functions

stype stvarType (const stvar *v)
 
const char * stvarKey (const stvar *v)
 

Detailed Description

Utility macros for type casting, checking, and manipulation.

Macro Definition Documentation

◆ stCheck

#define stCheck (   type,
  val 
)    STypeCheck_##type(type, val)

value stCheck(type, value)

Perform compile-time type checking on a value.

For primitive types, this is a no-op that returns the value unchanged. For object types (strings, objects, containers), verifies the value has the expected structure marker, providing compile-time type safety.

This is used internally by the argument passing macros but can also be used directly for type validation.

Parameters
typeType name
valValue to check
Returns
The value, unchanged (with compile-time validation)

Example:

string s = ...;
string checked = stCheck(string, s); // Validates s is a string
#define stCheck(type, val)
Definition stype.h:787

Definition at line 787 of file stype.h.

◆ stCheckPtr

#define stCheckPtr (   type,
  ptr 
)    STypeCheckPtr_##type(type, ptr)

pointer stCheckPtr(type, pointer)

Perform compile-time type checking on a pointer to a value.

Similar to stCheck() but for pointers. Validates that the pointer points to the expected type.

Parameters
typeType name
ptrPointer to check
Returns
The pointer, unchanged (with compile-time validation)

Definition at line 830 of file stype.h.

◆ stGetSize

#define stGetSize (   st)    ((st)->size)

uint16 stGetSize(stype st)

Extract the storage size from a type descriptor. Equivalent to st->size.

Parameters
stType descriptor
Returns
Size in bytes

Definition at line 694 of file stype.h.

◆ stHasFlag

#define stHasFlag (   st,
  fname 
)    (((st)->flags & stFlag(fname)) != 0)

bool stHasFlag(stype st, flagname)

Check if a type descriptor has a specific flag set.

Parameters
stType descriptor
fnameFlag name (Object, Custom, or PassPtr)
Returns
true if flag is set

Example:

if (stHasFlag(st, Object)) {
// This is an object-like type
}
#define stHasFlag(st, fname)
Definition stype.h:686

Definition at line 686 of file stype.h.

◆ stPtrCast

#define stPtrCast (   name,
 
)    ((SType_##name*)(v))

<type>* stPtrCast(type, value)

Cast a pointer to a pointer-to-type for the given stype name.

Parameters
nameType name
vPointer to cast
Returns
Pointer cast to pointer-to-type

Example:

void* ptr = ...;
int32* iptr = stPtrCast(int32, ptr);
#define stPtrCast(name, v)
Definition stype.h:387

Definition at line 387 of file stype.h.

◆ stRvalAddr

#define stRvalAddr (   type,
  rval 
)    ((stStorageType(type)[1]) { rval })

stStorageType(type)* stRvalAddr(type, rvalue)

Create an lvalue from an rvalue expression.

Uses C99 compound literals to create a temporary on the stack, allowing pointers to be taken of arbitrary expressions. This is primarily used internally for pass-by-pointer types (SUID, stvar, opaque) that need to pass rvalues where a pointer is required.

Parameters
typeType name
rvalRvalue expression
Returns
Pointer to a stack-allocated temporary containing the value

Example (internal use):

// Allows passing SUID literals where SUID* is expected
stRvalAddr(suid, suidMake())
#define stRvalAddr(type, rval)
Definition stype.h:852

Definition at line 852 of file stype.h.

◆ stTypeCast

#define stTypeCast (   name,
 
)    ((SType_##name)(v))

<type> stTypeCast(type, value)

Cast a value to the appropriate C type for the given stype name.

Parameters
nameType name (e.g., int32, string, object)
vValue to cast
Returns
Value cast to the appropriate type

Example:

void* ptr = ...;
string s = stTypeCast(string, ptr);
#define stTypeCast(name, v)
Definition stype.h:372

Definition at line 372 of file stype.h.

◆ stTypeId

#define stTypeId (   name)    STypeId_##name

uint32 stTypeId(type)

Get the compile-time type ID constant for a type name.

Parameters
nameType name
Returns
Type ID enum value (STypeId_xxx)

Example:

stype st = ...;
if (st->id == stTypeId(int32)) { ... }
#define stTypeId(name)
Definition stype.h:610
uint32 id
hierarchical type ID: STCLASS | STST_subtype | discriminant
Definition stype.h:1410

Definition at line 610 of file stype.h.

◆ stTypeSize

#define stTypeSize (   name)    STypeSize_##name

size_t stTypeSize(type)

Get the compile-time storage size for a type name.

Parameters
nameType name
Returns
Size in bytes

Example:

void* buffer = xaAlloc(stTypeSize(MyStruct) * count);
#define stTypeSize(name)
Definition stype.h:660
#define xaAlloc(size,...)
Definition xalloc.h:199

Definition at line 660 of file stype.h.

◆ stvarTypeId

#define stvarTypeId (   v)    (stvarType(v)->id)

uint32 stvarTypeId(const stvar *v)

Returns the type ID of the value contained in a variant.

Convenience shorthand for stvarType(v)->id. Handy for switch statements over a variant's runtime type using the stTypeId() constants.

Parameters
vPointer to variant
Returns
Type ID enum value (see stTypeId())

Example:

switch (stvarTypeId(val)) {
case stTypeId(int32): ...
case stTypeId(string): ...
}
#define stvarTypeId(v)
Definition stype.h:479

Definition at line 479 of file stype.h.

Function Documentation

◆ stvarKey()

const char * stvarKey ( const stvar v)
inline

const char* stvarKey(const stvar *v)

Returns the key attached to a variant, or NULL if it has none.

Keyed variants are created with stvark() (or stvarkn()), which attaches a name so the variant can be located by key instead of by type and position. The name is a plain C string rather than a cx string – there is no portable way to embed a length inside an expression macro, and keys are short enough that a plain compare costs nothing.

The name is metadata: it is preserved by copy, but deliberately ignored by compare and hash, so attaching a key never changes a variant's value semantics in a container.

Parameters
vPointer to variant
Returns
Key name, or NULL if the variant is unkeyed

Example:

stvar v = stvark(host, string, hostname);
const char *k = stvarKey(&v); // "host"
#define stvark(key, typen, val)
Definition stvar.h:204
#define stvar(typen, val)
Definition stvar.h:162
const char * stvarKey(const stvar *v)
Definition stype.h:514

Definition at line 514 of file stype.h.

◆ stvarType()

stype stvarType ( const stvar v)
inline

stype stvarType(const stvar *v)

Returns the canonical type descriptor for a variant.

Parameters
vPointer to variant
Returns
Canonical stype descriptor (never carries the ownership tag)

Definition at line 457 of file stype.h.

Referenced by stvarCopy().