Skip to content

Commit

Permalink
Added caching of diagram elements' full_names
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Nov 3, 2024
1 parent 93ffcc8 commit 49d3c90
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/class_diagram/model/class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ std::string class_::full_name_no_ns() const

std::string class_::full_name(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 @@ -88,6 +91,9 @@ 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
6 changes: 6 additions & 0 deletions src/class_diagram/model/concept.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ std::string concept_::full_name(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 @@ -67,6 +70,9 @@ 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
10 changes: 9 additions & 1 deletion src/class_diagram/model/enum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ std::string enum_::full_name(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 @@ -47,7 +50,12 @@ std::string enum_::full_name(bool relative) const
else
ostr << name_and_ns();

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

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

return res;
}

std::vector<std::string> &enum_::constants() { return constants_; }
Expand Down
9 changes: 8 additions & 1 deletion src/class_diagram/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl(

LOG_DBG("Adding class template specialization {} with id {}", full_name,
id);

add_class(std::move(template_specialization_ptr));
}

Expand Down Expand Up @@ -2493,6 +2492,8 @@ void translation_unit_visitor::add_diagram_element(

void translation_unit_visitor::add_class(std::unique_ptr<class_> &&c)
{
c->complete(true);

if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kDirectory)) {
assert(!c->file().empty());
Expand Down Expand Up @@ -2522,6 +2523,8 @@ void translation_unit_visitor::add_class(std::unique_ptr<class_> &&c)
void translation_unit_visitor::add_objc_interface(
std::unique_ptr<objc_interface> &&c)
{
c->complete(true);

if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kDirectory)) {
assert(!c->file().empty());
Expand All @@ -2541,6 +2544,8 @@ void translation_unit_visitor::add_objc_interface(

void translation_unit_visitor::add_enum(std::unique_ptr<enum_> &&e)
{
e->complete(true);

if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kDirectory)) {
assert(!e->file().empty());
Expand Down Expand Up @@ -2569,6 +2574,8 @@ void translation_unit_visitor::add_enum(std::unique_ptr<enum_> &&e)

void translation_unit_visitor::add_concept(std::unique_ptr<concept_> &&c)
{
c->complete(true);

if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kDirectory)) {
assert(!c->file().empty());
Expand Down
9 changes: 9 additions & 0 deletions src/common/model/diagram_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,21 @@ class diagram_element : public decorated_element, public source_location {
virtual void apply_filter(
const diagram_filter &filter, const std::set<eid_t> &removed);

const std::optional<std::string> &full_name_cache() const
{
return full_name_cache_;
}

void cache_full_name(const std::string &fn) const { full_name_cache_ = fn; }

private:
eid_t id_{};
std::optional<eid_t> parent_element_id_{};
std::string name_;
std::vector<relationship> relationships_;
bool nested_{false};
bool complete_{false};

mutable std::optional<std::string> full_name_cache_;
};
} // namespace clanguml::common::model
2 changes: 2 additions & 0 deletions src/sequence_diagram/model/diagram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void diagram::add_participant(std::unique_ptr<participant> p)
{
const auto participant_id = p->id();

p->complete(true);

assert(participant_id.is_global());

if (participants_.find(participant_id) == participants_.end()) {
Expand Down
6 changes: 6 additions & 0 deletions src/sequence_diagram/model/participant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ std::string class_::full_name(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)
Expand All @@ -85,6 +88,9 @@ 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

0 comments on commit 49d3c90

Please sign in to comment.