Channels ▼
RSS

C/C++

Making Pimpl Easy


Behind the Interface

Safely tucked away, the implementation is hidden. Not merely separated from the interface into another class or a header file, but truly internal. It is all ours to implement as we wish, to optimize as we like, to modify as often as we need. Still, the deployment pattern is easy to remember and fairly straightforward to follow. Something like the following will be in some for-our-eyes-only book_implementation.cpp file:

template<>
struct
pimpl<Book>::implementation
{
    implementation(
        string const& the_title,
        string const& the_author)
    :
        title(the_title),
        author(the_author)
    {}
 
    string title;
    string author;
    int price;
};
 
Book::Book(
    string const& title,
    string const& author)
:
    base(title, author)
{}

string const&
Book::author() const
{
    implementation const& impl = **this;
    return impl.author;
}
 
void
Book::set_price(int new_price)
{
    (*this)->price = new_price;
}

In addition, if comparison functionality is required, as mentioned earlier, a class with value semantics will have to implement something like the following:

bool
Book::operator==(Book const& that) const
{
    implementation const& self = **this;
    return self.title = that.title && self.author == that.author;
}

Notably, pimpl<Book>::implementation is again a struct rather than a class. As long as the declaration is local to one file, there is generally little value in making it a class (your mileage may vary).

Another liberating and unifying feature is that we do not need to follow (and fight over) a particular naming convention to draw attention to member variables (like the trailing underscore, the 'm_' prefix or the myriad others). Member variables are accessed and clearly identified as impl.title or (*this)->title or something of that sort.

An important design-related point to note is that the external Book class describes and implements the behavior, while the internal pimpl<Book>::implementation is all about data. I consider that clean separation of data and behavior to be a good code-management technique and good programming style. Data and behavior are different views of a system. They serve different purposes and are easier managed when kept separate. At this point OO fans should not be getting up in arms about that perceived attempt to pull data and behavior apart. Indeed, the association of data with behavior is the cornerstone of the OO programming paradigm. However, in all (that I know of) languages that association is done in the most direct and economical way — by tying data and the behavior together in a class. Straightforward and good for many applications, that kind of data-behavior association is not exactly ideal for implementation hiding purposes. The Pimpl idiom creates data-behavior association in a different way that better suits our implementation-hiding purpose.

Fully Encapsulated Memory Management and Book::null()

Most often Pimpl implementations ultimately boil down to an opaque pointer to the internal implementation data. That data is allocated on the memory heap. That heap-allocated data has to be managed. The family of smart-pointer classes (like std::auto_ptr, boost::shared_ptr and the like) take good care of objects after they are created. Our technique takes it one step further by fully automating memory management with better encapsulated internal-data management and less room for user error. For our Book class instead of the more conventional:

Book::Book(string const& title, string const& author)
:
    base(new pimpl<Book>::implementation(title, author))
{}

we simply write:

Book::Book(string const& title, string const& author)
:
    base(title, author)
{}

All arguments passed to the base will be diligently forwarded to the matching pimpl<Book>::implementation constructor or fail to compile if a suitable constructor is not found. The base is an actual ready-to-go convenience typedef to simplify references to the base class. That forwarding mechanism works for the constructor with no parameters as well. That is,

Book::Book() : base() {}

or the same but not as explicit

Book::Book() {}

will try to call pimpl<Book>::implementation::implementation() and fail if there is no such.

Here it distinctly differs from the conventional approach (deployed by the smart-pointer family) where an implementation object is created manually and explicitly and then again manually associated with the interface object. The pimpl's approach demonstrates a considerably stronger (and automatically managed) association between the public pimpl-derived class (the interface) and its internal implementation. Hence, the default behavior is that there is always an implementation data behind every interface object. To override this default behavior we might write something like:

Book::Book() : base(null())
{
    // an invalid Book object is created
    // that does not have data behind it
}

void
Book::do_something()
{
    if (!*this)
    {
        // implementation is created only when needed.
        implementation* impl = new implementation(...);
        this->reset(impl);
    }
    // do actual processing
    ...
}

What happens here is that we explicitly (via null()) instruct the underlying pimpl base to be created empty/invalid (like the NULL pointer or an empty boost::shared_ptr()). Later we create an implementation object explicitly and assign the base to manage it. That technique is useful for lazy instantiation optimization (as in the example above) or to support dynamic polymorphism that is discussed later.

Above we used null() to create an invalid Book object with no internal data:

Book::Book() : base(null()) {}

We might use such an invalid Book object to indicate a no-book condition in the same fashion as the NULL pointer is used:

Book
find_book()
{
    ...
    // found nothing, return an invalid Book
    return Book();
}

...
Book book = find_book();

if (!book) report book-not-found;

Well, there is no need to write code constructing such an invalid object. All pimpl-based classes already have this — that same mentioned null(). Fully qualified for our Book example, it is Book::null(). Consequently, the code above is most likely to look as follows:

Book
find_book()
{
    ...
    // found nothing, return an invalid Book
    return Book::null();
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video