moka
moka

Reputation: 4501

c++: shared code across pimpl implementations

I am currently writing some code that has to be portable. For that purpose I am using the pimpl idiom since I think it seperates the actual implementations cleanly from the API. Anyways, the pimpl idiom only works very great if the implementations don't share code (i.e. some generic functions that are shared across implementations).

The other option would be abstract interfaces i guess.- Anyways since I am using pimpl across my whole project I really don't think it's a good idea to mix it with abstract interfaces (on the API level).

So what options would you suggest to share code across different pimpls? I thought about an abstract interface class for the pimpl itself, so that the actual API is still cleanly separated, but that seems like a weird idea too.

PS: I don't want to discuss if pimpl or abstract interfaces are better. From an API perspective I made the decision to go with pimpl and I'd like to stick with it.

Upvotes: 2

Views: 268

Answers (4)

Tom Kerr
Tom Kerr

Reputation: 10730

If the details of however you implement PIMPL are reusable, you shouldn't feel bad about making them public, per se. The main point is keeping the interface clean.

Sometimes you want to share helpers that really shouldn't be used externally, though. At that point, clearly documenting that things shouldn't be reused is the real problem you are trying to solve. I like to use detail/private headers for this.

// Implementation details, not for reuse
namespace some_public_ns {
namespace detail {
  // .. shared code
}}

Usually you'll put this detail code in its own header. Organizations will typically choose a convention for naming to avoid confusing.

thingajig.h
thingajig_priv.h
thingajig_detail.h

It won't keep people with shotguns out of your living room. I would say that you shouldn't try to. It's really hard to accidentally pull in this shared code, though.

Then in your cpp files that reuse the code, you can just include the detail namespace in anon namespace.

namespace some_public_ns {
namespace {
  using namespace detail;
}}

This allows you to keep clutter out of your namespaces, but keep from having to specify ::detail when implementing.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86509

Your class model can include abstract classes even if the implementation includes pimpls. The two are orthogonal.

However, if you're putting all your private methods into your pimpl, there can be a few hoops to jump through. Calling an instance method, wherever defined, requires a pointer to the instance. You can provide the instance pointer as an argument to a pimpl method, or implicitly within a private method in the outer class.

Another option would be to share by composition rather than inheritance, which has its own advantages.

Upvotes: 1

vz0
vz0

Reputation: 32933

You can just move the shared code to a new independent class, namespace or module.

Upvotes: 2

Naszta
Naszta

Reputation: 7744

VTK has great PIMPL design. Check it!

Here is the VTK coding standard.

Upvotes: 1

Related Questions