From 2d48a4eb140388c7c09683276e5d353f7d053c54 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Mon, 27 Sep 2021 23:33:01 +0200 Subject: [PATCH] Refactored class_template --- src/uml/class_diagram_model.cc | 48 ++++++++++++++++++++++++----- src/uml/class_diagram_model.h | 27 +++++++++++++--- src/uml/class_diagram_visitor.cc | 53 ++++++++++---------------------- 3 files changed, 79 insertions(+), 49 deletions(-) diff --git a/src/uml/class_diagram_model.cc b/src/uml/class_diagram_model.cc index eb6a1b56..4d20df51 100644 --- a/src/uml/class_diagram_model.cc +++ b/src/uml/class_diagram_model.cc @@ -366,10 +366,42 @@ bool operator==(const class_relationship &l, const class_relationship &r) // // class_template // +class_template::class_template(const std::string &type, const std::string &name, + const std::string &default_value, bool is_variadic) + : type_{type} + , name_{name} + , default_value_{default_value} + , is_variadic_{is_variadic} +{ + if (is_variadic) + name_ = name_ + "..."; +} + +void class_template::set_type(const std::string &type) { type_ = type; } + +std::string class_template::type() const { return type_; } + +void class_template::set_name(const std::string &name) { name_ = name; } + +std::string class_template::name() const { return name_; } + +void class_template::set_default_value(const std::string &value) +{ + default_value_ = value; +} + +std::string class_template::default_value() const { return default_value_; } + +void class_template::is_variadic(bool is_variadic) noexcept +{ + is_variadic_ = is_variadic; +} + +bool class_template::is_variadic() const noexcept { return is_variadic_; } bool operator==(const class_template &l, const class_template &r) { - return (l.name == r.name) && (l.type == r.type); + return (l.name() == r.name()) && (l.type() == r.type()); } // @@ -477,15 +509,17 @@ std::string class_::full_name(bool relative) const std::back_inserter(tnames), [this](const auto &tmplt) { std::vector res; - if (!tmplt.type.empty()) - res.push_back(ns_relative(using_namespaces(), tmplt.type)); + if (!tmplt.type().empty()) + res.push_back( + ns_relative(using_namespaces(), tmplt.type())); - if (!tmplt.name.empty()) - res.push_back(ns_relative(using_namespaces(), tmplt.name)); + if (!tmplt.name().empty()) + res.push_back( + ns_relative(using_namespaces(), tmplt.name())); - if (!tmplt.default_value.empty()) { + if (!tmplt.default_value().empty()) { res.push_back("="); - res.push_back(tmplt.default_value); + res.push_back(tmplt.default_value()); } return fmt::format("{}", fmt::join(res, " ")); diff --git a/src/uml/class_diagram_model.h b/src/uml/class_diagram_model.h index 7e28af18..6918dff5 100644 --- a/src/uml/class_diagram_model.h +++ b/src/uml/class_diagram_model.h @@ -225,13 +225,30 @@ private: scope_t scope_{scope_t::kNone}; }; -struct class_template { - std::string name; - std::string type; - std::string default_value; - bool is_variadic{false}; +class class_template { +public: + class_template(const std::string &type = "", const std::string &name = "", + const std::string &default_value = "", bool is_variadic = false); + + void set_type(const std::string &type); + std::string type() const; + + void set_name(const std::string &name); + std::string name() const; + + void set_default_value(const std::string &value); + std::string default_value() const; + + void is_variadic(bool is_variadic) noexcept; + bool is_variadic() const noexcept; friend bool operator==(const class_template &l, const class_template &r); + +private: + std::string type_; + std::string name_; + std::string default_value_; + bool is_variadic_{false}; }; class element : public decorated_element { diff --git a/src/uml/class_diagram_visitor.cc b/src/uml/class_diagram_visitor.cc index 2dfd4826..7fd68029 100644 --- a/src/uml/class_diagram_visitor.cc +++ b/src/uml/class_diagram_visitor.cc @@ -490,12 +490,7 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls, // Naive parse of template arguments: auto toks = util::split(ua, ","); for (const auto &t : toks) { - class_template ct; - ct.type = t; - ct.default_value = ""; - ct.is_variadic = false; - ct.name = ""; - c.add_template(std::move(ct)); + c.add_template({t}); const auto &primary_template_ref = static_cast( @@ -1026,37 +1021,20 @@ void tu_visitor::process_function_parameter( void tu_visitor::process_template_type_parameter( const cppast::cpp_template_type_parameter &t, class_ &parent) { - class_template ct; - ct.type = ""; - ct.default_value = ""; - ct.is_variadic = t.is_variadic(); - ct.name = t.name(); - if (ct.is_variadic) - ct.name += "..."; - parent.add_template(std::move(ct)); + parent.add_template({"", t.name(), "", t.is_variadic()}); } void tu_visitor::process_template_nontype_parameter( const cppast::cpp_non_type_template_parameter &t, class_ &parent) { - class_template ct; - ct.type = cppast::to_string(t.type()); - ct.name = t.name(); - ct.default_value = ""; - ct.is_variadic = t.is_variadic(); - if (ct.is_variadic) - ct.name += "..."; - parent.add_template(std::move(ct)); + parent.add_template( + {cppast::to_string(t.type()), t.name(), "", t.is_variadic()}); } void tu_visitor::process_template_template_parameter( const cppast::cpp_template_template_parameter &t, class_ &parent) { - class_template ct; - ct.type = ""; - ct.name = t.name() + "<>"; - ct.default_value = ""; - parent.add_template(std::move(ct)); + parent.add_template({"", t.name() + "<>"}); } void tu_visitor::process_friend(const cppast::cpp_friend &f, class_ &parent) @@ -1390,9 +1368,9 @@ class_ tu_visitor::build_template_instantiation( bool add_template_argument_as_base_class{false}; class_template ct; if (targ.type()) { - ct.type = cppast::to_string(targ.type().value()); + ct.set_type(cppast::to_string(targ.type().value())); - LOG_DBG("Template argument is a type {}", ct.type); + LOG_DBG("Template argument is a type {}", ct.type()); auto fn = cx::util::full_name( cppast::remove_cv(cx::util::unreferenced(targ.type().value())), ctx.entity_index, false); @@ -1468,16 +1446,16 @@ class_ tu_visitor::build_template_instantiation( else if (targ.expression()) { const auto &exp = targ.expression().value(); if (exp.kind() == cppast::cpp_expression_kind::literal_t) - ct.type = + ct.set_type( static_cast(exp) - .value(); + .value()); else if (exp.kind() == cppast::cpp_expression_kind::unexposed_t) - ct.type = + ct.set_type( static_cast(exp) .expression() - .as_string(); + .as_string()); - LOG_DBG("Template argument is an expression {}", ct.type); + LOG_DBG("Template argument is an expression {}", ct.type()); } // In case any of the template arguments are base classes, add @@ -1495,17 +1473,18 @@ class_ tu_visitor::build_template_instantiation( } if (add_template_argument_as_base_class) { - LOG_DBG("Adding template argument '{}' as base class", ct.type); + LOG_DBG( + "Adding template argument '{}' as base class", ct.type()); class_parent cp; cp.set_access(class_parent::access_t::kPublic); - cp.set_name(ct.type); + cp.set_name(ct.type()); tinst.add_parent(std::move(cp)); } } - LOG_DBG("Adding template argument '{}'", ct.type); + LOG_DBG("Adding template argument '{}'", ct.type()); tinst.add_template(std::move(ct)); }