Initial refactor of namespace handling

This commit is contained in:
Bartek Kryza
2022-03-04 23:38:18 +01:00
parent bee20e7f26
commit a67b459437
40 changed files with 620 additions and 201 deletions

View File

@@ -118,7 +118,7 @@ void generator::generate(
if (m.is_defaulted())
ostr << " = default";
ostr << " : " << ns_relative(uns, type);
ostr << " : " << uns.relative(type);
ostr << '\n';
}
@@ -154,9 +154,9 @@ void generator::generate(
if (!r.multiplicity_destination().empty())
puml_relation += " \"" + r.multiplicity_destination() + "\"";
relstr << m_model.to_alias(ns_relative(uns, c.full_name())) << " "
relstr << m_model.to_alias(uns.relative(c.full_name())) << " "
<< puml_relation << " "
<< m_model.to_alias(ns_relative(uns, destination));
<< m_model.to_alias(uns.relative(destination));
if (!r.label().empty()) {
relstr << " : " << plantuml_common::to_plantuml(r.scope())
@@ -197,7 +197,7 @@ void generator::generate(
ostr << "{static} ";
ostr << plantuml_common::to_plantuml(m.scope()) << m.name() << " : "
<< ns_relative(uns, m.type()) << '\n';
<< uns.relative(m.type()) << '\n';
}
ostr << "}" << '\n';
@@ -206,10 +206,8 @@ void generator::generate(
for (const auto &b : c.parents()) {
std::stringstream relstr;
try {
relstr << m_model.to_alias(ns_relative(uns, b.name()))
<< " <|-- "
<< m_model.to_alias(ns_relative(uns, c.full_name()))
<< '\n';
relstr << m_model.to_alias(uns.relative(b.name())) << " <|-- "
<< m_model.to_alias(uns.relative(c.full_name())) << '\n';
all_relations_str << relstr.str();
}
catch (error::uml_alias_missing &e) {
@@ -253,13 +251,13 @@ void generator::generate(
destination = r.destination();
relstr << m_model.to_alias(
ns_relative(m_config.using_namespace(), e.name()))
m_config.using_namespace().relative(e.name()))
<< " "
<< clanguml::common::generators::plantuml::to_plantuml(
r.type(), r.style())
<< " "
<< m_model.to_alias(
ns_relative(m_config.using_namespace(), destination));
m_config.using_namespace().relative(destination));
if (!r.label().empty())
relstr << " : " << r.label();

View File

@@ -24,8 +24,8 @@
namespace clanguml::class_diagram::model {
class_::class_(const std::vector<std::string> &using_namespaces)
: element{using_namespaces}
class_::class_(const common::model::namespace_ &using_namespace)
: element{using_namespace}
{
}
@@ -112,10 +112,12 @@ std::string class_::full_name_no_ns() const
std::string class_::full_name(bool relative) const
{
using namespace clanguml::util;
using clanguml::common::model::namespace_;
std::ostringstream ostr;
if (relative && starts_with(get_namespace(), using_namespace()))
ostr << ns_relative(using_namespace(), name_and_ns());
// if (relative && starts_with(get_namespace(), using_namespace()))
if (relative)
ostr << namespace_{name()}.relative_to(using_namespace()).to_string();
else
ostr << name_and_ns();
@@ -126,6 +128,8 @@ std::string class_::full_name(bool relative) const
std::ostringstream &class_::render_template_params(
std::ostringstream &ostr) const
{
using clanguml::common::model::namespace_;
if (!templates_.empty()) {
std::vector<std::string> tnames;
std::transform(templates_.cbegin(), templates_.cend(),
@@ -133,12 +137,14 @@ std::ostringstream &class_::render_template_params(
std::vector<std::string> res;
if (!tmplt.type().empty())
res.push_back(
util::ns_relative(using_namespace(), tmplt.type()));
res.push_back(namespace_{tmplt.type()}
.relative_to(using_namespace())
.to_string());
if (!tmplt.name().empty())
res.push_back(
util::ns_relative(using_namespace(), tmplt.name()));
res.push_back(namespace_{tmplt.name()}
.relative_to(using_namespace())
.to_string());
if (!tmplt.default_value().empty()) {
res.push_back("=");

View File

@@ -34,7 +34,7 @@ namespace clanguml::class_diagram::model {
class class_ : public common::model::element,
public common::model::stylable_element {
public:
class_(const std::vector<std::string> &using_namespaces);
class_(const common::model::namespace_ &using_namespace);
class_(const class_ &) = delete;
class_(class_ &&) noexcept = delete;

View File

@@ -82,7 +82,7 @@ void diagram::add_class(std::unique_ptr<class_> &&c)
auto ns = c->get_relative_namespace();
auto name = c->name();
add_element(ns, std::move(c));
ns.push_back(name);
ns |= name;
const auto ccc = get_element<class_>(ns);
assert(ccc.value().name() == name);
}

View File

@@ -24,8 +24,8 @@
namespace clanguml::class_diagram::model {
enum_::enum_(const std::vector<std::string> &using_namespaces)
: element{using_namespaces}
enum_::enum_(const common::model::namespace_ &using_namespace)
: element{using_namespace}
{
}
@@ -37,10 +37,11 @@ bool operator==(const enum_ &l, const enum_ &r)
std::string enum_::full_name(bool relative) const
{
using namespace clanguml::util;
using clanguml::common::model::namespace_;
std::ostringstream ostr;
if (relative)
ostr << ns_relative(using_namespace(), name());
ostr << namespace_{name()}.relative_to(using_namespace()).to_string();
else
ostr << name();

View File

@@ -27,7 +27,7 @@ namespace clanguml::class_diagram::model {
class enum_ : public common::model::element,
public common::model::stylable_element {
public:
enum_(const std::vector<std::string> &using_namespaces);
enum_(const common::model::namespace_ &using_namespaces);
enum_(const enum_ &) = delete;
enum_(enum_ &&) = default;

View File

@@ -38,14 +38,16 @@ void method_parameter::set_default_value(const std::string &value)
std::string method_parameter::default_value() const { return default_value_; }
std::string method_parameter::to_string(
const std::vector<std::string> &using_namespaces) const
const common::model::namespace_ &using_namespace) const
{
using namespace clanguml::util;
auto t = ns_relative(using_namespaces, type());
auto type_ns = common::model::namespace_{type()}
.relative_to(using_namespace)
.to_string();
if (default_value().empty())
return fmt::format("{} {}", t, name());
return fmt::format("{} {}", type_ns, name());
return fmt::format("{} {} = {}", t, name(), default_value());
return fmt::format("{} {} = {}", type_ns, name(), default_value());
}
}

View File

@@ -18,6 +18,7 @@
#pragma once
#include "common/model/decorated_element.h"
#include "common/model/namespace.h"
#include <string>
#include <vector>
@@ -36,7 +37,7 @@ public:
std::string default_value() const;
std::string to_string(
const std::vector<std::string> &using_namespaces) const;
const common::model::namespace_ &using_namespaces) const;
private:
std::string type_;

View File

@@ -154,14 +154,14 @@ translation_unit_context::get_type_alias_template(
void translation_unit_context::push_namespace(const std::string &ns)
{
namespace_.push_back(ns);
ns_ |= ns;
}
void translation_unit_context::pop_namespace() { namespace_.pop_back(); }
void translation_unit_context::pop_namespace() { ns_.pop_back(); }
const std::vector<std::string> &translation_unit_context::get_namespace() const
const common::model::namespace_ &translation_unit_context::get_namespace() const
{
return namespace_;
return ns_;
}
const cppast::cpp_entity_index &translation_unit_context::entity_index() const

View File

@@ -66,7 +66,7 @@ public:
void pop_namespace();
const std::vector<std::string> &get_namespace() const;
const common::model::namespace_ &get_namespace() const;
const cppast::cpp_entity_index &entity_index() const;
@@ -80,7 +80,7 @@ public:
private:
// Current visitor namespace
std::vector<std::string> namespace_;
common::model::namespace_ ns_;
// Reference to the cppast entity index
cppast::cpp_entity_index &entity_index_;

View File

@@ -200,7 +200,7 @@ void translation_unit_visitor::process_type_alias_template(
cx::util::full_name(ctx.get_namespace(), at),
type_safe::ref(at.type_alias().underlying_type()));
if (ctx.config().should_include(tinst->get_namespace(), tinst->name()))
if (ctx.config().should_include(tinst->get_namespace() | tinst->name()))
ctx.diagram().add_class(std::move(tinst));
}
}
@@ -222,15 +222,14 @@ void translation_unit_visitor::process_type_alias(
void translation_unit_visitor::process_namespace(
const cppast::cpp_entity &e, const cppast::cpp_namespace &ns_declaration)
{
std::vector<std::string> package_parent = ctx.get_namespace();
auto package_path = package_parent;
package_path.push_back(e.name());
auto package_parent = ctx.get_namespace();
auto package_path = package_parent | e.name();
auto usn = util::split(ctx.config().using_namespace()[0], "::");
auto usn = ctx.config().using_namespace();
if (ctx.config().should_include_package(util::join(package_path, "::"))) {
if (ctx.config().should_include_package(package_path)) {
auto p = std::make_unique<common::model::package>(usn);
util::remove_prefix(package_path, usn);
package_path = package_path.relative_to(usn);
p->set_name(e.name());
p->set_namespace(package_parent);
@@ -267,8 +266,7 @@ void translation_unit_visitor::process_enum_declaration(
return;
}
auto e_ptr = std::make_unique<enum_>(
util::split(ctx.config().using_namespace()[0], "::"));
auto e_ptr = std::make_unique<enum_>(ctx.config().using_namespace());
auto &e = *e_ptr;
e.set_name(enm.name());
e.set_namespace(ctx.get_namespace());
@@ -312,8 +310,7 @@ void translation_unit_visitor::process_class_declaration(
const cppast::cpp_class &cls,
type_safe::optional_ref<const cppast::cpp_template_specialization> tspec)
{
auto c_ptr = std::make_unique<class_>(
util::split(ctx.config().using_namespace()[0], "::"));
auto c_ptr = std::make_unique<class_>(ctx.config().using_namespace());
auto &c = *c_ptr;
c.is_struct(cls.class_kind() == cppast::cpp_class_kind::struct_t);
// c.set_name(cx::util::full_name(ctx.get_namespace(), cls));
@@ -1275,8 +1272,7 @@ bool translation_unit_visitor::find_relationships_in_template_instantiation(
const auto [ns, base_name] = cx::util::split_ns(fn);
auto ns_and_name = ns;
ns_and_name.push_back(base_name);
auto ns_and_name = ns | base_name;
auto full_name = fmt::format("{}", fmt::join(ns_and_name, "::"));
@@ -1403,8 +1399,7 @@ std::unique_ptr<class_> translation_unit_visitor::build_template_instantiation(
std::optional<clanguml::class_diagram::model::class_ *> parent)
{
// Create class_ instance to hold the template instantiation
auto tinst_ptr = std::make_unique<class_>(
util::split(ctx.config().using_namespace()[0], "::"));
auto tinst_ptr = std::make_unique<class_>(ctx.config().using_namespace());
auto &tinst = *tinst_ptr;
std::string full_template_name;
@@ -1430,7 +1425,7 @@ std::unique_ptr<class_> translation_unit_visitor::build_template_instantiation(
// Extract namespace from base template name
const auto [ns, name] = cx::util::split_ns(tinst_full_name);
tinst.set_name(name);
if (ns.empty())
if (ns.is_empty())
tinst.set_namespace(ctx.get_namespace());
else
tinst.set_namespace(ns);