title: C++: avoiding library-specific types in public interface
body: <p>I'm currently working on a little game engine project in  C++ using DirectX for rendering. The rendering part of the engine consists of classes such as <code>Model</code> and <code>Texture</code>. Because I would like to keep it (relatively) simple to switch to another rendering library (e.g. OpenGL) (and because I suppose it's just good encapsulation), I would like to keep the public interfaces of these classes completely devoid of any references to DirectX types, i.e. I would like to avoid providing public functions such as <code>ID3D11ShaderResourceView* GetTextureHandle();</code>.</p>
  
  <p>This becomes a problem, however, when a class such as <code>Model</code> requires the internal texture handle used by <code>Texture</code> to carry out its tasks - for instance when actually rendering the model. For simplicity's sake, let's replace DirectX with an arbitrary 3D rendering library that we'll call Lib3D. Here is an example demonstrating the issue I'm facing:</p>
  
  <pre><code>class Texture {
  private:
      Lib3DTexture mTexture;
  public:
      Texture(std::string pFileName)
          : mTexture(pFileName)
      {
      }
  };
  
  class Model {
  private:
      Texture* mTexture;
      Lib3DModel mModel;
  public:
      Model(std::string pFileName, Texture* pTexture)
          : mTexture(pTexture), mModel(pFileName)
      {
      }
      void Render()
      {
          mModel.RenderWithTexture( /* how do I get the Lib3DTexture member from Texture? */ );
      }
  };
  </code></pre>
  
  <p>Of course, I could provide a public <code>GetTextureHandle</code> function in <code>Texture</code> that simply returns a pointer to <code>mTexture</code>, but this would mean that if I change the underlying rendering library, I would also have to change the type returned by that function, thus changing the public interface of <code>Texture</code>. Worse yet, maybe the new library isn't even structured the same way, meaning I'd have to provide entirely new functions!</p>
  
  <p>The best solution I can think of is making <code>Model</code> a friend of <code>Texture</code> so that it can access <code>Texture</code>'s members directly. This seems slightly unwieldy, however, as I add more classes that make use of <code>Texture</code>. I have never used friendship much at all, so I'm not sure if this is even an acceptable usage case.</p>
  
  <p>So, my questions are:</p>
  
  <ul>
  <li>Is declaring Model a friend of Texture an acceptable use of
  friendship? Would it be a good solution?</li>
  <li>If no, what would you
  recommend? Do I need to redesign my class structure
  completely? In that case, any tips?</li>
  </ul>
  
  <p><em>PS: I realize that the title is not very descriptive and I apologize for that, but I didn't really know how to put it.</em></p>