当前位置:网站首页>Yaml parameter configuration based on singleton mode
Yaml parameter configuration based on singleton mode
2022-07-28 07:54:00 【Young Xia Linghu】
use Meyers Single case , Simply write the file parameter configuration library , Support multiple sub modules yaml File parameter configuration
config_manager.h
#include <map>
#include <string>
#include <mutex>
#include <iostream>
#include <yaml-cpp/yaml.h>
#include "../log/log.h"
class ConfigManager
{
public:
~ConfigManager();
private:
// Privatize default constructor 、 Copy construction and copy assignment are prohibited
ConfigManager();
ConfigManager(const ConfigManager &) = delete;
ConfigManager &operator=(const ConfigManager &) = delete;
// thread-safe interface.
bool Init();
// thread-safe interface.
bool Reset();
size_t NumModels() const {
return model_config_map_.size(); }
private:
bool InitInternal();
// key: model_name
std::map<std::string, std::string> model_config_map_;
std::mutex mutex_; // multi-thread init safe.
bool inited_ = false;
public:
// Local static variable singleton mode
bool GetModelConfig(const std::string &model_name, std::string &model_config);
static ConfigManager *getinstance();
};
config_manager.cpp
#include "config_manager.h"
ConfigManager::ConfigManager() {
}
ConfigManager::~ConfigManager() {
}
bool ConfigManager::Init()
{
std::lock_guard<std::mutex> lck(mutex_);
return InitInternal();
}
bool ConfigManager::InitInternal()
{
if (inited_)
{
return true;
}
model_config_map_.clear();
std::string yaml_module_path = "config/config_manager.yaml";
YAML::Node config;
try
{
config = YAML::LoadFile(yaml_module_path);
}
catch (YAML::Exception &ex)
{
AERROR << "yaml_module_path : " << yaml_module_path << " get file list error.";
}
AINFO << "config_root_path: " << yaml_module_path;
// Store the function class name and configuration parameter file path of each module in the dictionary model_config_map_ in
for (YAML::Node::iterator it = config.begin(); it != config.end(); it++)
{
model_config_map_[it->first.as<std::string>()] = it->second.as<std::string>();
}
AINFO << "finish to load ModelConfigs. NumModels: " << model_config_map_.size();
inited_ = false;
return true;
}
bool ConfigManager::GetModelConfig(const std::string &model_name, std::string &model_config)
{
if (!inited_ && !Init())
{
return false;
}
auto citer = model_config_map_.find(model_name);
if (citer == model_config_map_.end())
{
return false;
}
model_config = citer->second;
return true;
}
ConfigManager *ConfigManager::getinstance()
{
static ConfigManager obj;
return &obj;
}
边栏推荐
- .NET 6.0中使用Identity框架实现JWT身份认证与授权
- win系统添加打印机
- DNA modified osmium OS nanoparticles osnps DNA modified iridium nanoparticles irnps DNA
- Matplotlib绘图笔记基础直线、折线、曲线
- Mysql中有哪些不同的表格?
- Retryer of guava
- Tutorial (7.0) 06. Zero trust network access ztna * forticlient EMS * Fortinet network security expert NSE 5
- Analysis of collector principle
- On deep paging
- EMC中class A和class B哪个更严格?
猜你喜欢
随机推荐
【干货】32个EMC标准电路分享!
JUC原子类: CAS, Unsafe、CAS缺点、ABA问题如何解决详解
The net loss of users occurred again, and China Mobile, which lost face, launched ultra-low price packages to win users
Matplotlib绘图笔记基础直线、折线、曲线
Delete the nodes in the linked list - daily question
DNA cuinseqds near infrared CuInSe quantum dots wrapped deoxyribonucleic acid DNA
Clion debugging redis6 source code
4.1.4 why set the member variable to private
滴滴SQL面试题之打车业务问题如何分析
Summary of RFID radiation test
EMC问题的根源在哪?
【jvm优化】线上JVM调优实践
GD32使用ST的HAL库和GD官方库的一些体会
And is two numbers of S - two questions per day
任务管理器中,显示的CPU速度大于它的最大速度【主频】
xmpp 服务研究(二) prosody 创建账户
微信小程序隐藏滚动条的方法
MySQL view the memory size of a table
干货|分享一个EMC实际案例及整改过程
细说共模干扰和差模干扰









