Skip to content

Commit

Permalink
Added memoization for diagram_element::full_name method (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Nov 3, 2024
1 parent 49d3c90 commit b90a2b6
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 182 deletions.
3 changes: 2 additions & 1 deletion src/class_diagram/generators/json/class_diagram_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ void generator::generate_relationships(const T &c, nlohmann::json &parent) const
if (!target_element.has_value()) {
LOG_DBG("Skipping {} relation from '{}' to '{}' due "
"to unresolved destination id",
to_string(r.type()), c.full_name(), r.destination().value());
to_string(r.type()), c.full_name(true),
r.destination().value());
continue;
}

Expand Down
14 changes: 8 additions & 6 deletions src/class_diagram/generators/mermaid/class_diagram_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
catch (error::uml_alias_missing &e) {
LOG_DBG("Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), r.destination(), e.what());
to_string(r.type()), c.full_name(false), r.destination(),
e.what());
}
}

Expand Down Expand Up @@ -466,7 +467,7 @@ void generator::generate_relationships(
catch (error::uml_alias_missing &e) {
LOG_DBG("=== Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), destination, e.what());
to_string(r.type()), c.full_name(true), destination, e.what());
}
}

Expand Down Expand Up @@ -548,7 +549,7 @@ void generator::generate_relationships(
catch (error::uml_alias_missing &e) {
LOG_DBG("=== Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), destination, e.what());
to_string(r.type()), c.full_name(true), destination, e.what());
}
}

Expand Down Expand Up @@ -596,7 +597,7 @@ void generator::generate_relationships(const enum_ &e, std::ostream &ostr) const
LOG_DBG("Skipping {} relation from {} to {} due "
"to: {}",
clanguml::common::generators::mermaid::to_mermaid(r.type()),
e.full_name(), destination, ex.what());
e.full_name(true), destination, ex.what());
}
}
}
Expand Down Expand Up @@ -656,7 +657,8 @@ void generator::generate(const objc_interface &c, std::ostream &ostr) const
catch (error::uml_alias_missing &e) {
LOG_DBG("Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), r.destination(), e.what());
to_string(r.type()), c.full_name(true), r.destination(),
e.what());
}
}

Expand Down Expand Up @@ -768,7 +770,7 @@ void generator::generate_relationships(
catch (error::uml_alias_missing &e) {
LOG_DBG("=== Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), destination, e.what());
to_string(r.type()), c.full_name(true), destination, e.what());
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/class_diagram/generators/plantuml/class_diagram_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void generator::generate_alias(const class_ &c, std::ostream &ostr) const
if (!config().generate_fully_qualified_name())
full_name = c.full_name_no_ns();
else
full_name = c.full_name();
full_name = c.full_name(true);

assert(!full_name.empty());

Expand All @@ -101,7 +101,7 @@ void generator::generate_alias(const enum_ &e, std::ostream &ostr) const
if (!config().generate_fully_qualified_name())
ostr << "enum" << " \"" << e.name();
else
ostr << "enum" << " \"" << render_name(e.full_name());
ostr << "enum" << " \"" << render_name(e.full_name(true));

ostr << "\" as " << e.alias() << '\n';

Expand All @@ -116,7 +116,7 @@ void generator::generate_alias(const concept_ &c, std::ostream &ostr) const
if (!config().generate_fully_qualified_name())
ostr << "class" << " \"" << c.full_name_no_ns();
else
ostr << "class" << " \"" << render_name(c.full_name());
ostr << "class" << " \"" << render_name(c.full_name(true));

ostr << "\" as " << c.alias() << '\n';

Expand All @@ -134,7 +134,7 @@ void generator::generate_alias(
else
ostr << "protocol";

ostr << " \"" << render_name(e.full_name());
ostr << " \"" << render_name(e.full_name(true));

ostr << "\" as " << e.alias() << '\n';

Expand Down Expand Up @@ -185,7 +185,8 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
catch (error::uml_alias_missing &e) {
LOG_DBG("Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), r.destination(), e.what());
to_string(r.type()), c.full_name(true), r.destination(),
e.what());
}
}

Expand Down Expand Up @@ -416,7 +417,8 @@ void generator::generate(const objc_interface &c, std::ostream &ostr) const
catch (error::uml_alias_missing &e) {
LOG_DBG("Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), r.destination(), e.what());
to_string(r.type()), c.full_name(true), r.destination(),
e.what());
}
}

Expand Down Expand Up @@ -631,7 +633,7 @@ void generator::generate_relationships(
catch (error::uml_alias_missing &e) {
LOG_DBG("=== Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), destination, e.what());
to_string(r.type()), c.full_name(true), destination, e.what());
}
}

Expand Down Expand Up @@ -705,7 +707,7 @@ void generator::generate_relationships(
catch (error::uml_alias_missing &e) {
LOG_DBG("=== Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), destination, e.what());
to_string(r.type()), c.full_name(true), destination, e.what());
}
}

Expand Down Expand Up @@ -764,7 +766,7 @@ void generator::generate_relationships(const enum_ &e, std::ostream &ostr) const
"to: {}",
clanguml::common::generators::plantuml::to_plantuml(
r, config()),
e.full_name(), destination, ex.what());
e.full_name(true), destination, ex.what());
}
}
}
Expand Down Expand Up @@ -838,7 +840,7 @@ void generator::generate_relationships(
catch (error::uml_alias_missing &e) {
LOG_DBG("=== Skipping {} relation from {} to {} due "
"to: {}",
to_string(r.type()), c.full_name(), destination, e.what());
to_string(r.type()), c.full_name(true), destination, e.what());
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/class_diagram/model/class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,8 @@ std::string class_::full_name_no_ns() const
return ostr.str();
}

