代码之家  ›  专栏  ›  技术社区  ›  Gabe

如何处理外部库中的类型冗余?

  •  1
  • Gabe  · 技术社区  · 17 年前

    我正在编写一个计算机图形应用程序,它利用了几个不同的库,这些库提供了许多所需的功能。

    例如,在大学里,我编写了四元数、优化的3向量和优化的4x4矩阵类,我喜欢使用这些类,因为它们很高效,我对它们非常熟悉。我还编写了一个仅依赖于STL的K-D树实现。

    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);
    

    FixedPointType ConvertHyperPointToFixedPointType(const KdTree::HyperPoint &p)
    {
        FixedPointType fixedPoint;
        fixedPoint[0] = p[0];
        fixedPoint[1] = p[1];
        fixedPoint[2] = p[2];
    
        return fixedPoint;
     }
    

    class HyperPoint
    {
        ...
        operator FixedPointType() 
        {
             // Same content as ConvertHyperPointToFixedPointType above
        }
        ...
    };
    

    也许我应该翻一页。NET书籍并创建静态Convert类?

    是 Adapter

    更新
    TypeConverter

    paper 了解更多信息。

    class VectorConverter
    {
    public:
        VectorConverter(const VectorImp1 &v1)
        {
            _data[0] = v1[0];
            _data[1] = v1[1];
            _data[2] = v1[2];
        }
    
        VectorConverter(const VectorImp2 &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());
    

    2 回复  |  直到 17 年前
        1
  •  1
  •   Nicolas Dorier    17 年前

    适配器模式的问题在于,适配的类型应该是一个接口或抽象类,或者可能是一个具有多个虚拟方法的类。

    canonical

        2
  •  1
  •   Carl Seleborg    17 年前

    警告:

    template <class ToType, class FromType>
    ToType ConvertPoint(const FromType& FromValue)
    {
      ToType ToValue;
      ToValue[0] = FromValue[0];
      ToValue[1] = FromValue[1];
      ToValue[2] = FromValue[2];
    
      return ToValue;
    }
    
    // If the version above failed, the compiler will try this one.
    // This is called SFINAE
    template <class ToType, class FromType>
    ToType ConvertPoint(const FromType& FromValue)
    {
      ToType ToValue;
      ToValue[0] = FromValue.Get(0);
      ToValue[1] = FromValue.Get(1);
      ToValue[2] = FromValue.Get(2);
    
      return ToValue;
    }
    
    // Usage:
    KdTree::HyperPoint hyperPoint;
    FixedPointType fixedPoint = ConvertPoint<FixedPointType>(hyperPoint);
    

    shims