aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/option.ccm
blob: 0758c1a26bd14f8b1fca9765802ed8dae0305bf9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// This source code is released into the public domain.
export module nihil.config:option;

import nihil.std;
import nihil.error;
import nihil.ucl;

namespace nihil::config {

// Base class for options; this is what config_store interacts with.
//
// Base classes should override the four protected functions:
//
// 	get_string()
// 	set_string()
// 	get_ucl()
// 	set_ucl()
//
// Overriding any other members is not permitted.

export struct option
{
	virtual ~option();

	// Short name of this option.
	[[nodiscard]] auto name(this option const &) noexcept
		-> std::string_view;

	// Human-readable description of this option.
	[[nodiscard]] auto description(this option const &) noexcept
		-> std::string_view;

	// If true, this option is set to its default value.
	[[nodiscard]] auto is_default(this option const &) noexcept
		-> bool;

	/*
	 * Get or set this option as a string.  The specific conversion
	 * method depends on the derived option type.
	 */
	[[nodiscard]] auto string(this option const &) -> std::string;
	[[nodiscard]] auto string(this option &, std::string_view value)
		-> std::expected<void, error>;

	/*
	 * Return this object as a UCL object.  This is used when writing the
	 * configuration file.
	 */
	[[nodiscard]] auto ucl(this option const &)
		-> std::expected<ucl::object, error>;

	/*
	 * Set this object from a UCL object.  This is used when reading the
	 * configuration file.
	 */
	[[nodiscard]] auto ucl(this option &, ucl::object const &)
		-> std::expected<void, error>;

	// Not copyable or movable.
	option(option const &) = delete;
	auto operator=(option const &) -> option& = delete;

	option(option &&) = delete;
	auto operator=(option &&) -> option& = delete;

protected:
	option(std::string_view name, std::string_view description);

	auto is_default(this option &, bool) -> void;

	/*
	 * Get or set this option as a string.
	 */
	[[nodiscard]] virtual auto get_string() const
		-> std::string = 0;
	[[nodiscard]] virtual auto set_string(std::string_view)
		-> std::expected<void, error> = 0;

	/*
	 * Get or set this option as a UCL object.
	 */
	[[nodiscard]] virtual auto get_ucl() const
		-> std::expected<ucl::object, error> = 0;
	[[nodiscard]] virtual auto set_ucl(ucl::object const &)
		-> std::expected<void, error> = 0;

private:
	std::string	m_name;
	std::string	m_description;
	bool		m_is_default = true;
};

/*
 * Make options printable.  This is mostly useful for testing.
 */
export auto operator<<(std::ostream &strm, option const &opt) -> std::ostream &;

} // namespace nihil