base: Remove dubious/unused Flags functions
[gem5.git] / src / base / flags.hh
1 /*
2 * Copyright (c) 2020 Daniel R. Carvalho
3 * Copyright (c) 2008 The Hewlett-Packard Development Company
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef __BASE_FLAGS_HH__
31 #define __BASE_FLAGS_HH__
32
33 #include <type_traits>
34
35 /**
36 * Wrapper that groups a few flag bits under the same undelying container.
37 *
38 * @tparam T The type of the underlying container. Must be an unsigned integer.
39 */
40 template <typename T>
41 class Flags
42 {
43 private:
44 static_assert(std::is_unsigned<T>::value, "Flag type must be unsigned");
45
46 /** The undelying container of the flags' bits. */
47 T _flags;
48
49 public:
50 typedef T Type;
51
52 /**
53 * @ingroup api_flags
54 * @{
55 */
56
57 /**
58 * Initialize flags with a given value. If no value is provided, the
59 * flag bits are initialized cleared.
60 *
61 * @param flags The value to initialize the flags with.
62 */
63 Flags(Type flags=0) : _flags(flags) {}
64
65 operator const Type() const { return _flags; }
66
67 const Flags<T> &
68 operator=(T flags)
69 {
70 _flags = flags;
71 return *this;
72 }
73
74 /**
75 * Verifies whether any bit matching the given mask is set.
76 *
77 * @param mask The mask containing the bits to verify.
78 * @return True if any matching bit is set; false otherwise.
79 */
80 bool isSet(Type mask) const { return (_flags & mask); }
81
82 /**
83 * Verifies whether no bits matching the given mask are set.
84 *
85 * @param mask The mask containing the bits to verify.
86 * @return True if matching bits are set; false otherwise.
87 */
88 bool allSet(Type mask) const { return (_flags & mask) == mask; }
89
90 /**
91 * Verifies whether no bits matching the given mask are set.
92 *
93 * @param mask The mask containing the bits to verify.
94 * @return True if matching bits are cleared; false otherwise.
95 */
96 bool noneSet(Type mask) const { return (_flags & mask) == 0; }
97
98 /** Clear all flag's bits. */
99 void clear() { _flags = 0; }
100
101 /**
102 * Clear all flag's bits matching the given mask.
103 *
104 * @param mask The mask containing the bits to be cleared.
105 */
106 void clear(Type mask) { _flags &= ~mask; }
107
108 /**
109 * Set all flag's bits matching the given mask.
110 *
111 * @param mask The mask containing the bits to be set.
112 */
113 void set(Type mask) { _flags |= mask; }
114
115 /**
116 * Conditionally set or clear some bits of the flag, given a mask.
117 *
118 * @param mask The mask containing the bits to be modified.
119 * @param condition If true, set masked bits; otherwise, clear them.
120 */
121 void
122 set(Type mask, bool condition)
123 {
124 condition ? set(mask) : clear(mask);
125 }
126
127 /**
128 * Replace the contents of the bits matching the mask with the
129 * corresponding bits in the provided flags.
130 *
131 * This is equivalent to:
132 * flags.clear(mask); flags.set(flags & mask);
133 *
134 * @param flags Flags to extract new bits from.
135 * @param mask Mask used to determine which bits are replaced.
136 */
137 void
138 update(Type flags, Type mask)
139 {
140 _flags = (_flags & ~mask) | (flags & mask);
141 }
142 /** @} */ // end of api_flags
143 };
144
145 #endif // __BASE_FLAGS_HH__