config-loader is a static reflection framework written in C++17 from parse configuration file to native data structure.

Overview

config-loader 中文版

config-loader is a static reflection framework written in C++17 from parse configuration file to native data structure. It has the following characteristics:

  • Simple interface, users need to define data structure and provide corresponding configuration file, the framework uses meta-programming technology to generate load interface
  • The design conforms to the opening and closing principle, extends the data structure without modifying the framework
  • Currently supports XML and JSON format configuration files, a variety of methods can be flexibly composed
  • Lightweight, easy to integrate, less than ~1000 lines of code
  • Support nested data structure, STL container
  • Complete test cases

Future plans:

  • Support Yaml configuration file
  • Support from native data structure to config file, stringify data structure
  • Enable the supported formats through CMake options
  • Provide additional C++20 version

Get started quickly

Firstly use DEFINE_STRUCT macro to define the data structure:

// define and reflect a struct
DEFINE_STRUCT(Point,                          // struct Point {
              (double) x,                     //     double x;
              (double) y);                    //     double y;
                                              // };

// vector and string
DEFINE_STRUCT(SomeOfPoints,                   // struct SomeOfPoints {
              (std::string) name,             //     std::string name;
              (std::vector<Point>) points);   //     std::vector points;
                                              // };

Provide configuration files, load on demand:

SomeOfPoints someOfPoints;
auto res = JsonLoader().load(someOfPoints, [] {
    return R"(
        {
            "name": "Some of points",
            "points":[
                {"x": 1.2, "y": 3.4 },
                {"x": 5.6, "y": 7.8 },
                {"x": 2.2, "y": 3.3 }
            ]
        }
    )";
});
REQUIRE(res == Result::SUCCESS);
REQUIRE_THAT(someOfPoints.name, Equals("Some of points"));
REQUIRE(someOfPoints.points.size() == 3);

Or, through an XML configuration file.

Some of points 1.23.4 5.67.8 2.23.3 )"; }); REQUIRE(res == Result::SUCCESS); REQUIRE_THAT(someOfPoints.name, Equals("Some of points")); REQUIRE(someOfPoints.points.size() == 3); ">
SomeOfPoints someOfPoints;
auto res = XMLLoader().load(someOfPoints, [] {
    return R"(
        
        
            Some of points
            
                1.23.4
                5.67.8
                2.23.3
            
        
    )";
});
REQUIRE(res == Result::SUCCESS);
REQUIRE_THAT(someOfPoints.name, Equals("Some of points"));
REQUIRE(someOfPoints.points.size() == 3);

Sometimes, your software system needs a unified configuration management module to manage all data structures and corresponding configuration files. At this time, you can define the manager by composing each Loader.

("/etc/configs/Rect.xml"_path), JsonLoader() // Provide config file on demand ); ">
inline Deserializer ConfigLoaderManager(
    JsonLoader<Point>("/etc/configs/Point.json"_path),
    XMLLoader("/etc/configs/Rect.xml"_path),
    JsonLoader() // Provide config file on demand
);

Similarly, use the load interface to load on demand, and ConfigLoaderManager will automatically parse it according to the configured path and the given data structure. Your IDE should be able to get all the load interfaces.

("/etc/configs/Rect.xml"_path), 85 JsonLoader() 86 ); 87 ConfigLoaderManager.l 88 load(Rect &obj)~ f [LS] 89 load(Point &obj)~ f [LS] 90 } load(SomeOfPoints &obj, GET_CONTENT &&getContent)~ f [LS] ~ load(Rect &obj, GET_CONTENT &&getContent)~ f [LS] ~ load(Point &obj, GET_CONTENT &&getContent)~ f [LS] ">
 82     Deserializer ConfigLoaderManager(
 83             JsonLoader("/etc/configs/Point.json"_path),
 84             XMLLoader("/etc/configs/Rect.xml"_path),
 85             JsonLoader()
 86     );
 87     ConfigLoaderManager.l
 88                         load(Rect &obj)~                                   f [LS]
 89                         load(Point &obj)~                                  f [LS]
 90 }                       load(SomeOfPoints &obj, GET_CONTENT &&getContent)~ f [LS]
