Files
gem5/src/base/flags.hh
Daniel R. Carvalho 2aa5fb0d63 base: Remove dubious/unused Flags functions
The functions isSet(), noneSet(), and allSet() assume that
all bits of the underlying container have a corresponding
flag. This is typically not true, and the likelihood of
incorrectly using these functions is high.

Fortunately these functions are not being used anywhere,
and can be safely removed.

Alternatively, a mask could be provided on construction to
determine which bits of the underlying container correspond
to a flag bit, so that the proper bits are checked in these
functions.

Change-Id: Ia7cbfd0726943506a3f04dc417e67a0b57cdbf95
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/38736
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
2021-01-19 03:27:04 +00:00

146 lines
4.6 KiB
C++

/*
* Copyright (c) 2020 Daniel R. Carvalho
* Copyright (c) 2008 The Hewlett-Packard Development Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __BASE_FLAGS_HH__
#define __BASE_FLAGS_HH__
#include <type_traits>
/**
* 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 <typename T>
class Flags
{
private:
static_assert(std::is_unsigned<T>::value, "Flag type must be unsigned");
/** The undelying container of the flags' bits. */
T _flags;
public:
typedef T Type;
/**
* @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; }
const Flags<T> &
operator=(T flags)
{
_flags = flags;
return *this;
}
/**
* 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 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 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; }
/**
* 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)
{
_flags = (_flags & ~mask) | (flags & mask);
}
/** @} */ // end of api_flags
};
#endif // __BASE_FLAGS_HH__