aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-10-04 13:40:20 +0300
committerAqua-sama <aqua@iserlohn-fortress.net>2020-10-04 13:40:20 +0300
commite5809ca41c81750dbf59c2b170328df892a69e26 (patch)
treea9e4eff906ead096f97058ba80ed6869cb0db78a
parentFix deprecated warning in meson.build (diff)
downloadsmolbote-e5809ca41c81750dbf59c2b170328df892a69e26.tar.xz
Remove outparam section in Configuration parse
-rw-r--r--lib/configuration/configuration.cpp84
-rw-r--r--lib/configuration/configuration.h4
-rw-r--r--meson.build4
3 files changed, 60 insertions, 32 deletions
diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp
index 6e479f4..66617ed 100644
--- a/lib/configuration/configuration.cpp
+++ b/lib/configuration/configuration.cpp
@@ -27,12 +27,12 @@ Configuration::Configuration()
}
}
-Configuration::Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept
+Configuration::Configuration(const std::initializer_list<std::pair<std::string, conf_value_t>> &list) noexcept
#ifndef NO_QT_SPEC
: m_homePath(QStandardPaths::writableLocation(QStandardPaths::HomeLocation).toStdString())
#endif
{
- for(const auto &i : l) {
+ for(const auto &i : list) {
insert_or_assign(i.first, i.second);
}
}
@@ -53,20 +53,36 @@ inline auto strip(std::string &value)
return value;
}
-inline auto parse(const std::string &line, std::string &section)
+enum parse_result {
+ None,
+ Pair,
+ Section,
+ Include,
+};
+
+inline auto parse(const std::string &line, const std::string &section)
{
struct {
- bool pair = false;
+ parse_result result = None;
std::string key;
std::string value;
} ret;
- if(line[0] == '#' || line.length() == 0) {
+ if(line.front() == '#' || line.length() == 0) {
+ return ret;
+ }
+
+ if(line.starts_with("@@")) {
+ if(line.starts_with("@@include ")) {
+ ret.result = parse_result::Include;
+ ret.key = line.substr(10);
+ }
return ret;
}
if(line.front() == '[' && line.back() == ']') {
- section = line.substr(1, line.length() - 2) + '/';
+ ret.result = parse_result::Section;
+ ret.key = line.substr(1, line.length() - 2) + '/';
return ret;
}
@@ -85,7 +101,7 @@ inline auto parse(const std::string &line, std::string &section)
ret.value = line.substr(pos + 1);
strip(ret.value);
- ret.pair = true;
+ ret.result = parse_result::Pair;
return ret;
}
@@ -103,30 +119,40 @@ void Configuration::read(std::basic_istream<char> &input)
std::string line, section;
while(std::getline(input, line)) {
- if(line.rfind("@@") == 0) {
- if(line.rfind("@@include ") == 0) {
- read_file(line.substr(10));
- }
- continue;
+ const auto p = parse(line, section);
+ switch(p.result) {
+ case parse_result::None:
+ break;
+ case parse_result::Section:
+ section = p.key;
+ break;
+ case parse_result::Include:
+ read_file(p.key);
+ break;
+ case parse_result::Pair:
+ setValue(p.key, p.value);
+ break;
}
+ }
+}
+
+void Configuration::setValue(const std::string &key, const std::string &value)
+{
+ if(use_global) {
+ s_conf->setValue(key, value);
+ return;
+ }
- const auto pair = parse(line, section);
- if(pair.pair) {
-
- if(this->count(pair.key) == 0) {
- // no type has been specified for this key, assuming std::string
- insert(std::make_pair(pair.key, pair.value));
- continue;
- }
-
- auto v = at(pair.key);
- if(std::holds_alternative<std::string>(v)) {
- at(pair.key) = pair.value;
- } else if(std::holds_alternative<int>(v)) {
- at(pair.key) = std::stoi(pair.value);
- } else if(std::holds_alternative<bool>(v)) {
- at(pair.key) = (pair.value == "true");
- }
+ if(this->count(key) == 0) {
+ insert(std::make_pair(key, value));
+ } else {
+ auto v = at(key);
+ if(std::holds_alternative<std::string>(v)) {
+ at(key) = value;
+ } else if(std::holds_alternative<int>(v)) {
+ at(key) = std::stoi(value);
+ } else if(std::holds_alternative<bool>(v)) {
+ at(key) = (value == "true");
}
}
}
diff --git a/lib/configuration/configuration.h b/lib/configuration/configuration.h
index c2400b6..af55f62 100644
--- a/lib/configuration/configuration.h
+++ b/lib/configuration/configuration.h
@@ -40,7 +40,7 @@ class consumable(unconsumed) Configuration : private std::unordered_map<std::str
public:
return_typestate(unconsumed) Configuration();
- return_typestate(unconsumed) Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept;
+ return_typestate(unconsumed) Configuration(const std::initializer_list<std::pair<std::string, conf_value_t>> &list) noexcept;
Configuration(const Configuration &) = delete;
Configuration &operator=(const Configuration &) = delete;
@@ -126,6 +126,8 @@ public:
return T{};
}
+ callable_when(unconsumed) void setValue(const std::string &key, const std::string &value);
+
bool make_global();
private:
diff --git a/meson.build b/meson.build
index 73963a8..d12b130 100644
--- a/meson.build
+++ b/meson.build
@@ -30,7 +30,7 @@ endif
sourceset = import('sourceset')
cxx = meson.get_compiler('cpp')
-summary({'compiler': cxx.get_id()}, section: 'Compiler')
+summary({'id': cxx.get_id()}, section: 'Compiler')
# add some specific flags
add_project_arguments(cxx.get_supported_arguments([
@@ -67,7 +67,7 @@ add_project_arguments(cxx.get_supported_arguments([
'-Wlogical-op', # logical operations being used where bitwise were probably wanted
'-Wimplicit-fallthrough',
'-Wduplicated-cond', # if/else chain has duplicated conditions
- '-Wduplicated-brances', # if/else branches have duplicated code
+ '-Wduplicated-branches', # if/else branches have duplicated code
# casts
'-Wold-style-cast',
'-Wcast-align', # potential performance problem casts