• Stars
    star
    189
  • Rank 197,435 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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, JSON and YAML 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
  • 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 use DEFINE_SCHEMA macro to define the data structure:

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

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

Provide configuration files, using loadXML2Obj/loadJSON2Obj/loadYAML2Obj interfaces:

SomeOfPoints someOfPoints;
auto res = loadJSON2Obj(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.

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, [] {
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);

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

More Repositories

1

asyncio

asyncio is a c++20 library to write concurrent code using the async/await syntax.
C++
761
star
2

compilingTheory

My course design for compiler theory (Visualization).
C++
282
star
3

Laravel_AJAX_CRUD

A Laravel application for AJAX CRUD page.
PHP
119
star
4

Talk

A simple Java talk software.
Java
112
star
5

recipes

There is my code snippet.
C++
102
star
6

AnimalChess

Animal Fight Chess Game(斗兽棋) written in rust.
Rust
89
star
7

2017-HUAWEI-Codecraft

2017华为软件精英挑战赛,上合赛区,围墙编队
C++
88
star
8

Leetcode-Rust

My solutions for leetcode by rust lang.
Rust
69
star
9

HFUT_ChemLab

A Laravel application for HFUT Chemistry lab learning and exam system.
PHP
66
star
10

SlidePuzzle

Slide puzzle game written by Netcan. using the SDL engine and use of A* algorithm.
C++
62
star
11

HFUT_Thesis

合肥工业大学毕业设计(论文)模板
TeX
58
star
12

baike_contest

合工大宣百科竞赛平台 By Netcan
PHP
56
star
13

MyCrawler

我的爬虫合集
Python
55
star
14

HFUT_Market

A databases course design, using python.
Python
52
star
15

advanced-cpp20-programming

机工社《高级C++20编程》随书代码
C++
49
star
16

meta-list

Intuitive & Powerful C++20 consteval metaprogramming library(via value).
C++
46
star
17

ChineseChess

ChinessChess game written by rust.
Rust
23
star
18

LinAlg

实现一个线性代数库,为Python写扩展。《程序猿的数学3 线性代数》读后笔记
C++
19
star
19

NetcanOS

Netcan OS is an operation system for x86 PCs, for learning how os works.
C
14
star
20

gameOfLife

game of life using Javascript and canvas.
JavaScript
9
star
21

2018-HUAWEI-Codecraft

2018华为软件精英挑战赛。
C++
6
star
22

wordStatistics

A tool use for figuring word's frequency.
C++
5
star
23

FCEmulator

There is my graduation project, a FC Emulator.
C++
4
star
24

LeetCode

My Code for leetcode
C++
4
star
25

2017-HIKVISION-CodeChallenge

2017海康威视软件精英挑战赛
C++
3
star
26

qbittorrent-web-control

qbittorrent-web-control, developing under the guidance of AI.
TypeScript
3
star
27

Netcan_ICPC

The Project is Netcan's Programming Contest Experience
C++
3
star
28

LoveCalendar

A calendar to mark something.
Python
3
star
29

HFUT_Live

校内直播平台。
PHP
3
star
30

nano-caf

C++
2
star
31

presentation

My presentation collections for talks.
HTML
2
star
32

boost-cmake

CMake
2
star
33

netcan

self profile
Python
2
star
34

Tictactoe

A Python game, for network programming.
Python
2
star
35

netcan.github.io

My blog for something about programming.
SCSS
2
star
36

libnetcan-soft.so

Written in junior high school. on birthday 2012-05-23
C
1
star
37

TianMen_Weather

天门气象公众号开发。
Python
1
star
38

computerOrganization

计算机组成原理课设
Assembly
1
star
39

HFUT_LabReport

合肥工业大学LaTeX模板
TeX
1
star