From da85befc9a8a9c44877393e916ba89607fb9e40d Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Wed, 15 Sep 2021 13:53:39 +0200 Subject: [PATCH] Refactored class diagram visitor --- src/uml/class_diagram_visitor.cc | 97 ++++++++++++++++++++++++++++++++ src/uml/class_diagram_visitor.h | 78 ++++--------------------- 2 files changed, 108 insertions(+), 67 deletions(-) diff --git a/src/uml/class_diagram_visitor.cc b/src/uml/class_diagram_visitor.cc index 57039e5a..9b589cb5 100644 --- a/src/uml/class_diagram_visitor.cc +++ b/src/uml/class_diagram_visitor.cc @@ -72,6 +72,103 @@ scope_t cpp_access_specifier_to_scope(cppast::cpp_access_specifier_kind as) } } +// +// tu_context +// +tu_context::tu_context(cppast::cpp_entity_index &idx, + clanguml::model::class_diagram::diagram &d_, + const clanguml::config::class_diagram &config_) + : entity_index{idx} + , d{d_} + , config{config_} +{ +} + +bool tu_context::has_type_alias(const std::string &full_name) const +{ + bool res = alias_index.find(full_name) != alias_index.end(); + LOG_DBG("Alias {} {} found in index", full_name, res ? "" : "not"); + return res; +} + +void tu_context::add_type_alias(const std::string &full_name, + type_safe::object_ref &&ref) +{ + if (!has_type_alias(full_name)) { + LOG_DBG("Stored type alias: {} -> {} ", full_name, + cppast::to_string(ref.get())); + alias_index.emplace(full_name, std::move(ref)); + } +} + +type_safe::object_ref tu_context::get_type_alias( + const std::string &full_name) const +{ + assert(has_type_alias(full_name)); + + return alias_index.at(full_name); +} + +type_safe::object_ref tu_context::get_type_alias_final( + const cppast::cpp_type &t) const +{ + const auto fn = + cx::util::full_name(cppast::remove_cv(t), entity_index, false); + + if (has_type_alias(fn)) { + return get_type_alias_final(alias_index.at(fn).get()); + } + + return type_safe::ref(t); +} + +bool tu_context::has_type_alias_template(const std::string &full_name) const +{ + bool res = + alias_template_index.find(full_name) != alias_template_index.end(); + LOG_DBG("Alias template {} {} found in index", full_name, res ? "" : "not"); + return res; +} + +void tu_context::add_type_alias_template(const std::string &full_name, + type_safe::object_ref &&ref) +{ + if (!has_type_alias_template(full_name)) { + LOG_DBG("Stored type alias template for: {} ", full_name); + alias_template_index.emplace(full_name, std::move(ref)); + } +} + +type_safe::object_ref +tu_context::get_type_alias_template(const std::string &full_name) const +{ + assert(has_type_alias_template(full_name)); + + return alias_template_index.at(full_name); +} + +// +// element_visitor_context +// +template +element_visitor_context::element_visitor_context( + clanguml::model::class_diagram::diagram &d_, T &e) + : element(e) + , d{d_} +{ +} + +// +// tu_visitor +// + +tu_visitor::tu_visitor(cppast::cpp_entity_index &idx_, + clanguml::model::class_diagram::diagram &d_, + const clanguml::config::class_diagram &config_) + : ctx{idx_, d_, config_} +{ +} + void tu_visitor::operator()(const cppast::cpp_entity &file) { cppast::visit(file, diff --git a/src/uml/class_diagram_visitor.h b/src/uml/class_diagram_visitor.h index a2e891f1..53d9c782 100644 --- a/src/uml/class_diagram_visitor.h +++ b/src/uml/class_diagram_visitor.h @@ -44,76 +44,26 @@ namespace class_diagram { struct tu_context { tu_context(cppast::cpp_entity_index &idx, clanguml::model::class_diagram::diagram &d_, - const clanguml::config::class_diagram &config_) - : entity_index{idx} - , d{d_} - , config{config_} - { - } + const clanguml::config::class_diagram &config_); - bool has_type_alias(const std::string &full_name) const - { - bool res = alias_index.find(full_name) != alias_index.end(); - LOG_DBG("Alias {} {} found in index", full_name, res ? "" : "not"); - return res; - } + bool has_type_alias(const std::string &full_name) const; void add_type_alias(const std::string &full_name, - type_safe::object_ref &&ref) - { - if (!has_type_alias(full_name)) { - LOG_DBG("Stored type alias: {} -> {} ", full_name, - cppast::to_string(ref.get())); - alias_index.emplace(full_name, std::move(ref)); - } - } + type_safe::object_ref &&ref); type_safe::object_ref get_type_alias( - const std::string &full_name) const - { - assert(has_type_alias(full_name)); - - return alias_index.at(full_name); - } + const std::string &full_name) const; type_safe::object_ref get_type_alias_final( - const cppast::cpp_type &t) const - { - const auto fn = - cx::util::full_name(cppast::remove_cv(t), entity_index, false); + const cppast::cpp_type &t) const; - if (has_type_alias(fn)) { - return get_type_alias_final(alias_index.at(fn).get()); - } - - return type_safe::ref(t); - } - - bool has_type_alias_template(const std::string &full_name) const - { - bool res = - alias_template_index.find(full_name) != alias_template_index.end(); - LOG_DBG( - "Alias template {} {} found in index", full_name, res ? "" : "not"); - return res; - } + bool has_type_alias_template(const std::string &full_name) const; void add_type_alias_template(const std::string &full_name, - type_safe::object_ref &&ref) - { - if (!has_type_alias_template(full_name)) { - LOG_DBG("Stored type alias template for: {} ", full_name); - alias_template_index.emplace(full_name, std::move(ref)); - } - } + type_safe::object_ref &&ref); type_safe::object_ref get_type_alias_template( - const std::string &full_name) const - { - assert(has_type_alias_template(full_name)); - - return alias_template_index.at(full_name); - } + const std::string &full_name) const; std::vector namespace_; cppast::cpp_entity_index &entity_index; @@ -126,11 +76,8 @@ struct tu_context { }; template struct element_visitor_context { - element_visitor_context(clanguml::model::class_diagram::diagram &d_, T &e) - : element(e) - , d{d_} - { - } + element_visitor_context(clanguml::model::class_diagram::diagram &d_, T &e); + tu_context *ctx; T &element; @@ -142,10 +89,7 @@ class tu_visitor { public: tu_visitor(cppast::cpp_entity_index &idx_, clanguml::model::class_diagram::diagram &d_, - const clanguml::config::class_diagram &config_) - : ctx{idx_, d_, config_} - { - } + const clanguml::config::class_diagram &config_); void operator()(const cppast::cpp_entity &file);