From eb4382af0ec1e417891aac33b2febd413e0b90b5 Mon Sep 17 00:00:00 2001 From: "Daniel R. Carvalho" Date: Sat, 26 Dec 2020 11:13:02 -0300 Subject: [PATCH] base: Add documentation to flags.hh Add documentation to the Flags class. Use this opportunity to rename some arguments to make their intention clearer. Finally, the constructors have been merged using a default value of 0. Change-Id: I924b1d5c20a3e2066be64ab124ae1a5d96d4b3bf Signed-off-by: Daniel R. Carvalho Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/38735 Reviewed-by: Jason Lowe-Power Reviewed-by: Bobby R. Bruce Maintainer: Bobby R. Bruce Tested-by: kokoro --- src/base/flags.hh | 106 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 15 deletions(-) diff --git a/src/base/flags.hh b/src/base/flags.hh index 8cefd2d33..170abc544 100644 --- a/src/base/flags.hh +++ b/src/base/flags.hh @@ -1,4 +1,5 @@ /* + * Copyright (c) 2020 Daniel R. Carvalho * Copyright (c) 2008 The Hewlett-Packard Development Company * All rights reserved. * @@ -31,12 +32,18 @@ #include +/** + * Wrapper that groups a few flag bits under the same undelying container. + * + * @tparam T The type of the underlying container. Must be an unsigned integer. + */ template class Flags { private: static_assert(std::is_unsigned::value, "Flag type must be unsigned"); + /** The undelying container of the flags' bits. */ T _flags; public: @@ -46,18 +53,17 @@ class Flags * @ingroup api_flags * @{ */ - Flags() : _flags(0) {} - Flags(Type flags) : _flags(flags) {} - /** @} */ // end of api_flags /** - * @ingroup api_flags + * Initialize flags with a given value. If no value is provided, the + * flag bits are initialized cleared. + * + * @param flags The value to initialize the flags with. */ + Flags(Type flags=0) : _flags(flags) {} + operator const Type() const { return _flags; } - /** - * @ingroup api_flags - */ const Flags & operator=(T flags) { @@ -66,19 +72,89 @@ class Flags } /** - * @ingroup api_flags - * @{ + * Verifies whether any bit in the flags is set. + * + * @return True if any flag bit is set; false otherwise. */ bool isSet() const { return _flags; } - bool isSet(Type flags) const { return (_flags & flags); } + + /** + * Verifies whether any bit matching the given mask is set. + * + * @param mask The mask containing the bits to verify. + * @return True if any matching bit is set; false otherwise. + */ + bool isSet(Type mask) const { return (_flags & mask); } + + /** + * Verifies whether all bits in the flags are set. + * + * @return True if all flag bits are set; false otherwise. + */ bool allSet() const { return !(~_flags); } - bool allSet(Type flags) const { return (_flags & flags) == flags; } + + /** + * Verifies whether no bits matching the given mask are set. + * + * @param mask The mask containing the bits to verify. + * @return True if matching bits are set; false otherwise. + */ + bool allSet(Type mask) const { return (_flags & mask) == mask; } + + /** + * Verifies whether no bits in the flags are set. + * + * @return True if all flag bits are cleared; false otherwise. + */ bool noneSet() const { return _flags == 0; } - bool noneSet(Type flags) const { return (_flags & flags) == 0; } + + /** + * Verifies whether no bits matching the given mask are set. + * + * @param mask The mask containing the bits to verify. + * @return True if matching bits are cleared; false otherwise. + */ + bool noneSet(Type mask) const { return (_flags & mask) == 0; } + + /** Clear all flag's bits. */ void clear() { _flags = 0; } - void clear(Type flags) { _flags &= ~flags; } - void set(Type flags) { _flags |= flags; } - void set(Type f, bool val) { _flags = (_flags & ~f) | (val ? f : 0); } + + /** + * Clear all flag's bits matching the given mask. + * + * @param mask The mask containing the bits to be cleared. + */ + void clear(Type mask) { _flags &= ~mask; } + + /** + * Set all flag's bits matching the given mask. + * + * @param mask The mask containing the bits to be set. + */ + void set(Type mask) { _flags |= mask; } + + /** + * Conditionally set or clear some bits of the flag, given a mask. + * + * @param mask The mask containing the bits to be modified. + * @param condition If true, set masked bits; otherwise, clear them. + */ + void + set(Type mask, bool condition) + { + condition ? set(mask) : clear(mask); + } + + /** + * Replace the contents of the bits matching the mask with the + * corresponding bits in the provided flags. + * + * This is equivalent to: + * flags.clear(mask); flags.set(flags & mask); + * + * @param flags Flags to extract new bits from. + * @param mask Mask used to determine which bits are replaced. + */ void update(Type flags, Type mask) { -- 2.30.2