13 template<
class traits_t>
14 sbo_bytes<traits_t>::sbo_bytes(sbo_bytes&& other) noexcept
16 *
this = std::move(other);
19 template<
class traits_t>
20 sbo_bytes<traits_t>::~sbo_bytes() noexcept
22 QX_STATIC_ASSERT_EQ(
sizeof(sbo_bytes), nSBOSize);
26 template<
class traits_t>
27 sbo_bytes<traits_t>& sbo_bytes<traits_t>::operator=(sbo_bytes&& other) noexcept
29 if (!is_small() && !other.is_small())
31 std::swap(m_pData, other.m_pData);
33 else if (is_small() && other.is_small())
35 std::swap(m_Buffer, other.m_Buffer);
37 else if (is_small() && !other.is_small())
39 buffer thisBuffer = std::move(m_Buffer);
40 m_pData = other.m_pData;
41 other.m_Buffer = std::move(thisBuffer);
43 else if (!is_small() && other.is_small())
45 std::byte* thisAllocated = m_pData;
46 m_Buffer = std::move(other.m_Buffer);
47 other.m_pData = thisAllocated;
50 std::swap(m_nSize, other.m_nSize);
51 std::swap(m_nAllocatedSize, other.m_nAllocatedSize);
56 QX_PUSH_SUPPRESS_MSVC_WARNINGS(4701);
57 template<
class traits_t>
61 sbo_resize_type eSboResizeType,
62 bool bMemmove) noexcept
66 const size_type nSizeToAllocate =
67 eSboResizeType != sbo_resize_type::shrink_to_fit ? (nNewSize + nAlignment - 1) & ~(nAlignment - 1) : nNewSize;
69 const bool bSmallAtStart = is_small();
71 if (eSboResizeType == sbo_resize_type::shrink_to_fit
72 || eSboResizeType == sbo_resize_type::common && !bSmallAtStart && bShrinkToFitWhenSmall
74 || nSizeToAllocate > capacity())
78 if (nSizeToAllocate <= m_Buffer.size())
80 if (!bSmallAtStart && (bShrinkToFitWhenSmall || eSboResizeType == sbo_resize_type::shrink_to_fit))
85 std::memmove(buff.data(), m_pData, nSizeToAllocate);
92 m_nSize = nSizeToAllocate;
96 size_type nStartSize = 0;
100 nStartSize = m_nSize;
103 if (
void* pNewBlock = std::realloc(bSmallAtStart ?
nullptr : m_pData, nSizeToAllocate))
105 m_nAllocatedSize = nSizeToAllocate;
106 m_pData =
static_cast<std::byte*
>(pNewBlock);
108 if (bMemmove && bSmallAtStart && m_nSize > 0)
109 std::memmove(m_pData, buff.data(), nStartSize);
118 if (bRet && eSboResizeType == sbo_resize_type::common)
123 QX_POP_SUPPRESS_WARNINGS();
125 template<
class traits_t>
135 m_nAllocatedSize = 0;
138 template<
class traits_t>
142 return m_Buffer.data();
147 template<
class traits_t>
153 template<
class traits_t>
159 template<
class traits_t>
163 return m_Buffer.size();
165 return m_nAllocatedSize;
168 template<
class traits_t>
171 return m_nAllocatedSize == 0;
size_type size() const noexcept
Get SBO size (bytes)
bool resize(size_type nNewSize, size_type nAlignment, sbo_resize_type eSboResizeType, bool bMemmove) noexcept
Resize SBO.
void free() noexcept
Free allocated memory.
size_type capacity() const noexcept
Get SBO capacity (bytes)
bool is_small() const noexcept
Is the SBO small and fits into the local buffer.
std::byte * data() noexcept
Get SBO data: from a buffer or from a heap.
#define QX_CONST_CAST_THIS()
This macro is made for situations where you have a const method, and you need exactly the same method...