| chigraph master Systems programming language written for beginners in LLVM

Flags.hpp
Go to the documentation of this file.
1
3 #pragma once
4
5 #ifndef CHI_SUPPORT_FLAGS_HPP
6 #define CHI_SUPPORT_FLAGS_HPP
7
8 #include <type_traits>
9
10 namespace chi {
11
25 template <typename Enum>
26 struct Flags {
27 static_assert(std::is_enum<Enum>::value, "Template argument to flags must be an enum");
28
30 Flags() = default;
31
34 Flags(Enum e) : mData{static_cast<decltype(mData)>(e)} {}
35
38 Flags(const Flags& fl) = default;
39
42 Flags(Flags&& fl) = default;
43
47 Flags& operator=(const Flags& fl) = default;
48
52 Flags& operator=(Flags&& fl) = default;
53
57 bool operator==(const Flags& rhs) const { return mData == rhs.mData; }
58
61 bool operator!() const { return mData == 0; }
62
66 explicit operator bool() const { return mData != 0; }
67
71 Flags operator|(const Flags& rhs) const { return Flags{mData | rhs.mData}; }
72
76 Flags operator&(const Flags& rhs) const { return Flags{mData & rhs.mData}; }
77
81 Flags& operator|=(const Flags& rhs) {
82 mData |= rhs.mData;
83 return *this;
84 }
85
89 Flags& operator&=(const Flags& rhs) {
90 mData &= rhs.mData;
91 return *this;
92 }
93
94 private:
95 Flags(typename std::underlying_type<Enum>::type data) : mData{data} {}
96
97 typename std::underlying_type<Enum>::type mData = 0;
98 };
99
100 } // namespace chi
101
102 #endif // CHI_SUPPORT_FLAGS_HPP
chi::Flags::operator &=
Flags & operator &=(const Flags &rhs)
Bitwise AND assignment.
Definition: Flags.hpp:89
chi::Flags::operator==
bool operator==(const Flags &rhs) const
See if one Flags object is equal to the other.
Definition: Flags.hpp:57
chi::Flags::operator|=
Flags & operator|=(const Flags &rhs)
Bitwise OR assignment.
Definition: Flags.hpp:81
chi::Flags::operator &
Flags operator &(const Flags &rhs) const
Bitwise AND.
Definition: Flags.hpp:76
chi::Flags
A template class for type-safe flags.
Definition: Flags.hpp:26
chi::Flags::operator|
Flags operator|(const Flags &rhs) const
Bitwise OR.
Definition: Flags.hpp:71
chi::Flags::Flags
Flags()=default
Default constructor.
chi
The namespace where chigraph lives.
Definition: BitcodeParser.hpp:12
chi::Flags::operator!
bool operator!() const
See if one Flags object isn&#39;t equal to the other.
Definition: Flags.hpp:61
chi::Flags::Flags
Flags(Enum e)
Constructor from an Enum type.
Definition: Flags.hpp:34
chi::Flags::operator=
Flags & operator=(const Flags &fl)=default
Copy assignment.