Gromacs
2021.3
|
#include <gromacs/utility/classhelpers.h>
Helper class to manage a pointer to a private implementation class.
This helper provides the following benefits (all but the last could also be achieved with std::unique_ptr):
Move construction and assignment are also disallowed, but can be enabled by providing explicit move constructor and/or assignment.
Intended use:
Note that ExampleClass::~ExampleClass cannot be declared inline (or generated by the compiler) because the implementation of impl_ requires that ExampleClass::Impl be known in size, whereas it has only been forward declared. Only the translation unit where ExampleClass::Impl is declared can define the destructor for ExampleClass (which may be defaulted).
Public Member Functions | |
PrivateImplPointer (std::nullptr_t) | |
Allow implicit initialization from nullptr to support comparison. | |
PrivateImplPointer (Impl *ptr) | |
Initialize with the given implementation class. | |
void | reset (Impl *ptr) |
Sets a new implementation class and destructs the previous one. More... | |
Impl * | get () |
Access the raw pointer. | |
Impl * | operator-> () |
Access the implementation class as with a raw pointer. | |
Impl & | operator* () |
Access the implementation class as with a raw pointer. | |
const Impl * | operator-> () const |
Access the implementation class as with a raw pointer. | |
const Impl & | operator* () const |
Access the implementation class as with a raw pointer. | |
operator bool () const | |
Allows testing whether the implementation is initialized. | |
bool | operator== (const PrivateImplPointer &other) const |
Tests for equality (mainly useful against nullptr). | |
bool | operator!= (const PrivateImplPointer &other) const |
Tests for inequality (mainly useful against nullptr). | |
|
inline |
Sets a new implementation class and destructs the previous one.
Needed, e.g., to implement lazily initializable or copy-assignable classes.