std::string class_::full_name(bool relative) const
std::string class_::full_name_impl(bool relative) const
{
if (relative == false && complete() && full_name_cache())
return *full_name_cache();

using namespace clanguml::util;
using clanguml::common::model::namespace_;

Expand All @@ -91,9 +88,6 @@ std::string class_::full_name(bool relative) const
if (res.empty())
return "<<anonymous>>";

if (relative == false && complete())
cache_full_name(res);

return res;
}

Expand Down
25 changes: 13 additions & 12 deletions src/class_diagram/model/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,6 @@ class class_ : public common::model::template_element,
*/
const std::vector<class_method> &methods() const;

/**
* @brief Get class full name.
*
* This method renders the entire class name including all template
* parameters and/or arguments.
*
* @param relative Whether the class name should be relative to
* using_namespace
* @return Full class name.
*/
std::string full_name(bool relative = true) const override;

/**
* @brief Get unqualified class ful name.
*
Expand Down Expand Up @@ -155,6 +143,19 @@ class class_ : public common::model::template_element,
void apply_filter(const common::model::diagram_filter &filter,
const std::set<common::model::eid_t> &removed) override;

protected:
/**
* @brief Get class full name.
*
* This method renders the entire class name including all template
* parameters and/or arguments.
*
* @param relative Whether the class name should be relative to
* using_namespace
* @return Full class name.
*/
std::string full_name_impl(bool relative = true) const override;

private:
bool is_struct_{false};
bool is_union_{false};
Expand Down
8 changes: 1 addition & 7 deletions src/class_diagram/model/concept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ std::string concept_::full_name_no_ns() const
return ostr.str();
}

std::string concept_::full_name(bool relative) const
std::string concept_::full_name_impl(bool relative) const
{
using namespace clanguml::util;
using clanguml::common::model::namespace_;

if (relative == false && complete() && full_name_cache())
return *full_name_cache();

std::ostringstream ostr;

ostr << name_and_ns();
Expand All @@ -70,9 +67,6 @@ std::string concept_::full_name(bool relative) const
if (res.empty())
return "<<anonymous>>";

if (relative == false && complete())
cache_full_name(res);

return res;
}

Expand Down
5 changes: 3 additions & 2 deletions src/class_diagram/model/concept.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class concept_ : public common::model::element,

friend bool operator==(const concept_ &l, const concept_ &r);

std::string full_name(bool relative = true) const override;

std::string full_name_no_ns() const override;

/**
Expand Down Expand Up @@ -87,6 +85,9 @@ class concept_ : public common::model::element,
*/
const std::vector<std::string> &requires_statements() const;

protected:
std::string full_name_impl(bool relative = true) const override;

private:
std::vector<std::string> requires_expression_;

Expand Down
8 changes: 1 addition & 7 deletions src/class_diagram/model/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ bool operator==(const enum_ &l, const enum_ &r)
return (l.get_namespace() == r.get_namespace()) && (l.name() == r.name());
}

std::string enum_::full_name(bool relative) const
std::string enum_::full_name_impl(bool relative) const
{
using namespace clanguml::util;
using clanguml::common::model::namespace_;

if (relative == false && complete() && full_name_cache())
return *full_name_cache();

std::ostringstream ostr;
if (relative)
ostr << namespace_{name_and_ns()}
Expand All @@ -52,9 +49,6 @@ std::string enum_::full_name(bool relative) const

std::string res{ostr.str()};

if (relative == false && complete())
cache_full_name(res);

return res;
}

Expand Down
5 changes: 3 additions & 2 deletions src/class_diagram/model/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ class enum_ : public common::model::element,

friend bool operator==(const enum_ &l, const enum_ &r);

std::string full_name(bool relative = true) const override;

/**
* @brief Get the enums constants.
*
Expand All @@ -64,6 +62,9 @@ class enum_ : public common::model::element,
*/
std::optional<std::string> doxygen_link() const override;

protected:
std::string full_name_impl(bool relative = true) const override;

private:
std::vector<std::string> constants_;
};
Expand Down
2 changes: 1 addition & 1 deletion src/class_diagram/model/objc_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bool operator==(const objc_interface &l, const objc_interface &r)
return l.id() == r.id();
}

std::string objc_interface::full_name(bool /*relative*/) const
std::string objc_interface::full_name_impl(bool /*relative*/) const
{
return name();
}
Expand Down
5 changes: 3 additions & 2 deletions src/class_diagram/model/objc_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class objc_interface : public common::model::element,

friend bool operator==(const objc_interface &l, const objc_interface &r);

std::string full_name(bool relative = true) const override;

void add_member(objc_member &&member);

void add_method(objc_method &&method);
Expand All @@ -78,6 +76,9 @@ class objc_interface : public common::model::element,
*/
std::optional<std::string> doxygen_link() const override;

protected:
std::string full_name_impl(bool relative = true) const override;

private:
std::vector<objc_member> members_;
std::vector<objc_method> methods_;
Expand Down
Loading

0 comments on commit b90a2b6

Please sign in to comment.