This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByteBuffer.h
104 lines (86 loc) · 2.75 KB
/
ByteBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
This file is part of duckOS.
duckOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
duckOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with duckOS. If not, see <https://www.gnu.org/licenses/>.
Copyright (c) Byteduck 2016-2022. All rights reserved.
*/
#pragma once
#include <memory>
namespace Duck {
class ByteBuffer {
public:
/**
* Allocates a new ByteBuffer. Copies will point to the same memory - use clone() to create a copy.
* @param size The size, in bytes, of the buffer.
*/
explicit ByteBuffer(size_t size);
/**
* Creates a new ByteBuffer that points to existing memory.
* @param ptr The pointer to memory.
* @param size The size, in bytes, of the buffer.
* @return The new ByteBuffer.
*/
static ByteBuffer adopt(void* ptr, size_t size);
/**
* Creates a new ByteBuffer from a copy of existing memory.
* @param ptr The pointer to memory.
* @param size The size, in bytes, of the buffer.
* @return The new ByteBuffer.
*/
static ByteBuffer copy(const void* ptr, size_t size);
/**
* Creates a new ByteBuffer from existing memory, which will not be freed on destruction.
* @param ptr The pointer to memory.
* @param size The size, in bytes, of the buffer.
* @return The new ByteBuffer.
*/
static ByteBuffer shadow(void* ptr, size_t size);
/**
* Creates a clone of the ByteBuffer.
* @return A new, cloned ByteBuffer.
*/
[[nodiscard]] ByteBuffer clone() const;
/**
* Gets a pointer to the memory in the buffer.
* @tparam T The type of the pointer.
* @return The pointer.
*/
template<typename T>
[[nodiscard]] T* data() const {
return (T*) m_ptr->ptr;
}
/**
* Gets the size of the buffer.
* @return The size, in bytes, of the buffer.
*/
[[nodiscard]] size_t size() const;
/**
* Gets the size, in terms of a type, of the buffer.
* @tparam T The type to get the size in terms of.
* @return The size, in Ts, of the buffer.
*/
template<typename T>
[[nodiscard]] size_t size() const {
return m_size / sizeof(T);
}
private:
explicit ByteBuffer(void* ptr, size_t size);
class BufferRef {
public:
explicit BufferRef(void* ptr);
~BufferRef();
void* ptr;
bool free_on_destroy = true;
};
std::shared_ptr<BufferRef> m_ptr;
size_t m_size;
};
}