classConfig{friendclassSingleton<Config>;public:Config(){}~Config(){}boolReadConfig(const std::string config_path){
std::cout << config_path <<"---"<< config_path.empty()<< std::endl;if(config_path.empty()){
std::cout <<"congfig file does not exist."<< std::endl;returnfalse;}
std::ifstream input(config_path);
input >> config_info_;
input.close();returntrue;}#if1// c++14 template<typenameT>typenamestd::enable_if<std::is_same<T,int>::value,bool>::type
GetConfig(std::string key_str, T& value){bool ret =false;if(config_info_.empty()|| config_info_[key_str].isNull()){LOG(ERROR)<<"get config value failure, key is :"<< key_str;returnfalse;}if(config_info_[key_str].isConvertibleTo(Json::intValue)){
value = config_info_[key_str].asInt();
ret =true;;}LOG(INFO)<<"config "<< key_str <<" is :"<< value;return ret;}template<typenameT>typenamestd::enable_if<std::is_same<T, std::string>::value,bool>::type
GetConfig(std::string key_str, T& value){bool ret =false;if(config_info_.empty()|| config_info_[key_str].isNull()){LOG(ERROR)<<"get config value failure, key is :"<< key_str;returnfalse;}if(config_info_[key_str].isConvertibleTo(Json::stringValue)){
value = config_info_[key_str].asString();
ret =true;;}LOG(INFO)<<"config "<< key_str <<" is :"<< value;return ret;}template<typenameT>typenamestd::enable_if<std::is_same<T,double>::value,bool>::type
GetConfig(std::string key_str, T& value){bool ret =false;if(config_info_.empty()|| config_info_[key_str].isNull()){LOG(ERROR)<<"get config value failure, key is :"<< key_str;returnfalse;}if(config_info_[key_str].isConvertibleTo(Json::realValue)){
value = config_info_[key_str].asDouble();
ret =true;;}LOG(INFO)<<"config "<< key_str <<" is :"<< value;return ret;}template<typenameT>typenamestd::enable_if<std::is_same<T, Json::Value>::value,bool>::type
GetConfig(std::string key_str, T& value){if(config_info_.empty()|| config_info_[key_str].isNull()){LOG(ERROR)<<"get config value failure, key is :"<< key_str;returnfalse;}
value = config_info_[key_str];LOG(INFO)<<"config "<< key_str <<" is :"<< value;returntrue;}#elsetemplate<typenameT>boolGetConfig(const std::string key_str, T& value){if(config_info_.empty()|| config_info_[key_str].isNull()){LOG(ERROR)<<"get config value failure, key is :"<< key_str;returnfalse;}// if (std::is_same<T, int>::value)// {// value = config_info_[key_str].asInt();// }// else if (std::is_same<T, std::string>::value) {// value = config_info_[key_str].asString();// }// else if (std::is_same<T, double>::value) {// value = config_info_[key_str].asDouble();// }// else if (std::is_same<T, Json::Value>::value) {// value = config_info_[key_str];// }//else {// LOG(ERROR) << "get config value failure, The parameter type is incorrect :" << typeid(T).name();// return false;//}ifconstexpr(std::is_same_v<T,int>){
value = config_info_[key_str].asInt();}elseifconstexpr(std::is_same_v<T, std::string>){
value = config_info_[key_str].asString();}elseifconstexpr(std::is_same_v<T,double>){
value = config_info_[key_str].asDouble();}elseifconstexpr(std::is_same_v<T, Json::Value>){
value = config_info_[key_str];}else{LOG(ERROR)<<"get config value failure, The parameter type is incorrect :"<<typeid(T).name();returnfalse;}LOG(INFO)<<"config "<< key_str <<" is :"<< value;returntrue;}#endifprivate:
Json::Value config_info_;};typedef Singleton<Config> Configurator;