如何在不添加对库的依赖的情况下添加与库结构的兼容性?

How to add compatibility with library structure without adding dependency on library?

我目前正在编写一个有时会与 OpenCV 一起使用的库。因为 OpenCV 定义了一个 Point_ class 常用于我的库将在其中使用的某些上下文,所以我想添加选项以将 Point_s 作为参数传递给我的一些函数。也就是说,OpenCV 是一个非常繁重的库,我非常不希望仅仅为了访问它的 Point_ class.

而依赖它

定义我自己的 Point_ 相同 Point_ class 导致预期的多重定义错误。

我考虑过使用 pre-processor 宏来检查包含 Point_ 的 OpenCV header 是否已经被包含并且只在没有被包含的情况下定义它,但我担心如果我的库首先包含 header,多重定义错误将 return,这将使我的库难以为除我以外的任何人使用。

有没有办法提供一个仅在其他地方没有定义时才使用的定义,and/or 如果出现在其他地方则将被覆盖?

一种解决方案是在您的项目构建配置中处理此问题:在您的构建系统中设置预处理器定义(例如 COMPILE_WITH_OPENCV)。如果您使用 CMake,它将类似于

ADD_DEFINITIONS(-DCOMPILE_WITH_OPENCV)

在代码中:

#if defined COMPILE_WITH_OPENCV
#include "types.hpp" // openCV inlcude
#else
#include "my_types.hpp"  // your own Point_ definition
#endif

您可以根据您的点class定义您的库,并可选择生成conversions 对于 OpenCV 库类型(如果存在)。像这样:

#ifdef HAVE_OPENCV
#include <opencv2/opencv.hpp>
#endif

struct my_point
{
    double x;
    double y;

#ifdef HAVE_OPENCV
    my_point(cv::Point2d p): x(p.x), y(p.y) {}

    operator cv::Point2d() const { return {x, y}; }
#endif
};

my_point my_function(my_point p)
{
    return p;
}

int main()
{
    cv::Point2d p;

    // automatic conversions happen between OpenCV version
    // and your library's version
    cv::Point2d q = my_function(p);
}

因为转换运算符是微不足道的内联函数,编译器会将它们完全优化掉,让代码就像根本没有发生任何转换一样。

您可以选择(最好是 imo)进行转换 explicit 这可能会使代码更安全:

struct my_point
{
    double x;
    double y;

#ifdef HAVE_OPENCV
    // make these explicit
    explicit my_point(cv::Point2d p): x(p.x), y(p.y) {}

    explicit operator cv::Point2d() const { return {x, y}; }
#endif
};

my_point my_function(my_point p)
{
    return p;
}

int main()
{
    cv::Point2d p;

    // Now the library user needs to explicitly ask for
    // the conversions to take place
    cv::Point2d q = cv::Point2d(my_function(my_point(p)));
}