FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 25, 2008

Making Pimpl Easy

(Page 5 of 5)

Pimpl at Work

Having been using the described Pimpl quite extensively lately, I could not help noticing that deploying other programming techniques was easy with the Pimpl. We have already talked about lazy data instantiation. Other examples might include deploying the Singleton:

// declaration
struct Foo : public pimpl<Foo>::pointer_semantics
{
    // The public "constructor".
    // Does not create new data but returns
    // a reference to the singleton instance.
    Foo();
 
    private:
 
    // Actual constructor.
    Foo(parameters);
};

// implementation
Foo::Foo(): base(null())
{
    static Foo single_foo(parameters);
    *this = single_foo;
}

or managing/accessing a dictionary:

// In the implementation file
typedef std::map<string, Book> AllBooks;
 
static AllBooks books;
 
Book::Book(string const& title) : base(null())
{
    AllBooks::iterator it = books.find(title);
 
    // If the title found, return it.
    // Otherwise, return an invalid book.
    if (it != books.end()) *this = it->second;
}

or easy integration with boost::serialization and many other applications of the described Pimpl.

Conclusion

Writing a conventional Pimpl-based class is not hard. However, repeating the same scaffolding over and over again is tedious and error-prone. Why do that if we do not have to? The suggested Pimpl generalization technique seems flexible, minimal and elegant, and helpful. It is yet another small gadget in your programming toolbox to make your work fun. Grab it, use it, tell me if there is anything missed and/or wrong and together we will get it even better.

Acknowledgements

Many thanks to the people on the Boost developers mailing list for their constructive suggestions and especially to Peter Dimov for his incomplete-type management technique and the implementation of boost::impl_ptr ([10]) that I used the ideas for pimpl::impl_ptr from.

References

1. Guru of the Week #24. http://www.gotw.ca/gotw/024.htm
2. Herb Sutter. Exceptional C++ (Addison-Wesley, 1999)
3. J. Carolan. Constructing bullet-proof classes. In Proceedings C++ at Work'89 (SIGS Publications, 1989)
4. James O. Coplien. Advanced C++ Programming Styles and Idioms (Addison-Wesley, 1992)
5. Eric Gamma et al. Design Patterns (Addison-Wesley,1995)
6. Paul J. Asente & Ralph R. Swick. X Window System Toolkit (Butterworth-Heinemann, 1985)
7. Peter Kümmel. The Loki library. http://loki-lib.sourceforge.net/index.php?n=Idioms.Pimpl
8. Asger Mangaard. http://article.gmane.org/gmane.comp.lib.boost.devel/132547
9. Boost File Vault. http://www.boost-consulting.com/vault/index.php
10. Peter Dimov. The boost::impl_ptr source code. http://tech.groups.yahoo.com/group/boost/files/impl_ptr/

Previous Page | 1 The Pimpl Idiom | 2 The User Perspective | 3 Behind the Interface | 4 Pimpl and Dynamic Polymorphism | 5 Pimpl at Work
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK