From 20646758939562c0fbad4d52774efcfacf7e22fc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Dec 2021 14:10:34 +0000 Subject: RAII for glVertex and glBuffer --- lib/glBuffers.cpp | 13 +++++++++++++ lib/glBuffers.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/glVertexArrays.cpp | 13 +++++++++++++ lib/glVertexArrays.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 lib/glBuffers.cpp create mode 100644 lib/glBuffers.h create mode 100644 lib/glVertexArrays.cpp create mode 100644 lib/glVertexArrays.h (limited to 'lib') diff --git a/lib/glBuffers.cpp b/lib/glBuffers.cpp new file mode 100644 index 0000000..cb5ab2a --- /dev/null +++ b/lib/glBuffers.cpp @@ -0,0 +1,13 @@ +#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 new file mode 100644 index 0000000..e91293c --- /dev/null +++ b/lib/glBuffers.h @@ -0,0 +1,48 @@ +#ifndef GLBUFFERS_H +#define GLBUFFERS_H + +#include +#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); + NO_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 {}; +}; +using glBuffer = glBuffers<1>; + +#endif diff --git a/lib/glVertexArrays.cpp b/lib/glVertexArrays.cpp new file mode 100644 index 0000000..372b49b --- /dev/null +++ b/lib/glVertexArrays.cpp @@ -0,0 +1,13 @@ +#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 new file mode 100644 index 0000000..ab31751 --- /dev/null +++ b/lib/glVertexArrays.h @@ -0,0 +1,48 @@ +#ifndef GLVERTEXARRAYS_H +#define GLVERTEXARRAYS_H + +#include +#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); + NO_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 {}; +}; +using glVertexArray = glVertexArrays<1>; + +#endif -- cgit v1.2.3