当前位置:网站首页>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;
}
边栏推荐
- 【着色器实现Negative反色效果_Shader效果第十一篇】
- Dynamic memory management knowledge points
- 整改了七次,花了半个月时间,惨痛的EMC总结
- mysql:LIKE和REGEXP操作有什么区别?
- 深度学习基础宝典---激活函数、Batch Size、归一化
- protobuf 基本语法总结
- Eventbus of guava
- 5g commercial third year: driverless "going up the mountain" and "going to the sea"
- Synthesis of dna-ag2sqds DNA modified silver sulfide Ag2S quantum dots
- RFID辐射测试小结
猜你喜欢

Tutorial (7.0) 06. Zero trust network access ztna * forticlient EMS * Fortinet network security expert NSE 5

DNA脱氧核糖核酸修饰金属铂纳米颗粒PtNPS-DNA|科研试剂

XMPP Service Research (II) prosody create account

细说共模干扰和差模干扰

每日一题——分割等和子集

非关系型数据库之Redis【redis集群详细搭建】

任务管理器中,显示的CPU速度大于它的最大速度【主频】

EMC整改思路

Industry standards and certification of common electronic products

整改了七次,花了半个月时间,惨痛的EMC总结
随机推荐
再次出现用户净流失,大失颜面的中国移动推出超低价套餐争取用户
(daily question) - the longest substring without repeated characters
Clion debugging redis6 source code
MySQL basic knowledge learning (II)
Don't be afraid of ESD static electricity. This article tells you some solutions
华为交换机拆解,学EMC基本操作
动态内存管理知识点
收藏 | 结合个人经验,我总结了这7点EMC相关知识
DNA脱氧核糖核酸修饰金属铂纳米颗粒PtNPS-DNA|科研试剂
ArcGIS JS customizes the accessor and uses the watchutils related method to view the attribute
DNA修饰贵金属纳米颗粒|DNA修饰纳米铜颗粒CuNPS-DNA|研究要点
awk从入门到入土(16)awk变量类型探讨--关于数字和string两种类型
【jvm优化】线上JVM调优实践
近红外二区AgzS量子点包裹脱氧核糖核酸DNA|DNA-AgzSQDs(齐岳)
Near infrared two region agzs quantum dots wrapped deoxyribonucleic acid dna|dna agzsqds (Qiyue)
Tensorflow uses deep learning (II)
DNA modified rhodium RH nanoparticles rhnps DNA (DNA modified noble metal nanoparticles)
微信小程序隐藏滚动条的方法
MySQL view the memory size of a table
4.1.4为什么要将成员变量设置为private