diff --git a/src/class_diagram/generators/plantuml/class_diagram_generator.cc b/src/class_diagram/generators/plantuml/class_diagram_generator.cc index 965939a5..4071dc99 100644 --- a/src/class_diagram/generators/plantuml/class_diagram_generator.cc +++ b/src/class_diagram/generators/plantuml/class_diagram_generator.cc @@ -22,81 +22,12 @@ namespace clanguml::class_diagram::generators::plantuml { -std::string relative_to(std::string n, std::string c) -{ - if (c.rfind(n) == std::string::npos) - return c; - - return c.substr(n.size() + 2); -} - generator::generator( clanguml::config::class_diagram &config, diagram_model &model) - : m_config(config) - , m_model(model) + : common_generator{config, model} { } -std::string generator::to_string(scope_t scope) const -{ - switch (scope) { - case scope_t::kPublic: - return "+"; - case scope_t::kProtected: - return "#"; - case scope_t::kPrivate: - return "-"; - default: - return ""; - } -} - -std::string generator::to_string(relationship_t r, std::string style) const -{ - switch (r) { - case relationship_t::kOwnership: - case relationship_t::kComposition: - return style.empty() ? "*--" : fmt::format("*-[{}]-", style); - case relationship_t::kAggregation: - return style.empty() ? "o--" : fmt::format("o-[{}]-", style); - case relationship_t::kContainment: - return style.empty() ? "--+" : fmt::format("-[{}]-+", style); - case relationship_t::kAssociation: - return style.empty() ? "-->" : fmt::format("-[{}]->", style); - case relationship_t::kInstantiation: - return style.empty() ? "..|>" : fmt::format(".[{}].|>", style); - case relationship_t::kFriendship: - return style.empty() ? "<.." : fmt::format("<.[{}].", style); - case relationship_t::kDependency: - return style.empty() ? "..>" : fmt::format(".[{}].>", style); - default: - return ""; - } -} - -std::string generator::name(relationship_t r) const -{ - switch (r) { - case relationship_t::kOwnership: - case relationship_t::kComposition: - return "composition"; - case relationship_t::kAggregation: - return "aggregation"; - case relationship_t::kContainment: - return "containment"; - case relationship_t::kAssociation: - return "association"; - case relationship_t::kInstantiation: - return "instantiation"; - case relationship_t::kFriendship: - return "friendship"; - case relationship_t::kDependency: - return "dependency"; - default: - return "unknown"; - } -} - void generator::generate_alias(const class_ &c, std::ostream &ostr) const { std::string class_type{"class"}; @@ -118,6 +49,7 @@ void generator::generate_alias(const enum_ &e, std::ostream &ostr) const void generator::generate(const class_ &c, std::ostream &ostr) const { + namespace plantuml_common = clanguml::common::generators::plantuml; const auto &uns = m_config.using_namespace(); @@ -147,7 +79,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const std::string type{m.type()}; - ostr << to_string(m.scope()) << m.name(); + ostr << plantuml_common::to_plantuml(m.scope()) << m.name(); ostr << "("; if (m_config.generate_method_arguments() != @@ -190,10 +122,12 @@ void generator::generate(const class_ &c, std::ostream &ostr) const std::stringstream all_relations_str; std::set unique_relations; for (const auto &r : c.relationships()) { - if (!m_config.should_include_relationship(name(r.type()))) + if (!m_config.should_include_relationship( + common::model::to_string(r.type()))) continue; - LOG_DBG("== Processing relationship {}", to_string(r.type())); + LOG_DBG("== Processing relationship {}", + plantuml_common::to_plantuml(r.type(), r.style())); std::stringstream relstr; std::string destination; @@ -206,7 +140,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const if (!r.multiplicity_source().empty()) puml_relation += "\"" + r.multiplicity_source() + "\" "; - puml_relation += to_string(r.type(), r.style()); + puml_relation += plantuml_common::to_plantuml(r.type(), r.style()); if (!r.multiplicity_destination().empty()) puml_relation += " \"" + r.multiplicity_destination() + "\""; @@ -216,7 +150,8 @@ void generator::generate(const class_ &c, std::ostream &ostr) const << m_model.to_alias(ns_relative(uns, destination)); if (!r.label().empty()) { - relstr << " : " << to_string(r.scope()) << r.label(); + relstr << " : " << plantuml_common::to_plantuml(r.scope()) + << r.label(); rendered_relations.emplace(r.label()); } @@ -233,7 +168,8 @@ void generator::generate(const class_ &c, std::ostream &ostr) const catch (error::uml_alias_missing &e) { LOG_ERROR("=== Skipping {} relation from {} to {} due " "to: {}", - to_string(r.type()), c.full_name(), destination, e.what()); + plantuml_common::to_plantuml(r.type(), r.style()), + c.full_name(), destination, e.what()); } } @@ -251,13 +187,13 @@ void generator::generate(const class_ &c, std::ostream &ostr) const if (m.is_static()) ostr << "{static} "; - ostr << to_string(m.scope()) << m.name() << " : " + ostr << plantuml_common::to_plantuml(m.scope()) << m.name() << " : " << ns_relative(uns, m.type()) << '\n'; } ostr << "}" << '\n'; - if (m_config.should_include_relationship("inheritance")) + if (m_config.should_include_relationship("inheritance")) { for (const auto &b : c.parents()) { std::stringstream relstr; try { @@ -273,19 +209,10 @@ void generator::generate(const class_ &c, std::ostream &ostr) const b.name(), c.name(), e.what()); } } - - // - // Process notes - // - for (auto decorator : c.decorators()) { - auto note = std::dynamic_pointer_cast(decorator); - if (note && note->applies_to_diagram(m_config.name)) { - ostr << "note " << note->position << " of " << c.alias() << '\n' - << note->text << '\n' - << "end note\n"; - } } + generate_notes(ostr, c); + // Print relationships ostr << all_relations_str.str(); } @@ -306,18 +233,21 @@ void generator::generate(const enum_ &e, std::ostream &ostr) const ostr << "}" << '\n'; for (const auto &r : e.relationships()) { - if (!m_config.should_include_relationship(name(r.type()))) + if (!m_config.should_include_relationship( + common::model::to_string(r.type()))) continue; std::string destination; std::stringstream relstr; try { - destination = r.destination(); relstr << m_model.to_alias( ns_relative(m_config.using_namespace(), e.name())) - << " " << to_string(r.type()) << " " + << " " + << clanguml::common::generators::plantuml::to_plantuml( + r.type(), r.style()) + << " " << m_model.to_alias( ns_relative(m_config.using_namespace(), destination)); @@ -331,63 +261,20 @@ void generator::generate(const enum_ &e, std::ostream &ostr) const catch (error::uml_alias_missing &ex) { LOG_ERROR("Skipping {} relation from {} to {} due " "to: {}", - to_string(r.type()), e.full_name(), destination, ex.what()); + clanguml::common::generators::plantuml::to_plantuml( + r.type(), r.style()), + e.full_name(), destination, ex.what()); } } - // - // Process notes - // - for (auto decorator : e.decorators()) { - auto note = std::dynamic_pointer_cast(decorator); - if (note && note->applies_to_diagram(m_config.name)) { - ostr << "note " << note->position << " of " << e.alias() << '\n' - << note->text << '\n' - << "end note\n"; - } - } -} - -void generator::generate_config_layout_hints(std::ostream &ostr) const -{ - const auto &uns = m_config.using_namespace(); - - // Generate layout hints - for (const auto &[entity, hints] : m_config.layout()) { - for (const auto &hint : hints) { - std::stringstream hint_str; - try { - hint_str << m_model.to_alias(ns_relative(uns, entity)) - << " -[hidden]" - << clanguml::config::to_string(hint.hint) << "- " - << m_model.to_alias(ns_relative(uns, hint.entity)) - << '\n'; - ostr << hint_str.str(); - } - catch (error::uml_alias_missing &e) { - LOG_ERROR("=== Skipping layout hint from {} to {} due " - "to: {}", - entity, hint.entity, e.what()); - } - } - } + generate_notes(ostr, e); } void generator::generate(std::ostream &ostr) const { ostr << "@startuml" << '\n'; - for (const auto &b : m_config.puml().before) { - std::string note{b}; - std::tuple alias_match; - while (util::find_element_alias(note, alias_match)) { - auto alias = m_model.to_alias(ns_relative( - m_config.using_namespace(), std::get<0>(alias_match))); - note.replace( - std::get<1>(alias_match), std::get<2>(alias_match), alias); - } - ostr << note << '\n'; - } + generate_plantuml_directives(ostr, m_config.puml().before); if (m_config.should_include_entities("classes")) { for (const auto &c : m_model.classes()) { @@ -420,21 +307,11 @@ void generator::generate(std::ostream &ostr) const generate_config_layout_hints(ostr); - // Process aliases in any of the puml directives - for (const auto &b : m_config.puml().after) { - std::string note{b}; - std::tuple alias_match; - while (util::find_element_alias(note, alias_match)) { - auto alias = m_model.to_alias(ns_relative( - m_config.using_namespace(), std::get<0>(alias_match))); - note.replace( - std::get<1>(alias_match), std::get<2>(alias_match), alias); - } - ostr << note << '\n'; - } + generate_plantuml_directives(ostr, m_config.puml().after); ostr << "@enduml" << '\n'; } + std::ostream &operator<<(std::ostream &os, const generator &g) { g.generate(os); diff --git a/src/class_diagram/generators/plantuml/class_diagram_generator.h b/src/class_diagram/generators/plantuml/class_diagram_generator.h index eb48897c..148a50af 100644 --- a/src/class_diagram/generators/plantuml/class_diagram_generator.h +++ b/src/class_diagram/generators/plantuml/class_diagram_generator.h @@ -21,6 +21,7 @@ #include "class_diagram/model/diagram.h" #include "class_diagram/model/enum.h" #include "class_diagram/visitor/translation_unit_visitor.h" +#include "common/generators/plantuml/generator.h" #include "common/model/relationship.h" #include "config/config.h" #include "cx/compilation_database.h" @@ -40,43 +41,34 @@ namespace class_diagram { namespace generators { namespace plantuml { -using diagram_config = clanguml::class_diagram::model::diagram; +using diagram_config = clanguml::config::class_diagram; using diagram_model = clanguml::class_diagram::model::diagram; +template +using common_generator = + clanguml::common::generators::plantuml::generator; + using clanguml::class_diagram::model::class_; using clanguml::class_diagram::model::enum_; using clanguml::common::model::relationship_t; using clanguml::common::model::scope_t; + using namespace clanguml::util; -std::string relative_to(std::string n, std::string c); - -class generator { +class generator : public common_generator { public: - generator(clanguml::config::class_diagram &config, diagram_model &model); - - std::string to_string(scope_t scope) const; - - std::string to_string(relationship_t r, std::string style = "") const; - - std::string name(relationship_t r) const; + generator(diagram_config &config, diagram_model &model); void generate_alias(const class_ &c, std::ostream &ostr) const; void generate_alias(const enum_ &e, std::ostream &ostr) const; - void generate_config_layout_hints(std::ostream &ostr) const; - void generate(const class_ &c, std::ostream &ostr) const; void generate(const enum_ &e, std::ostream &ostr) const; - void generate(std::ostream &ostr) const; + void generate(std::ostream &ostr) const override; friend std::ostream &operator<<(std::ostream &os, const generator &g); - -private: - clanguml::config::class_diagram &m_config; - diagram_model &m_model; }; clanguml::class_diagram::model::diagram generate( diff --git a/src/common/generators/plantuml/generator.cc b/src/common/generators/plantuml/generator.cc new file mode 100644 index 00000000..8cc60542 --- /dev/null +++ b/src/common/generators/plantuml/generator.cc @@ -0,0 +1,59 @@ +/** + * src/common/generators/plantuml/generator.h + * + * Copyright (c) 2021-2022 Bartek Kryza + * + * 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. + */ +#include "generator.h" + +namespace clanguml::common::generators::plantuml { + +std::string to_plantuml(relationship_t r, std::string style) +{ + switch (r) { + case relationship_t::kOwnership: + case relationship_t::kComposition: + return style.empty() ? "*--" : fmt::format("*-[{}]-", style); + case relationship_t::kAggregation: + return style.empty() ? "o--" : fmt::format("o-[{}]-", style); + case relationship_t::kContainment: + return style.empty() ? "--+" : fmt::format("-[{}]-+", style); + case relationship_t::kAssociation: + return style.empty() ? "-->" : fmt::format("-[{}]->", style); + case relationship_t::kInstantiation: + return style.empty() ? "..|>" : fmt::format(".[{}].|>", style); + case relationship_t::kFriendship: + return style.empty() ? "<.." : fmt::format("<.[{}].", style); + case relationship_t::kDependency: + return style.empty() ? "..>" : fmt::format(".[{}].>", style); + default: + return ""; + } +} + +std::string to_plantuml(scope_t scope) +{ + switch (scope) { + case scope_t::kPublic: + return "+"; + case scope_t::kProtected: + return "#"; + case scope_t::kPrivate: + return "-"; + default: + return ""; + } +} + +} diff --git a/src/common/generators/plantuml/generator.h b/src/common/generators/plantuml/generator.h new file mode 100644 index 00000000..7e3a28c4 --- /dev/null +++ b/src/common/generators/plantuml/generator.h @@ -0,0 +1,120 @@ +/** + * src/common/generators/plantuml/generator.h + * + * Copyright (c) 2021-2022 Bartek Kryza + * + * 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. + */ +#pragma once + +#include "config/config.h" +#include "generator.h" +#include "util/error.h" +#include "util/util.h" + +#include + +namespace clanguml::common::generators::plantuml { + +using clanguml::common::model::relationship_t; +using clanguml::common::model::scope_t; + +std::string to_plantuml(relationship_t r, std::string style); + +std::string to_plantuml(scope_t scope); + +template class generator { +public: + generator(ConfigType &config, DiagramType &model) + : m_config{config} + , m_model{model} + { + } + + virtual ~generator() = default; + + virtual void generate(std::ostream &ostr) const = 0; + + void generate_config_layout_hints(std::ostream &ostr) const; + + void generate_plantuml_directives( + std::ostream &ostr, const std::vector &directives) const; + + void generate_notes( + std::ostream &ostr, const model::element &decorators) const; + +protected: + ConfigType &m_config; + DiagramType &m_model; +}; + +template +void generator::generate_config_layout_hints(std::ostream &ostr) const +{ + using namespace clanguml::util; + + const auto &uns = m_config.using_namespace(); + + // Generate layout hints + for (const auto &[entity, hints] : m_config.layout()) { + for (const auto &hint : hints) { + std::stringstream hint_str; + try { + hint_str << m_model.to_alias(ns_relative(uns, entity)) + << " -[hidden]" + << clanguml::config::to_string(hint.hint) << "- " + << m_model.to_alias(ns_relative(uns, hint.entity)) + << '\n'; + ostr << hint_str.str(); + } + catch (clanguml::error::uml_alias_missing &e) { + LOG_ERROR("=== Skipping layout hint from {} to {} due " + "to: {}", + entity, hint.entity, e.what()); + } + } + } +} + +template +void generator::generate_plantuml_directives( + std::ostream &ostr, const std::vector &directives) const +{ + for (const auto &b : directives) { + std::string note{b}; + std::tuple alias_match; + while (util::find_element_alias(note, alias_match)) { + auto alias = m_model.to_alias(util::ns_relative( + m_config.using_namespace(), std::get<0>(alias_match))); + note.replace( + std::get<1>(alias_match), std::get<2>(alias_match), alias); + } + ostr << note << '\n'; + } +} + +template +void generator::generate_notes( + std::ostream &ostr, const model::element &e) const +{ + for (auto decorator : e.decorators()) { + auto note = std::dynamic_pointer_cast(decorator); + if (note && note->applies_to_diagram(m_config.name)) { + ostr << "note " << note->position << " of " << e.alias() << '\n' + << note->text << '\n' + << "end note\n"; + } + } +} + +} diff --git a/src/common/model/enums.cc b/src/common/model/enums.cc new file mode 100644 index 00000000..a0c88f6a --- /dev/null +++ b/src/common/model/enums.cc @@ -0,0 +1,81 @@ +/** + * src/class_diagram/model/enums.cc + * + * Copyright (c) 2021-2022 Bartek Kryza + * + * 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. + */ +#include "enums.h" + +#include + +namespace clanguml::common::model { + +std::string to_string(relationship_t r) +{ + switch (r) { + case relationship_t::kNone: + return "none"; + case relationship_t::kExtension: + return "extension"; + case relationship_t::kComposition: + return "composition"; + case relationship_t::kAggregation: + return "aggregation"; + case relationship_t::kContainment: + return "containment"; + case relationship_t::kOwnership: + return "ownership"; + case relationship_t::kAssociation: + return "association"; + case relationship_t::kInstantiation: + return "instantiation"; + case relationship_t::kFriendship: + return "friendship"; + case relationship_t::kDependency: + return "dependency"; + default: + assert(false); + } +} + +std::string to_string(scope_t s) +{ + switch (s) { + case scope_t::kPublic: + return "public"; + case scope_t::kProtected: + return "protected"; + case scope_t::kPrivate: + return "private"; + case scope_t::kNone: + return "none"; + default: + assert(false); + } +} + +std::string to_string(access_t a) +{ + switch (a) { + case access_t::kPublic: + return "public"; + case access_t::kProtected: + return "protected"; + case access_t::kPrivate: + return "private"; + default: + assert(false); + } +} +} diff --git a/src/common/model/enums.h b/src/common/model/enums.h index 08c78d45..86a6a210 100644 --- a/src/common/model/enums.h +++ b/src/common/model/enums.h @@ -17,6 +17,8 @@ */ #pragma once +#include + namespace clanguml::common::model { enum class access_t { kPublic, kProtected, kPrivate }; @@ -36,4 +38,9 @@ enum class relationship_t { kDependency }; +std::string to_string(relationship_t r); + +std::string to_string(scope_t r); + +std::string to_string(access_t r); } diff --git a/src/common/model/relationship.cc b/src/common/model/relationship.cc index 96c92fd6..f384e40a 100644 --- a/src/common/model/relationship.cc +++ b/src/common/model/relationship.cc @@ -20,34 +20,6 @@ namespace clanguml::common::model { -std::string to_string(relationship_t r) -{ - switch (r) { - case relationship_t::kNone: - return "none"; - case relationship_t::kExtension: - return "extension"; - case relationship_t::kComposition: - return "composition"; - case relationship_t::kAggregation: - return "aggregation"; - case relationship_t::kContainment: - return "containment"; - case relationship_t::kOwnership: - return "ownership"; - case relationship_t::kAssociation: - return "association"; - case relationship_t::kInstantiation: - return "instantiation"; - case relationship_t::kFriendship: - return "frendship"; - case relationship_t::kDependency: - return "dependency"; - default: - return "invalid"; - } -} - relationship::relationship(relationship_t type, const std::string &destination, scope_t scope, const std::string &label, const std::string &multiplicity_source, diff --git a/src/common/model/relationship.h b/src/common/model/relationship.h index 0bb66390..d75addf8 100644 --- a/src/common/model/relationship.h +++ b/src/common/model/relationship.h @@ -24,8 +24,6 @@ namespace clanguml::common::model { -std::string to_string(relationship_t r); - class relationship : public common::model::decorated_element, public common::model::stylable_element { public: diff --git a/tests/t00035/t00035.cc b/tests/t00035/t00035.cc index 12bb4ca6..dbc01d47 100644 --- a/tests/t00035/t00035.cc +++ b/tests/t00035/t00035.cc @@ -1,15 +1,20 @@ namespace clanguml { namespace t00035 { -struct Top {}; +struct Top { +}; -struct Left {}; +struct Left { +}; -struct Center {}; +struct Center { +}; -struct Bottom {}; +struct Bottom { +}; -struct Right {}; +struct Right { +}; } // namespace t00035 } // namespace clanguml