From 7c03d93c367b842c464dca30e121bc4c20547c36 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Jan 2022 16:44:19 +0000 Subject: Generic solution for glGen/glDel arrays, then tidy-up the uses --- lib/glArrays.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 lib/glArrays.h (limited to 'lib/glArrays.h') diff --git a/lib/glArrays.h b/lib/glArrays.h new file mode 100644 index 0000000..ed05eeb --- /dev/null +++ b/lib/glArrays.h @@ -0,0 +1,76 @@ +#pragma once + +#include +#include // IWYU pragma: keep +#include +#include +#include + +template class glArraysBase { + static_assert(N > 0); + +public: + ~glArraysBase() = default; + NO_COPY(glArraysBase); + CUSTOM_MOVE(glArraysBase); + + // NOLINTNEXTLINE(hicpp-explicit-conversions) + inline operator GLuint() const + { + static_assert(N == 1, "Implicit cast only if N == 1"); + return ids.front(); + } + + inline auto + operator[](size_t n) const + { + return ids[n]; + } + + constexpr static auto size {N}; + +protected: + glArraysBase() noexcept = default; + std::array ids {}; +}; + +template inline glArraysBase::glArraysBase(glArraysBase && src) noexcept : ids {src.ids} +{ + std::fill(src.ids.begin(), src.ids.end(), 0); +} + +template +inline glArraysBase & +glArraysBase::operator=(glArraysBase && src) noexcept +{ + ids = src.ids; + std::fill(src.ids.begin(), src.ids.end(), 0); + return *this; +} + +template class glArrays : public glArraysBase { +public: + using glArraysBase::glArraysBase; + using glArraysBase::operator=; + + DEFAULT_MOVE_COPY(glArrays); + + inline glArrays() noexcept + { + (*Gen)(N, this->ids.data()); + } + + inline ~glArrays() noexcept + { + if (this->ids.front()) { + (*Del)(N, this->ids.data()); + } + } +}; + +template using glVertexArrays = glArrays; +using glVertexArray = glVertexArrays<1>; +template using glBuffers = glArrays; +using glBuffer = glBuffers<1>; +template using glTextures = glArrays; +using glTexture = glTextures<1>; -- cgit v1.2.3