Sane C++ Libraries
C++ Platform Abstraction Libraries
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
67#endif
68
69// Enables File library from reading data from file descriptor into a Buffer
70template <>
71struct GrowableBuffer<Buffer> : public IGrowableBuffer
72{
73 Buffer& buffer;
74 GrowableBuffer(Buffer& buffer) : buffer(buffer) {}
75 virtual DirectAccess getDirectAccess() override final { return {buffer.size(), buffer.capacity(), buffer.data()}; }
76 virtual bool tryGrowTo(size_t newSize) override final { return buffer.resizeWithoutInitializing(newSize); }
77};
79} // namespace SC
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition Compiler.h:78
constexpr T && move(T &value)
Converts an lvalue to an rvalue reference.
Definition Compiler.h:269
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