Refactored generator inheritance hierarchy

This commit is contained in:
Bartek Kryza
2023-07-31 23:44:56 +02:00
parent eded9f4292
commit c119a622fa
22 changed files with 535 additions and 427 deletions

View File

@@ -17,6 +17,7 @@
*/
#pragma once
#include "common/generators/generator.h"
#include "common/model/diagram_filter.h"
#include "config/config.h"
#include "util/error.h"
@@ -30,8 +31,6 @@
#include <glob/glob.hpp>
#include <inja/inja.hpp>
#include <ostream>
namespace clanguml::common::generators::plantuml {
using clanguml::common::model::access_t;
@@ -49,7 +48,9 @@ std::string to_plantuml(message_t r);
* @tparam ConfigType Configuration type
* @tparam DiagramType Diagram model type
*/
template <typename ConfigType, typename DiagramType> class generator {
template <typename ConfigType, typename DiagramType>
class generator
: public clanguml::common::generators::generator<ConfigType, DiagramType> {
public:
/**
* @brief Constructor
@@ -58,25 +59,36 @@ public:
* @param model Reference to instance of @link clanguml::model::diagram
*/
generator(ConfigType &config, DiagramType &model)
: m_config{config}
, m_model{model}
: clanguml::common::generators::generator<ConfigType, DiagramType>{
config, model}
{
init_context();
init_env();
}
virtual ~generator() = default;
~generator() override = default;
/**
* @brief Generate diagram
*
* This method must be implemented in subclasses for specific diagram
* types. It is responsible for calling other methods in appropriate
* order to generate the diagram into the output stream.
* This is the main diagram generation entrypoint. It is responsible for
* calling other methods in appropriate order to generate the diagram into
* the output stream. It generates diagram elements, that are common
* to all types of diagrams in a given generator.
*
* @param ostr Output stream
*/
virtual void generate(std::ostream &ostr) const = 0;
void generate(std::ostream &ostr) const override;
/**
* @brief Generate diagram specific part
*
* This method must be implemented in subclasses for specific diagram
* types.
*
* @param ostr Output stream
*/
virtual void generate_diagram(std::ostream &ostr) const = 0;
/**
* @brief Generate diagram layout hints
@@ -169,8 +181,6 @@ private:
void init_env();
protected:
ConfigType &m_config;
DiagramType &m_model;
mutable std::set<std::string> m_generated_aliases;
mutable inja::json m_context;
mutable inja::Environment m_env;
@@ -222,13 +232,35 @@ inja::json generator<C, D>::element_context(const E &e) const
return ctx;
}
template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
update_context();
ostr << "@startuml" << '\n';
generate_plantuml_directives(ostr, config.puml().before);
generate_diagram(ostr);
generate_plantuml_directives(ostr, config.puml().after);
generate_metadata(ostr);
ostr << "@enduml" << '\n';
}
template <typename C, typename D>
void generator<C, D>::generate_config_layout_hints(std::ostream &ostr) const
{
using namespace clanguml::util;
const auto &config = generators::generator<C, D>::config();
// Generate layout hints
for (const auto &[entity_name, hints] : m_config.layout()) {
for (const auto &[entity_name, hints] : config.layout()) {
for (const auto &hint : hints) {
try {
if (hint.hint == config::hint_t::together) {
@@ -255,7 +287,10 @@ template <typename C, typename D>
void generator<C, D>::generate_row_column_hints(std::ostream &ostr,
const std::string &entity_name, const config::layout_hint &hint) const
{
const auto &uns = m_config.using_namespace();
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
const auto &uns = config.using_namespace();
std::vector<std::string> group_elements;
std::vector<std::pair<std::string, std::string>> element_aliases_pairs;
@@ -265,9 +300,9 @@ void generator<C, D>::generate_row_column_hints(std::ostream &ostr,
std::copy(group_tail.begin(), group_tail.end(),
std::back_inserter(group_elements));
auto element_opt = this->m_model.get(entity_name);
auto element_opt = model.get(entity_name);
if (!element_opt)
element_opt = this->m_model.get((uns | entity_name).to_string());
element_opt = model.get((uns | entity_name).to_string());
for (auto it = cbegin(group_elements);
it != cend(group_elements) && std::next(it) != cend(group_elements);
@@ -275,13 +310,13 @@ void generator<C, D>::generate_row_column_hints(std::ostream &ostr,
const auto &first = *it;
const auto &second = *std::next(it);
auto first_opt = this->m_model.get(first);
auto first_opt = model.get(first);
if (!first_opt)
first_opt = this->m_model.get((uns | first).to_string());
first_opt = model.get((uns | first).to_string());
auto second_opt = this->m_model.get(second);
auto second_opt = model.get(second);
if (!second_opt)
second_opt = this->m_model.get((uns | second).to_string());
second_opt = model.get((uns | second).to_string());
element_aliases_pairs.emplace_back(
first_opt.value().alias(), second_opt.value().alias());
@@ -299,17 +334,20 @@ template <typename C, typename D>
void generator<C, D>::generate_position_hints(std::ostream &ostr,
const std::string &entity_name, const config::layout_hint &hint) const
{
const auto &uns = m_config.using_namespace();
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
const auto &uns = config.using_namespace();
const auto &hint_entity = std::get<std::string>(hint.entity);
auto element_opt = m_model.get(entity_name);
auto element_opt = model.get(entity_name);
if (!element_opt)
element_opt = m_model.get((uns | entity_name).to_string());
element_opt = model.get((uns | entity_name).to_string());
auto hint_element_opt = m_model.get(hint_entity);
auto hint_element_opt = model.get(hint_entity);
if (!hint_element_opt)
hint_element_opt = m_model.get((uns | hint_entity).to_string());
hint_element_opt = model.get((uns | hint_entity).to_string());
if (!element_opt || !hint_element_opt)
return;
@@ -327,6 +365,9 @@ template <typename C, typename D>
void generator<C, D>::generate_plantuml_directives(
std::ostream &ostr, const std::vector<std::string> &directives) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
using common::model::namespace_;
for (const auto &d : directives) {
@@ -339,8 +380,8 @@ void generator<C, D>::generate_plantuml_directives(
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(directive, alias_match)) {
const auto full_name =
m_config.using_namespace() | std::get<0>(alias_match);
auto element_opt = m_model.get(full_name.to_string());
config.using_namespace() | std::get<0>(alias_match);
auto element_opt = model.get(full_name.to_string());
if (element_opt)
directive.replace(std::get<1>(alias_match),
@@ -383,9 +424,11 @@ template <typename C, typename D>
void generator<C, D>::generate_notes(
std::ostream &ostr, const model::element &e) const
{
const auto &config = generators::generator<C, D>::config();
for (const auto &decorator : e.decorators()) {
auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
if (note && note->applies_to_diagram(m_config.name)) {
if (note && note->applies_to_diagram(config.name)) {
ostr << "note " << note->position << " of " << e.alias() << '\n'
<< note->text << '\n'
<< "end note\n";
@@ -396,7 +439,9 @@ void generator<C, D>::generate_notes(
template <typename C, typename D>
void generator<C, D>::generate_metadata(std::ostream &ostr) const
{
if (m_config.generate_metadata()) {
const auto &config = generators::generator<C, D>::config();
if (config.generate_metadata()) {
ostr << '\n'
<< "'Generated with clang-uml, version "
<< clanguml::version::CLANG_UML_VERSION << '\n'
@@ -408,42 +453,43 @@ template <typename C, typename D>
template <typename E>
void generator<C, D>::generate_link(std::ostream &ostr, const E &e) const
{
const auto &config = generators::generator<C, D>::config();
if (e.file().empty())
return;
ostr << " [[";
try {
if (!m_config.generate_links().link.empty()) {
if (!config.generate_links().link.empty()) {
ostr << env().render(
std::string_view{m_config.generate_links().link},
ostr << env().render(std::string_view{config.generate_links().link},
element_context(e));
}
}
catch (const inja::json::parse_error &e) {
LOG_ERROR("Failed to parse Jinja template: {}",
m_config.generate_links().link);
LOG_ERROR(
"Failed to parse Jinja template: {}", config.generate_links().link);
}
catch (const inja::json::exception &e) {
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
m_config.generate_links().link, e.what());
config.generate_links().link, e.what());
}
ostr << "{";
try {
if (!m_config.generate_links().tooltip.empty()) {
if (!config.generate_links().tooltip.empty()) {
ostr << env().render(
std::string_view{m_config.generate_links().tooltip},
std::string_view{config.generate_links().tooltip},
element_context(e));
}
}
catch (const inja::json::parse_error &e) {
LOG_ERROR("Failed to parse Jinja template: {}",
m_config.generate_links().link);
LOG_ERROR(
"Failed to parse Jinja template: {}", config.generate_links().link);
}
catch (const inja::json::exception &e) {
LOG_ERROR("Failed to render PlantUML directive: \n{}\n due to: {}",
m_config.generate_links().link, e.what());
config.generate_links().link, e.what());
}
ostr << "}";
@@ -454,7 +500,9 @@ template <typename C, typename D>
void generator<C, D>::print_debug(
const common::model::source_location &e, std::ostream &ostr) const
{
if (m_config.debug_mode())
const auto &config = generators::generator<C, D>::config();
if (config.debug_mode())
ostr << "' " << e.file() << ":" << e.line() << '\n';
}
@@ -468,21 +516,26 @@ std::ostream &operator<<(
template <typename C, typename D> void generator<C, D>::init_context()
{
if (m_config.git) {
m_context["git"]["branch"] = m_config.git().branch;
m_context["git"]["revision"] = m_config.git().revision;
m_context["git"]["commit"] = m_config.git().commit;
m_context["git"]["toplevel"] = m_config.git().toplevel;
const auto &config = generators::generator<C, D>::config();
if (config.git) {
m_context["git"]["branch"] = config.git().branch;
m_context["git"]["revision"] = config.git().revision;
m_context["git"]["commit"] = config.git().commit;
m_context["git"]["toplevel"] = config.git().toplevel;
}
}
template <typename C, typename D> void generator<C, D>::update_context() const
{
m_context["diagram"] = m_model.context();
m_context["diagram"] = generators::generator<C, D>::model().context();
}
template <typename C, typename D> void generator<C, D>::init_env()
{
const auto &model = generators::generator<C, D>::model();
const auto &config = generators::generator<C, D>::config();
//
// Add basic string functions to inja environment
//
@@ -532,10 +585,10 @@ template <typename C, typename D> void generator<C, D>::init_env()
// e.g.:
// {{ element("clanguml::t00050::A").comment }}
//
m_env.add_callback("element", 1, [this](inja::Arguments &args) {
m_env.add_callback("element", 1, [&model, &config](inja::Arguments &args) {
inja::json res{};
auto element_opt = m_model.get_with_namespace(
args[0]->get<std::string>(), m_config.using_namespace());
auto element_opt = model.get_with_namespace(
args[0]->get<std::string>(), config.using_namespace());
if (element_opt.has_value())
res = element_opt.value().context();
@@ -548,9 +601,9 @@ template <typename C, typename D> void generator<C, D>::init_env()
// Shortcut to:
// {{ element("A").alias }}
//
m_env.add_callback("alias", 1, [this](inja::Arguments &args) {
auto element_opt = m_model.get_with_namespace(
args[0]->get<std::string>(), m_config.using_namespace());
m_env.add_callback("alias", 1, [&model, &config](inja::Arguments &args) {
auto element_opt = model.get_with_namespace(
args[0]->get<std::string>(), config.using_namespace());
if (!element_opt.has_value())
throw clanguml::error::uml_alias_missing(
@@ -564,10 +617,10 @@ template <typename C, typename D> void generator<C, D>::init_env()
// Shortcut to:
// {{ element("A").comment }}
//
m_env.add_callback("comment", 1, [this](inja::Arguments &args) {
m_env.add_callback("comment", 1, [&model, &config](inja::Arguments &args) {
inja::json res{};
auto element_opt = m_model.get_with_namespace(
args[0]->get<std::string>(), m_config.using_namespace());
auto element_opt = model.get_with_namespace(
args[0]->get<std::string>(), config.using_namespace());
if (!element_opt.has_value())
throw clanguml::error::uml_alias_missing(