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

Data Structures

struct  STypeInfo
 

Typedefs

typedef void(* stDtorFunc) (stype st, stgeneric *gen, flags_t flags)
 
typedef intptr(* stCmpFunc) (stype st, stgeneric gen1, stgeneric gen2, flags_t flags)
 
typedef uint32(* stHashFunc) (stype st, stgeneric gen, flags_t flags)
 
typedef void(* stCopyFunc) (stype st, _stCopyDest_Anno_(st) stgeneric *dest, stgeneric src, flags_t flags)
 
typedef bool(* stConvertFunc) (stype destst, _stCopyDest_Anno_(destst) stgeneric *dest, stype srcst, stgeneric src, flags_t flags)
 
typedef bool(* stSerializeFunc) (stype st, stgeneric val, SerWriter *w)
 
typedef bool(* stDeserializeFunc) (stype st, stgeneric *val, SerReader *r)
 
typedef struct STypeInfo STypeInfo
 

Detailed Description

Function pointers for runtime-dispatched operations on typed values. These enable generic algorithms to work with any type that provides the required operations.

IMPORTANT: These are LOW LEVEL operations. In particular, stCopy and stConvert OVERWRITE the destination without checking. This allows them to efficiently initialize uninitialized memory, but caution is required with object types to avoid leaking memory.

Example leak scenario:

string s1 = _SL("original");
string s2 = _SL("new");
stCopy(string, &s1, s2); // LEAKS "original" - should use strDup!
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
#define stCopy(type, pdest, src,...)
Definition stype.h:1695

For managed types (strings, objects, containers), use their specific APIs for assignment operations. Use stype operations primarily for:

Typedef Documentation

◆ stCmpFunc

typedef intptr(* stCmpFunc) (stype st, stgeneric gen1, stgeneric gen2, flags_t flags)

intptr stCmpFunc(stype st, stgeneric gen1, stgeneric gen2, [flags])

Comparison function returning ordering of two values.

Parameters
stType descriptor for both values
gen1First value
gen2Second value
flagsOperation flags:
  • ST_CaseInsensitive: Perform case-insensitive comparison (if applicable)
  • ST_Equality: Caller only needs equality (0/non-zero), not ordering
Returns
Negative if gen1 < gen2, zero if equal, positive if gen1 > gen2

Definition at line 1305 of file stype.h.

◆ stConvertFunc

typedef bool(* stConvertFunc) (stype destst, _stCopyDest_Anno_(destst) stgeneric *dest, stype srcst, stgeneric src, flags_t flags)

bool stConvertFunc(stype destst, stgeneric* dest, stype srcst, stgeneric src, [flags])

Type conversion function attempting to convert from one type to another.

WARNING: Overwrites destination without destroying existing value.

May fail if conversion is not possible, out of range, or would lose precision (depending on flags). The source type is responsible for implementing conversions to other types.

Parameters
deststDestination type descriptor
destPointer to destination
srcstSource type descriptor
srcSource value
flagsConversion flags:
  • ST_Overflow: Allow overflow/underflow (no range checking)
  • ST_Lossless: Fail if conversion loses precision
Returns
true if conversion succeeded, false otherwise

Definition at line 1357 of file stype.h.

◆ stCopyFunc

typedef void(* stCopyFunc) (stype st, _stCopyDest_Anno_(st) stgeneric *dest, stgeneric src, flags_t flags)

void stCopyFunc(stype st, stgeneric* dest, stgeneric src, [flags])

Deep copy function that duplicates a value.

WARNING: Overwrites destination without destroying existing value. Use only on uninitialized memory or after calling stDestroy on dest.

For reference-counted types (strings, objects), increments the reference count. For value types, performs a bitwise copy. For complex types, may perform deep duplication.

Parameters
stType descriptor
destPointer to destination (must be valid for PassPtr types)
srcSource value to copy
flagsOptional operation flags

Definition at line 1335 of file stype.h.

◆ stDeserializeFunc

typedef bool(* stDeserializeFunc) (stype st, stgeneric *val, SerReader *r)

Deserialize a value of this type from a serialization backend.

The counterpart to stSerializeFunc; see its notes on why this pair is format-agnostic. Any existing value at val is overwritten, not destroyed — the caller is responsible for handing over an uninitialized or already-cleared slot.

Parameters
stType descriptor of the value
valReceives the value read
rBackend to read from
Returns
true on success; on failure the error is left in the reader

Definition at line 1387 of file stype.h.

◆ stDtorFunc

typedef void(* stDtorFunc) (stype st, stgeneric *gen, flags_t flags)

void stDtorFunc(stype st, stgeneric* gen, [flags])

Destructor function for releasing resources owned by a typed value.

Called when a value is being removed from a container or otherwise destroyed. Responsible for freeing memory, decrementing reference counts, and any other cleanup required by the type.

Parameters
stType descriptor for the value
genPointer to the value container (invalidated after call)
flagsOptional operation flags

Definition at line 1292 of file stype.h.

◆ stHashFunc

typedef uint32(* stHashFunc) (stype st, stgeneric gen, flags_t flags)

uint32 stHashFunc(stype st, stgeneric gen, [flags])

Hash function computing a 32-bit hash value for use in hash tables.

Must satisfy: if stCmp(a,b)==0, then stHash(a)==stHash(b)

Parameters
stType descriptor
genValue to hash
flagsOperation flags:
  • ST_CaseInsensitive: Case-insensitive hash (if applicable)
Returns
32-bit hash value

Definition at line 1318 of file stype.h.

◆ stSerializeFunc

typedef bool(* stSerializeFunc) (stype st, stgeneric val, SerWriter *w)

Serialize a value of this type to a serialization backend.

Optional, and format-agnostic: the implementation decomposes the value into abstract data-model nodes (serWrite*, serArrBegin, ...) and the backend decides what those become on the wire. A type never asks which format it is being written to; where it genuinely must adapt, it queries a capability via serWriterCaps().

Built-in types leave this NULL — the traverser in cx/serialize/sertraverse.c handles them from its switch over STypeId_*. Only custom types (STCLASS_USER, custom opaque) need to supply one, and the single implementation serves every format.

Parameters
stType descriptor of the value
valValue to write
wBackend to write to
Returns
true on success; on failure the error is left in the writer

Definition at line 1375 of file stype.h.

◆ STypeInfo

typedef struct STypeInfo STypeInfo

Type descriptor for all runtime types in the stype system.

Each type has exactly one canonical const STypeInfo in static storage. stype is const STypeInfo* - two type values are equal if and only if they point to the same canonical descriptor (use stEq() to compare, which handles Temporary canonicalization).

Built-in type descriptors are declared as extern const STypeInfo _sti_name and accessed via stType(name) / stTypeInfo(name). Custom types are defined at file scope with stDefine(name) { ... } and declared in headers with stDeclare(name).

Definition at line 305 of file stype.h.