~                           load(Rect &obj, GET_CONTENT &&getContent)~         f [LS]
~                           load(Point &obj, GET_CONTENT &&getContent)~        f [LS]

Notice

The current framework depends on the following two library:

  • tinyxml2, used for parsing xml configuration files
  • jsoncpp, used for parsing json configuration files

In the future, these libraries may be enabled through CMake options to avoid unnecessary dependencies in actual use: only using xml will only rely on the xml parsing library.

This framework requires configuration files to be provided in a standardized format. Taking XML as an example, the field name is required to correspond to the XML tag name, and the value corresponds to the text content of the XML; for the map data structure, the tag uses the attribute name as the key name.

The semantics of the current error code.

enum class Result {
    SUCCESS,              // parse successfully
    ERR_EMPTY_CONTENT,    // The parsing file is empty
    ERR_ILL_FORMED,       // Illegal parsing file
    ERR_MISSING_FIELD,    // Missing field
    ERR_EXTRACTING_FIELD, // Failed to parse the value
    ERR_TYPE,             // Type error
};
Issues
  • Support Enum

    Support Enum

    Hello!

    Thanks for your contribution to open source. I and my team are waiting for this library for a long time. We have self-made library which using RTTR for config loading but we think this have problems.

    Your library is more comfortable. It's great! But we can't find methods for using enums. Maybe can you help us?

    opened by horoshenkiy 1
Owner
Netcan
C++ Programmer
Netcan
Small configuration file parser library for C.

libConfuse Introduction Documentation Examples Build & Install Origin & References Introduction libConfuse is a configuration file parser library writ

null 410 Jun 21, 2022
Cross-platform C++ library providing a simple API to read and write INI-style configuration files

simpleini A cross-platform library that provides a simple API to read and write INI-style configuration files. It supports data files in ASCII, MBCS a

Brodie Thiesfield 703 Jun 27, 2022
Mast is setup tool for Linux Mint configuration which provides this functionalities

Mast Mint Additional Setup Tool discord what is Mast? Mast is setup tool for Linux Mint configuration which provides this functionalities

Jakub 8 Dec 25, 2021
Lua as an advanced configuration language for wayfire

wf-lua Experiment to use Lua as an advanced configuration language for wayfire. wf-lua is meant for use-cases where writing an actual wayfire plugin s

Javier Pollak 12 Feb 23, 2022
Device configuration for Redmi K30 Ultra

The Redmi K30 Ultra (codenamed "cezanne") is a high-end smartphone from Xiaomi.

Misty 5 Apr 9, 2022
CfgManipulator is a powerful tool for manipulating configuration files.

CfgManipulator is a powerful tool for manipulating configuration files

Sanya 1 Feb 3, 2022
Simple .INI file parser in C, good for embedded systems

inih (INI Not Invented Here) inih (INI Not Invented Here) is a simple .INI file parser written in C. It's only a couple of pages of code, and it was d

Ben Hoyt 1.8k Jun 27, 2022
ini file parser

Iniparser 4 I - Overview This modules offers parsing of ini files from the C level. See a complete documentation in HTML format, from this directory o

Nicolas D 795 Jun 22, 2022
A fast and easy to configure alternative to neofetch written in C and configured using Lua

lcfetch A fast and easy to configure alternative to neofetch written in C and configured using Lua (still in a very early stage)! IMPORTANT: I'm a new

Alejandro 23 Jan 18, 2022
Config and tools for config of tasmota devices from mysql database

tasmota-sql Tools for management of tasmota devices based on mysql. The tasconfig command can load config from tasmota and store in sql, or load from

RevK 3 Jan 8, 2022
Blog post on using a custom Bash builtin to parse INI config files

Writing a Bash Builtin in C to Parse INI Configs Why Not Just Parse INI Configs With Bash? Shell languages such as Bash excel at certain tasks, such a

