78 lines
2.5 KiB
C++
78 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "ddkit/base/IConfig.hpp"
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
#include <memory>
|
|
|
|
namespace ddkit {
|
|
namespace base {
|
|
|
|
class Config : public IConfig {
|
|
|
|
public:
|
|
// as both ctor-params are strings, make constructor-calls disambiguous:
|
|
struct Text {
|
|
explicit Text(const std::string& text)
|
|
: m_text(text) {}
|
|
std::string m_text;
|
|
};
|
|
struct Filename {
|
|
explicit Filename(const std::string& filename)
|
|
: m_filename(filename) {}
|
|
std::string m_filename;
|
|
};
|
|
|
|
class FileNotFound : public std::runtime_error {
|
|
public:
|
|
FileNotFound(const std::string& filename) : std::runtime_error("could not open file \"" + filename + "\""){}
|
|
};
|
|
|
|
Config();
|
|
Config(const nlohmann::json&);
|
|
Config(const Text& text); // call: Config config((Config::Text("{ abc = 17 }")));
|
|
Config(const Filename& filename);
|
|
Config(const Config&);
|
|
void operator=(Config const&);
|
|
|
|
// IConfig
|
|
bool hasKey(const std::string& key) const override;
|
|
std::string getStringValue(const std::string& key) const override;
|
|
int getIntValue(const std::string& key) const override;
|
|
int64_t getInt64Value(const std::string& key) const override;
|
|
bool getBoolValue(const std::string& key) const override;
|
|
|
|
// todo array into IConfig?
|
|
int getArraySize(const std::string& key) const;
|
|
|
|
// no template-getter any more (error-prone in conjunction with type-casts)
|
|
// no default-getter any more (error-prone in conjunction with typos)
|
|
// no optional-getter any more (too much tmsif-API-logic, might be useful in other place)
|
|
|
|
bool isInt64Value(const std::string& key) const; // int or int64
|
|
bool isBoolValue(const std::string& key) const;
|
|
bool isStringValue(const std::string& key) const;
|
|
bool isArrayValue(const std::string& key) const;
|
|
bool isListValue(const std::string& key) const; // has subKeys
|
|
|
|
std::vector<std::string> getSubKeys(const std::string& key) const;
|
|
|
|
Config getSubConfig(const std::string& key) const;
|
|
|
|
operator std::string() const;
|
|
|
|
private:
|
|
|
|
static nlohmann::json::json_pointer keyToJsonPointer(const std::string& key);
|
|
nlohmann::json getValue(const std::string& key) const;
|
|
bool hasKey(const nlohmann::json&, const std::string& key) const;
|
|
bool hasKey(const nlohmann::json& jsonObj, const nlohmann::json::json_pointer& jsonPtr) const;
|
|
|
|
nlohmann::json m_json;
|
|
};
|
|
|
|
} // namespace base
|
|
} // namespace ddkit
|