2.2 KiB
2.2 KiB
t00018 - Pimpl pattern
Config
compilation_database_dir: ..
output_directory: puml
diagrams:
t00018_class:
type: class
glob:
- ../../tests/t00018/**.cc
using_namespace:
- clanguml::t00018
include:
namespaces:
- clanguml::t00018
Source code
File t00018.h
#pragma once
#ifndef _MSC_VER
#include <experimental/propagate_const>
#endif
#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_impl.h
#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.cc
#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;
} // namespace t00018
} // namespace clanguml
File t00018_impl.cc
#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';
}
} // namespace impl
} // namespace t00018
} // namespace clanguml