aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/boolean.ccm
blob: 068dfdd87515f9b215057a6d90972f8ac65dc19e (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
/*
 * This source code is released into the public domain.
 */

module;

#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <expected>
#include <format>
#include <string>

#include <ucl.h>

export module nihil.ucl:boolean;

import :object;

namespace nihil::ucl {

export struct boolean final : object {
	using contained_type = bool;

	inline static constexpr object_type ucl_type = object_type::boolean;

	/*
	 * Create a boolean holding the value false.  Throws std::system_error
	 * on failure.
	 */
	boolean();

	/*
	 * Create a boolean holding a specific value.  Throws std::system_error
	 * on failure.
	 */
	explicit boolean(bool);

	/*
	 * Create a new boolean from a UCL object.  Throws type_mismatch
	 * on failure.
	 */
	boolean(ref_t, ::ucl_object_t const *uobj);
	boolean(noref_t, ::ucl_object_t *uobj);

	// Return this object's value.
	auto value(this boolean const &self) -> contained_type;
};

/*
 * Boolean constructors.  These return an error instead of throwing.
 */

export [[nodiscard]] auto
make_boolean(boolean::contained_type = false) -> std::expected<boolean, error>;

/*
 * Comparison operators.
 */

export auto operator== (boolean const &a, boolean const &b) -> bool;
export auto operator== (boolean const &a, boolean::contained_type b) -> bool;
export auto operator<=> (boolean const &a, boolean const &b)
	-> std::strong_ordering;
export auto operator<=> (boolean const &a, boolean::contained_type b)
	-> std::strong_ordering;

} // namespace nihil::ucl

/*
 * std::formatter for a boolean.  This provides the same format operations
 * as std::formatter<bool>.
 */
export template<>
struct std::formatter<nihil::ucl::boolean, char>
{
	std::formatter<bool> base_formatter;

	template<class ParseContext>
	constexpr ParseContext::iterator parse(ParseContext& ctx)
	{
		return base_formatter.parse(ctx);
	}

	template<class FmtContext>
	FmtContext::iterator format(nihil::ucl::boolean const &o,
				    FmtContext& ctx) const
	{
		return base_formatter.format(o.value(), ctx);
	}
};