Files
o3de/Code/Framework/AzNetworking/AzNetworking/DataStructures/ByteBuffer.inl
T
2021-03-08 14:30:57 -08:00

110 lines
3.3 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
namespace AzNetworking
{
template <AZStd::size_t SIZE>
inline constexpr AZStd::size_t ByteBuffer<SIZE>::GetCapacity()
{
return SIZE;
}
template <AZStd::size_t SIZE>
inline AZStd::size_t ByteBuffer<SIZE>::GetSize() const
{
return m_buffer.size();
}
template <AZStd::size_t SIZE>
inline bool ByteBuffer<SIZE>::Resize(AZStd::size_t newSize)
{
if (newSize > GetCapacity())
{
return false;
}
m_buffer.resize_no_construct(newSize);
return true;
}
template <AZStd::size_t SIZE>
inline const uint8_t* ByteBuffer<SIZE>::GetBuffer() const
{
return m_buffer.data();
}
template <AZStd::size_t SIZE>
inline uint8_t* ByteBuffer<SIZE>::GetBuffer()
{
return m_buffer.data();
}
template <AZStd::size_t SIZE>
inline const uint8_t* ByteBuffer<SIZE>::GetBufferEnd() const
{
return GetBuffer() + GetSize();
}
template <AZStd::size_t SIZE>
inline uint8_t* ByteBuffer<SIZE>::GetBufferEnd()
{
return GetBuffer() + GetSize();
}
template <AZStd::size_t SIZE>
inline bool ByteBuffer<SIZE>::CopyValues(const uint8_t* buffer, AZStd::size_t bufferSize)
{
if (Resize(bufferSize))
{
memcpy(m_buffer.data(), buffer, bufferSize);
return true;
}
return false;
}
template <AZStd::size_t SIZE>
bool ByteBuffer<SIZE>::IsSame(const uint8_t* buffer, AZStd::size_t bufferSize) const
{
return (m_buffer.size() == bufferSize)
&& (memcmp(m_buffer.data(), buffer, bufferSize) == 0);
}
template <AZStd::size_t SIZE>
inline bool ByteBuffer<SIZE>::operator==(const ByteBuffer& rhs) const
{
return (m_buffer.size() == rhs.m_buffer.size())
&& (memcmp(m_buffer.data(), rhs.m_buffer.data(), m_buffer.size()) == 0);
}
template <AZStd::size_t SIZE>
inline bool ByteBuffer<SIZE>::operator!=(const ByteBuffer& rhs) const
{
return (m_buffer.size() != rhs.m_buffer.size())
|| (memcmp(m_buffer.data(), rhs.m_buffer.data(), m_buffer.size()) != 0);
}
template <AZStd::size_t SIZE>
inline bool ByteBuffer<SIZE>::Serialize(ISerializer& serializer)
{
static constexpr AZStd::size_t RequiredBytes = AZ::RequiredBytesForValue<SIZE>();
using SizeType = typename AZ::SizeType<RequiredBytes, false>::Type;
// Important since we need to know the original and new sizes for vector resize
SizeType size = static_cast<SizeType>(GetSize());
uint32_t outSize = size;
return serializer.Serialize(size, "Size")
&& Resize(size)
&& serializer.SerializeBytes(m_buffer.data(), static_cast<uint32_t>(GetCapacity()), false, outSize, "Buffer")
&& (outSize == size);
}
}