aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/store.ccm
diff options
context:
space:
mode:
Diffstat (limited to 'nihil.config/store.ccm')
-rw-r--r--nihil.config/store.ccm75
1 files changed, 75 insertions, 0 deletions
diff --git a/nihil.config/store.ccm b/nihil.config/store.ccm
new file mode 100644
index 0000000..4d37ce0
--- /dev/null
+++ b/nihil.config/store.ccm
@@ -0,0 +1,75 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+module;
+
+/*
+ * The configuration store. There should only be one of these.
+ */
+
+#include <coroutine>
+#include <expected>
+#include <string>
+#include <map>
+
+export module nihil.config:store;
+
+import nihil.generator;
+import :option;
+
+namespace nihil::config {
+
+struct store final {
+ /*
+ * Get the global config store.
+ */
+ [[nodiscard]] static auto get() -> store &;
+
+ /*
+ * Register a new value with the config store.
+ */
+ [[nodiscard]] auto register_option(this store &, option *object)
+ -> std::expected<void, error>;
+
+ /*
+ * Remove a value from the config store.
+ */
+ [[nodiscard]] auto unregister_option(this store &, option *object)
+ -> std::expected<void, error>;
+
+ /*
+ * Fetch an existing value in the config store.
+ */
+ [[nodiscard]] auto fetch(this store const &, std::string_view name)
+ -> std::expected<option const *, error>;
+ [[nodiscard]] auto fetch(this store &, std::string_view name)
+ -> std::expected<option *, error>;
+
+ /*
+ * Fetch all values in the configuration store.
+ */
+ [[nodiscard]] auto all(this store const &self)
+ -> nihil::generator<option const *>;
+ [[nodiscard]] auto all(this store &self)
+ -> nihil::generator<option *>;
+
+ // Not movable or copyable.
+ store(store const &) = delete;
+ store(store &&) = delete;
+ store& operator=(store const &) = delete;
+ store& operator=(store &&) = delete;
+
+private:
+ store();
+
+ std::map<std::string_view, option *> m_options;
+};
+
+/*
+ * The public API.
+ */
+export auto get_option(std::string_view option_name)
+ -> std::expected<option *, error>;
+
+} // namespace nihil::config