aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/store.ccm
blob: 4d37ce0222bb81cfcca0c1bcac3c3fe32c971769 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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