title: Designing a shader class
body: <p>Since I have started learning OpenGL, I thought I would as well write a small C++ framework (for myself) to avoid the nausea that the excessive use of C-ish code is apparently causing. <code>:)</code> Since I am intending to stick with Qt, the framework uses some Qt classes. </p>
  
  <p>The first thing I really needed was an easy way to use shaders and programs. Here's my idea of the shader class. </p>
  
  <pre><code>class Shader
  {
  public:
      //create a shader with no source code 
      explicit Shader(GLenum shaderType);
      //create a shader with source code and compile it
      Shader(GLenum shaderType, const QString&amp; sourceCode);
      //create a shader from source file and compile it
      Shader(GLenum shaderType, QFile&amp; sourceFile);
      ~Shader();
  
      //change the source code and recompile
      void Source(QFile&amp; sourceFile);
      void Source(const QString&amp; sourceCode);
  
      GLuint get() const; //get the handle
  
  private:
      //common part for creation in different constructors
      void createShader(GLenum shaderType); 
      //compile
      void compile();
  
  private:
      GLuint handle;
  };
  </code></pre>
  
  <p>It must be pretty obvious what the different functions are doing. Each is calling the relevant OpenGL routines, checks for errors and throws exceptions in case of any failure. The constructor calls <code>glCreateShader</code>. Now the tricky part. The destructor needs to call <code>glDeleteShader(handle);</code> but in this case I have a dilemma:</p>
  
  <p><strong>Option 1:</strong> Disable assignment and copying. This has the upside of avoiding reference counting and the downside of being forced to use shared_pointers to put these in vectors and passing around in general.</p>
  
  <p><strong>Option 2:</strong> Enable reference counting. This has the obvious upside of enabling copying, and therefore storing in containers(which I will need to later pass a range of shaders to a program). The downside is the following:</p>
  
  <pre><code>Shader s1(GL_VERTEX_SHADER, QFile("MyVertexShader.vp"));
  Shader s2(s1);
  s2.Source(QFile("MyOtherVertexShader.vp"));
  </code></pre>
  
  <p>As you see, I changed the source of s1 via s2, because they share the same internal shader handle. To be honest, I don't see a big problem here. I wrote the class, so I know its copy-semantics are like this and I'm OK with it. The problem is I am not sure this kind of design is ever acceptable. All this could be achieved with Option1 + shared pointers, with the only difference that I don't want to have a shared pointer every time I create a shader (not for performance reasons - just for syntactic convenience).</p>
  
  <p><strong>Q1:</strong> Please comment on the options and optionally the whole idea.<sup><strong>1</strong></sup><br>
  <strong>Q2:</strong> If I were to choose option 2, do I have to implement it myself or there's a ready class in boost or Qt which I could derive from or have a member of and I would get a free reference counting?<br>
  <strong>Q3:</strong> Do you agree that making <code>Shader</code> an abstract class and having three derived classes <code>VertexShader</code>, <code>FragmentShader</code>, and <code>GeometryShader</code> would be overkill? <br></p>
  
  <p><sup><strong>1</strong></sup><sub> If you should refer me to an existing C++ OpenGL framework, that's very good(since I haven't actually found one) but that should really be a side note than an answer to my questions. Also note that I have seen a QGLShader class somewhere in the docs, but it is apparently not present in my version of Qt and I have my reasons to avoid upgrading right now.</sub></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks for the answers. I eventually decided to make my shader class immutable by removing the source functions. The shader gets compiled at creation and has no non-const member-functions. Thus a simple reference counting solves all my problems at once.</p>