A similar question was asked here, and I'm trying to do the same thing. But
- I'm trying a different approach by deriving from
std::array
, and - This is a very focused question about constructors only.
Here is my gl_wrap.h
:
// header guards snipped
#include <array>
#include <algorithm>
#include <cstring>
#include <GL/gl.h>
namespace gl_wrap {
// aliases for style consistency and to mark as parts of gl_wrap
using gl_name = GLuint;
using gl_enum = GLenum;
template<size_t N>
class buffer_objects : public std::array<gl_name, N> {
public:
buffer_objects() noexcept;
~buffer_objects();
buffer_objects(const buffer_objects&) = delete;
buffer_objects operator=(const buffer_objects&) = delete;
buffer_objects(buffer_objects&& from) noexcept;
buffer_objects& operator=(buffer_objects&& from) noexcept;
};
template<size_t N>
buffer_objects<N>::buffer_objects() noexcept
{
glGenBuffers(N, this->data());
}
template<size_t N>
buffer_objects<N>::~buffer_objects()
{
glDeleteBuffers(N, this->data());
}
template<size_t N>
buffer_objects<N>::buffer_objects(buffer_objects<N>&& from) noexcept
: std::array<gl_name, N>(std::move(from))
{
memset(from.data(), 0, N * sizeof(gl_name));
}
template<size_t N>
buffer_objects<N>& buffer_objects<N>::operator=(buffer_objects<N>&& from)
noexcept
{
std::array<gl_name, N>::operator=(std::move(from));
memset(from.data(), 0, N * sizeof(gl_name));
return *this;
}
}
// namespace gl_wrap
Some specific questions I have about this code/approach are, if applicable,
- Error handling design decision: should the constructors throw, or should I leave error checking, if desired, to the caller?
- Design decision: is it a good idea to use templates here? Will this lead to problematic code bloat if I'm using a lot of different-sized
buffer_objects
, or is it efficient? Can I evaluate this through profiling? - Are the
memcpy
s worth it to put the rvalues in a valid non-owning state, or can I treat the rvalues as non-owning leftovers? - Am I properly using the base class's move constructor?
std::array
class member variable instead. \$\endgroup\$