title: RAII wrapper for OpenGL objects
body: <p>I want to write a simple RAII wrapper for OpenGL objects (textures, frame buffers, etc.) I have noticed, that all <code>glGen*</code> and <code>glDelete*</code> functions share the same signature, so my first attempt was like this:</p>
  
  <pre><code>typedef void (__stdcall *GLGenFunction)(GLsizei, GLuint *);
  typedef void (__stdcall *GLDelFunction)(GLsizei, const GLuint *);
  
  template &lt;GLGenFunction glGenFunction, GLDelFunction glDelFunction&gt;
  class GLObject
  {
      GLuint m_name;
  public:
      GLObject() 
      {  
          glGenFunction(1, &amp;m_name);
      }
  
      ~GLObject()
      {
          glDelFunction(1, &amp;m_name);
      }
  
      GLuint getName() {return m_name;}
  };
  
  typedef GLObject&lt;glGenTextures, glDeleteTextures&gt; GLTexture;
  </code></pre>
  
  <p>It works fine for textures, but fails for frame buffers: <code>glGenFramebuffers</code> and <code>glDeleteFramebuffers</code> function addresses are not known at compile time, and cannot be used as template arguments. So I made second version:</p>
  
  <pre><code>class GLObjectBase
  {
      GLuint m_name;
      GLDelFunction m_delFunction;
  
  public:
      GLObjectBase(GLGenFunction genFunc, GLDelFunction delFunction)
          : m_delFunction(delFunction)
      {
          genFunc(1, &amp;m_name);
      }
  
      GLuint getName()
      {
          return m_name;
      }
  
  protected:
      ~GLObjectBase()
      {
          m_delFunction(1, &amp;m_name);
      }
  };
  
  class GLFrameBuffer : public GLObjectBase
  {
  public:
      GLFrameBuffer() : GLObjectBase(glGenFramebuffers, glDeleteFramebuffers) {}
  };
  </code></pre>
  
  <p>But I don't like it since I have to store del function pointer in each instance that will not change at run-time.</p>
  
  <p>How do I make wrapper class that stores only object name in each instance without resorting to create a bunch of almost copy-pasted classes?</p>
  
  <p>I could do something like this:</p>
  
  <pre><code>template &lt;int N&gt;
  class GLObject2
  {
      GLuint m_name;
      static GLDelFunction glDelFunction;
  public:
      GLObject2(GLGenFunction genFunction, GLDelFunction delFunc)
      {  
          genFunction(1, &amp;m_name);
          if ( glDelFunction == nullptr )
              glDelFunction = delFunc;
          ASSERT(glDelFunction == delFunc);
      }
  
      GLuint getName() {return m_name;}
  
  protected:
      ~GLObject2()
      {
          glDelFunction(1, &amp;m_name);
      }
  };
  
  template &lt;int N&gt;
  GLDelFunction GLObject2&lt;N&gt;::glDelFunction = nullptr;
  
  class GLTexture: public GLObject2&lt;1&gt;
  {
  public:
      GLTexture(): GLObject2&lt;1&gt;(glGenTextures, glDeleteTextures) {}
  };
  
  class GLRenderBuffer: public GLObject2&lt;2&gt;
  {
  public:
      GLRenderBuffer(): GLObject2&lt;2&gt;(glGenRenderbuffers, glDeleteRenderbuffers) {}
  };
  </code></pre>
  
  <p>Can anyone suggest more elegant solution?</p>