Start

Theory

Reference

Examples

← All examples
Configuration

Configuration Reader

Reading typed values out of a TOML config

Opens a TOML configuration file through ConfigurationManager and reads back typed settings (Get<int>, etc.) with explicit error handling on missing or mistyped keys, printing each to stdout.

Configuration
console example — no window, see captured output below

Sample output

configuration_reader output
[INFO][20:35:47.780][FileWatcher] New File Watcher Created
Before Set
Window.Width = 800
Window.Height = 600
Rendering.Target_Fps = 120
[INFO][20:35:47.786][AddWatch] Added new watch for "C:\\Users\\ricca\\AppData\\Local\\Temp\\asge_configuration_reader_example.toml" with mask Modified
After Set+Save
Window.Width = 1024
Window.Height = 600
Rendering.Target_Fps = 120

Run it yourself

build just this example
cmake --build --preset windows-debug --target configuration_reader
# binary lands under bin/ (e.g. bin/Debug/configuration_reader.exe on Windows)

See Installation for the full build setup.

Source

examples/configuration_reader/main.cpp
#include <iostream>
#include <filesystem>
#include <ASGE/Core/Configuration/ConfigurationManager.hpp>
#include <ASGE/Core/Filesystem/Filesystem.hpp>

using namespace asge::config;

namespace
{

void PrintIntSetting(ConfigurationManager const& cm, std::string const& inPath)
{
    auto result = cm.Get<int>(inPath);
    if (!result)
    {
        auto const err = result.Error();
        LOG_ERROR("Error reading ", inPath, ": ", err);
        return;
    }

    std::cout << inPath << " = " << result.Value() << "\n";
}

}

int main()
{
    // Work on a throwaway copy of the shipped conf.toml so this example can
    // freely Set/Save without mutating the checked-in fixture.
#ifdef _WIN32
    asge::filesystem::Path const sourcePath = "C:\\Users\\ricca\\Desktop\\dev\\asge\\examples\\configuration_reader\\conf.toml";
#else
    asge::filesystem::Path const sourcePath = "/home/ricca/personal/a-simple-game-engine/examples/configuration_reader/conf.toml";
#endif
    asge::filesystem::Path const workingPath = std::filesystem::temp_directory_path() / "asge_configuration_reader_example.toml";

    auto copy_result = asge::filesystem::Copy( sourcePath, workingPath );
    if (!copy_result)
    {
        auto const err = copy_result.Error();
        LOG_ERROR("Error copying conf: ", err);
        return 1;
    }

    ConfigurationManager cm( asge::concurrent::WithCancel() );
    auto load_result = cm.Load( workingPath );
    if (!load_result)
    {
        auto const err = load_result.Error();
        LOG_ERROR("Error loading conf: ", err);
        return 1;
    }

    std::cout << "Before Set" << std::endl;
    PrintIntSetting(cm, "Window.Width");
    PrintIntSetting(cm, "Window.Height");
    PrintIntSetting(cm, "Rendering.Target_Fps");
    cm.SetHotReloadEnabled( true );

    std::this_thread::sleep_for( std::chrono::seconds(2) );

    auto set_result = cm.Set<int>("Window.Width", 1024);
    if (!set_result)
    {
        auto const err = set_result.Error();
        LOG_ERROR("Error setting Window.Width: ", err);
        return 1;
    }

    auto save_result = cm.Save();
    if (!save_result)
    {
        auto const err = save_result.Error();
        LOG_ERROR("Error saving conf: ", err);
        return 1;
    }

    std::cout << "After Set+Save" << std::endl;
    PrintIntSetting(cm, "Window.Width");
    PrintIntSetting(cm, "Window.Height");
    PrintIntSetting(cm, "Rendering.Target_Fps");

    return 0;
}

Browse the full example on GitHub →