APIC – Utility Functions & System Helpers guide

Utility Functions & System Helpers Usage

Utility Functions & System Helpers

This section covers low-level utility functions for memory monitoring, string manipulation, file I/O, command-line argument parsing, and thread management. These utilities are widely used across simulation, profiling, and real-time systems.

Memory Utilities

String Utilities

Comprehensive functions for string trimming, splitting, case conversion, and prefix/suffix checks.

Command-Line Argument Utilities

The application stores its raw argv[] in SystemConfig.arguments at startup. Read any name="value" argument from it in a model's OnInit():

// Usage: projectApp.exe myArg="value"
std::string myArgValue;
getCmdOption(SystemConfig.arguments, "myArg", myArgValue);

// Boolean-style flag, e.g. projectApp.exe ?
if (cmdOptionExists(SystemConfig.arguments, "?"))
{
    // print usage/help and exit
}
This is the same generic mechanism used by the built-in config argument (projectApp.exe config="[path]/config.json") — any argument name works the same way.

File & Directory Utilities

Algorithms

Threading Utilities

These functions allow explicit control over thread priority and CPU affinity, useful in high-performance and low-latency applications.

Frame-Gating Utilities

CSimEngine::CMinorFrameGate – Guards a piece of code so it executes at most once per CoreEngine minor frame, regardless of how many times or from how many call sites it's invoked during that frame (e.g. a shared helper called from several stream triggers, or a function reachable from multiple code paths). Declare a static gate instance local to the code being guarded, and check ShouldRun() before proceeding.

static CSimEngine::CMinorFrameGate frameGate;
if (!frameGate.ShouldRun())
    return;

// ... code here runs at most once per minor frame ...
The gate is static, so it's shared across all calls to the enclosing function/scope — the first call in a given minor frame passes, and any further calls within the same frame are skipped until the next minor frame begins.

CSimEngine::CValueChangeGate – Guards a piece of code so it only runs when a specific element's value has actually changed since the last check, skipping redundant processing when nothing changed:

static CSimEngine::CValueChangeGate valueChangeGate(&pLRU->block.element);
if (!valueChangeGate.ShouldRun())
    return;

// ... code here runs only when pLRU->block.element changed since the last call ...
Combine CMinorFrameGate and CValueChangeGate to cheaply skip both duplicate-per-frame calls and unchanged-data processing before doing any real work.

Usage Examples


// Memory monitoring
ActivateMemoryMon();
CheckHeapIntegrity();

// File utilities
if (DirectoryExists("C:/Data")) {
    auto files = GetFiles("C:/Data", ".txt");
}

// String operations
std::string name = "SimulationFile.TXT";
if (endsWith(name, ".TXT")) {
    makeLower(name); // "simulationfile.txt"
}

// Thread utilities
std::thread t([](){ /* do work */ });
SetThreadPriority(&t, EThreadPriority::RealTime);
SetThreadAffinity(&t, std::vector{true, false, false, true});
t.join();