summaryrefslogtreecommitdiff
path: root/lib/glBuffers.h
blob: e2c09dfbc111d9e076f0b47a5da750a464c4409f (plain)
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
#ifndef GLBUFFERS_H
#define GLBUFFERS_H

#include <GL/glew.h>
#include <algorithm> // IWYU pragma: keep
#include <array>
#include <cstddef>
#include <special_members.hpp>

class glBuffersBase {
protected:
	static void gen(GLsizei, GLuint *);
	static void del(GLsizei, const GLuint *);
};

template<size_t N> class glBuffers : glBuffersBase {
public:
	glBuffers()
	{
		gen(N, ids.data());
	}

	~glBuffers()
	{
		del(N, ids.data());
	}

	NO_COPY(glBuffers);
	CUSTOM_MOVE(glBuffers);

	// NOLINTNEXTLINE(hicpp-explicit-conversions)
	operator GLuint() const
	{
		static_assert(N == 1, "Implicit cast only if N == 1");
		return ids.front();
	}

	auto
	operator[](size_t n) const
	{
		return ids[n];
	}

private:
	std::array<GLuint, N> ids {};
};

template<size_t N> glBuffers<N>::glBuffers(glBuffers<N> && src) noexcept : ids {src.ids}
{
	std::fill(src.ids.begin(), src.ids.end(), -1);
}

template<size_t N>
glBuffers<N> &
glBuffers<N>::operator=(glBuffers<N> && src) noexcept
{
	ids = src.ids;
	std::fill(src.ids.begin(), src.ids.end(), -1);
	return *this;
}

using glBuffer = glBuffers<1>;

#endif