c++ - Proper implementation of global configuration -
my goal have global constants in c++ game i'm working on (to represent graphics info , like). current implementation toss them in .h , include them everywhere. works, except every time change setting, entire code base must recompiled.
so, next idea toss them in configuration txt file , parse them in, way no code changed when settings change. parser simple enough, , put values constants, because parser code block, constants no longer global.
is there way solve this? perhaps way make them global despite being in block or way avoid recompiling when changing settings?
another way create 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 configuration saved in map, meaning long parsefile can read file , getvalue can parse type there no need recompile config class if 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;
Comments
Post a Comment