Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Buffer.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Internal/IGrowableBuffer.h"
5#include "../Memory/Segment.h"
6
7namespace SC
8{
9
12
13namespace detail
14{
15struct SegmentBuffer : public SegmentTrivial<char>, public SegmentSelfRelativePointer<char>
16{
17 static constexpr bool IsArray = false;
18};
19} // namespace detail
20
28struct Buffer : public Segment<detail::SegmentBuffer>
29{
30 using Segment::Segment;
31};
32
36template <int N>
37struct SmallBuffer : public Buffer
38{
39 SmallBuffer(SegmentAllocator allocator = SegmentAllocator::Global) noexcept : Buffer(N, allocator) {}
40 SmallBuffer(const Buffer& other) noexcept : SmallBuffer() { Buffer::operator=(other); }
41 SmallBuffer(Buffer&& other) noexcept : SmallBuffer() { Buffer::operator=(move(other)); }
42 Buffer& operator=(const Buffer& other) noexcept { return Buffer::operator=(other); }
43 Buffer& operator=(Buffer&& other) noexcept { return Buffer::operator=(move(other)); }
44
45 // clang-format off
46 SmallBuffer(const SmallBuffer& other) noexcept : SmallBuffer() { Buffer::operator=(other); }
47 SmallBuffer(SmallBuffer&& other) noexcept : SmallBuffer() { Buffer::operator=(move(other)); }
48 SmallBuffer& operator=(const SmallBuffer& other) noexcept { Buffer::operator=(other); return *this; }
49 SmallBuffer& operator=(SmallBuffer&& other) noexcept { Buffer::operator=(move(other)); return *this; }
50 // clang-format on
51
52 protected:
53 SmallBuffer(int num, SegmentAllocator allocator) : Buffer(N, allocator) { (void)num; }
54
55 private:
56 uint64_t inlineCapacity = N;
57 char inlineBuffer[N];
58};
59
60using BufferTL = detail::SegmentCustom<Buffer, Buffer, 0, SegmentAllocator::ThreadLocal>;
61template <int N>
62using SmallBufferTL = detail::SegmentCustom<SmallBuffer<N>, Buffer, N, SegmentAllocator::ThreadLocal>;
63
64#if SC_COMPILER_MSVC
65// Adding the SC_COMPILER_EXPORT on Buffer declaration causes MSVC to issue error C2491
66struct SC_COMPILER_EXPORT Buffer;
67#endif
68
69// Enables File library from reading data from file descriptor into a Buffer
70template <>
71struct SC_COMPILER_EXPORT GrowableBuffer<Buffer> : public IGrowableBuffer
72{
73 Buffer& buffer;
74 GrowableBuffer(Buffer& buffer) noexcept;
75 ~GrowableBuffer() noexcept;
76 static bool tryGrowTo(IGrowableBuffer&, size_t newSize) noexcept;
77 void finalize() noexcept;
78};
79
80template <int N>
81struct SC_COMPILER_EXPORT GrowableBuffer<SmallBuffer<N>> : public IGrowableBuffer
82{
83 Buffer& buffer;
84 GrowableBuffer(Buffer& buffer) noexcept : IGrowableBuffer(&GrowableBuffer::tryGrowTo), buffer(buffer)
85 {
86 IGrowableBuffer::directAccess = {buffer.size(), buffer.capacity(), buffer.data()};
87 }
88 ~GrowableBuffer() noexcept { finalize(); }
89
90 static bool tryGrowTo(IGrowableBuffer& gb, size_t newSize) noexcept
91 {
92 GrowableBuffer& self = static_cast<GrowableBuffer&>(gb);
93 const bool result = self.buffer.resizeWithoutInitializing(newSize);
94 self.directAccess = {self.buffer.size(), self.buffer.capacity(), self.buffer.data()};
95 return result;
96 }
97 void finalize() noexcept { (void)buffer.resizeWithoutInitializing(IGrowableBuffer::directAccess.sizeInBytes); }
98};
100} // namespace SC
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:257
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
An heap allocated byte buffer that can optionally use an inline buffer.
Definition Buffer.h:29
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition Segment.h:113
A SC::Buffer with a dedicated custom inline buffer to avoid heap allocation.
Definition Buffer.h:38