Refactored nested diagrams with element_view template

This commit is contained in:
Bartek Kryza
2023-05-27 22:31:46 +02:00
parent ba32b54395
commit e6fa19ff39
10 changed files with 445 additions and 576 deletions

View File

@@ -18,6 +18,7 @@
#pragma once
#include "common/model/diagram.h"
#include "common/model/element_view.h"
#include "common/model/package.h"
#include "common/model/source_file.h"
#include "common/types.h"
@@ -27,9 +28,13 @@
namespace clanguml::include_diagram::model {
using clanguml::common::opt_ref;
using clanguml::common::model::diagram_element;
using clanguml::common::model::source_file;
class diagram : public clanguml::common::model::diagram,
public clanguml::common::model::nested_trait<
clanguml::common::model::source_file,
public clanguml::common::model::element_view<source_file>,
public clanguml::common::model::nested_trait<source_file,
clanguml::common::model::filesystem_path> {
public:
diagram() = default;
@@ -41,33 +46,58 @@ public:
common::model::diagram_t type() const override;
common::optional_ref<common::model::diagram_element> get(
const std::string &full_name) const override;
opt_ref<diagram_element> get(const std::string &full_name) const override;
common::optional_ref<common::model::diagram_element> get(
common::model::diagram_element::id_t id) const override;
opt_ref<diagram_element> get(diagram_element::id_t id) const override;
void add_file(std::unique_ptr<common::model::source_file> &&f);
common::optional_ref<common::model::source_file> get_file(
const std::string &name) const;
template <typename ElementT>
opt_ref<ElementT> find(const std::string &name) const;
common::optional_ref<common::model::source_file> get_file(
common::model::diagram_element::id_t id) const;
template <typename ElementT>
opt_ref<ElementT> find(diagram_element::id_t id) const;
std::string to_alias(const std::string &full_name) const;
const common::reference_vector<common::model::source_file> &files() const;
const common::reference_vector<source_file> &files() const;
common::optional_ref<clanguml::common::model::diagram_element>
get_with_namespace(const std::string &name,
opt_ref<diagram_element> get_with_namespace(const std::string &name,
const common::model::namespace_ &ns) const override;
inja::json context() const override;
private:
common::reference_vector<common::model::source_file> files_;
};
template <typename ElementT>
opt_ref<ElementT> diagram::find(const std::string &name) const
{
// Convert the name to the OS preferred path
std::filesystem::path namePath{name};
namePath.make_preferred();
for (const auto &element : element_view<ElementT>::view()) {
const auto full_name = element.get().full_name(false);
if (full_name == namePath.string()) {
return {element};
}
}
return {};
}
template <typename ElementT>
opt_ref<ElementT> diagram::find(diagram_element::id_t id) const
{
for (const auto &element : element_view<ElementT>::view()) {
if (element.get().id() == id) {
return {element};
}
}
return {};
}
} // namespace clanguml::include_diagram::model
namespace clanguml::common::model {