137 lines
2.1 KiB
Markdown
137 lines
2.1 KiB
Markdown
# t00018 - Pimpl pattern
|
|
## Config
|
|
```yaml
|
|
compilation_database_dir: ..
|
|
output_directory: puml
|
|
diagrams:
|
|
t00018_class:
|
|
type: class
|
|
glob:
|
|
- ../../tests/t00018/**.h
|
|
- ../../tests/t00018/**.cc
|
|
using_namespace:
|
|
- clanguml::t00018
|
|
include:
|
|
namespaces:
|
|
- clanguml::t00018
|
|
|
|
```
|
|
## Source code
|
|
File t00018_impl.h
|
|
```cpp
|
|
#pragma once
|
|
|
|
#include "t00018.h"
|
|
|
|
namespace clanguml {
|
|
namespace t00018 {
|
|
namespace impl {
|
|
|
|
class widget {
|
|
int n;
|
|
|
|
public:
|
|
void draw(const clanguml::t00018::widget &w) const;
|
|
void draw(const clanguml::t00018::widget &w);
|
|
widget(int n);
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
```
|
|
File t00018.h
|
|
```cpp
|
|
#pragma once
|
|
|
|
#include <experimental/propagate_const>
|
|
#include <iostream>
|
|
#include <memory>
|
|
|
|
namespace clanguml {
|
|
namespace t00018 {
|
|
|
|
namespace impl {
|
|
class widget;
|
|
}
|
|
|
|
// Pimpl example based on https://en.cppreference.com/w/cpp/language/pimpl
|
|
class widget {
|
|
std::unique_ptr<impl::widget> pImpl;
|
|
|
|
public:
|
|
void draw() const;
|
|
void draw();
|
|
bool shown() const { return true; }
|
|
widget(int);
|
|
~widget();
|
|
|
|
widget(widget &&);
|
|
|
|
widget(const widget &) = delete;
|
|
widget &operator=(widget &&);
|
|
widget &operator=(const widget &) = delete;
|
|
};
|
|
}
|
|
}
|
|
|
|
```
|
|
File t00018.cc
|
|
```cpp
|
|
#include "t00018.h"
|
|
#include "t00018_impl.h"
|
|
|
|
namespace clanguml {
|
|
namespace t00018 {
|
|
|
|
void widget::draw() const { pImpl->draw(*this); }
|
|
|
|
void widget::draw() { pImpl->draw(*this); }
|
|
|
|
widget::widget(int n)
|
|
: pImpl{std::make_unique<impl::widget>(n)}
|
|
{
|
|
}
|
|
|
|
widget::widget(widget &&) = default;
|
|
|
|
widget::~widget() = default;
|
|
|
|
widget &widget::operator=(widget &&) = default;
|
|
}
|
|
}
|
|
|
|
```
|
|
File t00018_impl.cc
|
|
```cpp
|
|
#include "t00018_impl.h"
|
|
#include "t00018.h"
|
|
|
|
namespace clanguml {
|
|
namespace t00018 {
|
|
namespace impl {
|
|
|
|
widget::widget(int n)
|
|
: n(n)
|
|
{
|
|
}
|
|
|
|
void widget::draw(const clanguml::t00018::widget &w) const
|
|
{
|
|
if (w.shown())
|
|
std::cout << "drawing a const widget " << n << '\n';
|
|
}
|
|
|
|
void widget::draw(const clanguml::t00018::widget &w)
|
|
{
|
|
if (w.shown())
|
|
std::cout << "drawing a non-const widget " << n << '\n';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
```
|
|
## Generated UML diagrams
|
|

|