PIMPL Pattern Guide¶
Qwt hides private members with the PIMPL (Pointer to Implementation) pattern and the macros described below.
Key Features¶
The pattern keeps private members out of the header, reduces compilation dependencies, leaves binary compatibility unchanged, and uses a fixed set of project macros.
What is the PIMPL Pattern¶
PIMPL (Pointer to Implementation) is a C++ design pattern, also known as the "Compilation Firewall" pattern.
Drawbacks of the Traditional Approach¶
Without PIMPL, every private member is exposed in the header file; the traditional approach looks like this:
1 2 3 4 5 6 7 8 9 | |
Advantages of the PIMPL Pattern¶
With the PIMPL pattern, the header keeps only the public interface plus a QWT_DECLARE_PRIVATE declaration, while all members move into MyClass::PrivateData:
1 2 3 4 5 6 7 8 9 10 | |
Tip — PIMPL Pattern Advantages
- Build Speed: Modifying private members only requires recompiling a single cpp file
- Binary Compatibility: Changes to private members do not affect ABI compatibility
- Clean Headers: Header files only contain public interface declarations
Qwt PIMPL Macros¶
The project defines the following macros in src/qwt_global.h:
| Macro | Purpose | Usage Location |
|---|---|---|
QWT_DECLARE_PRIVATE(Class) |
Declare private data pointer | In class declaration |
QWT_DECLARE_PUBLIC(Class) |
Declare public host pointer | In PrivateData class |
QWT_PIMPL_CONSTRUCT |
Initialize private data pointer | Constructor initializer list |
QWT_D(name) |
Get private data pointer (non-const) | In member functions |
QWT_DC(name) |
Get private data pointer (const) | In const member functions |
Implementation Steps¶
Step 1: Declare Private Pointer in Header File¶
Use the QWT_DECLARE_PRIVATE macro in the class declaration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Step 2: Define the PrivateData Class in the cpp File¶
Define the private data class in the cpp file, using the QWT_DECLARE_PUBLIC macro:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Step 3: Constructor Initialization¶
Use the QWT_PIMPL_CONSTRUCT macro to initialize the private pointer:
1 2 3 4 5 6 7 8 9 | |
Info — Macro Expansion
QWT_PIMPL_CONSTRUCTexpands to:m_data(qwt_make_unique<PrivateData>(this))
Step 4: Destructor Handling¶
The destructor automatically deletes private data:
1 2 3 4 | |
Step 5: Accessing Private Data in Member Functions¶
Use the QWT_D and QWT_DC macros to access private data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Complete Example¶
The class diagram below shows how MyClass and its PrivateData reference each other through m_data and q_ptr:
classDiagram
class MyClass {
+QWT_DECLARE_PRIVATE(MyClass)
+setTitle(title : QString)
+title() : QString
-m_data : unique_ptr~PrivateData~
}
class PrivateData {
+QWT_DECLARE_PUBLIC(MyClass)
+title : QString
+value : int
+data : QVector~double~
-q_ptr : MyClass*
}
MyClass --> PrivateData : m_data
PrivateData --> MyClass : q_ptr
Header File MyClass.h¶
The complete header declares only the public interface and the QWT_DECLARE_PRIVATE(MyClass) macro, with no private members exposed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Source File MyClass.cpp¶
The implementation file defines MyClass::PrivateData with QWT_DECLARE_PUBLIC, initializes it via QWT_PIMPL_CONSTRUCT, and accesses it through QWT_D/QWT_DC:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
Best Practices¶
When to Use the PIMPL Pattern¶
Tip — Recommended Scenarios
- The class has many private members or private methods
- The class header has many dependencies and you want to reduce compilation dependencies
- Binary compatibility needs to be maintained (e.g., public API of a library)
- Private members may change frequently
When PIMPL is Not Necessary¶
Info — Optional Scenarios
- Simple, small classes with few private members
- Internal helper classes that do not need to hide their implementation
- Performance-sensitive scenarios (PIMPL has a slight overhead from indirect access)
Important Notes¶
Warning — Important Reminders
- Constructor Must Initialize: Use
QWT_PIMPL_CONSTRUCTor manually initializem_data- Use QWT_DC for Const Functions: Ensure const correctness
- PrivateData Constructor Parameter: Must accept the host pointer to initialize
q_ptr- Do Not Expose PrivateData in the Header: The PrivateData class definition should only exist in the cpp file
Macro Expansion Details¶
Each macro expands as follows:
| Macro | Expansion |
|---|---|
QWT_DECLARE_PRIVATE(Class) |
std::unique_ptr<PrivateData> m_data; + d_func() method |
QWT_DECLARE_PUBLIC(Class) |
Class* q_ptr; + q_func() method |
QWT_PIMPL_CONSTRUCT |
m_data(qwt_make_unique<PrivateData>(this)) |
QWT_D(name) |
Class::PrivateData* name = d_func(); |
QWT_DC(name) |
const Class::PrivateData* name = d_func(); |