C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
flags.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#if __has_include(<version>)
26#include <version>
27#endif
28
29#if __cpp_concepts >= 201907L
30#include <type_traits>
31#endif
32
33#if __cpp_lib_three_way_comparison >= 201907L
34#include <compare>
35#endif
36
37namespace essence {
38 template <typename T>
39#if __cpp_concepts >= 201907L
40 requires std::is_enum_v<T>
41#endif
42 class flags {
43 public:
44 constexpr flags() noexcept : value_{} {}
45
46 constexpr flags(T value) noexcept : value_{static_cast<std::underlying_type_t<T>>(value)} {} // NOLINT(*-explicit-constructor)
47
48 constexpr operator T() const noexcept { // NOLINT(*-explicit-constructor)
49 return T{value_};
50 }
51
52 constexpr bool operator!() const noexcept {
53 return value_ == 0;
54 }
55
56 constexpr explicit operator bool() const noexcept {
57 return value_ != 0;
58 }
59
60#if __cpp_lib_three_way_comparison >= 201907L
61 constexpr bool operator==(const flags&) const noexcept = default;
62 constexpr auto operator<=>(const flags&) const noexcept = default;
63#else
64 constexpr bool operator==(const flags& right) const noexcept {
65 return value_ == right.value_;
66 }
67
68 constexpr bool operator!=(const flags& right) const noexcept {
69 return value_ != right.value_;
70 }
71
72 constexpr bool operator<(const flags& right) const noexcept {
73 return value_ < right.value_;
74 }
75
76 constexpr bool operator>(const flags& right) const noexcept {
77 return value_ > right.value_;
78 }
79
80 constexpr bool operator<=(const flags& right) const noexcept {
81 return value_ <= right.value_;
82 }
83
84 constexpr bool operator>=(const flags& right) const noexcept {
85 return value_ >= right.value_;
86 }
87#endif
88
89 constexpr flags& operator&=(const flags& right) noexcept {
90 value_ &= right.value_;
91
92 return *this;
93 }
94
95 constexpr flags& operator|=(const flags& right) noexcept {
96 value_ |= right.value_;
97
98 return *this;
99 }
100
101 friend constexpr flags operator&(const flags& left, const flags& right) noexcept {
102 return T{left.value_ & right.value_};
103 }
104
105 friend constexpr flags operator|(const flags& left, const flags& right) noexcept {
106 return T{left.value_ | right.value_};
107 }
108
109 private:
110 std::underlying_type_t<T> value_;
111 };
112
113 template <typename T>
114 constexpr flags<T> operator&(T left, T right) noexcept
115#if __cpp_concepts >= 201907L
116 requires std::is_enum_v<T>
117#endif
118 {
119 return flags{left} & flags{right};
120 }
121
122 template <typename T>
123 constexpr flags<T> operator|(T left, T right) noexcept
124#if __cpp_concepts >= 201907L
125 requires std::is_enum_v<T>
126#endif
127 {
128 return flags{left} | flags{right};
129 }
130
131 template <typename T>
132#if __cpp_concepts >= 201907L
133 requires std::is_enum_v<T>
134#endif
135 flags(T) -> flags<T>;
136} // namespace essence
Definition flags.hpp:42