aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.config/option.ccm
blob: 1be542e51d8d98ca922f0a1eadcd6e3b7cb9b805 (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
99
100
101
102
103
104
/*
 * This source code is released into the public domain.
 */

module;

#include <iostream>
#include <string>

#include <ucl++.h>

export module nihil.config:option;

import nihil.ucl;
import :error;

namespace nihil::config {

/*
 * Base class for options; this is what config_store interacts with.
 */

export struct option
{
	// Short name of this option.
	auto name(this option const &self) noexcept -> std::string_view
	{
		return self._name;
	}

	// Human-readable description of this option.
	auto description(this option const &self) noexcept -> std::string_view
	{
		return self._description;
	}

	// If true, this option is set to its default value.
	auto is_default(this option const &self) noexcept -> bool
	{
		return self._is_default;
	}

	// Get or set this option as a string.
	auto string(this option const &self) -> std::string
	{
		return self.get_string();
	}

	void string(this option &self, std::string_view value)
	{
		self.set_string(value);
		self._is_default = false;
	}

	/*
	 * Return this object as a UCL object.  This is used when writing the
	 * configuration file.
	 */
	virtual auto to_ucl() const -> ucl::object = 0;

	/*
	 * Set this object from a UCL object.  This is used when reading the
	 * configuration file.
	 */
	virtual auto from_ucl(ucl::object const &) -> void = 0;

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

protected:
	option(std::string_view name,
	       std::string_view description)
		: _name(name)
		, _description(description)
	{
	}

	auto is_default(bool b) -> void
	{
		_is_default = b;
	}

	/*
	 * Get or set this option as a string.
	 */
	virtual auto get_string() const -> std::string = 0;
	virtual auto set_string(std::string_view) -> void = 0;

private:
	std::string _name;
	std::string _description;
	bool _is_default = true;
};

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

} // namespace nihil