PIMPL Pattern Guide¶
This document introduces the usage of the PIMPL (Pointer to Implementation) pattern in the DAWidgets project, helping developers correctly use the macro definitions provided by the project.
Core Features¶
Features
- ✅ Hide Implementation Details: Private members and data are hidden in the implementation class
- ✅ Reduce Header Dependencies: Lower compilation dependencies, faster builds
- ✅ Binary Compatibility: Implementation changes do not affect binary compatibility
- ✅ Unified Macros: The project provides standard macros to simplify implementation
- ✅ Consistent with data-workbench: Macro definitions are verbatim identical, submodule integration without conflicts
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¶
1 2 3 4 5 6 7 8 9 | |
Advantages of the PIMPL Pattern¶
1 2 3 4 5 6 7 8 9 10 | |
PIMPL Pattern Advantages
- Build Speed: Modifying private members only requires recompiling a single cpp file
- Binary Compatibility: Private member changes do not affect ABI compatibility
- Clean Headers: Headers contain only public interface declarations
DAWidgets PIMPL Macros¶
The project defines the following macros in src/DAWidgetsGlobals.h:
| Macro | Purpose | Usage Location |
|---|---|---|
DA_DECLARE_PRIVATE(Class) |
Declare the private data pointer | Inside class declaration |
DA_DECLARE_PUBLIC(Class) |
Declare the host pointer | Inside PrivateData class |
DA_PIMPL_CONSTRUCT |
Initialize the private data pointer | Constructor initializer list |
DA_D(name) |
Get private data pointer (non-const) | In member functions |
DA_DC(name) |
Get private data pointer (const) | In const member functions |
DA_Q(class, name) |
Get host pointer (non-const) | In PrivateData methods |
DA_QC(class, name) |
Get host pointer (const) | In PrivateData const methods |
Maintenance Contract
The macro definitions in DAWidgetsGlobals.h must be verbatim identical to the corresponding macros in the data-workbench project's DAGlobals.h. Both sides are guarded by #ifndef, so whichever header is included first in any translation unit is safe (first-include-wins).
Implementation Steps¶
Step 1: Declare the Private Pointer in the Header¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Step 2: Define the PrivateData Class in the cpp File¶
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 | |
Step 3: Constructor Initialization¶
1 2 3 4 5 6 7 8 9 10 | |
Macro Expansion
DA_PIMPL_CONSTRUCT expands to: d_ptr(std::make_unique<PrivateData>(this))
Step 4: Access Private Data in Member Functions¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Complete Example¶
classDiagram
class DASelectComboBox {
+DA_DECLARE_PRIVATE(DASelectComboBox)
+setOptions(options : QStringList)
+options() : QStringList
-d_ptr : unique_ptr~PrivateData~
}
class PrivateData {
+DA_DECLARE_PUBLIC(DASelectComboBox)
+options : QStringList
+tags : QStringList
+allowCreate : bool
-q_ptr : DASelectComboBox*
}
DASelectComboBox --> PrivateData : d_ptr
PrivateData --> DASelectComboBox : q_ptr
Best Practices¶
When to Use the PIMPL Pattern¶
Recommended Scenarios
- The class has many private members or private methods
- The header has many dependencies and you want to reduce compilation coupling
- You need to maintain binary compatibility (e.g. for a library's public API)
- Private members may change frequently
Important Notes¶
Important Reminders
- Constructor must initialize: Use
DA_PIMPL_CONSTRUCTor manually initialized_ptr - Use DA_DC in const functions: Ensures const correctness
- PrivateData constructor parameter: Must accept the host pointer to initialize
q_ptr - PrivateData must not be exposed in headers: The PrivateData class definition only exists in the cpp file
Macro Expansion Details¶
| Macro | Expansion |
|---|---|
DA_DECLARE_PRIVATE(Class) |
class PrivateData; + friend + unique_ptr<PrivateData> d_ptr; + d_func() |
DA_DECLARE_PUBLIC(Class) |
friend + Class* q_ptr; + q_func() |
DA_PIMPL_CONSTRUCT |
d_ptr(std::make_unique<PrivateData>(this)) |
DA_D(name) |
PrivateData* name = d_func(); |
DA_DC(name) |
const PrivateData* name = d_func(); |