scaffold some common services
This commit is contained in:
56
common/config/ConfigService.hpp
Normal file
56
common/config/ConfigService.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libconfig.h++>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "common/config/IConfig.hpp"
|
||||
|
||||
// TODO: would be cool for the config file to include other config files so theyre a bit more compartmentalized
|
||||
// then the main file will be like a hub and would be more like each component having its own config file
|
||||
class ConfigService {
|
||||
|
||||
public:
|
||||
|
||||
ConfigService(const std::string& filePath);
|
||||
~ConfigService() = default;
|
||||
|
||||
bool loadFromFile(const std::string& filePath);
|
||||
|
||||
template<typename Config>
|
||||
bool getConfig(const std::string& type, const std::string& id, typename Config::Params* params) const {
|
||||
|
||||
Config config { params };
|
||||
|
||||
try {
|
||||
const libconfig::Setting& configs = config_.lookup(type);
|
||||
for (int index = 0; index < configs.getLength(); ++index) {
|
||||
const libconfig::Setting& configSetting = configs[index];
|
||||
|
||||
std::string configId;
|
||||
if (!configSetting.lookupValue("Id", configId) || configId != id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return config.parseConfig(configSetting);
|
||||
}
|
||||
} catch (const libconfig::SettingException& ex) {
|
||||
std::cout << "libconfig setting exception: " << ex.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::string& lastError() const;
|
||||
|
||||
private:
|
||||
|
||||
libconfig::Config config_;
|
||||
std::string lastError_;
|
||||
};
|
||||
Reference in New Issue
Block a user