diff --git a/src/Common.h b/src/Common.h index 184c85e..9dc41ab 100644 --- a/src/Common.h +++ b/src/Common.h @@ -2,8 +2,10 @@ #include #include #include +#include #include #include +#include #include "Log.h" @@ -51,6 +53,14 @@ using EnumType = std::underlying_type_t; namespace Utils { + template + constexpr bool IsMapLike = false; + + template + constexpr bool IsMapLike> = true; + template + constexpr bool IsMapLike> = true; + inline std::string ToStringPrecision(double x, const char* fmt) { char buf[128]; diff --git a/src/Config.cpp b/src/Config.cpp index fa46539..f19950f 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -72,8 +72,9 @@ void ApplyProperty(bool& propertyToSet, const std::string_view& value) } } -template<> -void ApplyProperty>(std::pair& propertyToSet, const std::string_view& value) +// Why does C++ not have partial template specialization? +template +void ApplyProperty(std::pair& propertyToSet, const std::string_view& value) { // TODO: Ignore escaped delimiter (e.g. \, is the same as ,) const char delim = ','; @@ -81,7 +82,6 @@ void ApplyProperty>(std::pair>(std::pair +template, bool> = true> void AddConfigVar(const std::string& propertyName, T& propertyToSet, std::string_view line, bool& setConfig) { const char* whitespace = " \t"; @@ -130,11 +130,26 @@ void AddConfigVar(const std::string& propertyName, T& propertyToSet, std::string value = value.substr(beginValue, endValue - beginValue + 1); // Set value - ApplyProperty(propertyToSet, value); + ApplyProperty(propertyToSet, value); LOG("Set value for " << propertyName << ": " << value); + setConfig = true; } +// Specialization for std::[unordered_]map +template, bool> = true> +void AddConfigVar(const std::string& propertyName, MapLike& propertyToSet, std::string_view line, bool& setConfig) +{ + bool hasntSetProperty = !setConfig; + std::pair buf; + AddConfigVar(propertyName, buf, line, setConfig); + if (setConfig && hasntSetProperty) + { + // This was found + propertyToSet[buf.first] = buf.second; + } +} + void Config::Load() { const char* xdgConfigHome = getenv("XDG_CONFIG_HOME"); @@ -211,20 +226,8 @@ void Config::Load() AddConfigVar("Location", config.location, lineView, foundProperty); - std::pair buf; - bool hasntFoundProperty = !foundProperty; - AddConfigVar("SNIIconSize", buf, lineView, foundProperty); - if (foundProperty && hasntFoundProperty) - { - // This was found - config.sniIconSizes[buf.first] = buf.second; - } - hasntFoundProperty = !foundProperty; - AddConfigVar("SNIPaddingTop", buf, lineView, foundProperty); - if (foundProperty && hasntFoundProperty) - { - config.sniPaddingTop[buf.first] = buf.second; - } + AddConfigVar("SNIIconSize", config.sniIconSizes, lineView, foundProperty); + AddConfigVar("SNIPaddingTop", config.sniPaddingTop, lineView, foundProperty); if (foundProperty == false) {