qxLib
sbo_bytes.h
Go to the documentation of this file.
1 /**
2 
3  @file sbo_bytes.h
4  @author Khrapov
5  @date 20.12.2025
6  @copyright © Nick Khrapov, 2025. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <array>
12 #include <cstddef>
13 
14 #include <qx/macros/common.h>
15 
16 namespace qx
17 {
18 
19 enum class sbo_resize_type
20 {
21  common,
22  reserve,
23  shrink_to_fit
24 };
25 
26 /**
27 
28  @class sbo_bytes
29  @brief A type erased small buffer object that works with raw data
30  @tparam traits_t - SBO traits type
31  @author Khrapov
32  @date 20.12.2025
33 
34 **/
35 template<class traits_t>
36 class sbo_bytes
37 {
38 public:
39  using traits_type = traits_t;
40  using size_type = typename traits_type::size_type;
41 
42  // the final size of the whole sbo_bytes object, including internal data
43  static constexpr size_type nSBOSize = traits_type::nSBOSize;
44  static_assert(nSBOSize >= 32);
45 
46  // when the size changes so it becomes less or equal a buffer size, should we free a memory and move back to a buffer?
47  static constexpr bool bShrinkToFitWhenSmall = traits_type::bShrinkToFitWhenSmall;
48 
49  static constexpr size_type nBufferSize = nSBOSize - 2 * sizeof(size_type);
50  using buffer = std::array<std::byte, nBufferSize>;
51 
52 public:
53  sbo_bytes() noexcept = default;
54  sbo_bytes(sbo_bytes&& other) noexcept;
55 
56  ~sbo_bytes() noexcept;
57 
58  sbo_bytes& operator=(sbo_bytes&& other) noexcept;
59 
60  /**
61  @brief Resize SBO
62  @param nNewSize - new size (bytes)
63  @param nAlignment - alignment (if 16 then size 13->16 16->16 18->32)
64  @param eSboResizeType - a resize type
65  @param bMemmove - in case the content is relocated, should we call memmove or the callee will handle the moving?
66  @retval - true if memory alloc is successful
67  **/
68  bool resize(size_type nNewSize, size_type nAlignment, sbo_resize_type eSboResizeType, bool bMemmove) noexcept;
69 
70  /**
71  @brief Free allocated memory
72  **/
73  void free() noexcept;
74 
75  /**
76  @brief Get SBO data: from a buffer or from a heap
77  @retval - SBO data pointer
78  **/
79  std::byte* data() noexcept;
80 
81  /**
82  @brief Get SBO data: from a buffer or from a heap
83  @retval - SBO data pointer
84  **/
85  const std::byte* data() const noexcept;
86 
87  /**
88  @brief Get SBO size (bytes)
89  @retval - SBO size (bytes)
90  **/
91  size_type size() const noexcept;
92 
93  /**
94  @brief Get SBO capacity (bytes)
95  @retval - SBO capacity (bytes), can't be less than nSBOSize
96  **/
97  size_type capacity() const noexcept;
98 
99  /**
100  @brief Is the SBO small and fits into the local buffer
101  @retval - true if the SBO is small and fits into the local buffer
102  **/
103  bool is_small() const noexcept;
104 
105 private:
106  union
107  {
108  buffer m_Buffer;
109  std::byte* m_pData = nullptr;
110  };
111 
112  size_type m_nSize = 0;
113  size_type m_nAllocatedSize = 0;
114 };
115 
116 } // namespace qx
117 
118 #include <qx/sbo/sbo_bytes.inl>
A type erased small buffer object that works with raw data.
Definition: sbo_bytes.h:37
size_type size() const noexcept
Get SBO size (bytes)
Definition: sbo_bytes.inl:154
bool resize(size_type nNewSize, size_type nAlignment, sbo_resize_type eSboResizeType, bool bMemmove) noexcept
Resize SBO.
Definition: sbo_bytes.inl:58
void free() noexcept
Free allocated memory.
Definition: sbo_bytes.inl:126
size_type capacity() const noexcept
Get SBO capacity (bytes)
Definition: sbo_bytes.inl:160
bool is_small() const noexcept
Is the SBO small and fits into the local buffer.
Definition: sbo_bytes.inl:169
std::byte * data() noexcept
Get SBO data: from a buffer or from a heap.
Definition: sbo_bytes.inl:139