aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.util/flagset.ccm
blob: 4c422230cbdf9c0464e38be8eff437c90f2cae1e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// This source code is released into the public domain.
export module nihil.util:flagset;

/*
 * flagset: a type-safe flags type.
 */

import nihil.std;

namespace nihil {

export template <std::integral base_type, typename Tag>
struct flagset final
{
	using underlying_type = base_type;

	/*
	 * Create an empty flags.
	 */
	flagset() noexcept = default;

	/*
	 * Copyable.
	 */
	flagset(flagset const &other) noexcept
		: m_value(other.m_value)
	{
	}

	/*
	 * Create flags from an integer mask.
	 */
	template <base_type flag>
	[[nodiscard]] static constexpr auto mask() noexcept -> flagset
	{
		return flagset(flag);
	}

	/*
	 * Create flags for a specific bit.
	 */
	template <unsigned bitnr>
	[[nodiscard]] static constexpr auto bit() noexcept -> flagset
	{
		static_assert(bitnr < std::numeric_limits<base_type>::digits);
		return flagset(static_cast<base_type>(1) << bitnr);
	}

	/*
	 * Create flags from a runtime value.
	 */
	[[nodiscard]] static auto from_int(base_type value) noexcept -> flagset
	{
		return flagset(value);
	}

	/*
	 * Assign this flagset.
	 */
	auto operator=(this flagset &lhs, flagset rhs) noexcept -> flagset &
	{
		if (&lhs != &rhs)
			lhs.m_value = rhs.m_value;
		return lhs;
	}

	/*
	 * The integer value of this flagset.
	 */
	[[nodiscard]] constexpr auto value(this flagset self) noexcept -> base_type
	{
		return self.m_value;
	}

	/*
	 * True if this flagset has any bits set.
	 */
	[[nodiscard]] explicit constexpr operator bool(this flagset self) noexcept
	{
		return self.m_value != 0;
	}

	/*
	 * Set bits.
	 */
	constexpr auto operator|=(this flagset &lhs, flagset rhs) noexcept -> flagset &
	{
		lhs.m_value |= rhs.value();
		return lhs;
	}

	/*
	 * Mask bits.
	 */
	constexpr auto operator&=(this flagset &lhs, flagset rhs) noexcept -> flagset &
	{
		lhs.m_value &= rhs.value();
		return lhs;
	}

	/*
	 * Invert bits.
	 */
	[[nodiscard]] constexpr auto operator~(this flagset self) noexcept -> flagset
	{
		return flagset(~self.m_value);
	}

	/*
	 * xor bits.
	 */
	constexpr auto operator^=(this flagset &lhs, flagset rhs) noexcept -> flagset
	{
		lhs.m_value ^= rhs.value();
		return lhs;
	}

private:
	base_type m_value = 0;

	explicit constexpr flagset(base_type mask) noexcept
		: m_value(mask)
	{
	}

	[[nodiscard]] friend auto operator|(flagset lhs, flagset rhs) noexcept -> flagset
	{
		return (lhs |= rhs);
	}

	[[nodiscard]] friend auto operator&(flagset lhs, flagset rhs) noexcept -> flagset
	{
		return (lhs &= rhs);
	}

	[[nodiscard]] friend auto operator^(flagset lhs, flagset rhs) noexcept -> flagset
	{
		return (lhs ^= rhs);
	}

	[[nodiscard]] friend auto
	operator==(flagset lhs, flagset rhs) noexcept -> bool
	{
		return lhs.value() == rhs.value();
	}

	friend auto operator<<(std::ostream &strm, flagset<base_type, Tag> flags) -> std::ostream &
	{
		std::print(strm, "{}", flags);
		return strm;
	}
};

} // namespace nihil

/*
 * Formatting for flagset.
 */
export template <std::integral base_type, typename Tag, typename Char>
struct std::formatter<nihil::flagset<base_type, Tag>, Char>
{
	using flags_t = nihil::flagset<base_type, Tag>;

	template <typename ParseContext>
	constexpr auto parse(ParseContext &ctx) -> ParseContext::iterator
	{
		return ctx.begin();
	}

	template <typename FmtContext>
	auto format(flags_t flags, FmtContext &ctx) const -> FmtContext::iterator
	{
		auto constexpr digits = std::numeric_limits<base_type>::digits;
		auto value = flags.value();
		auto it = ctx.out();
		*it++ = Char{'<'};

		auto printed_any = false;

		for (unsigned i = 0; i < digits; ++i) {
			unsigned bit = (digits - 1) - i;

			auto this_bit = static_cast<base_type>(1) << bit;
			if ((value & this_bit) == 0)
				continue;

			if (printed_any)
				*it++ = Char{','};

			if (bit > 10)
				*it++ = Char{'0'} + (bit / 10);
			*it++ = Char{'0'} + (bit % 10);

			printed_any = true;
		}

		*it++ = Char{'>'};
		return it;
	}
};