Proper implementation of global configuration

萝らか妹 提交于 2019-12-02 22:40:51

Another way to do this would be to create a singleton class.

#include <fstream>
#include <map>
#include <string>

class ConfigStore
{
public:
    static ConfigStore& get()
    {
        static ConfigStore instance;
        return instance;
    }
    void parseFile(std::ifstream& inStream);
    template<typename _T>
    _T getValue(std::string key);
private:
    ConfigStore(){};
    ConfigStore(const ConfigStore&);
    ConfigStore& operator=(const ConfigStore&);
    std::map<std::string,std::string> storedConfig;
};

Here the configuration is saved in a map, meaning as long as parseFile can read the file and getValue can parse the type there is no need to recompile the config class if you add new keys.

Usage:

std::ifstream input("somefile.txt");
ConfigStore::get().parseFile(input);
std::cout<<ConfigStore::get().getValue<std::string>(std::string("thing"))<<std::endl;

The way I used solve this is to put the variables in a separate global namespace, which is in a header file named something like config.h, then include that file everywhere.

// In config.h

#ifndef CONFIG_H
#define CONFIG_H

namespace config
{
    extern int some_config_int;
    extern std::string some_config_string;

    bool load_config_file();
}

#endif

The in a source file, you define the variable and also set them to a default value. This source file also have the code to load the variables from your configuration file.

// In config.cpp

namespace config
{
    int some_config_int = 123;
    std::string some_config_string = "foo";
}

bool config::load_config_file()
{
    // Code to load and set the configuration variables
}

Now in every source file you need the configuration variables, include config.h and access them like config::some_config_int.

However, there is no "proper" way of solving this, all ways that work are proper in my eyes.

What about creating functions that return your constants that you can specify in a .cxx file? For example:

// foo.h
const int BAR();

// foo.cxx
const int BAR() {
    return 10;
};

put only the declarations in head file and put the definitions in a cpp file. then you change the definitions in cpp file will not cause all code recompiled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!