liblcf
Loading...
Searching...
No Matches
flag_set.h
Go to the documentation of this file.
1/*
2 * This file is part of liblcf. Copyright (c) 2020 liblcf authors.
3 * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4 *
5 * liblcf is Free/Libre Open Source Software, released under the MIT License.
6 * For the full copyright and license information, please view the COPYING
7 * file that was distributed with this source code.
8 */
9
10#ifndef LCF_FLAG_SET_H
11#define LCF_FLAG_SET_H
12
13#include <bitset>
14
15template <typename E, size_t N=32>
16class FlagSet {
17 public:
18 using underlying = std::bitset<N>;
19 using reference = typename underlying::reference;
20
21 constexpr FlagSet() = default;
22 FlagSet(std::initializer_list<E> ilist);
23
24 constexpr bool operator[](E tag) const { return _bits[size_t(tag)]; }
25 reference operator[](E tag) { return _bits[size_t(tag)]; }
26
27 size_t count() const { return _bits.count(); }
28 bool any() const noexcept { return _bits.any(); }
29 bool none() const noexcept { return _bits.none(); }
30
31 size_t size() const { return _bits.size(); }
32
33 FlagSet operator~() const noexcept { FlagSet o; o._bits = ~_bits; return o; }
34 FlagSet& operator&=(const FlagSet& o) noexcept { _bits &= o._bits; return *this; }
35 FlagSet& operator|=(const FlagSet& o) noexcept { _bits |= o._bits; return *this; }
36 FlagSet& operator^=(const FlagSet& o) noexcept { _bits ^= o._bits; return *this; }
37
38 template <typename EE, size_t NN>
39 friend bool operator==(const FlagSet<EE,NN>& l, const FlagSet<EE,NN>& r);
40
41 friend struct std::hash<FlagSet<E,N>>;
42 private:
43 std::bitset<N> _bits = {};
44};
45
46template <typename E, size_t N>
47inline bool operator==(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
48 return l._bits == r._bits;
49}
50
51template <typename E, size_t N>
52inline bool operator!=(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
53 return !(l == r);
54}
55
56template <typename E, size_t N>
57inline FlagSet<E,N> operator&(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
58 auto copy = l;
59 copy &= r;
60 return copy;
61}
62
63template <typename E, size_t N>
64inline FlagSet<E,N> operator|(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
65 auto copy = l;
66 copy |= r;
67 return copy;
68}
69
70template <typename E, size_t N>
71inline FlagSet<E,N> operator^(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
72 auto copy = l;
73 copy ^= r;
74 return copy;
75}
76
77template <typename E, size_t N>
78inline FlagSet<E,N>::FlagSet(std::initializer_list<E> ilist) {
79 for (auto&& tag: ilist) {
80 _bits.set(static_cast<size_t>(tag));
81 }
82}
83
84namespace std {
85template <typename E, size_t N>
86 struct hash<FlagSet<E,N>> {
87 size_t operator()(const FlagSet<E,N>& fs) {
88 return hash<typename FlagSet<E,N>::underlying>()(fs._bits);
89 }
90 };
91}
92
93
94
95#endif
FlagSet & operator^=(const FlagSet &o) noexcept
Definition: flag_set.h:36
FlagSet & operator|=(const FlagSet &o) noexcept
Definition: flag_set.h:35
bool none() const noexcept
Definition: flag_set.h:29
bool any() const noexcept
Definition: flag_set.h:28
reference operator[](E tag)
Definition: flag_set.h:25
size_t size() const
Definition: flag_set.h:31
constexpr bool operator[](E tag) const
Definition: flag_set.h:24
FlagSet & operator&=(const FlagSet &o) noexcept
Definition: flag_set.h:34
std::bitset< N > underlying
Definition: flag_set.h:18
friend bool operator==(const FlagSet< EE, NN > &l, const FlagSet< EE, NN > &r)
FlagSet operator~() const noexcept
Definition: flag_set.h:33
size_t count() const
Definition: flag_set.h:27
constexpr FlagSet()=default
typename underlying::reference reference
Definition: flag_set.h:19
std::bitset< N > _bits
Definition: flag_set.h:43
bool operator!=(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:52
FlagSet< E, N > operator&(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:57
FlagSet< E, N > operator^(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:71
bool operator==(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:47
FlagSet< E, N > operator|(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:64
STL namespace.
size_t operator()(const FlagSet< E, N > &fs)
Definition: flag_set.h:87