Address various feedback around RewindableFixedVector

This commit is contained in:
puvvadar
2021-05-25 15:38:13 -07:00
parent e47fb1b7ea
commit 050574715a
2 changed files with 63 additions and 87 deletions
@@ -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<TYPE, SIZE>& operator=(const RewindableFixedVector<TYPE, SIZE>& RHS);
constexpr RewindableFixedVector<TYPE, SIZE>& operator=(const RewindableFixedVector<TYPE, SIZE>& 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<TYPE, SIZE>& RHS) const;
constexpr bool operator ==(const RewindableFixedVector<TYPE, SIZE>& 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<TYPE, SIZE>& RHS) const;
constexpr bool operator !=(const RewindableFixedVector<TYPE, SIZE>& 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<TYPE, Multiplayer::RewindHistorySize>* const_iterator;
const_iterator begin() const { return m_container.cbegin(); }
const_iterator end() const { return m_container.cend(); }
typedef RewindableObject<TYPE, Multiplayer::RewindHistorySize>* 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<RewindableObject<TYPE, Multiplayer::RewindHistorySize>, SIZE> m_container;
// Synchronized value for vector size, prefer using size() locally which checks m_container.size()
RewindableObject<uint32_t, Multiplayer::RewindHistorySize> m_size;
RewindableObject<uint32_t, Multiplayer::RewindHistorySize> m_serializedSize;
};
}
@@ -15,26 +15,22 @@
namespace Multiplayer
{
template <typename TYPE, uint32_t SIZE>
inline RewindableFixedVector<TYPE, SIZE>::RewindableFixedVector(const TYPE& initialValue, uint32_t count)
constexpr RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline RewindableFixedVector<TYPE, SIZE>::~RewindableFixedVector()
RewindableFixedVector<TYPE, SIZE>::~RewindableFixedVector()
{
;
}
template <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer)
constexpr bool RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::Serialize(AzNetworking::ISerializer& serializer, AzNetworking::IBitset& deltaRecord)
constexpr bool RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::copy_values(const TYPE* buffer, uint32_t bufferSize)
constexpr bool RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline RewindableFixedVector<TYPE, SIZE>& RewindableFixedVector<TYPE, SIZE>::operator=(const RewindableFixedVector<TYPE, SIZE>& RHS)
constexpr RewindableFixedVector<TYPE, SIZE>& RewindableFixedVector<TYPE, SIZE>::operator=(const RewindableFixedVector<TYPE, SIZE>& 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 <typename TYPE, uint32_t SIZE>
bool RewindableFixedVector<TYPE, SIZE>::operator ==(const RewindableFixedVector<TYPE, SIZE>& RHS) const
constexpr bool RewindableFixedVector<TYPE, SIZE>::operator ==(const RewindableFixedVector<TYPE, SIZE>& 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 <typename TYPE, uint32_t SIZE>
bool RewindableFixedVector<TYPE, SIZE>::operator !=(const RewindableFixedVector<TYPE, SIZE>& RHS) const
constexpr bool RewindableFixedVector<TYPE, SIZE>::operator !=(const RewindableFixedVector<TYPE, SIZE>& rhs) const
{
return !(*this == RHS);
return !(*this == rhs);
}
template <typename TYPE, uint32_t SIZE>
bool RewindableFixedVector<TYPE, SIZE>::resize(uint32_t count)
constexpr bool RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::resize_no_construct(uint32_t count)
constexpr bool RewindableFixedVector<TYPE, SIZE>::resize_no_construct(uint32_t count)
{
if (count > SIZE)
{
@@ -172,70 +154,64 @@ namespace Multiplayer
}
template <typename TYPE, uint32_t SIZE>
inline void RewindableFixedVector<TYPE, SIZE>::clear()
constexpr void RewindableFixedVector<TYPE, SIZE>::clear()
{
resize(0);
m_container.clear();
}
template <typename TYPE, uint32_t SIZE>
inline const TYPE& RewindableFixedVector<TYPE, SIZE>::operator[](uint32_t index) const
constexpr const TYPE& RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline TYPE& RewindableFixedVector<TYPE, SIZE>::operator[](uint32_t index)
constexpr TYPE& RewindableFixedVector<TYPE, SIZE>::operator[](uint32_t index)
{
AZ_Assert(index < size(), "Out of bounds access (requested %u, reserved %u)", index, size());
return m_container[index].Modify();
}
template <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::push_back(const TYPE& value)
constexpr bool RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::pop_back()
constexpr bool RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline bool RewindableFixedVector<TYPE, SIZE>::empty() const
constexpr bool RewindableFixedVector<TYPE, SIZE>::empty() const
{
return m_container.empty();
}
template <typename TYPE, uint32_t SIZE>
inline const TYPE& RewindableFixedVector<TYPE, SIZE>::back() const
constexpr const TYPE& RewindableFixedVector<TYPE, SIZE>::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 <typename TYPE, uint32_t SIZE>
inline uint32_t RewindableFixedVector<TYPE, SIZE>::size() const
constexpr uint32_t RewindableFixedVector<TYPE, SIZE>::size() const
{
return m_container.size();
}