Make InputDeviceId's constructor constexpr. (#5433)

* Make InputDeviceId's constructor constexpr.

See also https://github.com/o3de/o3de/pull/4220

Signed-off-by: bosnichd <bosnichd@amazon.com>

* Updates based on review feedback.

Signed-off-by: bosnichd <bosnichd@amazon.com>
monroegm-disable-blank-issue-2
bosnichd 4 years ago committed by GitHub
parent 354802d387
commit eb775a48dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,28 +27,4 @@ namespace AzFramework
;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
const char* InputChannelId::GetName() const
{
return m_name.c_str();
}
////////////////////////////////////////////////////////////////////////////////////////////////
const AZ::Crc32& InputChannelId::GetNameCrc32() const
{
return m_crc32;
}
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputChannelId::operator==(const InputChannelId& other) const
{
return (m_crc32 == other.m_crc32);
}
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputChannelId::operator!=(const InputChannelId& other) const
{
return !(*this == other);
}
} // namespace AzFramework

@ -39,53 +39,58 @@ namespace AzFramework
////////////////////////////////////////////////////////////////////////////////////////////
//! Constructor
//! \param[in] name Name of the input channel (will be ignored if exceeds MAX_NAME_LENGTH)
//! \param[in] name Name of the input channel (will be truncated if exceeds MAX_NAME_LENGTH)
explicit constexpr InputChannelId(AZStd::string_view name = "")
: m_name(name)
, m_crc32(name)
: m_name(name.substr(0, MAX_NAME_LENGTH))
, m_crc32(name.substr(0, MAX_NAME_LENGTH))
{
}
constexpr InputChannelId(const InputChannelId& other) = default;
constexpr InputChannelId(InputChannelId&& other) = default;
constexpr InputChannelId& operator=(const InputChannelId& other)
{
m_name = other.m_name;
m_crc32 = other.m_crc32;
return *this;
}
constexpr InputChannelId& operator=(InputChannelId&& other)
{
m_name = AZStd::move(other.m_name);
m_crc32 = AZStd::move(other.m_crc32);
other.m_crc32 = 0;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////
// Default copying and moving
AZ_DEFAULT_COPY_MOVE(InputChannelId);
////////////////////////////////////////////////////////////////////////////////////////////
//! Default destructor
~InputChannelId() = default;
////////////////////////////////////////////////////////////////////////////////////////////
//! Access to the input channel's name
//! \return Name of the input channel
const char* GetName() const;
constexpr const char* GetName() const
{
return m_name.c_str();
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Access to the crc32 of the input channel's name
//! \return crc32 of the input channel name
const AZ::Crc32& GetNameCrc32() const;
constexpr const AZ::Crc32& GetNameCrc32() const
{
return m_crc32;
}
////////////////////////////////////////////////////////////////////////////////////////////
///@{
//! Equality comparison operator
//! \param[in] other Another instance of the class to compare for equality
bool operator==(const InputChannelId& other) const;
bool operator!=(const InputChannelId& other) const;
///@}
constexpr bool operator==(const InputChannelId& other) const
{
return m_crc32 == other.m_crc32;
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Inequality comparison operator
//! \param[in] other Another instance of the class to compare for inequality
constexpr bool operator!=(const InputChannelId& other) const
{
return !(*this == other);
}
private:
////////////////////////////////////////////////////////////////////////////////////////////
// Variables
AZStd::fixed_string<MAX_NAME_LENGTH> m_name; //!< Name of the input channel
AZ::Crc32 m_crc32; //!< Crc32 of the input channel
AZ::Crc32 m_crc32; //!< Crc32 of the input channel name
};
} // namespace AzFramework

@ -14,14 +14,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
{
////////////////////////////////////////////////////////////////////////////////////////////////
const char* InputDeviceGamepad::Name("gamepad");
const InputDeviceId InputDeviceGamepad::IdForIndex0(Name, 0);
const InputDeviceId InputDeviceGamepad::IdForIndex1(Name, 1);
const InputDeviceId InputDeviceGamepad::IdForIndex2(Name, 2);
const InputDeviceId InputDeviceGamepad::IdForIndex3(Name, 3);
const InputDeviceId InputDeviceGamepad::IdForIndexN(AZ::u32 n) { return InputDeviceId(Name, n); }
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceGamepad::IsGamepadDevice(const InputDeviceId& inputDeviceId)
{

@ -32,16 +32,16 @@ namespace AzFramework
public:
////////////////////////////////////////////////////////////////////////////////////////////
//! The name used to identify any game-pad input device
static const char* Name;
static constexpr inline const char* Name{"gamepad"};
////////////////////////////////////////////////////////////////////////////////////////////
//! The id used to identify a game-pad input device with a specific index
///@{
static const InputDeviceId IdForIndex0;
static const InputDeviceId IdForIndex1;
static const InputDeviceId IdForIndex2;
static const InputDeviceId IdForIndex3;
static const InputDeviceId IdForIndexN(AZ::u32 n);
static constexpr inline InputDeviceId IdForIndex0{Name, 0};
static constexpr inline InputDeviceId IdForIndex1{Name, 1};
static constexpr inline InputDeviceId IdForIndex2{Name, 2};
static constexpr inline InputDeviceId IdForIndex3{Name, 3};
static constexpr inline InputDeviceId IdForIndexN(AZ::u32 n) { return InputDeviceId(Name, n); }
///@}
////////////////////////////////////////////////////////////////////////////////////////////

@ -29,71 +29,4 @@ namespace AzFramework
;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
InputDeviceId::InputDeviceId(const char* name, AZ::u32 index)
: m_crc32(name)
, m_index(index)
{
memset(m_name, 0, AZ_ARRAY_SIZE(m_name));
azstrncpy(m_name, NAME_BUFFER_SIZE, name, MAX_NAME_LENGTH);
}
////////////////////////////////////////////////////////////////////////////////////////////////
InputDeviceId::InputDeviceId(const InputDeviceId& other)
: m_crc32(other.m_crc32)
, m_index(other.m_index)
{
memset(m_name, 0, AZ_ARRAY_SIZE(m_name));
azstrcpy(m_name, NAME_BUFFER_SIZE, other.m_name);
}
////////////////////////////////////////////////////////////////////////////////////////////////
InputDeviceId& InputDeviceId::operator=(const InputDeviceId& other)
{
azstrcpy(m_name, NAME_BUFFER_SIZE, other.m_name);
m_crc32 = other.m_crc32;
m_index = other.m_index;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////////
const char* InputDeviceId::GetName() const
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////////////////////
const AZ::Crc32& InputDeviceId::GetNameCrc32() const
{
return m_crc32;
}
////////////////////////////////////////////////////////////////////////////////////////////////
AZ::u32 InputDeviceId::GetIndex() const
{
return m_index;
}
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceId::operator==(const InputDeviceId& other) const
{
return (m_crc32 == other.m_crc32) && (m_index == other.m_index);
}
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceId::operator!=(const InputDeviceId& other) const
{
return !(*this == other);
}
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceId::operator<(const InputDeviceId& other) const
{
if (m_index == other.m_index)
{
return m_crc32 < other.m_crc32;
}
return m_index < other.m_index;
}
} // namespace AzFramework

@ -11,6 +11,7 @@
#include <AzCore/Math/Crc.h>
#include <AzCore/RTTI/ReflectContext.h>
#include <AzCore/std/hash.h>
#include <AzCore/std/string/fixed_string.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
@ -22,8 +23,7 @@ namespace AzFramework
public:
////////////////////////////////////////////////////////////////////////////////////////////
// Constants
static const int NAME_BUFFER_SIZE = 64;
static const int MAX_NAME_LENGTH = NAME_BUFFER_SIZE - 1;
static constexpr int MAX_NAME_LENGTH = 64;
////////////////////////////////////////////////////////////////////////////////////////////
// Allocator
@ -41,17 +41,16 @@ namespace AzFramework
//! Constructor
//! \param[in] name Name of the input device (will be truncated if exceeds MAX_NAME_LENGTH)
//! \param[in] index Index of the input device (optional)
explicit InputDeviceId(const char* name, AZ::u32 index = 0);
////////////////////////////////////////////////////////////////////////////////////////////
//! Copy constructor
//! \param[in] other Another instance of the class to copy from
InputDeviceId(const InputDeviceId& other);
explicit constexpr InputDeviceId(AZStd::string_view name, AZ::u32 index = 0)
: m_name(name.substr(0, MAX_NAME_LENGTH))
, m_crc32(name.substr(0, MAX_NAME_LENGTH))
, m_index(index)
{
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Copy assignment operator
//! \param[in] other Another instance of the class to copy from
InputDeviceId& operator=(const InputDeviceId& other);
// Default copying and moving
AZ_DEFAULT_COPY_MOVE(InputDeviceId);
////////////////////////////////////////////////////////////////////////////////////////////
//! Default destructor
@ -60,12 +59,18 @@ namespace AzFramework
////////////////////////////////////////////////////////////////////////////////////////////
//! Access to the input device's name
//! \return Name of the input device
const char* GetName() const;
constexpr const char* GetName() const
{
return m_name.c_str();
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Access to the crc32 of the input device's name
//! \return crc32 of the input device name
const AZ::Crc32& GetNameCrc32() const;
constexpr const AZ::Crc32& GetNameCrc32() const
{
return m_crc32;
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Access to the input device's index. Used for differentiating between multiple instances
@ -75,27 +80,45 @@ namespace AzFramework
//! at startup using indicies 0->3. As gamepads connect/disconnect at runtime we assign the
//! appropriate (system dependent) local user id (see InputDevice::GetAssignedLocalUserId).
//! \return Index of the input device
AZ::u32 GetIndex() const;
constexpr AZ::u32 GetIndex() const
{
return m_index;
}
////////////////////////////////////////////////////////////////////////////////////////////
///@{
//! Equality comparison operator
//! \param[in] other Another instance of the class to compare for equality
bool operator==(const InputDeviceId& other) const;
bool operator!=(const InputDeviceId& other) const;
///@}
constexpr bool operator==(const InputDeviceId& other) const
{
return (m_crc32 == other.m_crc32) && (m_index == other.m_index);
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Inequality comparison operator
//! \param[in] other Another instance of the class to compare for inequality
constexpr bool operator!=(const InputDeviceId& other) const
{
return !(*this == other);
}
////////////////////////////////////////////////////////////////////////////////////////////
//! Less than comparison operator
//! \param[in] other Another instance of the class to compare
bool operator<(const InputDeviceId& other) const;
constexpr bool operator<(const InputDeviceId& other) const
{
if (m_index == other.m_index)
{
return m_crc32 < other.m_crc32;
}
return m_index < other.m_index;
}
private:
////////////////////////////////////////////////////////////////////////////////////////////
// Variables
char m_name[NAME_BUFFER_SIZE]; //!< Name of the input device
AZ::Crc32 m_crc32; //!< Crc32 of the input device
AZ::u32 m_index; //!< Index of the input device
AZStd::fixed_string<MAX_NAME_LENGTH> m_name; //!< Name of the input device
AZ::Crc32 m_crc32; //!< Crc32 of the input device name
AZ::u32 m_index; //!< Index of the input device
};
} // namespace AzFramework

@ -15,9 +15,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
{
////////////////////////////////////////////////////////////////////////////////////////////////
const InputDeviceId InputDeviceKeyboard::Id("keyboard");
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceKeyboard::IsKeyboardDevice(const InputDeviceId& inputDeviceId)
{

@ -33,7 +33,7 @@ namespace AzFramework
public:
////////////////////////////////////////////////////////////////////////////////////////////
//! The id used to identify the primary physical keyboard input device
static const InputDeviceId Id;
static constexpr inline InputDeviceId Id{"keyboard"};
////////////////////////////////////////////////////////////////////////////////////////////
//! Check whether an input device id identifies a physical keyboard (regardless of index)

@ -14,9 +14,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
{
////////////////////////////////////////////////////////////////////////////////////////////////
const InputDeviceId InputDeviceMotion::Id("motion");
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceMotion::IsMotionDevice(const InputDeviceId& inputDeviceId)
{

@ -28,7 +28,7 @@ namespace AzFramework
public:
////////////////////////////////////////////////////////////////////////////////////////////
//! The id used to identify the primary motion input device
static const InputDeviceId Id;
static constexpr inline InputDeviceId Id{"motion"};
////////////////////////////////////////////////////////////////////////////////////////////
//! Check whether an input device id identifies a motion device (regardless of index)

@ -15,18 +15,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
{
////////////////////////////////////////////////////////////////////////////////////////////////
const AZ::u32 InputDeviceMouse::MovementSampleRateDefault = 60;
////////////////////////////////////////////////////////////////////////////////////////////////
const AZ::u32 InputDeviceMouse::MovementSampleRateQueueAll = std::numeric_limits<AZ::u32>::max();
////////////////////////////////////////////////////////////////////////////////////////////////
const AZ::u32 InputDeviceMouse::MovementSampleRateAccumulateAll = 0;
////////////////////////////////////////////////////////////////////////////////////////////////
const InputDeviceId InputDeviceMouse::Id("mouse");
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceMouse::IsMouseDevice(const InputDeviceId& inputDeviceId)
{

@ -31,23 +31,23 @@ namespace AzFramework
////////////////////////////////////////////////////////////////////////////////////////////
//! Default sample rate for raw mouse movement events that aims to strike a balance between
//! responsiveness and performance.
static const AZ::u32 MovementSampleRateDefault;
static constexpr inline AZ::u32 MovementSampleRateDefault{60};
////////////////////////////////////////////////////////////////////////////////////////////
//! Sample rate for raw mouse movement that will cause all events received in the same frame
//! to be queued and dispatched as individual events. This results in maximum responsiveness
//! but may potentially impact performance depending how many events happen over each frame.
static const AZ::u32 MovementSampleRateQueueAll;
static constexpr inline AZ::u32 MovementSampleRateQueueAll{std::numeric_limits<AZ::u32>::max()};
////////////////////////////////////////////////////////////////////////////////////////////
//! Sample rate for raw mouse movement that will cause all events received in the same frame
//! to be accumulated and dispatched as a single event. Optimal for performance, but results
//! in sluggish/unresponsive mouse movement, especially when running at low frame rates.
static const AZ::u32 MovementSampleRateAccumulateAll;
static constexpr inline AZ::u32 MovementSampleRateAccumulateAll{0};
////////////////////////////////////////////////////////////////////////////////////////////
//! The id used to identify the primary mouse input device
static const InputDeviceId Id;
static constexpr inline InputDeviceId Id{"mouse"};
////////////////////////////////////////////////////////////////////////////////////////////
//! Check whether an input device id identifies a mouse (regardless of index)

@ -15,9 +15,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
{
////////////////////////////////////////////////////////////////////////////////////////////////
const InputDeviceId InputDeviceTouch::Id("touch");
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceTouch::IsTouchDevice(const InputDeviceId& inputDeviceId)
{

@ -25,7 +25,7 @@ namespace AzFramework
public:
////////////////////////////////////////////////////////////////////////////////////////////
//! The id used to identify the primary touch input device
static const InputDeviceId Id;
static constexpr inline InputDeviceId Id{"touch"};
////////////////////////////////////////////////////////////////////////////////////////////
//! Check whether an input device id identifies a touch device (regardless of index)

@ -14,9 +14,6 @@
////////////////////////////////////////////////////////////////////////////////////////////////////
namespace AzFramework
{
////////////////////////////////////////////////////////////////////////////////////////////////
const InputDeviceId InputDeviceVirtualKeyboard::Id("virtual_keyboard");
////////////////////////////////////////////////////////////////////////////////////////////////
bool InputDeviceVirtualKeyboard::IsVirtualKeyboardDevice(const InputDeviceId& inputDeviceId)
{

@ -25,7 +25,7 @@ namespace AzFramework
public:
////////////////////////////////////////////////////////////////////////////////////////////
//! The id used to identify the primary virtual keyboard input device
static const InputDeviceId Id;
static constexpr inline InputDeviceId Id{"virtual_keyboard"};
////////////////////////////////////////////////////////////////////////////////////////////
//! Check whether an input device id identifies a virtual keyboard (regardless of index)

@ -48,6 +48,24 @@ namespace InputUnitTests
AZStd::unique_ptr<InputSystemComponent> m_inputSystemComponent;
};
////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(InputTest, InputChannelId_ConstExpression_CopyConstructorSuccessfull)
{
constexpr InputChannelId testInputChannelId1("TestInputChannelId");
constexpr InputChannelId testInputChannelId2(testInputChannelId1);
static_assert(testInputChannelId1 == testInputChannelId2);
EXPECT_EQ(testInputChannelId1, testInputChannelId2);
}
////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(InputTest, InputDeviceId_ConstExpression_CopyConstructorSuccessfull)
{
constexpr InputDeviceId testInputDeviceId1("TestInputDeviceId");
constexpr InputDeviceId testInputDeviceId2(testInputDeviceId1);
static_assert(testInputDeviceId1 == testInputDeviceId2);
EXPECT_EQ(testInputDeviceId1, testInputDeviceId2);
}
////////////////////////////////////////////////////////////////////////////////////////////////
TEST_F(InputTest, InputContext_InitWithDataStruct_InitializationSuccessfull)
{

Loading…
Cancel
Save