A lightweight C++ 20 configuration system for managing settings.
-
Include the Files: Add
config.hpp
andconfig.cpp
to your project. -
Set Default Values: In
config.cpp
, initialize default values for your settings using theconfig::init()
function.void config::init() { // Add default values here config::set<bool>("test_bool", false); config::set<float>("test_float", 1.f); }
-
Access Settings: Use
config::get<type>(const char*)
to retrieve the value of a setting andconfig::set<type>(const char*, type value)
to modify it.// Example: Getting the value of a setting bool boolValue = *config::get<bool>("test_bool"); float floatValue = *config::get<float>("test_float");
-
Save and Load Configurations: Use
config::save(std::string)
andconfig::load(std::string)
to save and load configurations respectively.// Example: Saving and loading configs config::save("config_name.cfg"); config::load("config_name.cfg");
#include <iostream>
#include "config.hpp"
int main() {
// Initialize the configuration system
config::init();
// Get the path to the config folder
std::cout << "Config Folder Path: " << config::get_config_folder() << std::endl;
// List all config files in the config folder
std::cout << "Config Files:" << std::endl;
for (const auto& config_name : config::config_names)
std::cout << "- " << config_name << std::endl;
// Example: Accessing setting values
std::cout << "Test Bool Value: " << (*config::get<bool>("test_bool") ? "true" : "false") << std::endl;
std::cout << "Test Float Value: " << *config::get<float>("test_float") << std::endl;
return 0;
}