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

Modules

 File Operations
 
 Variable Binding
 
 Settings Access
 

Data Structures

struct  SetsBindSpec
 

Typedefs

typedef struct SetsBindSpec SetsBindSpec
 

Detailed Description

The Settings module extends the SSD tree system to provide persistent configuration storage with automatic background flushing and optional variable binding.

Key Features:

Basic Usage:

// Open settings file from VFS
VFS *vfs = vfsCreate();
SSDNode *settings = setsOpen(vfs, _S"config.json", timeS(30));
// Read/write settings like a normal SSD tree
setsSet(settings, _S"window/width", int32, 1920);
setsSet(settings, _S"window/height", int32, 1080);
int32 width = setsGet(settings, _S"window/width", int32, 800);
// Changes are automatically flushed to disk every 30 seconds
// When done, close to ensure final flush
setsClose(&settings);
objRelease(&vfs);
#define vfsCreate(flags)
Definition vfsobj.h:77
#define objRelease(pinst)
Definition objclass.h:223
#define setsSet(sets, path, type, val)
Definition settings.h:320
#define setsGet(sets, path, type, out, def)
Definition settings.h:301
bool setsClose(SSDNode **psets)
SSDNode * setsOpen(VFS *vfs, strref path, int64 flush_interval)
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392
#define timeS(s)
Definition time.h:18
VFS Object.
Definition vfsobj.h:25

Variable Binding:

The settings module can bind specific paths to program variables, allowing automatic two-way synchronization. When a setting is read, the bound variable is updated. When the bound variable changes, the setting is updated at the next flush interval.

// Define variables to bind
static struct {
int32 width;
int32 height;
string theme;
} config;
// Define binding specifications
static SetsBindSpec bindings[] = {
{ .name = _S"window/width", .offset = offsetof(typeof(config), width),
.deftyp = stvar(int32, 1024) },
{ .name = _S"window/height", .offset = offsetof(typeof(config), height),
.deftyp = stvar(int32, 768) },
{ .name = _S"display/theme", .offset = offsetof(typeof(config), theme),
.deftyp = stvar(string, _S"dark") },
{ 0 } // NULL terminator
};
// Bind settings to variables
setsBind(settings, bindings, &config);
// Now config.width/height/theme are automatically synchronized
config.width = 1920; // Will be written to settings at next flush
bool setsBind(SSDNode *sets, SetsBindSpec *bindings, void *base)
#define stvar(typen, val)
Definition stvar.h:153
string name
Path to the setting (e.g., "window/width")
Definition settings.h:106

Supported Types for Binding:

Integration with SSD Tree:

The settings module is built on top of the SSD tree system and uses specialized node classes:

All standard SSD tree operations work on settings trees, including path-based access, subtree operations, and locking. The settings module overrides node factories to ensure hashtable nodes are created as SettingsHashNode instances with binding support.

Thread Safety:

Settings trees inherit the thread-safe locking mechanisms from SSD trees. A dedicated background thread monitors all open settings trees and periodically checks for changes and flushes modified trees to disk.

Typedef Documentation

◆ SetsBindSpec

typedef struct SetsBindSpec SetsBindSpec

Binding specification for connecting settings paths to program variables.

Used with setsBind() to establish two-way synchronization between settings and variables. The array must be terminated with a {0} entry (NULL name).