Added template memento pattern
This commit is contained in:
@@ -24,6 +24,7 @@
|
|||||||
* [t00023](./test_cases/t00023.md) - Strategy pattern
|
* [t00023](./test_cases/t00023.md) - Strategy pattern
|
||||||
* [t00024](./test_cases/t00024.md) - Proxy pattern
|
* [t00024](./test_cases/t00024.md) - Proxy pattern
|
||||||
* [t00025](./test_cases/t00025.md) - Template proxy pattern
|
* [t00025](./test_cases/t00025.md) - Template proxy pattern
|
||||||
|
* [t00026](./test_cases/t00026.md) - Template memento pattern
|
||||||
## Sequence diagrams
|
## Sequence diagrams
|
||||||
* [t20001](./test_cases/t20001.md) - Basic sequence diagram
|
* [t20001](./test_cases/t20001.md) - Basic sequence diagram
|
||||||
## Configuration diagrams
|
## Configuration diagrams
|
||||||
|
|||||||
83
docs/test_cases/t00026.md
Normal file
83
docs/test_cases/t00026.md
Normal 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
|
||||||
|

|
||||||
BIN
docs/test_cases/t00026_class.png
Normal file
BIN
docs/test_cases/t00026_class.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 32 KiB |
12
tests/t00026/.clanguml
Normal file
12
tests/t00026/.clanguml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
compilation_database_dir: ..
|
||||||
|
output_directory: puml
|
||||||
|
diagrams:
|
||||||
|
t00026_class:
|
||||||
|
type: class
|
||||||
|
glob:
|
||||||
|
- ../../tests/t00026/t00026.cc
|
||||||
|
using_namespace:
|
||||||
|
- clanguml::t00026
|
||||||
|
include:
|
||||||
|
namespaces:
|
||||||
|
- clanguml::t00026
|
||||||
58
tests/t00026/t00026.cc
Normal file
58
tests/t00026/t00026.cc
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
54
tests/t00026/test_case.h
Normal file
54
tests/t00026/test_case.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
/**
|
||||||
|
* tests/t00026/test_case.cc
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST_CASE("t00026", "[test-case][class]")
|
||||||
|
{
|
||||||
|
auto [config, db] = load_config("t00026");
|
||||||
|
|
||||||
|
auto diagram = config.diagrams["t00026_class"];
|
||||||
|
|
||||||
|
REQUIRE(diagram->name == "t00026_class");
|
||||||
|
|
||||||
|
REQUIRE(diagram->include.namespaces.size() == 1);
|
||||||
|
REQUIRE_THAT(diagram->include.namespaces,
|
||||||
|
VectorContains(std::string{"clanguml::t00026"}));
|
||||||
|
|
||||||
|
REQUIRE(diagram->exclude.namespaces.size() == 0);
|
||||||
|
|
||||||
|
REQUIRE(diagram->should_include("clanguml::t00026::A"));
|
||||||
|
|
||||||
|
auto model = generate_class_diagram(db, diagram);
|
||||||
|
|
||||||
|
REQUIRE(model.name == "t00026_class");
|
||||||
|
|
||||||
|
auto puml = generate_class_puml(diagram, model);
|
||||||
|
AliasMatcher _A(puml);
|
||||||
|
|
||||||
|
REQUIRE_THAT(puml, StartsWith("@startuml"));
|
||||||
|
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
|
||||||
|
REQUIRE_THAT(puml, IsClassTemplate("Memento", "T"));
|
||||||
|
REQUIRE_THAT(puml, IsClassTemplate("Originator", "T"));
|
||||||
|
REQUIRE_THAT(puml, IsClassTemplate("Caretaker", "T"));
|
||||||
|
REQUIRE_THAT(puml,
|
||||||
|
IsInstantiation(_A("Originator<T>"), _A("Originator<std::string>")));
|
||||||
|
REQUIRE_THAT(puml,
|
||||||
|
IsInstantiation(_A("Caretaker<T>"), _A("Caretaker<std::string>")));
|
||||||
|
|
||||||
|
save_puml(
|
||||||
|
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
|
||||||
|
}
|
||||||
@@ -129,6 +129,7 @@ using namespace clanguml::test::matchers;
|
|||||||
#include "t00023/test_case.h"
|
#include "t00023/test_case.h"
|
||||||
#include "t00024/test_case.h"
|
#include "t00024/test_case.h"
|
||||||
#include "t00025/test_case.h"
|
#include "t00025/test_case.h"
|
||||||
|
#include "t00026/test_case.h"
|
||||||
|
|
||||||
//
|
//
|
||||||
// Sequence diagram tests
|
// Sequence diagram tests
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ test_cases:
|
|||||||
- name: t00025
|
- name: t00025
|
||||||
title: Template proxy pattern
|
title: Template proxy pattern
|
||||||
description:
|
description:
|
||||||
|
- name: t00026
|
||||||
|
title: Template memento pattern
|
||||||
|
description:
|
||||||
Sequence diagrams:
|
Sequence diagrams:
|
||||||
- name: t20001
|
- name: t20001
|
||||||
title: Basic sequence diagram
|
title: Basic sequence diagram
|
||||||
|
|||||||
Reference in New Issue
Block a user