Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automate pimpl'ing of C++ classes -- is there an easy way?

Pimpl's are a source of boilerplate in a lot of C++ code. They seem like the kind of thing that a combination of macros, templates, and maybe a little external tool help could solve, but I'm not sure what the easiest way would be. I've seen templates that help do some of the lifting but not much -- you still end up needing to write forwarding functions for every method of the class that you're trying to wrap. Is there an easier way?

I'm imagining a tool used as part of the make process. You want your public headers to be pimpl'd classes, so you provide some input file, say pimpl.in, that lists the classes (implemented un-pimpl'd) that you'd like to wrap, then that file is examined, the pimpl classes are generated and only their headers (not the original class's headers) are installed during a 'make install'. The problem is I don't see any way to do this without a full blown C++ parser, something even compiler vendors can't get right. Maybe the classes could be written in someway that makes the job of an external tool easier, but I'm sure I'd end up missing all sorts of corner cases (e.g. templated classes and/or templated member functions).

Any ideas? Has anyone else come with a solution for this problem already?

like image 414
Joseph Garvin Avatar asked Oct 25 '09 17:10

Joseph Garvin


People also ask

What is impl CPP?

History of C++ [edit] "Pointer to implementation" or "pImpl" is a C++ programming technique that removes implementation details of a class from its object representation by placing them in a separate class, accessed through an opaque pointer: // -------------------- // interface (widget.

Why is Pimpl an idiom?

The pimpl idiom is a modern C++ technique to hide implementation, to minimize coupling, and to separate interfaces. Pimpl is short for "pointer to implementation." You may already be familiar with the concept but know it by other names like Cheshire Cat or Compiler Firewall idiom.


2 Answers

No, there isn't an easy answer. :-( I would think with nearly every OO expert saying "prefer composition over inheritance", there would be language support for making composition a whole lot easier than inheritance.

like image 141
Ðаn Avatar answered Oct 01 '22 12:10

Ðаn


I am not saying this is good (just something that sprang to mind).
But you could experiment with overloading the operator ->

#include <memory>
#include <iostream>

class X
{
    public:
    void plop()
    {
        std::cout << "Plop\n";
    }
};

class PimplX
{
    public:
        PimplX()
        {
            pimpl.reset(new X);
        }
        X* operator->()
        {
            return pimpl.get();
        }
    private:
        PimplX(PimplX const&);
        PimplX& operator=(PimplX const&);
        std::auto_ptr<X>    pimpl;
};   


int main()
{    
    PimplX      x;

    x->plop();
}
like image 41
Martin York Avatar answered Oct 01 '22 12:10

Martin York