-
Notifications
You must be signed in to change notification settings - Fork 1
/
conjure_enum_bitset.hpp
404 lines (346 loc) · 16.2 KB
/
conjure_enum_bitset.hpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//-----------------------------------------------------------------------------------------
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: Copyright (C) 2024 Fix8 Market Technologies Pty Ltd
// SPDX-FileType: SOURCE
//
// conjure_enum (header only)
// by David L. Dight
// see https://github.com/fix8mt/conjure_enum
//
// Lightweight header-only C++20 enum and typename reflection
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice (including the next paragraph)
// shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//----------------------------------------------------------------------------------------
// enum_bitset
//----------------------------------------------------------------------------------------
#ifndef FIX8_CONJURE_ENUM_BITSET_HPP_
#define FIX8_CONJURE_ENUM_BITSET_HPP_
//----------------------------------------------------------------------------------------
#include <bitset>
#include <cstdint>
#include <functional>
#include <stdexcept>
#if __has_include(<format>)
# include <format>
#else
#include <sstream>
#endif
#include <bit>
//-----------------------------------------------------------------------------------------
namespace FIX8 {
//-----------------------------------------------------------------------------------------
template<typename T>
concept valid_bitset_enum = valid_enum<T> and requires(T)
{
requires conjure_enum<T>::is_continuous();
requires conjure_enum<T>::get_actual_enum_min_value() == 0;
requires conjure_enum<T>::get_actual_enum_max_value() < conjure_enum<T>::count();
};
//-----------------------------------------------------------------------------------------
// bitset based on supplied enum
// Note: your enum sequence must be 0 based, continuous and the last enum value < count of enumerations
//-----------------------------------------------------------------------------------------
template<valid_bitset_enum T>
class enum_bitset
{
static constexpr auto countof { conjure_enum<T>::count() };
using U = std::conditional_t<countof <= 8, std::uint_least8_t,
std::conditional_t<countof <= 16, std::uint_least16_t,
std::conditional_t<countof <= 32, std::uint_least32_t,
std::conditional_t<countof <= 64, std::uint_least64_t, void>>>>;
static_assert(std::integral<U>, "requested bitset overflow");
static constexpr U all_bits { (std::size_t{1} << (countof & (sizeof(U) * 8 - 1))) - 1 };
static constexpr U unused_bits { sizeof(U) * 8 - countof };
template <typename R>
class _reference
{
R& _owner;
U _idx;
constexpr _reference(R& obj, U idx) noexcept : _owner(obj), _idx(idx) {}
friend class enum_bitset;
public:
constexpr _reference& operator=(bool val) noexcept
{
val ? _owner.set(_idx) : _owner.reset(_idx);
return *this;
}
constexpr _reference& operator=(const _reference& val) noexcept
{
if (this != &val)
*this = static_cast<bool>(val);
return *this;
}
constexpr operator bool() const noexcept { return _owner.test(_idx); }
};
template<T val>
static constexpr U to_underlying() noexcept { return static_cast<U>(val); } // C++23: upgrade to std::to_underlying
static constexpr U to_underlying(T val) noexcept { return static_cast<U>(val); }
U _present{};
#if __has_include(<format>)
static constexpr std::array _hexfmtarr { "{:#x}", "{:#X}", "{:x}", "{:#X}" };
#endif
static constexpr int correct_count(int val) noexcept { return val ? val - unused_bits : 0; }
public:
using enum_bitset_underlying_type = U;
using reference = _reference<enum_bitset>;
using const_reference = _reference<const enum_bitset>;
explicit constexpr enum_bitset(U bits) noexcept : _present(bits) {}
explicit constexpr enum_bitset(std::bitset<countof> from) : _present(U(from.to_ullong())) {}
constexpr enum_bitset(std::string_view from, bool anyscope=false, char sep='|', bool ignore_errors=true)
: _present(factory(from, anyscope, sep, ignore_errors)) {}
template<valid_bitset_enum... E>
requires (sizeof...(E) > 1)
constexpr enum_bitset(E... comp) noexcept : _present((0u | ... | (1 << to_underlying(comp)))) {}
template<std::integral... I>
requires (sizeof...(I) > 1)
constexpr enum_bitset(I... comp) noexcept : _present((0u | ... | (1 << comp))) {}
constexpr enum_bitset() = default;
constexpr ~enum_bitset() = default;
constexpr std::size_t count() const noexcept
{ return std::popcount(static_cast<std::make_unsigned_t<U>>(_present)); } // C++23: upgrade to std::bitset when count is constexpr
constexpr std::size_t not_count() const noexcept { return countof - count(); }
constexpr int countl_zero() const noexcept { return correct_count(std::countl_zero(_present)); }
constexpr int countl_one() const noexcept { return correct_count(std::countl_one(_present)); }
constexpr int countr_zero() const noexcept { return std::countr_zero(_present); }
constexpr int countr_one() const noexcept { return std::countr_one(_present); }
constexpr std::size_t size() const noexcept { return countof; }
constexpr unsigned long to_ulong() const
{
if (std::bit_width<U>(_present) > 32)
throw std::overflow_error("overflow");
return static_cast<unsigned long>(_present);
}
constexpr unsigned long long to_ullong() const noexcept { return static_cast<unsigned long long>(_present); }
constexpr U get_underlying() const noexcept { return _present; }
constexpr int get_underlying_bit_size() const noexcept { return 8 * sizeof(U); }
constexpr U get_bit_mask() const noexcept { return all_bits; }
constexpr U get_unused_bit_mask() const noexcept { return ((1 << get_underlying_bit_size()) - 1) ^ all_bits; }
// subscript
constexpr auto operator[](U pos) noexcept { return reference(*this, pos); }
constexpr auto operator[](U pos) const noexcept { return const_reference(*this, pos); }
constexpr auto operator[](T what) noexcept { return (*this)[to_underlying(what)]; }
constexpr auto operator[](T what) const noexcept { return (*this)[to_underlying(what)]; }
/// set
constexpr void set(U pos, bool value=true) noexcept { value ? _present |= 1 << pos : _present &= ~(1 << pos); }
constexpr void set(T what, bool value=true) noexcept { set(to_underlying(what), value); }
constexpr void set() noexcept { _present = all_bits; }
template<T what>
constexpr void set() noexcept
{
if constexpr (constexpr auto uu{to_underlying<what>()}; uu < countof)
_present |= 1 << uu;
}
template<T... comp>
requires (sizeof...(comp) > 1)
constexpr void set() noexcept { (set<comp>(),...); }
template<valid_bitset_enum... E>
requires (sizeof...(E) > 1)
constexpr void set(E... comp) noexcept { return (... | (set(comp))); }
/// flip
template<T what>
constexpr void flip() noexcept
{
if constexpr (constexpr auto uu{to_underlying<what>()}; uu < countof)
_present ^= 1 << uu;
}
constexpr void flip() noexcept { _present = ~_present & all_bits; }
constexpr void flip(U pos) noexcept { _present ^= 1 << pos; }
constexpr void flip(T what) noexcept { flip(to_underlying(what)); }
/// rotate
constexpr enum_bitset& rotl(int cnt) noexcept { set(_present << cnt | _present >> (countof * 8 - cnt)); return *this; }
constexpr enum_bitset& rotr(int cnt) noexcept { set(_present >> cnt | _present << (countof * 8 - cnt)); return *this; }
/// reset
template<T what>
constexpr void reset() noexcept
{
if constexpr (constexpr auto uu{to_underlying<what>()}; uu < countof)
_present &= ~(1 << uu);
}
constexpr void reset() noexcept { _present = 0; }
constexpr void reset(U pos) noexcept { _present &= ~(1 << pos); }
constexpr void reset(T what) noexcept { reset(to_underlying(what)); }
template<T... comp>
requires (sizeof...(comp) > 1)
constexpr void reset() noexcept { (reset<comp>(),...); }
template<std::integral... I>
constexpr void reset(I...comp) noexcept { (reset(comp),...); }
/// test
constexpr bool test(U pos) const noexcept { return _present & 1 << pos; }
constexpr bool test(T what) const noexcept { return test(to_underlying(what)); }
constexpr bool test() const noexcept { return _present; }
template<T what>
constexpr bool test() const noexcept
{
if constexpr (constexpr auto uu{to_underlying<what>()}; uu < countof)
return test(uu);
else
return false;
}
template<T... comp>
constexpr bool any_of() const noexcept { return (... || test<comp>()); }
template<std::integral... I>
constexpr bool any_of(I...comp) const noexcept { return (... || test(U(comp))); }
template<valid_bitset_enum... E>
constexpr bool any_of(E...comp) const noexcept { return (... || test(U(comp))); }
template<T... comp>
constexpr bool all_of() const noexcept { return (... && test<comp>()); }
template<std::integral... I>
constexpr bool all_of(I...comp) const noexcept { return (... && test(U(comp))); }
template<valid_bitset_enum... E>
constexpr bool all_of(E...comp) const noexcept { return (... && test(U(comp))); }
template<T... comp>
constexpr bool none_of() const noexcept { return (... && !test<comp>()); }
template<std::integral... I>
constexpr bool none_of(I...comp) const noexcept { return (... && !test(U(comp))); }
template<valid_bitset_enum... E>
constexpr bool none_of(E...comp) const noexcept { return (... && !test(U(comp))); }
constexpr bool any() const noexcept { return count(); }
constexpr bool all() const noexcept { return _present == all_bits; }
constexpr bool none() const noexcept { return !*this; }
constexpr bool has_single_bit() const noexcept { return std::has_single_bit(_present); }
/// operators
constexpr enum_bitset& operator<<=(std::size_t pos) noexcept { _present <<= pos; return *this; }
constexpr enum_bitset& operator>>=(std::size_t pos) noexcept { _present >>= pos; return *this; }
constexpr enum_bitset& operator&=(T other) noexcept { _present &= 1 << to_underlying(other); return *this; }
constexpr enum_bitset& operator|=(T other) noexcept { _present |= 1 << to_underlying(other); return *this; }
constexpr enum_bitset& operator^=(T other) noexcept { _present ^= 1 << to_underlying(other); return *this; }
constexpr enum_bitset& operator&=(U other) noexcept { _present &= other; return *this; }
constexpr enum_bitset& operator|=(U other) noexcept { _present |= other; return *this; }
constexpr enum_bitset& operator^=(U other) noexcept { _present ^= other; return *this; }
constexpr enum_bitset operator<<(int pos) const noexcept { return enum_bitset(_present << pos); }
constexpr enum_bitset operator>>(int pos) const noexcept { return enum_bitset(_present >> pos); }
constexpr enum_bitset operator&(T other) const noexcept { return enum_bitset(_present & 1 << to_underlying(other)); }
constexpr enum_bitset operator|(T other) const noexcept { return enum_bitset(_present | 1 << to_underlying(other)); }
constexpr enum_bitset operator^(T other) const noexcept { return enum_bitset(_present ^ 1 << to_underlying(other)); }
constexpr enum_bitset operator~() const noexcept { return enum_bitset(~_present & all_bits); }
constexpr operator auto() const noexcept { return std::bitset<countof>(_present); }
constexpr operator bool() const noexcept { return count(); }
/// for_each, for_each_n
template<typename Fn, typename... Args>
requires std::invocable<Fn&&, T, Args...>
[[maybe_unused]] constexpr auto for_each(Fn&& func, Args&&... args) noexcept
{
return for_each_n(static_cast<int>(countof), std::forward<Fn>(func), std::forward<Args>(args)...);
}
template<typename C, typename Fn, typename... Args> // specialisation for member function with object
requires std::invocable<Fn&&, C, T, Args...>
[[maybe_unused]] constexpr auto for_each(Fn&& func, C *obj, Args&&... args) noexcept
{
return for_each(std::bind(std::forward<Fn>(func), obj, std::placeholders::_1, std::forward<Args>(args)...));
}
template<typename Fn, typename... Args>
requires std::invocable<Fn&&, T, Args...>
[[maybe_unused]] constexpr auto for_each_n(int n, Fn&& func, Args&&... args) noexcept
{
for (int ii{}, jj{}; ii < static_cast<int>(countof) && jj < n; ++ii)
if (const auto ev{conjure_enum<T>::values[ii]}; test(ev))
std::invoke(std::forward<Fn>(func), ev, std::forward<Args>(args)...), ++jj;
return std::bind(std::forward<Fn>(func), std::placeholders::_1, std::forward<Args>(args)...);
}
template<typename C, typename Fn, typename... Args> // specialisation for member function with object
requires std::invocable<Fn&&, C, T, Args...>
[[maybe_unused]] constexpr auto for_each_n(int n, Fn&& func, C *obj, Args&&... args) noexcept
{
return for_each_n(n, std::bind(std::forward<Fn>(func), obj, std::placeholders::_1, std::forward<Args>(args)...));
}
/// create a bitset from custom separated enum string
static constexpr U factory(std::string_view src, bool anyscope, char sep, bool ignore_errors)
{
enum_bitset result;
constexpr auto trim([](std::string_view src) noexcept ->auto
{
const auto bg(src.find_first_not_of(" \t"));
return bg == std::string_view::npos ? src : src.substr(bg, src.find_last_not_of(" \t") - bg + 1);
});
auto process([anyscope,&result](std::string_view src) noexcept ->auto
{
if (anyscope && !conjure_enum<T>::has_scope(src))
src = conjure_enum<T>::add_scope(src);
if (auto ev { conjure_enum<T>::string_to_enum(src) }; ev)
{
result |= *ev;
return true;
}
return false;
});
for (std::string_view::size_type pos{}, fnd{};; pos = fnd + 1)
{
if ((fnd = src.find_first_of(sep, pos)) != std::string_view::npos)
{
if (auto srcp { trim(src.substr(pos, fnd - pos)) }; !process(trim(srcp)) && !ignore_errors)
throw std::invalid_argument(std::string(srcp).c_str());
continue;
}
if (pos < src.size())
if (auto srcp { trim(src.substr(pos, src.size() - pos)) }; !process(trim(srcp)) && !ignore_errors)
throw std::invalid_argument(std::string(srcp).c_str());
break;
}
return result._present;
}
constexpr std::string to_string(char zero='0', char one='1') const noexcept
{
return std::bitset<countof>(_present).to_string(zero, one);
}
template<bool showbase=true, bool uppercase=false>
#if __has_include(<format>)
constexpr std::string to_hex_string() const noexcept
{
return std::format(_hexfmtarr[(showbase ? 0 : 2) + (uppercase ? 1 : 0)], _present);
}
#else
std::string to_hex_string() const noexcept
{
std::ostringstream ostr;
if (showbase)
ostr << std::showbase;
if (uppercase)
ostr << std::uppercase;
ostr << std::hex << _present;
return ostr.str();
}
#endif
constexpr std::string to_hex_string() const noexcept { return to_hex_string<>(); }
friend constexpr std::ostream& operator<<(std::ostream& os, const enum_bitset& what) noexcept
{
return os << what.to_string();
}
};
template<typename T>
constexpr enum_bitset<T> operator&(const enum_bitset<T>& lh, const enum_bitset<T>& rh) noexcept
{ return lh.operator&(rh.to_ulong()); }
template<typename T>
constexpr enum_bitset<T> operator|(const enum_bitset<T>& lh, const enum_bitset<T>& rh) noexcept
{ return lh.operator|(rh.to_ulong()); }
template<typename T>
constexpr enum_bitset<T> operator^(const enum_bitset<T>& lh, const enum_bitset<T>& rh) noexcept
{ return lh.operator^(rh.to_ulong()); }
//-----------------------------------------------------------------------------------------
} // FIX8
/// std::hash specialization for enum_bitset.
template<typename T>
struct std::hash<FIX8::enum_bitset<T>>
{
std::size_t operator()(const FIX8::enum_bitset<T>& bs) const noexcept
{
return std::hash<std::size_t>()(bs.get_underlying());
}
};
#endif // FIX8_CONJURE_ENUM_BITSET_HPP_