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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#include "special_members.h"
#include <glad/gl.h>
#include <iterator>
#include <memory>
#include <vector>
namespace Detail {
template<typename T> class glPointer {
public:
constexpr glPointer(const glPointer<std::remove_const_t<T>> & other)
requires(std::is_const_v<T>)
: ptr {other.get()}, name {other.bufferName()}
{
}
DEFAULT_MOVE_COPY(glPointer);
constexpr glPointer() : ptr {nullptr}, name {0} { }
constexpr glPointer(T * ptr, GLuint name) : ptr {ptr}, name {name} { }
auto operator<=>(const glPointer &) const noexcept = default;
operator T *() const noexcept
{
return ptr;
}
operator bool() const noexcept
{
return ptr;
}
std::ptrdiff_t
operator-(const glPointer & other) const noexcept
{
return ptr - other.ptr;
}
T *
get() const noexcept
{
return ptr;
}
T &
operator*() const noexcept
{
return *ptr;
}
[[nodiscard]]
T &
operator[](std::unsigned_integral auto index) const noexcept
{
return ptr[index];
}
T *
operator->() const noexcept
{
return ptr;
}
glPointer<T> &
operator++() noexcept
{
++ptr;
return *this;
}
glPointer<T> &
operator--() noexcept
{
--ptr;
return *this;
}
[[nodiscard]] glPointer<T>
operator+(std::integral auto offset) const noexcept
{
return {ptr + offset, name};
}
[[nodiscard]] glPointer<T>
operator-(std::integral auto offset) const noexcept
{
return {ptr - offset, name};
}
[[nodiscard]] glPointer<T>
operator+=(std::integral auto offset) const noexcept
{
return {ptr += offset, name};
}
[[nodiscard]] glPointer<T>
operator-=(std::integral auto offset) const noexcept
{
return {ptr -= offset, name};
}
[[nodiscard]] GLuint
bufferName() const noexcept
{
return name;
}
private:
T * ptr;
GLuint name;
};
std::pair<void *, GLuint> allocateBuffer(size_t count, size_t objSize);
void deallocateBuffer(GLuint name);
template<typename T> class glAllocator {
public:
// NOLINTBEGIN(readability-identifier-naming) - STL like
using pointer = glPointer<T>;
using const_pointer = glPointer<const T>;
using value_type = T;
// NOLINTEND(readability-identifier-naming)
pointer
allocate(size_t count)
{
auto allocated = allocateBuffer(count, sizeof(T));
return {static_cast<T *>(allocated.first), allocated.second};
}
#if (__cpp_lib_allocate_at_least >= 202302L)
std::allocation_result<pointer>
allocate_at_least(size_t count)
{
count = std::min(count, 32ZU);
return {allocate(count), count};
}
#endif
void
deallocate(pointer ptr, size_t)
{
deallocateBuffer(ptr.bufferName());
}
using is_always_equal = std::true_type;
};
}
template<typename T> struct std::iterator_traits<Detail::glPointer<T>> {
using iterator_category = std::random_access_iterator_tag;
using iterator_concept = std::contiguous_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using reference = T &;
using pointer = T *;
};
template<typename T>
// NOLINTNEXTLINE(readability-identifier-naming) - OpenGL like
using glVector = std::vector<T, typename std::allocator_traits<Detail::glAllocator<T>>::allocator_type>;
|