aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-02-09 19:56:05 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-02-09 19:56:05 -0500
commit699656e05be8d594e7fe2663af8ecbc46bfb96e4 (patch)
tree406c79276811769a02ed9c20d6aef1a4f2cb0ad7
parentd4f62d9d2d13e3e809a65d04cf67b58e4a38a4c8 (diff)
documentation clean up; add make_ini_config
-rw-r--r--README.md12
-rw-r--r--ini_config.hpp31
2 files changed, 21 insertions, 22 deletions
diff --git a/README.md b/README.md
index c83786d..2aae128 100644
--- a/README.md
+++ b/README.md
@@ -2,14 +2,14 @@
A single-header library that converts INI-formatted string literals to a key-value pair list at compile-time.
+Requires C++20; tested on gcc 10.1 and clang trunk. Passes `-Wall -Wextra -pedantic`.
+
## Features
* Direct accesses to values are compile-time evaluated, allowing an INI config to replace a list of macros or `constexpr` globals.
* Values can be accessed as strings, integers, or floating-point numbers.
- * Run-time support: can iterate through the key-value list or check for a key's existance, both with optional filtering by section.
-
-Requires C++20. Tested to work on gcc 10.1 and clang trunk. Passes `-Wall -Wextra -pedantic`.
+ * Run-time support includes key lookup, iteration through the key-value list, or key existance checking; all of which can be filtered by section.
-[Try it on Godbolt.](https://godbolt.org/z/WTPzE3)
+[Try it on Godbolt.](https://godbolt.org/z/Ys1o9G)
### INI format notes
* Handles single-line `key=value` pairs (extra whitespace is okay)
@@ -31,6 +31,9 @@ color = gray
lives = 9
)"_ini;
+// Or, go for a more functional look:
+//constexpr auto config = make_ini_config<R"( ... )">;
+
auto KVPcount = config.size(); // = 3
for (auto kvp : config) {} // Iterate through all KVPs
// (or use begin()/end())
@@ -48,3 +51,4 @@ config.tryget(argv[2]); // Same interface and behavior as get(
config.trycontains("color"); // Run-time evaluated to true
```
See the header file for further documentation.
+
diff --git a/ini_config.hpp b/ini_config.hpp
index e844b04..38ff9eb 100644
--- a/ini_config.hpp
+++ b/ini_config.hpp
@@ -235,18 +235,20 @@ class ini_config
}
public:
+ // Stores a key-value pair, including a section identifier
struct kvp {
const char_type *section = nullptr;
const char_type *first = nullptr;
const char_type *second = nullptr;
};
+
class iterator {
const char_type *m_pos = nullptr;
kvp m_current = {};
constexpr const auto& get_next() noexcept {
if (*m_pos == '\0') {
- // At the end
+ // Set first to nullptr to indicate that we're at the end
m_current.first = nullptr;
} else {
// Enter new section(s) if necessary
@@ -266,7 +268,7 @@ public:
using difference_type = long int;
using value_type = kvp;
- // Parameter is a location within kvp_buffer
+ // 'pos' is a location within kvp_buffer
constexpr iterator(const char_type *pos) noexcept
: m_pos(pos)
{
@@ -330,12 +332,12 @@ public:
constexpr auto begin() const noexcept {
return iterator(kvp_buffer);
}
- constexpr auto cbegin() const noexcept {
- return begin();
- }
constexpr auto end() const noexcept {
return iterator(kvp_buffer + sizeof(kvp_buffer) / sizeof(char_type) - 1);
}
+ constexpr auto cbegin() const noexcept {
+ return begin();
+ }
constexpr auto cend() const noexcept {
return end();
}
@@ -376,7 +378,7 @@ public:
};
/**
- * Creates a 'view' for the given section (use with ranged for).
+ * Creates a 'view' for the given section, for use with ranged for.
*/
constexpr auto section(const char_type *s) const noexcept {
return section_view(*this, s);
@@ -423,7 +425,8 @@ public:
}
/**
- * Attempts to get the value for the given key.
+ * tryget() calls are for run-time use when 'sec' or 'key'
+ * is not known at compile-time.
*/
auto tryget(const char_type *key) const noexcept {
for (auto kvp : *this) {
@@ -432,16 +435,10 @@ public:
}
return "";
}
- /**
- * Attempts to get the value for the given key, converted to the given type.
- */
template<typename T> requires(std::integral<T> || std::floating_point<T>)
T tryget(const char_type *key) const noexcept {
return from_string<T>(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))
@@ -449,9 +446,6 @@ public:
}
return "";
}
- /**
- * Attempts to get the value for the given key, converted to the given type.
- */
template<typename T> requires(std::integral<T> || std::floating_point<T>)
T tryget(const char_type *sec, const char_type *key) const noexcept {
return from_string<T>(tryget(sec, key));
@@ -484,7 +478,8 @@ consteval auto operator ""_ini()
// For MSVC, the below alternative seems promising, though
// MSVC v19.28 complains about running out of heap.
-//template <ini_config::string_container Input>
-//constexpr auto make_ini_config = ini_config::ini_config<Input>();
+template <ini_config::string_container Input>
+constexpr auto make_ini_config = ini_config::ini_config<Input>();
#endif // TCSULLIVAN_INI_CONFIG_HPP
+