summaryrefslogtreecommitdiff
path: root/lib/glVertexArrays.h
blob: 803206ad97e2d27524ecc6dc1e6da1d59ebd4f24 (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 GLVERTEXARRAYS_H
#define GLVERTEXARRAYS_H

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

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

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

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

	NO_COPY(glVertexArrays);
	CUSTOM_MOVE(glVertexArrays);

	// 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> glVertexArrays<N>::glVertexArrays(glVertexArrays<N> && src) noexcept : ids {src.ids}
{
	std::fill(src.ids.begin(), src.ids.end(), -1);
}

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

using glVertexArray = glVertexArrays<1>;

#endif