1.10
flagsbase.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_FLAGSBASE_H
7#define EGT_FLAGSBASE_H
8
14#include <egt/detail/meta.h>
15#include <initializer_list>
16#include <limits>
17#include <set>
18
19namespace egt
20{
21inline namespace v1
22{
23
32template <class T>
34{
35public:
36
41 using Underlying = typename std::underlying_type<T>::type;
42
43 constexpr FlagsBase() noexcept = default;
44
46 // cppcheck-suppress noExplicitConstructor
47 // NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
48 constexpr FlagsBase(const T flag) noexcept
49 : m_flags(static_cast<Underlying>(flag))
50 {}
51
53 // cppcheck-suppress noExplicitConstructor
54 constexpr FlagsBase(std::initializer_list<T> flags) noexcept
55 {
56 for (auto& flag : flags)
57 m_flags |= static_cast<Underlying>(flag);
58 }
59
64 EGT_NODISCARD constexpr bool is_set(const T flag) const noexcept
65 {
66 return m_flags & static_cast<Underlying>(flag);
67 }
68
74 EGT_NODISCARD constexpr bool is_set(std::initializer_list<T> flags) const noexcept
75 {
76 for (auto& flag : flags)
77 if (!is_set(flag))
78 return false;
79 return true;
80 }
81
87 constexpr bool set(const T flag) noexcept
88 {
89 auto state = is_set(flag);
90 if (!state)
91 m_flags |= static_cast<Underlying>(flag);
92 return !state;
93 }
94
100 constexpr bool set(std::initializer_list<T> flags) noexcept
101 {
102 bool inserted = false;
103 for (auto& flag : flags)
104 {
105 if (!is_set(flag))
106 {
107 m_flags |= static_cast<Underlying>(flag);
108 inserted = true;
109 }
110 }
111
112 return inserted;
113 }
114
120 constexpr bool clear(const T flag) noexcept
121 {
122 bool changed = false;
123 if (is_set(flag))
124 {
125 m_flags &= ~(static_cast<Underlying>(flag));
126 changed = true;
127 }
128
129 return changed;
130 }
131
136 EGT_NODISCARD constexpr bool empty() const noexcept
137 {
138 return m_flags == 0;
139 }
140
145 constexpr bool clear() noexcept
146 {
147 const bool diff = m_flags != 0;
148 m_flags = 0;
149 return diff;
150 }
151
153 EGT_NODISCARD std::set<T> get() const
154 {
155 std::set<T> result;
156 const auto bits = std::numeric_limits<Underlying>::digits;
157 for (auto b = 0; b < bits; b++)
158 {
159 if ((m_flags & (1u << b)))
160 result.insert(static_cast<T>(1u << b));
161 }
162
163 return result;
164 }
165
167 EGT_NODISCARD constexpr const Underlying& raw() const
168 {
169 return m_flags;
170 }
171
173 constexpr Underlying& raw()
174 {
175 return m_flags;
176 }
177
178protected:
179
181 // cppcheck-suppress noExplicitConstructor
182 // NOLINTNEXTLINE(hicpp-explicit-conversions)
183 constexpr FlagsBase(const Underlying flags) noexcept
184 : m_flags(flags)
185 {}
186
189};
190
191}
192}
193
194#endif
Utility class for managing a set of flags.
Definition flagsbase.h:34
EGT_NODISCARD constexpr bool is_set(std::initializer_list< T > flags) const noexcept
Test if the specified flags are set.
Definition flagsbase.h:74
EGT_NODISCARD std::set< T > get() const
Get a std::set of all set flags.
Definition flagsbase.h:153
constexpr FlagsBase(std::initializer_list< T > flags) noexcept
Construct with an initializer_list of flags.
Definition flagsbase.h:54
EGT_NODISCARD constexpr bool empty() const noexcept
Returns true if there are no flags set.
Definition flagsbase.h:136
constexpr Underlying & raw()
Get the raw underlying value.
Definition flagsbase.h:173
EGT_NODISCARD constexpr const Underlying & raw() const
Get the raw underlying value.
Definition flagsbase.h:167
Underlying m_flags
The flags.
Definition flagsbase.h:188
constexpr bool set(const T flag) noexcept
Set the specified flag.
Definition flagsbase.h:87
constexpr FlagsBase() noexcept=default
typename std::underlying_type< T >::type Underlying
This is the underlying type of the flags, which is used internally for efficient bitwise operation on...
Definition flagsbase.h:41
constexpr bool clear() noexcept
Unset all flags.
Definition flagsbase.h:145
constexpr bool clear(const T flag) noexcept
Clear, or unset, the specified flag.
Definition flagsbase.h:120
EGT_NODISCARD constexpr bool is_set(const T flag) const noexcept
Test if the specified flag is set.
Definition flagsbase.h:64
constexpr bool set(std::initializer_list< T > flags) noexcept
Set the specified flags.
Definition flagsbase.h:100
EGT framework namespace.
Definition animation.h:24