diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-01 16:44:19 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-01 16:44:19 +0000 |
commit | 7c03d93c367b842c464dca30e121bc4c20547c36 (patch) | |
tree | 6df340dc3b0bd16f6ced2a051a68fc8a3c67a08c /lib | |
parent | First iteration with font/text support (diff) | |
download | ilt-7c03d93c367b842c464dca30e121bc4c20547c36.tar.bz2 ilt-7c03d93c367b842c464dca30e121bc4c20547c36.tar.xz ilt-7c03d93c367b842c464dca30e121bc4c20547c36.zip |
Generic solution for glGen/glDel arrays, then tidy-up the uses
Diffstat (limited to 'lib')
-rw-r--r-- | lib/glArrays.cpp | 21 | ||||
-rw-r--r-- | lib/glArrays.h | 76 | ||||
-rw-r--r-- | lib/glBuffers.cpp | 13 | ||||
-rw-r--r-- | lib/glBuffers.h | 64 | ||||
-rw-r--r-- | lib/glVertexArrays.cpp | 13 | ||||
-rw-r--r-- | lib/glVertexArrays.h | 64 |
6 files changed, 97 insertions, 154 deletions
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 <type_traits> + +// Base +static_assert(!std::is_default_constructible_v<glArraysBase<1>>); +static_assert(!std::is_copy_constructible_v<glArraysBase<1>>); +static_assert(!std::is_copy_assignable_v<glArraysBase<1>>); +static_assert(std::is_nothrow_move_constructible_v<glArraysBase<1>>); +static_assert(std::is_nothrow_move_assignable_v<glArraysBase<1>>); + +// Specialisations (glBuffer is an example of the typedef) +static_assert(std::is_nothrow_default_constructible_v<glBuffer>); +static_assert(!std::is_trivially_default_constructible_v<glBuffer>); +static_assert(std::is_nothrow_destructible_v<glBuffer>); +static_assert(!std::is_trivially_destructible_v<glBuffer>); +static_assert(std::is_default_constructible_v<glBuffer>); +static_assert(!std::is_copy_constructible_v<glBuffer>); +static_assert(!std::is_copy_assignable_v<glBuffer>); +static_assert(std::is_nothrow_move_constructible_v<glBuffer>); +static_assert(std::is_nothrow_move_assignable_v<glBuffer>); +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 <GL/glew.h> +#include <algorithm> // IWYU pragma: keep +#include <array> +#include <cstddef> +#include <special_members.hpp> + +template<size_t N> 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<GLuint, N> ids {}; +}; + +template<size_t N> inline glArraysBase<N>::glArraysBase(glArraysBase<N> && src) noexcept : ids {src.ids} +{ + std::fill(src.ids.begin(), src.ids.end(), 0); +} + +template<size_t N> +inline glArraysBase<N> & +glArraysBase<N>::operator=(glArraysBase<N> && src) noexcept +{ + ids = src.ids; + std::fill(src.ids.begin(), src.ids.end(), 0); + return *this; +} + +template<size_t N, auto Gen, auto Del> class glArrays : public glArraysBase<N> { +public: + using glArraysBase<N>::glArraysBase; + using glArraysBase<N>::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<size_t N> using glVertexArrays = glArrays<N, &glGenVertexArrays, &glDeleteVertexArrays>; +using glVertexArray = glVertexArrays<1>; +template<size_t N> using glBuffers = glArrays<N, &glGenBuffers, &glDeleteBuffers>; +using glBuffer = glBuffers<1>; +template<size_t N> using glTextures = glArrays<N, &glGenTextures, &glDeleteTextures>; +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 <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 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 <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 |