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.cpp | 21 ++++++++++++++ lib/glArrays.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/glBuffers.cpp | 13 --------- lib/glBuffers.h | 64 ------------------------------------------ lib/glVertexArrays.cpp | 13 --------- lib/glVertexArrays.h | 64 ------------------------------------------ 6 files changed, 97 insertions(+), 154 deletions(-) create mode 100644 lib/glArrays.cpp create mode 100644 lib/glArrays.h delete mode 100644 lib/glBuffers.cpp delete mode 100644 lib/glBuffers.h delete mode 100644 lib/glVertexArrays.cpp delete mode 100644 lib/glVertexArrays.h (limited to 'lib') diff --git a/lib/glArrays.cpp b/lib/glArrays.cpp new file mode 100644 index 0000000..7c5b2ea --- /dev/null +++ b/lib/glArrays.cpp @@ -0,0 +1,21 @@ +#include "glArrays.h" +#include + +// Base +static_assert(!std::is_default_constructible_v>); +static_assert(!std::is_copy_constructible_v>); +static_assert(!std::is_copy_assignable_v>); +static_assert(std::is_nothrow_move_constructible_v>); +static_assert(std::is_nothrow_move_assignable_v>); + +// Specialisations (glBuffer is an example of the typedef) +static_assert(std::is_nothrow_default_constructible_v); +static_assert(!std::is_trivially_default_constructible_v); +static_assert(std::is_nothrow_destructible_v); +static_assert(!std::is_trivially_destructible_v); +static_assert(std::is_default_constructible_v); +static_assert(!std::is_copy_constructible_v); +static_assert(!std::is_copy_assignable_v); +static_assert(std::is_nothrow_move_constructible_v); +static_assert(std::is_nothrow_move_assignable_v); +static_assert(sizeof(glBuffer) == sizeof(GLuint)); 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>; diff --git a/lib/glBuffers.cpp b/lib/glBuffers.cpp deleted file mode 100644 index cb5ab2a..0000000 --- a/lib/glBuffers.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "glBuffers.h" - -void -glBuffersBase::gen(GLsizei n, GLuint * ids) -{ - glGenBuffers(n, ids); -} - -void -glBuffersBase::del(GLsizei n, const GLuint * ids) -{ - glDeleteBuffers(n, ids); -} diff --git a/lib/glBuffers.h b/lib/glBuffers.h deleted file mode 100644 index e2c09df..0000000 --- a/lib/glBuffers.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef GLBUFFERS_H -#define GLBUFFERS_H - -#include -#include // IWYU pragma: keep -#include -#include -#include - -class glBuffersBase { -protected: - static void gen(GLsizei, GLuint *); - static void del(GLsizei, const GLuint *); -}; - -template 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 ids {}; -}; - -template glBuffers::glBuffers(glBuffers && src) noexcept : ids {src.ids} -{ - std::fill(src.ids.begin(), src.ids.end(), -1); -} - -template -glBuffers & -glBuffers::operator=(glBuffers && src) noexcept -{ - ids = src.ids; - std::fill(src.ids.begin(), src.ids.end(), -1); - return *this; -} - -using glBuffer = glBuffers<1>; - -#endif diff --git a/lib/glVertexArrays.cpp b/lib/glVertexArrays.cpp deleted file mode 100644 index 372b49b..0000000 --- a/lib/glVertexArrays.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "glVertexArrays.h" - -void -glVertexArraysBase::gen(GLsizei n, GLuint * ids) -{ - glGenVertexArrays(n, ids); -} - -void -glVertexArraysBase::del(GLsizei n, const GLuint * ids) -{ - glDeleteVertexArrays(n, ids); -} diff --git a/lib/glVertexArrays.h b/lib/glVertexArrays.h deleted file mode 100644 index 803206a..0000000 --- a/lib/glVertexArrays.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef GLVERTEXARRAYS_H -#define GLVERTEXARRAYS_H - -#include -#include // IWYU pragma: keep -#include -#include -#include - -class glVertexArraysBase { -protected: - static void gen(GLsizei, GLuint *); - static void del(GLsizei, const GLuint *); -}; - -template 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 ids {}; -}; - -template glVertexArrays::glVertexArrays(glVertexArrays && src) noexcept : ids {src.ids} -{ - std::fill(src.ids.begin(), src.ids.end(), -1); -} - -template -glVertexArrays & -glVertexArrays::operator=(glVertexArrays && src) noexcept -{ - ids = src.ids; - std::fill(src.ids.begin(), src.ids.end(), -1); - return *this; -} - -using glVertexArray = glVertexArrays<1>; - -#endif -- cgit v1.2.3