应用程序设置存储
我有一个 C++ 应用程序。我需要从文件加载一些配置数据。
结构是:
Root(singleton) → ConfigManager(); LogManager(); ...;
因此,所有管理器都是在 Roo'ts ctor 中创建的,我可以使用方法获取指向它们的指针: Root::Get().GetSomeManager()
;
int main()
{
// Here all managers are initialized
Root::Get();
// App cycle
Root::Get().Deinitialize();
return 0;
}
ConfigManager
允许我通过传递的密钥从文件值加载。
问题是: 如何将文件中的值存储在某些全局额外文件中?
我编写了文件 Config.hpp
,代码如下:
const int val = Root::Get().GetConfig()->GetValue("Key");
问题是这个文件可能会在配置管理器初始化之前包含在内,或者不包含?
我知道这段代码很糟糕,但我不知道如何写得更好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,您希望在应用程序启动时将配置设置存储在内存中,这样您就不需要在运行时访问配置文件。但是,当您已经有了这样的单例时,为什么还要决定使用全局变量呢?
我会将这些设置存储为 ConfigManager 的私有属性,并将它们作为 ConfigManager 初始化的一部分进行初始化。然后,当调用像
Root::Get().GetConfig()->GetValue("Key")
这样的东西时,它已经返回这些私有属性之一的值。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.