Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Simple C++ Config Loader Framework(Serialization & Reflection)

License

NotificationsYou must be signed in to change notification settings

netcan/config-loader

Repository files navigation

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

  • Simple interface, users need todefine data structure and provide correspondingconfiguration file, the framework uses meta-programming technology to generateload interface
  • The design conforms to the opening and closing principle, extends the data structure without modifying the framework
  • Currently supports XML, JSON and YAML format configuration files, a variety of methods can beflexibly composed
  • Lightweight, easy to integrate, less than ~1000 lines of code
  • Support nested data structure, STL container
  • Complete test cases
  • Support from native data structure to config file, stringify data structure

Future plans:

  • Provide additional C++20 version

Build & Run

Build

$ git clone --recursive https://github.com/netcan/config-loader.git$cd config-loader$ mkdir build$cd build$ cmake ..$ make -j

Run

$cd bin/$ ./config_loader_test

Quickly start

Firstly useDEFINE_SCHEMA macro to define the data structure:

// define and reflect a structDEFINE_SCHEMA(Point,// struct Point {              (double) x,//     double x;              (double) y);//     double y;// };// vector and stringDEFINE_SCHEMA(SomeOfPoints,// struct SomeOfPoints {              (std::string) name,//     std::string name;              (std::vector<Point>) points);//     std::vector<Point> points;// };

Provide configuration files, usingloadXML2Obj/loadJSON2Obj/loadYAML2Obj interfaces:

SomeOfPoints someOfPoints;auto res = loadJSON2Obj(someOfPoints, [] {returnR"(        {            "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.

SomeOfPoints someOfPoints;auto res = loadXML2Obj(someOfPoints,"configs/xml/SomeOfPoints.xml");REQUIRE(res == Result::SUCCESS);REQUIRE_THAT(someOfPoints.name, Equals("Some of points"));REQUIRE(someOfPoints.points.size() == 3);

Through a YAML configuration file.

SomeOfPoints someOfPoints;auto res = loadYAML2Obj(someOfPoints, [] {returnR"(        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);

Notice

The current framework depends on the following libraries:

  • tinyxml2, used for parsing xml configuration files
  • jsoncpp, used for parsing json configuration files
  • yamlcpp, used for parsing yaml 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 themap data structure, the tag uses the attributename as the key name.

The semantics of the current error code.

enumclassResult {    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};

About

Simple C++ Config Loader Framework(Serialization & Reflection)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp