如何正确包装第三方库结构?

发布于 2024-11-03 09:27:07 字数 578 浏览 2 评论 0原文

在我的项目中,我使用不断变化的第三方库。我有这个库的包装类(桥模式+ Pimpl 模式)。因此,除了包装器实现之外,我的所有来源都没有看到该库。 该库有一个选项结构,例如

struct Options {
  double distance;
  double weight;
  int num_rabbits;
  // etc
};

我希望该结构在 GUI 模块中可用,以构建一个允许设置这些选项的对话框。我需要为其创建一个包装结构吗?例如

struct MP_ThatLibOptions {
  // exact member copy
  double distance;
  double weight;
  int num_rabbits;
  // etc
};

,或者我应该直接将该结构头包含到我的源代码中吗?

包装器的缺点是:复制粘贴、用于结构之间转换的额外代码。 直接包含的缺点是:破坏了 pimpl 习惯用法。

这个问题可能还有其他解决方案吗?

另外我想强调的是,第 3 方库在不断变化,因此我必须采用我的源来支持该库的每个新版本。

In my project I use a 3rd party library which is constantly changing. I have a wrapper class (Bridge pattern + Pimpl pattern) for this library. So none of my sources except wrapper implementation sees that library.
This library has an Options struct, e.g.

struct Options {
  double distance;
  double weight;
  int num_rabbits;
  // etc
};

I want this struct to be available in GUI module, to build a dialog that allows to set these options. Do I need to create a wrapper struct for it? E.g.

struct MP_ThatLibOptions {
  // exact member copy
  double distance;
  double weight;
  int num_rabbits;
  // etc
};

Or shall I directly include that struct header into my sources?

The drawbacks of wrapper are: copy-paste, additional code for converting between structs.
The drawbacks of direct include are: breaking the pimpl idiom.

May be there are any other solutions for this problem?

Also I want to emphasize that the 3rd party library is constantly changing, so I must adopt my sources to support every new version of the library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-11-10 09:27:07

Options 结构似乎是公共 API 的一部分,而不是 pimpl 习惯用法旨在隐藏的私有实现,因此您似乎应该在源代码中包含标头。

如果您这样做,当第 3 方代码发生更改时,唯一可能损坏的代码是直接使用 Options 结构成员的代码,但大概当发生更改时,您的代码无论如何都需要进行适当的更改,因为这听起来像这将是一个 API 更改,所以看起来您已被覆盖。

The Options struct appears to be part of the public API, and not the private implementation that the pimpl idiom is design to hide, so it would seem that you should be including the header in your sources.

If you do this, the only code that will be liable to breakage when the 3rd party code changes is that code which uses the members of the Options struct directly, but presumably when that changes, your code needs to change appropriately anyway since that sounds like it would be an API change, so seems like you're covered.

╭⌒浅淡时光〆 2024-11-10 09:27:07

当我使用 Bridge + pimpl 模式时,我决定完全隐藏第 3 方库。

As I use Bridge + pimpl patterns I've decided to totally hide the 3rd party library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文