How to Use APIC Utilities

Apply the memory, string, file, command-line, and threading helper functions in your code.

This guide shows how to use APIC's low-level utility functions for memory monitoring, string manipulation, file I/O, command-line parsing, and thread management in simulation, profiling, and real-time code.

1. Monitor memory health

Call ActivateMemoryMon() early in your program, then use CheckHeapIntegrity() at checkpoints to catch heap corruption before it causes a crash.

Global memory monitoring is process-wide - enable it from exactly one model's OnInit(), the same way OGIInit() is called from exactly one model. Calling ActivateMemoryMon() from more than one model is redundant and not an error, but there is no reason to do it.

2. Manipulate strings

FunctionPurpose
s2ws / ws2sConvert between std::string and std::wstring.
ltrim / rtrimRemove leading or trailing characters (default null character).
splitSplit a string into a std::vector<std::string> using a delimiter.
startsWith / endsWithCheck if a string starts or ends with a given prefix/suffix.
replaceAllReplace all occurrences of a substring in a string.
makeLower / makeUpperConvert strings or char arrays to lower/upper case.
strStartWithCase-insensitive prefix check.
CountCharInStringCount occurrences of a specific character.
str2num / str2dnumConvert strings to integer or double, with optional error reporting.

3. Parse command-line arguments

Use cmdOptionExists to guard optional flags, then call getCmdOption to read the associated value.

4. Work with files and directories

5. Use array algorithms

6. Control threads

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

FunctionPurpose
EThreadPriorityEnum of priority levels: Undef, Lowest, BelowNormal, Normal, AboveNormal, Highest, RealTime.
SetThreadPriority / SetCurrentThreadPrioritySet priority for a specific thread or the current thread.
SetThreadAffinity / SetCurrentThreadAffinityPin threads to specific CPU cores.
SetRealtimePrioritySet a thread to real-time priority on specified cores.
GetThreadPriority / GetCurrentThreadPriorityRetrieve the priority of a thread or the current thread.
GetThreadAffinity / GetCurrentThreadAffinityGet CPU core affinity of a thread or the current thread.
GetNumberOfCoresReturns the number of CPU cores available.
IsLowLatencyKernelChecks if the system runs a low-latency kernel and returns its version.

7. Put it together

// 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();