Jesse Hathaway 14 Apr 6, 2022
ELF static analysis and injection framework that parse, manipulate and camouflage ELF files.

elfspirit elfspirit is a useful program that parse, manipulate and camouflage ELF files. It provides a variety of functions, including adding or delet

null 11 Jun 15, 2022
A compiling time static reflection framework for C++

static_reflect This is a fully compiling time static reflection lightweight framework for C++. It provides a very rich compile-time reflection functio

null 7 May 14, 2022
json file config for ESP8266 arduino framework

Description Every project I have is the same architecture and appconf is one of the components I use every time. This is an attempt at making a public

Garkusnko Nick 1 Nov 9, 2021
Meta - static reflection tools for c++. i mostly use this with entt.

meta Static reflection tools for C++. I use it with EnTT but it can work with anything. The main features the library provides are: Registering types

Nikhilesh S 8 Jan 16, 2022
BerylDB is a data structure data manager that can be used to store data as key-value entries.

BerylDB is a data structure data manager that can be used to store data as key-value entries. The server allows channel subscription and is optimized to be used as a cache repository. Supported structures include lists, sets, and keys.

BerylDB 195 Jun 24, 2022
Header-only TOML config file parser and serializer for C++17 (and later!).

toml++ homepage ✨ This README is fine, but the toml++ homepage is better. ✨ Library features Header-only Supports the latest TOML release (v1.0.0), pl

Mark Gillard 807 Jun 27, 2022
Header-only TOML config file parser and serializer for C++17 (and later!).

toml++ homepage ✨ This README is fine, but the toml++ homepage is better. ✨ Library features Header-only Supports the latest TOML release (v1.0.0), pl

Mark Gillard 805 Jun 21, 2022
Simple and fast configuration file library (written in C99)

Features Configuration file reading Supported operating systems Ubuntu MacOS Windows Build requirements C99 compiler CMake 3.10+ Cloning git clone htt

Nikita Fediuchin 3 May 26, 2022
json2cpp is compiles a json file into static constexpr data structures that can be used at compile time or runtime

json2cpp json2cpp is compiles a json file into static constexpr data structures that can be used at compile time or runtime. Features Literally 0 runt

Jason Turner 158 Jun 13, 2022
CITL's static analysis engine for native code artifacts

citl-static-analyzer Fast binary hardening analysis tooling. Building on Linux The build process varies by Linux distribution, owing to differences be

Cyber Independent Testing Lab 15 Feb 14, 2022
It's a static library that's provide a way to do hooking (intercepting software components) in native shared object from some Android Packages

ARM_hook It's a static library that's provide a way to do hooking (intercepting software components) in native shared object from some Android Package

Gabriel Correia 1 Feb 17, 2022
The official Open-Asset-Importer-Library Repository. Loads 40+ 3D-file-formats into one unified and clean data structure.

Open Asset Import Library (assimp) A library to import and export various 3d-model-formats including scene-post-processing to generate missing render

Open Asset Import Library 7.9k Jun 24, 2022
Contribute a Data Structure you coded or solve the problems given in the Description.md file in any language! Happy coding!

Pro Lang Contribute a Data Structure you coded or solve the problems given in the Description.md file (can be found in the both the folders) in any la

IEEE-IAS VIT 1 Jan 16, 2022
A PE parser written as an exercise to study the PE file structure.

Description A PE parser written as an exercise to study the PE file structure. It parses the following parts of PE32 and PE32+ files: DOS Header Rich

Ahmed Hesham 15 Jun 22, 2022
Simple and lightweight pathname parser for C. This module helps to parse dirname, basename, filename and file extension .

Path Module For C File name and extension parsing functionality are removed because it's difficult to distinguish between a hidden dir (ex: .git) and

Prajwal Chapagain 3 Feb 25, 2022
A BOF to parse the imports of a provided PE-file, optionally extracting symbols on a per-dll basis.

PE Import Enumerator BOF What is this? This is a BOF to enumerate DLL files to-be-loaded by a given PE file. Depending on the number of arguments, thi

null 74 May 15, 2022