I have a c++ application. I need to load some config data from file.
The structure is:
Root(singleton) → ConfigManager(); LogManager(); ...;
So all managers are created in Roo’ts ctor and I can get pointers to them using method: Root::Get().GetSomeManager();
int main()
{
// Here all managers are initialized
Root::Get();
// App cycle
Root::Get().Deinitialize();
return 0;
}
ConfigManager allows me to load from file values by passed key.
The question is:
How can I store values from file in some global extra file?
I wrote file Config.hpp with code which looks like:
const int val = Root::Get().GetConfig()->GetValue("Key");
The problem is that this file is possibly could be included before the Config manager is initialized, or not?
I know this code is bad, but I don’t know how to write it better.
From what I understand you want to store config settings in memory on application startup so that you don’t need to access config file in runtime. But why have you decided to use global variables for it when you have singleton like that already?
I would store these settings as private attributes of ConfigManager and initialize them as a part of initialization of ConfigManager. And then when something like
Root::Get().GetConfig()->GetValue("Key")is called, it would return value of one of these private attributes already.