Improve set(T,bool), constexpr, tests, AuditMode
This commit is contained in:
parent
656e8c0b84
commit
d3672feac1
|
@ -11,14 +11,17 @@
|
|||
|
||||
namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
||||
{
|
||||
// til::enumset is a subclass of std::bitset, storing a fixed size array of
|
||||
// boolean elements, the positions in the array being identified by values
|
||||
// from a given enumerated type. By default it holds the same number of
|
||||
// bits as a size_t value.
|
||||
template<typename T>
|
||||
// til::enumset stores a fixed size array of boolean elements, the positions
|
||||
// in the array being identified by values from a given enumerated type.
|
||||
// Position N corresponds to bit 1<<N in the UnderlyingType integer.
|
||||
//
|
||||
// If you only need 32 positions for your T(ype), UnderlyingType can be set uint32_t.
|
||||
// It defaults to uintptr_t allowing you to set as many positions as a pointer has bits.
|
||||
// This class doesn't statically assert that your given position fits into UnderlyingType.
|
||||
template<typename T, typename UnderlyingType = uintptr_t>
|
||||
class enumset
|
||||
{
|
||||
using underlying_type = uintptr_t;
|
||||
static_assert(std::is_unsigned_v<UnderlyingType>);
|
||||
|
||||
public:
|
||||
// Method Description:
|
||||
|
@ -29,7 +32,9 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
{
|
||||
}
|
||||
|
||||
underlying_type bits() const noexcept
|
||||
// Method Description:
|
||||
// - Returns the underlying bit positions as a copy.
|
||||
constexpr UnderlyingType bits() const noexcept
|
||||
{
|
||||
return _data;
|
||||
}
|
||||
|
@ -38,7 +43,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
// - Returns the value of the bit at the given position.
|
||||
// Throws std::out_of_range if it is not a valid position
|
||||
// in the bitset.
|
||||
bool test(const T pos) const noexcept
|
||||
constexpr bool test(const T pos) const noexcept
|
||||
{
|
||||
const auto mask = to_underlying(pos);
|
||||
return (_data & mask) != 0;
|
||||
|
@ -46,7 +51,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
|
||||
// Method Description:
|
||||
// - Returns true if any of the bits are set to true.
|
||||
bool any() const noexcept
|
||||
constexpr bool any() const noexcept
|
||||
{
|
||||
return _data != 0;
|
||||
}
|
||||
|
@ -54,7 +59,7 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
// Method Description:
|
||||
// - Returns true if any of the bits in the given positions are true.
|
||||
TIL_ENUMSET_VARARG
|
||||
bool any(Args... positions) const noexcept
|
||||
constexpr bool any(Args... positions) const noexcept
|
||||
{
|
||||
const auto mask = to_underlying(positions...);
|
||||
return (_data & mask) != 0;
|
||||
|
@ -62,15 +67,15 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
|
||||
// Method Description:
|
||||
// - Returns true if all of the bits are set to true.
|
||||
bool all() const noexcept
|
||||
constexpr bool all() const noexcept
|
||||
{
|
||||
return _data == ~underlying_type(0);
|
||||
return _data == ~UnderlyingType{ 0 };
|
||||
}
|
||||
|
||||
// Method Description:
|
||||
// - Returns true if all of the bits in the given positions are true.
|
||||
TIL_ENUMSET_VARARG
|
||||
bool all(Args... positions) const noexcept
|
||||
constexpr bool all(Args... positions) const noexcept
|
||||
{
|
||||
const auto mask = to_underlying(positions...);
|
||||
return (_data & mask) == mask;
|
||||
|
@ -89,14 +94,11 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
// - Sets the bit in the given position to the specified value.
|
||||
constexpr enumset& set(const T pos, const bool val) noexcept
|
||||
{
|
||||
if (val)
|
||||
{
|
||||
set(pos);
|
||||
}
|
||||
else
|
||||
{
|
||||
reset(pos);
|
||||
}
|
||||
const auto mask = to_underlying(pos);
|
||||
// false == 0 --> UnderlyingType(-0) == 0b0000...
|
||||
// true == 1 --> UnderlyingType(-1) == 0b1111...
|
||||
#pragma warning(suppress : 4804) // '-': unsafe use of type 'bool' in operation
|
||||
_data = (_data & ~mask) | (-val & mask);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -120,17 +122,17 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
|
|||
|
||||
private:
|
||||
template<typename... Args>
|
||||
static constexpr underlying_type to_underlying(Args... positions) noexcept
|
||||
static constexpr UnderlyingType to_underlying(Args... positions) noexcept
|
||||
{
|
||||
return ((underlying_type{ 1 } << static_cast<underlying_type>(positions)) | ...);
|
||||
return ((UnderlyingType{ 1 } << static_cast<UnderlyingType>(positions)) | ...);
|
||||
}
|
||||
|
||||
template<>
|
||||
static constexpr underlying_type to_underlying() noexcept
|
||||
static constexpr UnderlyingType to_underlying() noexcept
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
underlying_type _data{};
|
||||
UnderlyingType _data{};
|
||||
};
|
||||
}
|
||||
|
|
|
@ -542,7 +542,7 @@ std::wstring TerminalInput::_GenerateSGRSequence(const COORD position,
|
|||
// - delta: The scroll wheel delta of the input event
|
||||
// Return value:
|
||||
// True iff the alternate buffer is active and alternate scroll mode is enabled and the event is a mouse wheel event.
|
||||
bool TerminalInput::_ShouldSendAlternateScroll(const unsigned int button, const short delta) const
|
||||
bool TerminalInput::_ShouldSendAlternateScroll(const unsigned int button, const short delta) const noexcept
|
||||
{
|
||||
return _mouseInputState.inAlternateBuffer &&
|
||||
_inputMode.test(Mode::AlternateScroll) &&
|
||||
|
@ -555,7 +555,7 @@ bool TerminalInput::_ShouldSendAlternateScroll(const unsigned int button, const
|
|||
// - delta: The scroll wheel delta of the input event
|
||||
// Return value:
|
||||
// True iff the input sequence was sent successfully.
|
||||
bool TerminalInput::_SendAlternateScroll(const short delta) const
|
||||
bool TerminalInput::_SendAlternateScroll(const short delta) const noexcept
|
||||
{
|
||||
if (delta > 0)
|
||||
{
|
||||
|
|
|
@ -250,7 +250,7 @@ const wchar_t* const CTRL_QUESTIONMARK_SEQUENCE = L"\x7F";
|
|||
const wchar_t* const CTRL_ALT_SLASH_SEQUENCE = L"\x1b\x1f";
|
||||
const wchar_t* const CTRL_ALT_QUESTIONMARK_SEQUENCE = L"\x1b\x7F";
|
||||
|
||||
void TerminalInput::SetInputMode(const Mode mode, const bool enabled)
|
||||
void TerminalInput::SetInputMode(const Mode mode, const bool enabled) noexcept
|
||||
{
|
||||
// If we're changing a tracking mode, we always clear other tracking modes first.
|
||||
// We also clear out the last saved mouse position & button.
|
||||
|
@ -271,7 +271,7 @@ void TerminalInput::SetInputMode(const Mode mode, const bool enabled)
|
|||
_inputMode.set(mode, enabled);
|
||||
}
|
||||
|
||||
bool TerminalInput::GetInputMode(const Mode mode) const
|
||||
bool TerminalInput::GetInputMode(const Mode mode) const noexcept
|
||||
{
|
||||
return _inputMode.test(mode);
|
||||
}
|
||||
|
|
|
@ -52,8 +52,8 @@ namespace Microsoft::Console::VirtualTerminal
|
|||
AlternateScroll
|
||||
};
|
||||
|
||||
void SetInputMode(const Mode mode, const bool enabled);
|
||||
bool GetInputMode(const Mode mode) const;
|
||||
void SetInputMode(const Mode mode, const bool enabled) noexcept;
|
||||
bool GetInputMode(const Mode mode) const noexcept;
|
||||
void ForceDisableWin32InputMode(const bool win32InputMode) noexcept;
|
||||
|
||||
#pragma region MouseInput
|
||||
|
@ -127,8 +127,8 @@ namespace Microsoft::Console::VirtualTerminal
|
|||
const short modifierKeyState,
|
||||
const short delta);
|
||||
|
||||
bool _ShouldSendAlternateScroll(const unsigned int button, const short delta) const;
|
||||
bool _SendAlternateScroll(const short delta) const;
|
||||
bool _ShouldSendAlternateScroll(const unsigned int button, const short delta) const noexcept;
|
||||
bool _SendAlternateScroll(const short delta) const noexcept;
|
||||
|
||||
static constexpr unsigned int s_GetPressedButton(const MouseButtonState state) noexcept;
|
||||
#pragma endregion
|
||||
|
|
|
@ -23,12 +23,12 @@ StateMachine::StateMachine(std::unique_ptr<IStateMachineEngine> engine) :
|
|||
_ActionClear();
|
||||
}
|
||||
|
||||
void StateMachine::SetParserMode(const Mode mode, const bool enabled)
|
||||
void StateMachine::SetParserMode(const Mode mode, const bool enabled) noexcept
|
||||
{
|
||||
_parserMode.set(mode, enabled);
|
||||
}
|
||||
|
||||
bool StateMachine::GetParserMode(const Mode mode) const
|
||||
bool StateMachine::GetParserMode(const Mode mode) const noexcept
|
||||
{
|
||||
return _parserMode.test(mode);
|
||||
}
|
||||
|
|
|
@ -48,8 +48,8 @@ namespace Microsoft::Console::VirtualTerminal
|
|||
Ansi,
|
||||
};
|
||||
|
||||
void SetParserMode(const Mode mode, const bool enabled);
|
||||
bool GetParserMode(const Mode mode) const;
|
||||
void SetParserMode(const Mode mode, const bool enabled) noexcept;
|
||||
bool GetParserMode(const Mode mode) const noexcept;
|
||||
|
||||
void ProcessCharacter(const wchar_t wch);
|
||||
void ProcessString(const std::wstring_view string);
|
||||
|
|
|
@ -64,7 +64,7 @@ class EnumSetTests
|
|||
VERIFY_ARE_EQUAL(0b10100u, flags.bits());
|
||||
|
||||
Log::Comment(L"Set bit 0 to true");
|
||||
flags.set(Flags::Zero);
|
||||
flags.set(Flags::Zero, true);
|
||||
VERIFY_ARE_EQUAL(0b10101u, flags.bits());
|
||||
|
||||
Log::Comment(L"Reset bit 2 to false, leaving 0 and 4 true");
|
||||
|
@ -72,7 +72,7 @@ class EnumSetTests
|
|||
VERIFY_ARE_EQUAL(0b10001u, flags.bits());
|
||||
|
||||
Log::Comment(L"Set bit 0 to false, leaving 4 true");
|
||||
flags.reset(Flags::Zero);
|
||||
flags.set(Flags::Zero, false);
|
||||
VERIFY_ARE_EQUAL(0b10000u, flags.bits());
|
||||
|
||||
Log::Comment(L"Flip bit 4, leaving all bits false ");
|
||||
|
@ -103,11 +103,11 @@ class EnumSetTests
|
|||
til::enumset<Flags> flags{ Flags::Zero, Flags::Two, Flags::Four };
|
||||
VERIFY_ARE_EQUAL(0b10101u, flags.bits());
|
||||
|
||||
Log::Comment(L"Test bits with the any method");
|
||||
VERIFY_IS_FALSE(flags.any(Flags::One));
|
||||
VERIFY_IS_TRUE(flags.any(Flags::Two));
|
||||
VERIFY_IS_FALSE(flags.any(Flags::Three));
|
||||
VERIFY_IS_TRUE(flags.any(Flags::Four));
|
||||
Log::Comment(L"Test bits 1 through 4 with the test method");
|
||||
VERIFY_IS_FALSE(flags.test(Flags::One));
|
||||
VERIFY_IS_TRUE(flags.test(Flags::Two));
|
||||
VERIFY_IS_FALSE(flags.test(Flags::Three));
|
||||
VERIFY_IS_TRUE(flags.test(Flags::Four));
|
||||
|
||||
Log::Comment(L"Test if any bits are set");
|
||||
VERIFY_IS_TRUE(flags.any());
|
||||
|
|
Loading…
Reference in a new issue