aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/store.ccm
blob: 77b44b56bdb28806b1b3163ce79e24bf6d1ac15a (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
76
77
78
79
80
/*
 * This source code is released into the public domain.
 */

module;

/*
 * The configuration store.  There should only be one of these.
 */

#include <coroutine>
#include <string>
#include <map>

export module nihil.config:store;

import nihil;
import :error;
import :option;

namespace nihil::config {

// Exception thrown on an attempt to fetch an undefined option.
export struct unknown_option final : error {
	unknown_option(std::string_view option_name);
	auto option_name(this unknown_option const &self) -> std::string_view;

private:
	std::string _option_name;
};

struct store final {
	/*
	 * Get the global config store.
	 */
	static auto get() -> store &;

	/*
	 * Register a new value with the config store.
	 */
	auto register_option(this store &self, option *object) -> void;

	/*
	 * Remove a value from the config store.
	 */
	auto unregister_option(this store &self, option *object) -> void;

	/*
	 * Fetch an existing value in the config store.
	 */
	auto fetch(this store const &self, std::string_view name) -> option &;

	/*
	 * Fetch all values in the configuration store.
	 */
	auto all(this store const &self) -> nihil::generator<option const &>;

	// Not movable or copyable.
	store(store const &) = delete;
	store(store &&) = delete;
	store& operator=(store const &) = delete;
	store& operator=(store &&) = delete;

private:
	/*
	 * The global configuration store, created by init() and accessed via
	 * get().
	 */
	inline static store *instance = nullptr;

	std::map<std::string_view, option *> options;
	store() = default;
};

/*
 * The public API.
 */
export auto get_option(std::string_view option_name) -> option &;

} // namespace nihil::config