oh_my_loam/common/config.h

36 lines
713 B
C
Raw Normal View History

2020-10-16 00:22:03 +08:00
#pragma once
#include <yaml-cpp/yaml.h>
#include <string>
#include "log.h"
2020-10-16 18:08:31 +08:00
#include "macros.h"
2020-10-16 00:22:03 +08:00
2020-10-16 18:08:31 +08:00
namespace oh_my_loam {
2020-10-16 00:22:03 +08:00
class Config {
public:
void SetConfigFile(const std::string& file) {
config_.reset(new YAML::Node);
*config_ = YAML::LoadFile(file);
}
template <typename T>
2020-10-16 18:08:31 +08:00
const T Get(const std::string& key) const {
2020-10-16 00:22:03 +08:00
AFATAL_IF(!config_) << "No config exists: please call SetConfigFile.";
return (*config_)[key].as<T>();
}
const YAML::Node& config() const {
AFATAL_IF(!config_) << "No config exists: please call SetConfigFile.";
return *config_;
}
private:
std::shared_ptr<YAML::Node> config_{nullptr};
2020-10-16 18:08:31 +08:00
DECLARE_SINGLETON(Config)
2020-10-16 00:22:03 +08:00
};
2020-10-16 18:08:31 +08:00
} // namespace oh_my_loam