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

Provides a unified foreach macro for iterating over various container types and iterable objects. The macro expands to type-specific iteration code while presenting a consistent interface.

Overview

The foreach macro adapts to different container types, automatically handling iterator initialization, validity checking, advancement, and cleanup. The syntax varies by container type to accommodate different iteration patterns.

Supported Container Types

Dynamic Arrays (sarray)

Iterates over elements in a dynamic array with direct indexed access. Elements are copied by assignment (shallow copy), allowing iteration over value types without pointers.

Syntax:

foreach(sarray, index, ElementType, element, array)

Parameters:

Examples:

// Iterating over pointer types
sa_ObjIface impls = {0};
// ... populate array ...
foreach (sarray, i, ObjIface*, impl, impls) {
if (!_objCheckIface(impl))
return false;
}
// Iterating over value types (no pointer needed)
sa_int32 numbers = {0};
// ... populate array ...
foreach (sarray, i, int32, num, numbers) {
total += num;
}

Hash Tables (hashtable)

Iterates over all entries in a hash table in insertion order. CX hashtables preserve the order in which entries were added.

Syntax:

foreach(hashtable, iterator, table)

Parameters:

Accessing Values: Use htiKey(type, iterator) and htiVal(type, iterator) to copy entries from the table, or use htiKeyPtr(type, iterator) and htiValPtr(type, iterator) to get direct pointers to the stored keys and values without copying:

// Copying values out of the table
hashtable deferred = {0};
// ... populate table ...
foreach (hashtable, it, deferred) {
ComplexTask *task = htiKey(object, it);
// process task...
}
// Direct access to stored values (no copy)
hashtable settings = {0};
// ... populate with string keys and int32 values ...
foreach (hashtable, it, settings) {
string *key = htiKeyPtr(string, it);
int32 *value = htiValPtr(int32, it);
// Modify value in place: (*value)++
}
#define htiKeyPtr(type, iter)
Definition hashtable.h:171
#define htiKey(type, iter)
Definition hashtable.h:187
#define htiValPtr(type, iter)
Definition hashtable.h:179
Complex task with dependencies, scheduling, and resource management.

Strings (string)

Iterates over chunks (runs) of contiguous bytes in a string. This efficiently handles both simple strings (single chunk) and rope strings (multiple chunks).

Syntax:

foreach(string, iterator, str)

Parameters:

Iterator Fields:

Example:

foreach (string, it, str) {
if (!sbufPWrite(sb, it.bytes, it.len))
return false;
}
bool sbufPWrite(StreamBuffer *sb, const uint8 *buf, size_t sz)

File System Search (vfssearch)

Iterates over directory entries in a VFS, optionally matching a search pattern.

Syntax:

foreach(vfssearch, iterator, vfs, path, pattern, typefilter, stat)

Parameters:

Iterator Fields:

Example:

VFS *vfs = vfsCreate(0);
vfsMountFS(vfs, _S"/", _S"c:/data");
foreach(vfssearch, iter, vfs, _S"/logs", _S"*.log", FS_File, false) {
// Process iter.name (log files only)
}
objRelease(&vfs);
@ FS_File
Path is a regular file.
Definition fs.h:96
#define vfsCreate(flags)
Definition vfsobj.h:77
#define vfsMountFS(vfs, path, fsroot,...)
Definition vfs.h:116
#define objRelease(pinst)
Definition objclass.h:223
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
VFS Object.
Definition vfsobj.h:25

SSD Tree Nodes (ssd)

Iterates over key-value pairs in an SSDTree node with automatic locking. The iteration occurs within a locked transaction for thread safety.

Syntax:

foreach(ssd, iterator, index, key, value, node)

Parameters:

Example:

SSDNode *btree = ssdSubtreeB(tree, _S"config");
foreach(ssd, oiter, idx, name, val, btree) {
if (strEq(name, _S"setting1") && stvarIs(val, int32)) {
// Process val->data.st_int32
}
}
#define ssdSubtreeB(root, path)
Definition ssdtree.h:145
bool strEq(strref s1, strref s2)
#define stvarIs(svar, type)
Definition stvar.h:267

Note: The node is automatically locked for the duration of the iteration.

Iterable Objects (object)

Iterates over objects implementing the Iterable interface, which provides an Iterator object for traversal.

Syntax:

foreach(object, iterator, IteratorType, obj)

Parameters:

Example:

MyIterable *collection = ...;
foreach(object, iter, MyIterator, collection) {
stvar val;
if (iter->_->get(iter, &val)) {
// Process val
}
}
#define stvar(typen, val)
Definition stvar.h:153

Note: The iterator is automatically released when the loop exits.

Usage Guidelines