Giter Site home page Giter Site logo

cppconfigframework's Introduction

C++ Config Framework

The purpose of this config framework is to simplify creation and loading of config files for C++ applications.

The framework can be used to create cross-platform applications, since it is using only Qt and STL libraries.

Requirements

Requirements document can be found here.

Design

Design document can be found here.

Build and deployment

For building the following requirements are needed:

  • CMake v3.5 or later
  • C++14 compliant compiler
  • Qt v5.9 or later (only Core and Test modules)
  • Cedar Framework

If the library needs to be deployed to a custom location you need to set the CMAKE_INSTALL_PREFIX variable or if you need to use a custom Qt installation and/or CedarFramework is not deployed to one of the standard locations you need to set the CMAKE_PREFIX_PATH variable.

Example:

$ cmake -DCMAKE_INSTALL_PREFIX=path/to/install/dir -DCMAKE_PREFIX_PATH="path/to/qt/dir;path/to/CedarFramework/dir" path/to/source/dir

Then build and deploy the library:

$ cmake --build . --target install

Usage

CMake Integration

To use this module in a CMake project you need to have a built and deployed CedarFramework dependency. Then build and deploy CppConfigFramework and add this to your project's CMakeLists.txt:

find_package(CppConfigFramework REQUIRED)

And link it to your target:

target_link_libraries(target_name PUBLIC CppConfigFramework::CppConfigFramework)

Creating a configuration class

Here is an example of a configuration class that has three parameters with one of them being a sub-configuration class:

#include <CppConfigFramework/ConfigLoader.hpp>

class ExampleSubConfig : public CppConfigFramework::ConfigLoader
{
public:
    ~ExampleSubConfig() override = default;

    QString name;

private:
    bool loadConfigParameters(
        const CppConfigFramework::ConfigObjectNode &config) override
    {
        return loadRequiredConfigParameter(&name, "name", config);
    }
};

class ExampleConfig : public CppConfigFramework::ConfigLoader
{
public:
    ~ExampleConfig() override = default;

    QString type;
    int count;
    ExampleSubConfig sub;

private:
    bool loadConfigParameters(
        const CppConfigFramework::ConfigObjectNode &config) override
    {
        if (!loadRequiredConfigParameter(&type, "type", config))
        {
            return false;
        }

        count = 0;
        if (!loadOptionalConfigParameter(&count, "count", config))
        {
            return false;
        }

        return sub.loadConfig("sub", config);
    }
};

Loading a configuration file

To load the configuration you need to do something like this:

#include <CppConfigFramework/ConfigReader.hpp>
#include <CppConfigFramework/ConfigNodePath.hpp>

int main()
{
    using namespace CppConfigFramework;
    
    ConfigReader configReader;
    EnvironmentVariables environmentVariables;

    auto config = configReader.read("path/to/config/file",
                                    QDir("path/to/config/dir"),
                                    ConfigNodePath::ROOT_PATH,
                                    ConfigNodePath::ROOT_PATH,
                                    {},
                                    &environmentVariables);

    ExampleConfig exampleConfig;
    exampleConfig.loadConfig(*config);
}

For this example the following configuration file will be used:

{
    "config":
    {
        "type": "test",
        "sub":
        {
            "name": "name"
        }
    }
}

Which will produce the following values:

Parameter Value
exampleConfig.type "test"
exampleConfig.count 0 (default value)
exampleConfig.sub.name "name"

cppconfigframework's People

Contributors

djurodrljaca avatar ipeperko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.