|
CX Framework
Cross-platform C utility framework
|
Macros | |
| #define | stvlFind(list, key, type, pvar) _stvlFind(list, #key, stCheckedPtrArg(type, pvar)) |
| #define | stvlFindPtr(list, key) _stvlFindPtr(list, #key, stType(ptr)) |
| #define | stvlFindObj(list, key, class) objDynCast(class, (ObjInst*)_stvlFindPtr(list, #key, stType(object))) |
| #define | stvlFindVal(list, key, type) (_stvlFindVal(list, #key, stType(type)).st_##type) |
| #define | stvlHasKey(list, key) _stvlHasKey(list, #key) |
Lookup by key name rather than by type and position.
stvlNext() scans forward from the cursor for the next variant of a type, advances past the match, and discards everything it skipped. That is the right contract for positional arguments, which arrive in a known order.
Keys exist precisely so that order does not matter, so stvlFind() does the opposite: it scans the whole list from the start and mutates nothing – the cursor is left exactly where it was.
The two address the same argument list without interfering, in either order:
The two modes are disjoint: stvlNext() skips keyed variants entirely. A keyed argument is reachable only through stvlFind(). This is deliberate – if positional walking could consume keyed arguments, adding one to an existing call would silently shift every same-typed positional argument after it, which is exactly the fragility keys exist to remove. It also means a caller can add a keyed argument to a call without renumbering or reordering anything.
Duplicate keys in one argument list resolve to the first match. This cannot be caught at compile time; debug builds assert on it, release builds take the first. Do not rely on the behaviour.
| #define stvlFind | ( | list, | |
| key, | |||
| type, | |||
| pvar | |||
| ) | _stvlFind(list, #key, stCheckedPtrArg(type, pvar)) |
bool stvlFind(stvlist *list, key, type, type *pvar)
Find a variant by key name and type, without disturbing the walker.
Scans the entire list from the beginning for a variant whose key matches key and whose type matches type, and copies its value to pvar. The cursor is not moved and the list is not modified.
The key is written as a bare token and stringized, matching stvark().
| list | Pointer to list walker (not modified) |
| key | Key name as a bare token (not a string literal) |
| type | Expected type name |
| pvar | Pointer to storage receiving the value |
Example:
| #define stvlFindObj | ( | list, | |
| key, | |||
| class | |||
| ) | objDynCast(class, (ObjInst*)_stvlFindPtr(list, #key, stType(object))) |
ClassName* stvlFindObj(stvlist *list, key, ClassName)
Find an object variant by key name and dynamic-cast it, without disturbing the walker.
As stvlFindPtr(), but restricted to object variants and passed through objDynCast, so the result is NULL unless the object is compatible with the named class.
| list | Pointer to list walker (not modified) |
| key | Key name as a bare token (not a string literal) |
| class | Target class name for dynamic cast |
Example:
| #define stvlFindPtr | ( | list, | |
| key | |||
| ) | _stvlFindPtr(list, #key, stType(ptr)) |
void* stvlFindPtr(stvlist *list, key)
Find a pointer-typed variant by key name, without disturbing the walker.
As stvlFind() for pointer-like types (ptr, objects, and PassPtr types). Returns the stored pointer directly.
| list | Pointer to list walker (not modified) |
| key | Key name as a bare token (not a string literal) |
Example:
| #define stvlFindVal | ( | list, | |
| key, | |||
| type | |||
| ) | (_stvlFindVal(list, #key, stType(type)).st_##type) |
type stvlFindVal(stvlist *list, key, type)
Find a variant by key name and type, and return its value, without disturbing the walker.
As stvlFind(), but returns the value directly, or zero if there is no matching variant. Use stvlFind() when zero is a valid value and you need to tell it apart from a missing one. The value is borrowed from the list, as with stvlAt().
| list | Pointer to list walker (not modified) |
| key | Key name as a bare token (not a string literal) |
| type | Expected type name |
Example:
| #define stvlHasKey | ( | list, | |
| key | |||
| ) | _stvlHasKey(list, #key) |
bool stvlHasKey(stvlist *list, key)
Test whether a keyed variant exists, regardless of its type.
| list | Pointer to list walker (not modified) |
| key | Key name as a bare token (not a string literal) |
Example: