Added template decorator pattern

This commit is contained in:
Bartek Kryza
2021-07-25 22:51:46 +02:00
parent 16e60ec8c5
commit ee753f5595
11 changed files with 221 additions and 2 deletions

View File

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

View File

@@ -74,7 +74,6 @@ struct StringMemento {
Caretaker<std::string> caretaker;
Originator<std::string> originator;
};
}
}

79
docs/test_cases/t00027.md Normal file
View File

@@ -0,0 +1,79 @@
# t00027 - Template decorator pattern
## Config
```yaml
compilation_database_dir: ..
output_directory: puml
diagrams:
t00027_class:
type: class
glob:
- ../../tests/t00027/t00027.cc
using_namespace:
- clanguml::t00027
include:
namespaces:
- clanguml::t00027
```
## Source code
File t00027.cc
```cpp
#include <memory>
#include <tuple>
#include <type_traits>
namespace clanguml {
namespace t00027 {
class Shape {
public:
virtual void display() = 0;
virtual ~Shape() = default;
};
template <template <typename> class... T>
class Line : public Shape, public T<Line<>>... {
public:
void display() override
{
std::apply([](auto &&... x) { (x.display(), ...); },
std::forward_as_tuple(T<Line<>>()...));
}
};
template <template <typename> class... T>
class Text : public Shape, public T<Text<>>... {
public:
void display() override
{
std::apply([](auto &&... x) { (x.display(), ...); },
std::forward_as_tuple(T<Text<>>()...));
}
};
struct ShapeDecorator {
virtual void display() = 0;
};
template <typename T> class Color : public ShapeDecorator {
public:
void display() override {}
};
template <typename T> class Weight : public ShapeDecorator {
public:
void display() override {}
};
struct Window {
Line<Color, Weight> border;
Line<Color> divider;
Text<Color, Weight> title;
Text<Color> description;
};
}
}
```
## Generated UML diagrams
![t00027_class](./t00027_class.png "Template decorator pattern")

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View File

@@ -271,7 +271,8 @@ public:
try {
relstr << m_model.to_alias(uns, ns_relative(uns, b.name))
<< " <|-- "
<< m_model.to_alias(uns, ns_relative(uns, c.name))
<< m_model.to_alias(
uns, ns_relative(uns, c.full_name(uns)))
<< std::endl;
ostr << relstr.str();
}

View File

@@ -319,6 +319,8 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
cp.access = class_parent::access_t::kPublic;
}
LOG_DBG("Found base class {} for class {}", cp.name, c.name);
c.bases.emplace_back(std::move(cp));
}

12
tests/t00027/.clanguml Normal file
View File

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00027_class:
type: class
glob:
- ../../tests/t00027/t00027.cc
using_namespace:
- clanguml::t00027
include:
namespaces:
- clanguml::t00027

55
tests/t00027/t00027.cc Normal file
View File

@@ -0,0 +1,55 @@
#include <memory>
#include <tuple>
#include <type_traits>
namespace clanguml {
namespace t00027 {
class Shape {
public:
virtual void display() = 0;
virtual ~Shape() = default;
};
template <template <typename> class... T>
class Line : public Shape, public T<Line<>>... {
public:
void display() override
{
std::apply([](auto &&... x) { (x.display(), ...); },
std::forward_as_tuple(T<Line<>>()...));
}
};
template <template <typename> class... T>
class Text : public Shape, public T<Text<>>... {
public:
void display() override
{
std::apply([](auto &&... x) { (x.display(), ...); },
std::forward_as_tuple(T<Text<>>()...));
}
};
struct ShapeDecorator {
virtual void display() = 0;
};
template <typename T> class Color : public ShapeDecorator {
public:
void display() override {}
};
template <typename T> class Weight : public ShapeDecorator {
public:
void display() override {}
};
struct Window {
Line<Color, Weight> border;
Line<Color> divider;
Text<Color, Weight> title;
Text<Color> description;
};
}
}

66
tests/t00027/test_case.h Normal file
View File

@@ -0,0 +1,66 @@
/**
* tests/t00027/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("t00027", "[test-case][class]")
{
auto [config, db] = load_config("t00027");
auto diagram = config.diagrams["t00027_class"];
REQUIRE(diagram->name == "t00027_class");
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00027"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00027::A"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00027_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, IsAbstractClass(_A("Shape")));
REQUIRE_THAT(puml, IsAbstractClass(_A("ShapeDecorator")));
REQUIRE_THAT(puml, IsClassTemplate("Line", "T<>"));
REQUIRE_THAT(puml, IsClassTemplate("Text", "T<>"));
REQUIRE_THAT(puml, IsInstantiation(_A("Line<T<>>"), _A("Line<Color>")));
REQUIRE_THAT(
puml, IsInstantiation(_A("Line<T<>>"), _A("Line<Color,Weight>")));
REQUIRE_THAT(puml, IsInstantiation(_A("Text<T<>>"), _A("Text<Color>")));
REQUIRE_THAT(
puml, IsInstantiation(_A("Text<T<>>"), _A("Text<Color,Weight>")));
REQUIRE_THAT(
puml, IsAggregation(_A("Window"), _A("Line<Color,Weight>"), "+border"));
REQUIRE_THAT(
puml, IsAggregation(_A("Window"), _A("Line<Color>"), "+divider"));
REQUIRE_THAT(
puml, IsAggregation(_A("Window"), _A("Text<Color,Weight>"), "+title"));
REQUIRE_THAT(
puml, IsAggregation(_A("Window"), _A("Text<Color>"), "+description"));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -130,6 +130,7 @@ using namespace clanguml::test::matchers;
#include "t00024/test_case.h"
#include "t00025/test_case.h"
#include "t00026/test_case.h"
#include "t00027/test_case.h"
//
// Sequence diagram tests

View File

@@ -75,6 +75,9 @@ test_cases:
- name: t00026
title: Template memento pattern
description:
- name: t00027
title: Template decorator pattern
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram