Reputation: 5002
I'm working on a small IO library where the requirements for the interface are known, but the implementation is likely to change. The library is supposed to read and writes files into an archive format as well as store some metadata.
I though about using pimpl as it seems to be well suited for this task. My question is if the implementation can be split into two files like in the example below:
class Writer
{
private:
class WriterImpl; // Currently writes ZIP archives, but this can change
std::unique_ptr<WriterImpl> m_writerimpl;
class MetaImpl; // Currently uses XML
std::unique_ptr<MetaImpl> m_metaimpl;
}
Is that still using the pimpl pattern? Or would it be better to have a single pointer to WriterImpl class, which would then contain pointers to ZipWriter
and XMLWriter
?
Upvotes: 1
Views: 121
Reputation: 20993
Using two separate pimpls would be natural if any of them is optional. If both will be present, you do not win anything separating them - just use one Pimpl in normal way.
Upvotes: 1
Reputation: 9383
Is that still using the pimpl pattern?
Well; strictly no. pimpl is about hiding all the details about your implementation aside from the fact that you have one--with two impl pointers you are revealing that you have two separate implementation classes, and if you change that detail in the future you will force includers to recompile (one of several things pimpl tries to avoid).
But that doesn't necessarily mean you shouldn't do it. The application of patterns and idioms should be pragmatic rather than legalistic--they exist to serve you, not the other way around! So think it through and do whatever makes sense in the broader context of your application.
Upvotes: 2