Search code examples
c++wrapperbridgepimpl-idiom

How to correctly wrap 3rd party library struct?


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.


Solution

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