Circus 0.0.1-alpha
C++ Serialization Framework
Loading...
Searching...
No Matches
enum_flag.hpp
Go to the documentation of this file.
1
5
6#pragma once
7#include <cstdint>
8#include <iostream>
9
10#include "../circus_traits.hpp"
11
12namespace circus::utils {
13
23template <typename T>
24 requires(traits::Flaggable<T>)
25class enum_flag {
26 static_assert(traits::Flaggable<T>,
27 "T must be an enum with u32 or u64 underlying type");
28
29 public:
30 using enum_type = T;
31 using underlying_type = typename std::underlying_type<T>::type;
32
35
37 enum_flag() = default;
38
43 enum_flag(T initial) : flags(static_cast<underlying_type>(initial)) {};
44
51 *this |= flags;
52 return *this;
53 };
54
60 constexpr enum_flag &operator|=(T fs) {
61 flags |= static_cast<underlying_type>(fs);
62 return *this;
63 };
64
70 constexpr enum_flag &operator&=(T fs) {
71 flags &= static_cast<underlying_type>(fs);
72 return *this;
73 };
74
81 template <typename U>
82 [[nodiscard]] constexpr bool has(U e) const noexcept {
83 static_assert((traits::Flaggable<U>), "Types must be flaggable");
84 static_assert((std::is_same_v<U, enum_type>),
85 "Types must match 'this' enum type");
86 return ((flags & static_cast<underlying_type>(e)) ==
87 static_cast<underlying_type>(e));
88 }
89
96 template <typename... Args>
97 [[nodiscard]] constexpr bool has_any(Args &&...args) const noexcept {
98 return (has(std::forward<Args>(args)) || ...);
99 }
100
107 template <typename... Args>
108 [[nodiscard]] constexpr bool has_all(Args &&...args) const noexcept {
109 return (has(std::forward<Args>(args)) && ...);
110 }
111
113 constexpr bool operator==(const enum_flag<T> &other) const noexcept {
114 return this->flags == other.flags;
115 }
116
118 constexpr bool operator!=(const enum_flag<T> &other) const noexcept {
119 return this->flags != other.flags;
120 }
121
126 constexpr bool operator()() const noexcept { return flags != 0; };
127
129 ~enum_flag() = default;
130};
131
132} // namespace circus::utils
133
141template <typename E>
143constexpr E operator|(E lhs, E rhs) {
144 using U = std::underlying_type_t<E>;
145 return static_cast<E>(static_cast<U>(lhs) | static_cast<U>(rhs));
146}
147
155template <typename E>
157constexpr E operator&(E lhs, E rhs) {
158 using U = std::underlying_type_t<E>;
159 return static_cast<E>(static_cast<U>(lhs) & static_cast<U>(rhs));
160}
161
168template <typename E>
170constexpr E operator~(E val) {
171 using U = std::underlying_type_t<E>;
172 return static_cast<E>(~static_cast<U>(val));
173}
Provides compile-time traits and C++20 concepts for type introspection used throughout the Circus lib...
~enum_flag()=default
Destructor.
constexpr bool has_any(Args &&...args) const noexcept
Checks if this enum_flag contains any of the specified flags.
Definition enum_flag.hpp:97
constexpr bool operator!=(const enum_flag< T > &other) const noexcept
Inequality operator.
Definition enum_flag.hpp:118
enum_flag()=default
Default constructor initializes with no flags set.
constexpr enum_flag & operator|=(T fs)
Bitwise OR assignment operator to add flags.
Definition enum_flag.hpp:60
constexpr bool has(U e) const noexcept
Checks if this enum_flag contains all bits of a given flag.
Definition enum_flag.hpp:82
enum_flag & operator=(T flags)
Assignment operator from enum type.
Definition enum_flag.hpp:50
enum_flag(T initial)
Constructs enum_flag with an initial flag value.
Definition enum_flag.hpp:43
underlying_type flags
Definition enum_flag.hpp:34
constexpr bool operator()() const noexcept
Boolean conversion operator indicating if any flag is set.
Definition enum_flag.hpp:126
constexpr bool has_all(Args &&...args) const noexcept
Checks if this enum_flag contains all of the specified flags.
Definition enum_flag.hpp:108
constexpr enum_flag & operator&=(T fs)
Bitwise AND assignment operator to mask flags.
Definition enum_flag.hpp:70
typename std::underlying_type< CIRCUS_ERROR_TYPES >::type underlying_type
Definition enum_flag.hpp:31
constexpr bool operator==(const enum_flag< T > &other) const noexcept
Equality operator.
Definition enum_flag.hpp:113
CIRCUS_ERROR_TYPES enum_type
Definition enum_flag.hpp:30
Concept for enum types that are eligible for flagging (i.e., bitmask ops).
Definition circus_traits.hpp:127
constexpr E operator&(E lhs, E rhs)
Bitwise AND operator for enum types flagged by circus::traits::Flaggable.
Definition enum_flag.hpp:157
constexpr E operator|(E lhs, E rhs)
Bitwise OR operator for enum types flagged by circus::traits::Flaggable.
Definition enum_flag.hpp:143
constexpr E operator~(E val)
Bitwise NOT operator for enum types flagged by circus::traits::Flaggable.
Definition enum_flag.hpp:170