From da829c21ddc303bd4fcc1ae543724fde55eec7db Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Mon, 27 Sep 2021 21:53:42 +0200 Subject: [PATCH] Refactored class_element --- src/puml/class_diagram_generator.cc | 30 +++---- src/uml/class_diagram_model.cc | 84 ++++++++++++++++++- src/uml/class_diagram_model.h | 71 ++++++++++++---- src/uml/class_diagram_visitor.cc | 124 ++++++++++++---------------- 4 files changed, 210 insertions(+), 99 deletions(-) diff --git a/src/puml/class_diagram_generator.cc b/src/puml/class_diagram_generator.cc index 79f4293e..3ee285e6 100644 --- a/src/puml/class_diagram_generator.cc +++ b/src/puml/class_diagram_generator.cc @@ -139,23 +139,23 @@ void generator::generate(const class_ &c, std::ostream &ostr) const // Process methods // for (const auto &m : c.methods()) { - if (!m_config.should_include(m.scope)) + if (!m_config.should_include(m.scope())) continue; - if (m.is_pure_virtual) + if (m.is_pure_virtual()) ostr << "{abstract} "; - if (m.is_static) + if (m.is_static()) ostr << "{static} "; - std::string type{m.type}; + std::string type{m.type()}; - ostr << to_string(m.scope) << m.name; + ostr << to_string(m.scope()) << m.name(); ostr << "("; if (true) { // TODO: add option to disable parameter generation std::vector params; - std::transform(m.parameters.begin(), m.parameters.end(), + std::transform(m.parameters().cbegin(), m.parameters().cend(), std::back_inserter(params), [this](const auto &mp) { return mp.to_string(m_config.using_namespace); }); @@ -163,15 +163,15 @@ void generator::generate(const class_ &c, std::ostream &ostr) const } ostr << ")"; - if (m.is_const) + if (m.is_const()) ostr << " const"; - assert(!(m.is_pure_virtual && m.is_defaulted)); + assert(!(m.is_pure_virtual() && m.is_defaulted())); - if (m.is_pure_virtual) + if (m.is_pure_virtual()) ostr << " = 0"; - if (m.is_defaulted) + if (m.is_defaulted()) ostr << " = default"; ostr << " : " << ns_relative(uns, type); @@ -238,18 +238,18 @@ void generator::generate(const class_ &c, std::ostream &ostr) const // Process members // for (const auto &m : c.members()) { - if (!m_config.should_include(m.scope)) + if (!m_config.should_include(m.scope())) continue; if (!m_config.include_relations_also_as_members && - rendered_relations.find(m.name) != rendered_relations.end()) + rendered_relations.find(m.name()) != rendered_relations.end()) continue; - if (m.is_static) + if (m.is_static()) ostr << "{static} "; - ostr << to_string(m.scope) << m.name << " : " - << ns_relative(uns, m.type) << '\n'; + ostr << to_string(m.scope()) << m.name() << " : " + << ns_relative(uns, m.type()) << '\n'; } ostr << "}" << '\n'; diff --git a/src/uml/class_diagram_model.cc b/src/uml/class_diagram_model.cc index aaa44524..a686c46e 100644 --- a/src/uml/class_diagram_model.cc +++ b/src/uml/class_diagram_model.cc @@ -122,6 +122,43 @@ void decorated_element::add_decorators( } } +// +// class_element +// +class_element::class_element( + scope_t scope, const std::string &name, const std::string &type) + : scope_{scope} + , name_{name} + , type_{type} +{ +} + +scope_t class_element::scope() const { return scope_; } + +std::string class_element::name() const { return name_; } + +std::string class_element::type() const { return type_; } + +// +// class_member +// +class_member::class_member( + scope_t scope, const std::string &name, const std::string &type) + : class_element{scope, name, type} +{ +} + +bool class_member::is_relationship() const { return is_relationship_; } + +void class_member::set_is_relationship(bool is_relationship) +{ + is_relationship_ = is_relationship; +} + +bool class_member::is_static() const { return is_static_; } + +void class_member::set_is_static(bool is_static) { is_static_ = is_static; } + // // element // @@ -183,6 +220,51 @@ std::string method_parameter::to_string( return fmt::format("{} {} = {}", t, name, default_value); } +// +// class_method +// +class_method::class_method( + scope_t scope, const std::string &name, const std::string &type) + : class_element{scope, name, type} +{ +} + +bool class_method::is_pure_virtual() const { return is_pure_virtual_; } + +void class_method::set_is_pure_virtual(bool is_pure_virtual) +{ + is_pure_virtual_ = is_pure_virtual; +} + +bool class_method::is_virtual() const { return is_virtual_; } + +void class_method::set_is_virtual(bool is_virtual) { is_virtual_ = is_virtual; } + +bool class_method::is_const() const { return is_const_; } + +void class_method::set_is_const(bool is_const) { is_const_ = is_const; } + +bool class_method::is_defaulted() const { return is_defaulted_; } + +void class_method::set_is_defaulted(bool is_defaulted) +{ + is_defaulted_ = is_defaulted; +} + +bool class_method::is_static() const { return is_static_; } + +void class_method::set_is_static(bool is_static) { is_static_ = is_static; } + +const std::vector &class_method::parameters() const +{ + return parameters_; +} + +void class_method::add_parameter(method_parameter &¶meter) +{ + parameters_.emplace_back(std::move(parameter)); +} + // // class_relationship // @@ -317,7 +399,7 @@ bool class_::is_abstract() const // TODO check if all base abstract methods are overriden // with non-abstract methods return std::any_of(methods_.begin(), methods_.end(), - [](const auto &method) { return method.is_pure_virtual; }); + [](const auto &method) { return method.is_pure_virtual(); }); } // diff --git a/src/uml/class_diagram_model.h b/src/uml/class_diagram_model.h index 74f5f11f..7e478d78 100644 --- a/src/uml/class_diagram_model.h +++ b/src/uml/class_diagram_model.h @@ -83,15 +83,35 @@ private: std::vector> decorators_; }; -struct class_element : public decorated_element { - scope_t scope; - std::string name; - std::string type; +class class_element : public decorated_element { +public: + class_element( + scope_t scope, const std::string &name, const std::string &type); + + scope_t scope() const; + std::string name() const; + std::string type() const; + +private: + scope_t scope_; + std::string name_; + std::string type_; }; -struct class_member : public class_element { - bool is_relationship{false}; - bool is_static{false}; +class class_member : public class_element { +public: + class_member( + scope_t scope, const std::string &name, const std::string &type); + + bool is_relationship() const; + void set_is_relationship(bool is_relationship); + + bool is_static() const; + void set_is_static(bool is_static); + +private: + bool is_relationship_{false}; + bool is_static_{false}; }; struct method_parameter : public decorated_element { @@ -103,13 +123,36 @@ struct method_parameter : public decorated_element { const std::vector &using_namespaces) const; }; -struct class_method : public class_element { - std::vector parameters; - bool is_pure_virtual{false}; - bool is_virtual{false}; - bool is_const{false}; - bool is_defaulted{false}; - bool is_static{false}; +class class_method : public class_element { +public: + class_method( + scope_t scope, const std::string &name, const std::string &type); + + bool is_pure_virtual() const; + void set_is_pure_virtual(bool is_pure_virtual); + + bool is_virtual() const; + void set_is_virtual(bool is_virtual); + + bool is_const() const; + void set_is_const(bool is_const); + + bool is_defaulted() const; + void set_is_defaulted(bool is_defaulted); + + bool is_static() const; + void set_is_static(bool is_static); + + const std::vector ¶meters() const; + void add_parameter(method_parameter &¶meter); + +private: + std::vector parameters_; + bool is_pure_virtual_{false}; + bool is_virtual_{false}; + bool is_const_{false}; + bool is_defaulted_{false}; + bool is_static_{false}; }; struct class_parent { diff --git a/src/uml/class_diagram_visitor.cc b/src/uml/class_diagram_visitor.cc index c06cf3dc..0ed47cca 100644 --- a/src/uml/class_diagram_visitor.cc +++ b/src/uml/class_diagram_visitor.cc @@ -668,11 +668,8 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c, { bool template_instantiation_added_as_aggregation{false}; - class_member m; - m.name = mv.name(); - m.type = cppast::to_string(mv.type()); - m.scope = detail::cpp_access_specifier_to_scope(as); - m.is_static = false; + class_member m{detail::cpp_access_specifier_to_scope(as), mv.name(), + cppast::to_string(mv.type())}; if (mv.comment().has_value()) m.add_decorators(decorators::parse(mv.comment().value())); @@ -686,7 +683,7 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c, tr.kind()); if (tr.kind() == cppast::cpp_type_kind::builtin_t) { - LOG_DBG("Builtin type found for field: {}", m.name); + LOG_DBG("Builtin type found for field: {}", m.name()); } else if (tr.kind() == cppast::cpp_type_kind::user_defined_t) { LOG_DBG("Processing user defined type field {} {}", @@ -716,8 +713,8 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c, class_relationship r; r.destination = type; r.type = relationship_type; - r.label = m.name; - r.scope = m.scope; + r.label = m.name(); + r.scope = m.scope(); r.set_style(m.style_spec()); auto [decorator_rtype, decorator_rmult] = m.relationship(); @@ -747,11 +744,8 @@ void tu_visitor::process_anonymous_enum( { for (const auto &ev : en) { if (ev.kind() == cppast::cpp_entity_kind::enum_value_t) { - class_member m; - m.name = ev.name(); - m.type = "enum"; // TODO: Try to figure out real enum type - m.scope = detail::cpp_access_specifier_to_scope(as); - m.is_static = false; + class_member m{ + detail::cpp_access_specifier_to_scope(as), ev.name(), "enum"}; c.add_member(std::move(m)); } } @@ -760,11 +754,10 @@ void tu_visitor::process_anonymous_enum( void tu_visitor::process_static_field(const cppast::cpp_variable &mv, class_ &c, cppast::cpp_access_specifier_kind as) { - class_member m; - m.name = mv.name(); - m.type = cppast::to_string(mv.type()); - m.scope = detail::cpp_access_specifier_to_scope(as); - m.is_static = true; + class_member m{detail::cpp_access_specifier_to_scope(as), mv.name(), + cppast::to_string(mv.type())}; + + m.set_is_static(true); if (mv.comment().has_value()) m.add_decorators(decorators::parse(mv.comment().value())); @@ -778,15 +771,13 @@ void tu_visitor::process_static_field(const cppast::cpp_variable &mv, class_ &c, void tu_visitor::process_method(const cppast::cpp_member_function &mf, class_ &c, cppast::cpp_access_specifier_kind as) { - class_method m; - m.name = util::trim(mf.name()); - m.type = cppast::to_string(mf.return_type()); - m.is_pure_virtual = cppast::is_pure(mf.virtual_info()); - m.is_virtual = cppast::is_virtual(mf.virtual_info()); - m.is_const = cppast::is_const(mf.cv_qualifier()); - m.is_defaulted = false; - m.is_static = false; - m.scope = detail::cpp_access_specifier_to_scope(as); + class_method m{detail::cpp_access_specifier_to_scope(as), + util::trim(mf.name()), cppast::to_string(mf.return_type())}; + m.set_is_pure_virtual(cppast::is_pure(mf.virtual_info())); + m.set_is_virtual(cppast::is_virtual(mf.virtual_info())); + m.set_is_const(cppast::is_const(mf.cv_qualifier())); + m.set_is_defaulted(false); + m.set_is_static(false); if (mf.comment().has_value()) m.add_decorators(decorators::parse(mf.comment().value())); @@ -797,7 +788,7 @@ void tu_visitor::process_method(const cppast::cpp_member_function &mf, for (auto ¶m : mf.parameters()) process_function_parameter(param, m, c); - LOG_DBG("Adding method: {}", m.name); + LOG_DBG("Adding method: {}", m.name()); c.add_method(std::move(m)); } @@ -806,22 +797,23 @@ void tu_visitor::process_template_method( const cppast::cpp_function_template &mf, class_ &c, cppast::cpp_access_specifier_kind as) { - class_method m; - m.name = util::trim(mf.name()); + std::string type; if (mf.function().kind() == cppast::cpp_entity_kind::constructor_t) - m.type = "void"; + type = "void"; else - m.type = cppast::to_string( + type = cppast::to_string( static_cast(mf.function()) .return_type()); - m.is_pure_virtual = false; - m.is_virtual = false; - m.is_const = cppast::is_const( + + class_method m{ + detail::cpp_access_specifier_to_scope(as), util::trim(mf.name()), type}; + m.set_is_pure_virtual(false); + m.set_is_virtual(false); + m.set_is_const(cppast::is_const( static_cast(mf.function()) - .cv_qualifier()); - m.is_defaulted = false; - m.is_static = false; - m.scope = detail::cpp_access_specifier_to_scope(as); + .cv_qualifier())); + m.set_is_defaulted(false); + m.set_is_static(false); if (mf.comment().has_value()) m.add_decorators(decorators::parse(mf.comment().value())); @@ -837,7 +829,7 @@ void tu_visitor::process_template_method( for (auto ¶m : mf.function().parameters()) process_function_parameter(param, m, c, template_parameter_names); - LOG_DBG("Adding template method: {}", m.name); + LOG_DBG("Adding template method: {}", m.name()); c.add_method(std::move(m)); } @@ -845,15 +837,13 @@ void tu_visitor::process_template_method( void tu_visitor::process_static_method(const cppast::cpp_function &mf, class_ &c, cppast::cpp_access_specifier_kind as) { - class_method m; - m.name = util::trim(mf.name()); - m.type = cppast::to_string(mf.return_type()); - m.is_pure_virtual = false; - m.is_virtual = false; - m.is_const = false; - m.is_defaulted = false; - m.is_static = true; - m.scope = detail::cpp_access_specifier_to_scope(as); + class_method m{detail::cpp_access_specifier_to_scope(as), + util::trim(mf.name()), cppast::to_string(mf.return_type())}; + m.set_is_pure_virtual(false); + m.set_is_virtual(false); + m.set_is_const(false); + m.set_is_defaulted(false); + m.set_is_static(true); if (mf.comment().has_value()) m.add_decorators(decorators::parse(mf.comment().value())); @@ -864,7 +854,7 @@ void tu_visitor::process_static_method(const cppast::cpp_function &mf, for (auto ¶m : mf.parameters()) process_function_parameter(param, m, c); - LOG_DBG("Adding static method: {}", m.name); + LOG_DBG("Adding static method: {}", m.name()); c.add_method(std::move(m)); } @@ -872,15 +862,13 @@ void tu_visitor::process_static_method(const cppast::cpp_function &mf, void tu_visitor::process_constructor(const cppast::cpp_constructor &mf, class_ &c, cppast::cpp_access_specifier_kind as) { - class_method m; - m.name = util::trim(mf.name()); - m.type = "void"; - m.is_pure_virtual = false; - m.is_virtual = false; - m.is_const = false; - m.is_defaulted = false; - m.is_static = false; - m.scope = detail::cpp_access_specifier_to_scope(as); + class_method m{detail::cpp_access_specifier_to_scope(as), + util::trim(mf.name()), "void"}; + m.set_is_pure_virtual(false); + m.set_is_virtual(false); + m.set_is_const(false); + m.set_is_defaulted(false); + m.set_is_static(true); if (mf.comment().has_value()) m.add_decorators(decorators::parse(mf.comment().value())); @@ -897,15 +885,13 @@ void tu_visitor::process_constructor(const cppast::cpp_constructor &mf, void tu_visitor::process_destructor(const cppast::cpp_destructor &mf, class_ &c, cppast::cpp_access_specifier_kind as) { - class_method m; - m.name = util::trim(mf.name()); - m.type = "void"; - m.is_pure_virtual = false; - m.is_virtual = cppast::is_virtual(mf.virtual_info()); - m.is_const = false; - m.is_defaulted = false; - m.is_static = false; - m.scope = detail::cpp_access_specifier_to_scope(as); + class_method m{detail::cpp_access_specifier_to_scope(as), + util::trim(mf.name()), "void"}; + m.set_is_pure_virtual(false); + m.set_is_virtual(cppast::is_virtual(mf.virtual_info())); + m.set_is_const(false); + m.set_is_defaulted(false); + m.set_is_static(true); c.add_method(std::move(m)); } @@ -1056,7 +1042,7 @@ void tu_visitor::process_function_parameter( } } - m.parameters.emplace_back(std::move(mp)); + m.add_parameter(std::move(mp)); } void tu_visitor::process_template_type_parameter(