Sane C++ Libraries
C++ Platform Abstraction Libraries
Buffer.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Segment.h"
5
6namespace SC
7{
8
11
13{
14 using Type = char;
15};
16
24struct Buffer : public Segment<SegmentBuffer>
25{
26 using Segment::Segment;
27};
28
32template <int N>
33struct SmallBuffer : public Buffer
34{
35 SmallBuffer() : Buffer(inlineHeader, N) {}
36 SmallBuffer(const Buffer& other) : SmallBuffer() { Buffer::operator=(other); }
37 SmallBuffer(Buffer&& other) : SmallBuffer() { Buffer::operator=(move(other)); }
38 Buffer& operator=(const Buffer& other) { return Buffer::operator=(other); }
39 Buffer& operator=(Buffer&& other) { return Buffer::operator=(move(other)); }
40
41 // clang-format off
42 SmallBuffer(const SmallBuffer& other) : SmallBuffer() { Buffer::operator=(other); }
43 SmallBuffer(SmallBuffer&& other) : SmallBuffer() { Buffer::operator=(move(other)); }
44 SmallBuffer& operator=(const SmallBuffer& other) { Buffer::operator=(other); return *this; }
45 SmallBuffer& operator=(SmallBuffer&& other) { Buffer::operator=(move(other)); return *this; }
46 // clang-format on
47
48 private:
49 SegmentHeader inlineHeader;
50 char inlineBuffer[N];
51};
52
53#if SC_COMPILER_MSVC
54// Adding the SC_COMPILER_EXPORT on Buffer declaration causes MSVC to issue error C2491
56#endif
57
59} // 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
An heap allocated byte buffer that can optionally use an inline buffer.
Definition: Buffer.h:25
Basic allocator for SC::Segment using Memory functions.
Definition: Segment.h:203
Definition: Buffer.h:13
Definition: Segment.h:11
A slice of contiguous memory, prefixed by and header containing size and capacity.
Definition: Segment.h:35
Allows SC::Segment handle trivial types.
Definition: Segment.h:189
A SC::Buffer with a dedicated custom inline buffer to avoid heap allocation.
Definition: Buffer.h:34