summaryrefslogtreecommitdiff
path: root/cpp/pom.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/pom.cpp')
-rw-r--r--cpp/pom.cpp32
1 files changed, 18 insertions, 14 deletions
diff --git a/cpp/pom.cpp b/cpp/pom.cpp
index a883d34..4e4f7e0 100644
--- a/cpp/pom.cpp
+++ b/cpp/pom.cpp
@@ -14,19 +14,19 @@ Configuration::~Configuration() {
Configuration::Configuration(): C(nullptr) {}
-Configuration::Configuration(void *c): C(c) {}
-
Error::~Error() { if (m_is_original) free(C); }
Error::Error(void *C_error): C(C_error), m_is_original(true) {
const pom_error *next = pom_error_next(static_cast<const pom_error *>(C));
- m_next = std::unique_ptr<Error>(new Error(static_cast<const void *>(next)));
+ if (next)
+ m_next = std::unique_ptr<Error>(new Error(static_cast<const void *>(next)));
}
// const_cast here to avoid creating a separate ConstError type
Error::Error(const void *C_error): C(const_cast<void *>(C_error)), m_is_original(false) {
const pom_error *next = pom_error_next(static_cast<const pom_error *>(C));
- m_next = std::unique_ptr<Error>(new Error(static_cast<const void *>(next)));
+ if (next)
+ m_next = std::unique_ptr<Error>(new Error(static_cast<const void *>(next)));
}
std::string_view Error::file() const noexcept {
@@ -89,32 +89,30 @@ static size_t readable_read(void *udata, char *buf, size_t count) {
return static_cast<Reader *>(udata)->read(buf, count);
}
-Configuration Configuration::load(std::string_view filename, Reader &source, const Settings *settings) {
+Configuration::Configuration(std::string_view filename, Reader &source, const Settings *settings) {
pom_settings C_settings = {};
if (settings) {
settings->to_C(static_cast<void *>(&C_settings));
}
pom_error *error;
std::string filename_str(filename);
- void *C_conf = pom_load(&C_settings, filename_str.c_str(), readable_read, &source, &error);
+ C = pom_load(&C_settings, filename_str.c_str(), readable_read, &source, &error);
if (error) {
throw Error(static_cast<void *>(error));
}
- return Configuration(C_conf);
}
-Configuration Configuration::load(std::string_view path, const Settings *settings) {
+Configuration::Configuration(std::string_view path, const Settings *settings) {
pom_settings C_settings = {};
if (settings) {
settings->to_C(static_cast<void *>(&C_settings));
}
pom_error *error;
std::string path_str(path);
- void *C_conf = pom_load_path(&C_settings, path_str.c_str(), &error);
+ C = pom_load_path(&C_settings, path_str.c_str(), &error);
if (error) {
throw Error(static_cast<void *>(error));
}
- return Configuration(C_conf);
}
class StringViewReader: public Reader {
@@ -129,9 +127,9 @@ private:
std::string_view string;
};
-Configuration Configuration::load(std::string_view filename, std::string_view string, const Settings *settings) {
+Configuration::Configuration(std::string_view filename, std::string_view string, const Settings *settings) {
StringViewReader reader(string);
- return load(filename, reader, settings);
+ Configuration(filename, reader, settings);
}
class IStreamReader: public Reader {
@@ -146,16 +144,22 @@ private:
};
-Configuration Configuration::load(std::string_view filename, std::istream &stream, const Settings *settings) {
+Configuration::Configuration(std::string_view filename, std::istream &stream, const Settings *settings) {
IStreamReader reader(stream);
- return load(filename, reader, settings);
+ Configuration(filename, reader, settings);
}
void Configuration::merge(const Configuration &other) {
+ if (!other.C) return;
+ if (!C) {
+ C = pom_conf_copy(static_cast<const pom_conf *>(other.C));
+ return;
+ }
pom_conf_merge(static_cast<pom_conf *>(C), static_cast<const pom_conf *>(other.C));
}
std::optional<std::string_view> Configuration::get(std::string_view key) const {
+ if (!C) return {};
std::string key_str(key);
const char *value = pom_conf_get(static_cast<const pom_conf *>(C), key_str.c_str());
if (!value)