Added template memento pattern

This commit is contained in:
Bartek Kryza
2021-07-25 22:19:00 +02:00
parent 98cf942a9e
commit 16e60ec8c5
8 changed files with 212 additions and 0 deletions

View File

@@ -24,6 +24,7 @@
* [t00023](./test_cases/t00023.md) - Strategy pattern
* [t00024](./test_cases/t00024.md) - Proxy pattern
* [t00025](./test_cases/t00025.md) - Template proxy pattern
* [t00026](./test_cases/t00026.md) - Template memento pattern
## Sequence diagrams
* [t20001](./test_cases/t20001.md) - Basic sequence diagram
## Configuration diagrams

83
docs/test_cases/t00026.md Normal file
View File

@@ -0,0 +1,83 @@
# t00026 - Template memento pattern
## Config
```yaml
compilation_database_dir: ..
output_directory: puml
diagrams:
t00026_class:
type: class
glob:
- ../../tests/t00026/t00026.cc
using_namespace:
- clanguml::t00026
include:
namespaces:
- clanguml::t00026
```
## Source code
File t00026.cc
```cpp
#include <iostream>
#include <memory>
#include <unordered_map>
namespace clanguml {
namespace t00026 {
template <typename T> class Memento {
public:
Memento(T &&v)
: m_value(std::forward<T>(v))
{
}
T value() const { return m_value; }
private:
T m_value;
};
template <typename T> class Originator {
public:
Originator(T &&v)
: m_value(std::forward<T>(v))
{
}
Memento<T> memoize_value() const { return Memento<T>{m_value}; }
void load(const Memento<T> &m) { m_value = m.value(); }
void print() const { std::cout << m_value << std::endl; }
void set(T &&v) { m_value = std::forward<T>(v); }
private:
T m_value;
};
template <typename T> class Caretaker {
public:
Memento<T> &state(const std::string &n) { return m_mementos.at(n); }
void set_state(const std::string &s, Memento<T> &&m)
{
m_mementos.try_emplace(s, std::move(m));
}
private:
std::unordered_map<std::string, Memento<T>> m_mementos;
};
struct StringMemento {
Caretaker<std::string> caretaker;
Originator<std::string> originator;
};
}
}
```
## Generated UML diagrams
![t00026_class](./t00026_class.png "Template memento pattern")

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB