1.10
flags.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_FLAGS_H
7#define EGT_FLAGS_H
8
14#include <egt/detail/enum.h>
15#include <egt/detail/meta.h>
16#include <egt/detail/string.h>
17#include <egt/flagsbase.h>
18#include <egt/signal.h>
19#include <iosfwd>
20#include <sstream>
21#include <string>
22#include <vector>
23
24namespace egt
25{
26inline namespace v1
27{
28
38template<class T>
39class Flags : public FlagsBase<T>
40{
41public:
42 using FlagsBase<T>::FlagsBase;
43
44 constexpr Flags() noexcept = default;
45
57 explicit Flags(const std::string& str)
58 {
59 this->from_string(str);
60 }
61
65 explicit Flags(const char* str)
66 {
67 this->from_string(str);
68 }
69
71 // cppcheck-suppress noExplicitConstructor
72 // NOLINTNEXTLINE(hicpp-explicit-conversions)
73 constexpr Flags(T flag) noexcept
74 : FlagsBase<T>(flag)
75 {
76 }
77
79 constexpr Flags(const Flags& rhs) noexcept
80 : FlagsBase<T>(rhs)
81 {
82 // signals are not copied!
83 }
84
86 // NOLINTNEXTLINE(cert-oop54-cpp)
87 Flags& operator=(const Flags& rhs)
88 {
89 if (&rhs != this)
90 {
91 const auto res = (*this != rhs);
92
93 // signals are not copied!
95
96 if (res)
98 }
99 return *this;
100 }
101
102 Flags(Flags&&) noexcept = default;
103 Flags& operator=(Flags&&) noexcept = default;
104 ~Flags() noexcept = default;
105
107 bool set(T flag) noexcept
108 {
109 const auto res = FlagsBase<T>::set(flag);
110 if (res)
112 return res;
113 }
114
116 bool set(std::initializer_list<T> flags) noexcept
117 {
118 const auto res = FlagsBase<T>::set(flags);
119 if (res)
121 return res;
122 }
123
125 bool clear(T flag) noexcept
126 {
127 const auto res = FlagsBase<T>::clear(flag);
128 if (res)
130 return res;
131 }
132
134 bool clear() noexcept
135 {
136 const auto res = FlagsBase<T>::clear();
137 if (res)
139 return res;
140 }
141
143 constexpr Flags<T> operator|(const T& flag) const noexcept
144 {
145 return {this->m_flags | static_cast<typename FlagsBase<T>::Underlying>(flag)};
146 }
147
149 constexpr Flags<T> operator&(const T& flag) const noexcept
150 {
151 return {this->m_flags& static_cast<typename FlagsBase<T>::Underlying>(flag)};
152 }
153
155 constexpr static const char FLAGS_DELIMITER = '|';
156
160 EGT_NODISCARD std::string to_string() const
161 {
162 std::ostringstream ss;
163 if (!this->empty())
164 {
165 bool first = true;
166 const auto container = this->get();
167 for (const auto& item : container)
168 {
169 if (first)
170 first = false;
171 else
172 ss << FLAGS_DELIMITER;
173 ss << detail::enum_to_string(item);
174 }
175 }
176 return ss.str();
177 }
178
184 void from_string(const std::string& str)
185 {
186 this->clear();
187 std::vector<std::string> tokens;
188 detail::tokenize(str, FLAGS_DELIMITER, tokens);
189 for (auto& flag : tokens)
190 this->set(detail::enum_from_string<T>(detail::trim(flag)));
191 }
192};
193
195enum class RuleE {};
196static_assert(detail::rule_of_5<Flags<RuleE>>(), "must fulfill rule of 5");
197
199template<class T>
200constexpr bool operator==(const Flags<T>& lhs, const Flags<T>& rhs)
201{
202 return lhs.raw() == rhs.raw();
203}
204
206template<class T>
207constexpr bool operator!=(const Flags<T>& lhs, const Flags<T>& rhs)
208{
209 return !(lhs == rhs);
210}
211
213template<class T>
214std::ostream& operator<<(std::ostream& os, const Flags<T>& flags)
215{
216 return os << flags.tostring();
217}
218
219}
220}
221
222#endif
Utility class for managing a set of flags.
Definition flagsbase.h:34
EGT_NODISCARD std::set< T > get() const
Get a std::set of all set flags.
Definition flagsbase.h:153
EGT_NODISCARD constexpr bool empty() const noexcept
Returns true if there are no flags set.
Definition flagsbase.h:136
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
Utility class for managing a set of flags with the ability to observe changes to the flags.
Definition flags.h:40
constexpr Flags(T flag) noexcept
Set a single flag.
Definition flags.h:73
Flags(Flags &&) noexcept=default
bool set(std::initializer_list< T > flags) noexcept
Set multiple flags.
Definition flags.h:116
void from_string(const std::string &str)
Convert from string.
Definition flags.h:184
constexpr Flags() noexcept=default
static constexpr const char FLAGS_DELIMITER
Delimiter used to separate flags in string representation.
Definition flags.h:155
bool clear(T flag) noexcept
Clear a single flag.
Definition flags.h:125
constexpr Flags(const Flags &rhs) noexcept
Copy constructor.
Definition flags.h:79
EGT_NODISCARD std::string to_string() const
Convert the flags to strings.
Definition flags.h:160
Signal on_change
Event signal.
Definition flags.h:51
constexpr Flags< T > operator|(const T &flag) const noexcept
Or operator.
Definition flags.h:143
constexpr Flags< T > operator&(const T &flag) const noexcept
And operator.
Definition flags.h:149
Flags & operator=(const Flags &rhs)
Assignment operator.
Definition flags.h:87
bool set(T flag) noexcept
Set a single flag.
Definition flags.h:107
Flags(const char *str)
Definition flags.h:65
bool clear() noexcept
Clear all flags.
Definition flags.h:134
Signal class used for defining a signal and dispatching events.
Definition signal.h:30
void invoke(Args... args)
Invoke all handlers with the specified args.
Definition signal.h:85
EGT_API std::string trim(const std::string &source, const std::string &t=" \t\n\r")
Trim delimiters off both sides of a std::string.
EGT_API void tokenize(const std::string &str, char delimiter, std::vector< std::string > &tokens)
Tokenize a std::string.
constexpr const char * enum_to_string(T const &e)
Convert an enum to a string.
Definition enum.h:55
constexpr bool operator!=(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:509
EGT_API std::ostream & operator<<(std::ostream &os, const Color &color)
Overloaded std::ostream insertion operator.
constexpr bool operator==(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:500
EGT framework namespace.
Definition animation.h:24