From f6bc63096b216ed8d670f31306f42bac61daec68 Mon Sep 17 00:00:00 2001 From: clyne Date: Sun, 7 Feb 2021 20:36:16 -0500 Subject: [PATCH] add try...() calls for non-constant usage --- ini_config.hpp | 72 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/ini_config.hpp b/ini_config.hpp index 59d779c..e844b04 100644 --- a/ini_config.hpp +++ b/ini_config.hpp @@ -77,7 +77,7 @@ class ini_config } template - consteval static int_type from_string(const char_type *str) noexcept { + constexpr static int_type from_string(const char_type *str) noexcept { int_type ret = 0; bool neg = *str == '-'; if (neg) @@ -87,7 +87,7 @@ class ini_config return !neg ? ret : -ret; } template - consteval static float_type from_string(const char_type *str) noexcept { + constexpr static float_type from_string(const char_type *str) noexcept { float_type ret = 0; bool neg = *str == '-'; if (neg) @@ -357,10 +357,7 @@ public: */ constexpr auto end(const char_type *section) const noexcept { auto it = begin(section); - while (++it != end()) { - if (!stringmatch(it->section, section)) - break; - } + while (++it != end() && stringmatch(it->section, section)); return it; } @@ -386,8 +383,8 @@ public: } /** - * Finds and returns the value paired with the given key. - * Returns an empty string on failure. + * Returns the value for the given key as a string. + * Returns "" if key does not exist. */ consteval auto get(const char_type *key) const noexcept { for (auto kvp : *this) { @@ -397,17 +394,16 @@ public: return ""; } /** - * Finds and returns the value paired with the given key, - * converting it to the specified integral or floating-point type. - * Returns zero on failure. + * Returns the value for the given key, converted to the given type. + * Returns zero if key does not exist. */ template requires(std::integral || std::floating_point) consteval T get(const char_type *key) const noexcept { return from_string(get(key)); } /** - * Finds and returns the value paired with the given key, in the given section. - * Returns an empty string on failure. + * Returns the value for the given key in the given section. + * Returns "" on failure. */ consteval auto get(const char_type *sec, const char_type *key) const noexcept { for (auto kvp : section(sec)) { @@ -417,8 +413,8 @@ public: return ""; } /** - * Finds and returns the value paired with the given key in the given section, - * converting it to the specified integral or floating-point type. + * Returns the value for the given key in the given section, + * converting it to the specified type. * Returns zero on failure. */ template requires(std::integral || std::floating_point) @@ -426,12 +422,53 @@ public: return from_string(get(sec, key)); } + /** + * Attempts to get the value for the given key. + */ + auto tryget(const char_type *key) const noexcept { + for (auto kvp : *this) { + if (stringmatch(kvp.first, key)) + return kvp.second; + } + return ""; + } + /** + * Attempts to get the value for the given key, converted to the given type. + */ + template requires(std::integral || std::floating_point) + T tryget(const char_type *key) const noexcept { + return from_string(tryget(key)); + } + /** + * Attempts to get the value for the given key. + */ + auto tryget(const char_type *sec, const char_type *key) const noexcept { + for (auto kvp : section(sec)) { + if (stringmatch(kvp.first, key)) + return kvp.second; + } + return ""; + } + /** + * Attempts to get the value for the given key, converted to the given type. + */ + template requires(std::integral || std::floating_point) + T tryget(const char_type *sec, const char_type *key) const noexcept { + return from_string(tryget(sec, key)); + } + consteval bool contains(const char_type *key) const noexcept { return *get(key) != '\0'; } consteval bool contains(const char_type *sec, const char_type *key) const noexcept { return *get(sec, key) != '\0'; } + bool trycontains(const char_type *key) const noexcept { + return *tryget(key) != '\0'; + } + bool trycontains(const char_type *sec, const char_type *key) const noexcept { + return *tryget(sec, key) != '\0'; + } }; } // namespace ini_config @@ -445,4 +482,9 @@ consteval auto operator ""_ini() return ini_config::ini_config(); } +// For MSVC, the below alternative seems promising, though +// MSVC v19.28 complains about running out of heap. +//template +//constexpr auto make_ini_config = ini_config::ini_config(); + #endif // TCSULLIVAN_INI_CONFIG_HPP