diff --git a/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.h b/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.h index 2e265bdb6f..662013d033 100644 --- a/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.h +++ b/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.h @@ -30,12 +30,12 @@ namespace Multiplayer { public: //! Default constructor - RewindableFixedVector() = default; + constexpr RewindableFixedVector() = default; //! Construct and initialize buffer to the provided value //! @param initialValue initial value to set the internal buffer to //! @param count initial value to reserve in the vector - RewindableFixedVector(const TYPE& initialValue, uint32_t count); + constexpr RewindableFixedVector(const TYPE& initialValue, uint32_t count); //! Destructor ~RewindableFixedVector(); @@ -43,86 +43,86 @@ namespace Multiplayer //! Serialization method for fixed vector contained rewindable objects //! @param serializer ISerializer instance to use for serialization //! @return bool true for success, false for serialization failure - bool Serialize(AzNetworking::ISerializer& serializer); + constexpr bool Serialize(AzNetworking::ISerializer& serializer); //! Serialization method for fixed vector contained rewindable objects //! @param serializer ISerializer instance to use for serialization //! @return bool true for success, false for serialization failure - bool Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset &deltaRecord); + constexpr bool Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset &deltaRecord); //! Copies elements from the buffer pointed to by Buffer to this FixedSizeVector instance, vector size will be set to BufferSize //! @param buffer pointer to the buffer to copy //! @param bufferSize number of elements in the buffer to copy //! @return bool true on success, false if the input data was too large to fit in the vector - bool copy_values(const TYPE* buffer, uint32_t bufferSize); + constexpr bool copy_values(const TYPE* buffer, uint32_t bufferSize); //! Copy buffer from the provided vector //! @param RHS instance to copy from - RewindableFixedVector& operator=(const RewindableFixedVector& RHS); + constexpr RewindableFixedVector& operator=(const RewindableFixedVector& rhs); //! Equality operator, returns true if the current instance is equal to RHS - //! @param RHS the FixedSizeVector instance to test for equality against + //! @param rhs the FixedSizeVector instance to test for equality against //! @return bool true if equal, false if not - bool operator ==(const RewindableFixedVector& RHS) const; + constexpr bool operator ==(const RewindableFixedVector& rhs) const; //! Inequality operator, returns true if the current instance is not equal to RHS - //! @param RHS the FixedSizeVector instance to test for inequality against + //! @param rhs the FixedSizeVector instance to test for inequality against //! @return bool false if equal, true if not equal - bool operator !=(const RewindableFixedVector& RHS) const; + constexpr bool operator !=(const RewindableFixedVector& rhs) const; //! Resizes the vector to the requested number of elements, initializing new elements if necessary //! @param count the number of elements to size the vector to //! @return bool true on success - bool resize(uint32_t count); + constexpr bool resize(uint32_t count); //! Resizes the vector to the requested number of elements, without initialization //! @param count the number of elements to size the vector to //! @return bool true on success - bool resize_no_construct(uint32_t count); + constexpr bool resize_no_construct(uint32_t count); //! Resets the vector, returning it to size 0 - void clear(); + constexpr void clear(); //! Const element access //! @param Index index of the element to return //! @return const reference to the requested element - const TYPE& operator[](uint32_t index) const; + constexpr const TYPE& operator[](uint32_t index) const; //! Non-const element access //! @param Index index of the element to return //! @return non-const reference to the requested element - TYPE& operator[](uint32_t index); + constexpr TYPE& operator[](uint32_t index); //! Pushes a new element to the back of the vector //! @param Value value to append to the back of this vector //! @return boolean true on success, false if the vector was full - bool push_back(const TYPE& value); + constexpr bool push_back(const TYPE& value); //! Pops the last element off the vector, decreasing the vector's size by one //! @return bool true on success, false if the vector was empty - bool pop_back(); + constexpr bool pop_back(); //! Returns if the vector is empty //! @return bool true on empty, false if the vector contains valid elements - bool empty() const; + constexpr bool empty() const; //! Gets the last element of the vector - const TYPE& back() const; + constexpr const TYPE& back() const; //! Gets the size of the vector - uint32_t size() const; + constexpr uint32_t size() const; typedef const RewindableObject* const_iterator; const_iterator begin() const { return m_container.cbegin(); } const_iterator end() const { return m_container.cend(); } typedef RewindableObject* iterator; - iterator begin() { return m_container.begin(); } - iterator end() { return m_container.end(); } + constexpr iterator begin() { return m_container.begin(); } + constexpr iterator end() { return m_container.end(); } private: AZStd::fixed_vector, SIZE> m_container; // Synchronized value for vector size, prefer using size() locally which checks m_container.size() - RewindableObject m_size; + RewindableObject m_serializedSize; }; } diff --git a/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.inl b/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.inl index f1c4284fa2..c48e534f4f 100644 --- a/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.inl +++ b/Gems/Multiplayer/Code/Include/Multiplayer/NetworkTime/RewindableFixedVector.inl @@ -15,26 +15,22 @@ namespace Multiplayer { template - inline RewindableFixedVector::RewindableFixedVector(const TYPE& initialValue, uint32_t count) + constexpr RewindableFixedVector::RewindableFixedVector(const TYPE& initialValue, uint32_t count) { - resize_no_construct(count); - for (uint32_t idx = 0l idx < size(); ++idx) - { - m_container[idx] = initialValue; - } + m_container.resize(count, initialValue) } template - inline RewindableFixedVector::~RewindableFixedVector() + RewindableFixedVector::~RewindableFixedVector() { ; } template - inline bool RewindableFixedVector::Serialize(AzNetworking::ISerializer& serializer) + constexpr bool RewindableFixedVector::Serialize(AzNetworking::ISerializer& serializer) { - m_size = m_container.size(); - if(!m_size.Serialize(serializer) && !resize(m_size)) + m_serializedSize = m_container.size(); + if(!m_serializedSize.Serialize(serializer) && !resize(m_serializedSize)) { return false; } @@ -51,18 +47,18 @@ namespace Multiplayer } template - inline bool RewindableFixedVector::Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset& deltaRecord) + constexpr bool RewindableFixedVector::Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset& deltaRecord) { if (deltaRecord.GetBit(SIZE)) { - uint32_t origSize = m_size; - m_size = m_container.size(); - if(!m_size.Serialize(serializer) && !resize(m_size)) + uint32_t origSize = m_serializedSize; + m_serializedSize = m_container.size(); + if(!m_serializedSize.Serialize(serializer) && !resize(m_serializedSize)) { return false; } - if ((serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject) && origSize == m_size) + if ((serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject) && origSize == m_serializedSize) { deltaRecord.SetBit(SIZE, false); } @@ -88,7 +84,7 @@ namespace Multiplayer } template - inline bool RewindableFixedVector::copy_values(const TYPE* buffer, uint32_t bufferSize) + constexpr bool RewindableFixedVector::copy_values(const TYPE* buffer, uint32_t bufferSize) { if (!resize(bufferSize)) { @@ -99,41 +95,35 @@ namespace Multiplayer { m_container[idx] = buffer[idx]; } - + return true; } - template - inline RewindableFixedVector& RewindableFixedVector::operator=(const RewindableFixedVector& RHS) + constexpr RewindableFixedVector& RewindableFixedVector::operator=(const RewindableFixedVector& rhs) { resize(RHS.size()); for (uint32_t idx = 0; idx < size(); ++i) { - m_container[idx] = RHS.m_container[idx]; + m_container[idx] = rhs.m_container[idx]; } return *this; } template - bool RewindableFixedVector::operator ==(const RewindableFixedVector& RHS) const + constexpr bool RewindableFixedVector::operator ==(const RewindableFixedVector& rhs) const { - if (this->size() != RHS.size()) - { - return false; - } - - return m_container == RHS.m_container && m_size == m_size; + return m_container == rhs.m_container && m_serializedSize == rhs.m_serializedSize && size == rhs.size(); } template - bool RewindableFixedVector::operator !=(const RewindableFixedVector& RHS) const + constexpr bool RewindableFixedVector::operator !=(const RewindableFixedVector& rhs) const { - return !(*this == RHS); + return !(*this == rhs); } template - bool RewindableFixedVector::resize(uint32_t count) + constexpr bool RewindableFixedVector::resize(uint32_t count) { if (count > SIZE) { @@ -145,21 +135,13 @@ namespace Multiplayer return true; } - if (count > size()) - { - for (uint32_t idx = size(); idx < count; ++idx) - { - m_container[idx] = TYPE(); - } - } - - m_container.resize(count); + m_container.resize(count, TYPE()); return true; } template - inline bool RewindableFixedVector::resize_no_construct(uint32_t count) + constexpr bool RewindableFixedVector::resize_no_construct(uint32_t count) { if (count > SIZE) { @@ -172,70 +154,64 @@ namespace Multiplayer } template - inline void RewindableFixedVector::clear() + constexpr void RewindableFixedVector::clear() { - resize(0); + m_container.clear(); } template - inline const TYPE& RewindableFixedVector::operator[](uint32_t index) const + constexpr const TYPE& RewindableFixedVector::operator[](uint32_t index) const { AZ_Assert(index < size(), "Out of bounds access (requested %u, reserved %u)", index, size()); return m_container[index].Get(); } template - inline TYPE& RewindableFixedVector::operator[](uint32_t index) + constexpr TYPE& RewindableFixedVector::operator[](uint32_t index) { AZ_Assert(index < size(), "Out of bounds access (requested %u, reserved %u)", index, size()); return m_container[index].Modify(); } template - inline bool RewindableFixedVector::push_back(const TYPE& value) + constexpr bool RewindableFixedVector::push_back(const TYPE& value) { - const uint32_t iBufferSize = size(); - - if (!resize(iBufferSize + 1)) + if (size() < SIZE) { - return false; + m_container.push_back(value); + return true; } - m_container[iBufferSize] = value; - - return true; + return false; } template - inline bool RewindableFixedVector::pop_back() + constexpr bool RewindableFixedVector::pop_back() { - const uint32_t iBufferSize = size(); - - if (iBufferSize <= 0) + if (size() > 0) { - return false; + m_container.pop_back(); + return true; } - resize(iBufferSize - 1); - - return true; + return false; } template - inline bool RewindableFixedVector::empty() const + constexpr bool RewindableFixedVector::empty() const { return m_container.empty(); } template - inline const TYPE& RewindableFixedVector::back() const + constexpr const TYPE& RewindableFixedVector::back() const { AZ_Assert(size() > 0, "Attempted to get back element of an empty RewindableFixedVector"); - return m_container[size() - 1].Get(); + return m_container.back().Get(); } template - inline uint32_t RewindableFixedVector::size() const + constexpr uint32_t RewindableFixedVector::size() const { return m_container.size(); }