liblcf
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 
15 template <typename E, size_t N=32>
16 class 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 
46 template <typename E, size_t N>
47 inline bool operator==(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
48  return l._bits == r._bits;
49 }
50 
51 template <typename E, size_t N>
52 inline bool operator!=(const FlagSet<E,N>& l, const FlagSet<E,N>& r) {
53  return !(l == r);
54 }
55 
56 template <typename E, size_t N>
57 inline 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 
63 template <typename E, size_t N>
64 inline 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 
70 template <typename E, size_t N>
71 inline 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 
77 template <typename E, size_t N>
78 inline 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 
84 namespace std {
85 template <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::size
size_t size() const
Definition: flag_set.h:31
operator^
FlagSet< E, N > operator^(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:71
FlagSet::any
bool any() const noexcept
Definition: flag_set.h:28
operator|
FlagSet< E, N > operator|(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:64
FlagSet::operator[]
reference operator[](E tag)
Definition: flag_set.h:25
FlagSet::operator~
FlagSet operator~() const noexcept
Definition: flag_set.h:33
FlagSet::operator&=
FlagSet & operator&=(const FlagSet &o) noexcept
Definition: flag_set.h:34
FlagSet::none
bool none() const noexcept
Definition: flag_set.h:29
FlagSet::operator^=
FlagSet & operator^=(const FlagSet &o) noexcept
Definition: flag_set.h:36
FlagSet::underlying
std::bitset< N > underlying
Definition: flag_set.h:18
FlagSet::operator|=
FlagSet & operator|=(const FlagSet &o) noexcept
Definition: flag_set.h:35
FlagSet::_bits
std::bitset< N > _bits
Definition: flag_set.h:43
FlagSet::operator==
friend bool operator==(const FlagSet< EE, NN > &l, const FlagSet< EE, NN > &r)
std::hash< FlagSet< E, N > >::operator()
size_t operator()(const FlagSet< E, N > &fs)
Definition: flag_set.h:87
operator!=
bool operator!=(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:52
FlagSet::operator[]
constexpr bool operator[](E tag) const
Definition: flag_set.h:24
FlagSet
Definition: flag_set.h:16
FlagSet::reference
typename underlying::reference reference
Definition: flag_set.h:19
operator==
bool operator==(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:47
operator&
FlagSet< E, N > operator&(const FlagSet< E, N > &l, const FlagSet< E, N > &r)
Definition: flag_set.h:57
FlagSet::count
size_t count() const
Definition: flag_set.h:27
FlagSet::FlagSet
constexpr FlagSet()=default