title: How to handle type redundancy in external libraries?
body: <p>I'm writing a computer graphics application that makes use of several different libraries that provide many of the required features.</p>
  
  <p>For example, in college I wrote quaternion, optimized 3 vector, and optimized 4x4 matrix classes that I like to use since they are efficient and I'm very familiar with them.  I also wrote a K-D Tree implementation that only depends on the STL.</p>
  
  <p>Finally, I'm using ITK to compute the minimum eigenvalue (and associated eigenvector) of a given symmetric real matrix, among other things.</p>
  
  <p>With all of these libraries containing their own matrix and vector implementations, I find myself doing a lot of this sort of thing:</p>
  
  <pre><code>FixedPointType closestPointCoords;
  KdTree::HyperPoint target(3);
  target[0] = transformedPoint[0];
  target[1] = transformedPoint[1];
  target[2] = transformedPoint[2];
  KdTree::Neighbor result = FixedPointTree.FindNearestNeighbor(target);
  minimumDistance = result.GetDistance();
  closestPointCoords[0] = result.GetPoint().Get(0);
  closestPointCoords[1] = result.GetPoint().Get(1);
  closestPointCoords[2] = result.GetPoint().Get(2);
  </code></pre>
  
  <p>Obviously I could write a function that looks something like:  </p>
  
  <pre><code>FixedPointType ConvertHyperPointToFixedPointType(const KdTree::HyperPoint &amp;p)
  {
      FixedPointType fixedPoint;
      fixedPoint[0] = p[0];
      fixedPoint[1] = p[1];
      fixedPoint[2] = p[2];
  
      return fixedPoint;
   }
  </code></pre>
  
  <p>I'd like to avoid adding inter-library dependancy by doing something like this:</p>
  
  <pre><code>class HyperPoint
  {
      ...
      operator FixedPointType() 
      {
           // Same content as ConvertHyperPointToFixedPointType above
      }
      ...
  };
  </code></pre>
  
  <p>But I'm wondering if anyone has come up with a more elegant solution to this problem.  </p>
  
  <p>Perhaps I should take a page out of the .NET book and create a static Convert class?  </p>
  
  <p>How have you handled type redundancy across libraries and the need to frequently convert between the various types?  </p>
  
  <p>Is the <a href="http://en.wikipedia.org/wiki/Adapter_pattern" rel="nofollow noreferrer">Adapter</a> pattern appropriate here?  </p>
  
  <p><strong>UPDATE</strong><br>
  After some more exploring I came across <a href="http://msdn.microsoft.com/en-us/library/ayybcxe5(VS.71).aspx" rel="nofollow noreferrer">TypeConverter</a>.  So this problem looks like it's been addressed in C#.  I'm still looking for a clean, efficient and simple way to do it in C++.  So far I'm thinking the canonical types, or maybe just the bare ConvertHyperPointToFixedPointType kind of functions.</p>
  
  <p><strong>UPDATE 2</strong>
  After even more reading it sounds like the canonical type will actually act as the shim.  See this <a href="http://www.synesis.com.au/resources/articles/cpp/shims.pdf" rel="nofollow noreferrer">paper</a> for more info.  </p>
  
  <p>Here's some sample code to illustrate my current inclination:</p>
  
  <pre><code>class VectorConverter
  {
  public:
      VectorConverter(const VectorImp1 &amp;v1)
      {
          _data[0] = v1[0];
          _data[1] = v1[1];
          _data[2] = v1[2];
      }
  
      VectorConverter(const VectorImp2 &amp;v2)
      {
          _data[0] = v2.Get(0);
          _data[1] = v2.Get(1);
          _data[2] = v2.Get(2);
      }
  
      operator VectorImp1()
      {
          return VectorImp1(_data[0], _data[1], _data[2]);
      }
  
      operator VectorImp2()
      {
          VectorImp2 v2;
          v2.Set(0, _data[0]);
          v2.Set(1, _data[1]);
          v2.Set(2, _data[2]);
          return v2;
      }
  
      // actual vector implementation goes here or a wrapper around an existing one
  };
  
  // example usage
  VectorImp1 closestPointCoords, target;
  // for the sake of illustration I included the cast, but you could implicitly cast
  KdTree::Neighbor result = FixedPointTree.FindNearestNeighbor((VectorImp2)VectorConverter(target));
  minimumDistance = result.GetDistance();
  closestPointCoords = (VectorImp1)VectorConverter(result.GetPoint());
  </code></pre>
  
  <p>Thanks for all your help.  Feel free to update this post if someone comes across something better.</p>