-
Notifications
You must be signed in to change notification settings - Fork 15
pImpl implementation generator
pImpl is a C++ pattern also known as 'Pointer to Implementation'. According to this pattern C++ developer divides the class into two parts: public (interface) part and private (implementation). Both parts are C++ classes, but only interface is visible to the class users, and implementation is hidden behind pointer. Here is the basic sample:
Public header file:
class SomeClassImpl;
class SomeClass
{
public:
SomeClass();
~SomeClass();
void Foo();
private:
std::unique_ptr<SomeClassImpl> m_impl;
};
Private header file:
class SomeClassImpl;
{
public:
void Foo()
{
std::cout << "Hello world!" << std::endl;
}
};
Source file with public class implementation:
#include "SomeClass.h"
#include "SomeClassImpl.h"
SomeClass::SomeClass()
: m_impl(new SomeClassImpl())
{
}
void SomeClass::Foo()
{
m_impl->Foo();
}
The problem is obvious. Source file with public class implementation contains a lot of boilerplate code. Because of implementation of the public class methods is trivial: makes call of the corresponding method of the 'private' class and passes arguments to it. But then you change the interface of the 'public' class you should make the appropriate changes in the file with public class methods implementation: add/remove/reorder params, change return types, add or remove methods. And you should do it carefully.
Now, the code generation tool can do it for you! It analyzes the 'public' class interface and generates appropriate and suitable implementations of its methods. It's simple!