2 * Copyright (c) 2020 Daniel R. Carvalho
3 * Copyright (c) 2008 The Hewlett-Packard Development Company
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.
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.
30 #ifndef __BASE_FLAGS_HH__
31 #define __BASE_FLAGS_HH__
33 #include <type_traits>
36 * Wrapper that groups a few flag bits under the same undelying container.
38 * @tparam T The type of the underlying container. Must be an unsigned integer.
44 static_assert(std::is_unsigned<T>::value, "Flag type must be unsigned");
46 /** The undelying container of the flags' bits. */
58 * Initialize flags with a given value. If no value is provided, the
59 * flag bits are initialized cleared.
61 * @param flags The value to initialize the flags with.
63 Flags(Type flags=0) : _flags(flags) {}
65 operator const Type() const { return _flags; }
75 * Verifies whether any bit matching the given mask is set.
77 * @param mask The mask containing the bits to verify.
78 * @return True if any matching bit is set; false otherwise.
80 bool isSet(Type mask) const { return (_flags & mask); }
83 * Verifies whether no bits matching the given mask are set.
85 * @param mask The mask containing the bits to verify.
86 * @return True if matching bits are set; false otherwise.
88 bool allSet(Type mask) const { return (_flags & mask) == mask; }
91 * Verifies whether no bits matching the given mask are set.
93 * @param mask The mask containing the bits to verify.
94 * @return True if matching bits are cleared; false otherwise.
96 bool noneSet(Type mask) const { return (_flags & mask) == 0; }
98 /** Clear all flag's bits. */
99 void clear() { _flags = 0; }
102 * Clear all flag's bits matching the given mask.
104 * @param mask The mask containing the bits to be cleared.
106 void clear(Type mask) { _flags &= ~mask; }
109 * Set all flag's bits matching the given mask.
111 * @param mask The mask containing the bits to be set.
113 void set(Type mask) { _flags |= mask; }
116 * Conditionally set or clear some bits of the flag, given a mask.
118 * @param mask The mask containing the bits to be modified.
119 * @param condition If true, set masked bits; otherwise, clear them.
122 set(Type mask, bool condition)
124 condition ? set(mask) : clear(mask);
128 * Replace the contents of the bits matching the mask with the
129 * corresponding bits in the provided flags.
131 * This is equivalent to:
132 * flags.clear(mask); flags.set(flags & mask);
134 * @param flags Flags to extract new bits from.
135 * @param mask Mask used to determine which bits are replaced.
138 update(Type flags, Type mask)
140 _flags = (_flags & ~mask) | (flags & mask);
142 /** @} */ // end of api_flags
145 #endif // __BASE_FLAGS_HH__