From 1c9f347c91fc07bbf41f055595ed880b5deecf07 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Tue, 4 Jun 2024 21:01:45 +0200 Subject: [PATCH] Refactored id_t to types.cpp --- src/common/clang_utils.cc | 5 +- src/common/model/diagram_element.cc | 2 +- src/common/model/diagram_element.h | 2 +- src/common/types.cc | 69 ++++++++++++++++ src/common/types.h | 78 +++---------------- .../json/sequence_diagram_generator.cc | 4 +- .../mermaid/sequence_diagram_generator.cc | 3 +- .../plantuml/sequence_diagram_generator.cc | 4 +- src/sequence_diagram/model/diagram.cc | 3 +- 9 files changed, 95 insertions(+), 75 deletions(-) diff --git a/src/common/clang_utils.cc b/src/common/clang_utils.cc index 66631685..231f08f4 100644 --- a/src/common/clang_utils.cc +++ b/src/common/clang_utils.cc @@ -450,7 +450,10 @@ template <> common::id_t to_id(const clang::CXXRecordDecl &declaration) return to_id(get_qualified_name(declaration)); } -template <> common::id_t to_id(const clang::EnumType &t) { return to_id(*t.getDecl()); } +template <> common::id_t to_id(const clang::EnumType &t) +{ + return to_id(*t.getDecl()); +} template <> common::id_t to_id(const std::filesystem::path &file) { diff --git a/src/common/model/diagram_element.cc b/src/common/model/diagram_element.cc index 97ab0ff4..4051b7c7 100644 --- a/src/common/model/diagram_element.cc +++ b/src/common/model/diagram_element.cc @@ -26,7 +26,7 @@ namespace clanguml::common::model { diagram_element::diagram_element() = default; -const common::id_t & diagram_element::id() const { return id_; } +const common::id_t &diagram_element::id() const { return id_; } void diagram_element::set_id(common::id_t id) { id_ = id; } diff --git a/src/common/model/diagram_element.h b/src/common/model/diagram_element.h index 3074dffe..eeec8a3d 100644 --- a/src/common/model/diagram_element.h +++ b/src/common/model/diagram_element.h @@ -53,7 +53,7 @@ public: * * @return Elements id. */ - const common::id_t& id() const; + const common::id_t &id() const; /** * Set elements id. diff --git a/src/common/types.cc b/src/common/types.cc index 2fe79d12..c2e9286a 100644 --- a/src/common/types.cc +++ b/src/common/types.cc @@ -20,6 +20,75 @@ namespace clanguml::common { +id_t::id_t() + : value_{0ULL} + , is_global_{true} +{ +} + +id_t::id_t(int64_t id) + : value_{static_cast(id)} + , is_global_{false} +{ +} + +id_t::id_t(type id) + : value_{id} + , is_global_{true} +{ +} + +id_t &id_t::operator=(int64_t ast_id) +{ + // Can't assign ast_id if the id is already a global one + assert(!is_global_); + + value_ = static_cast(ast_id); + return *this; +} + +bool id_t::is_global() const { return is_global_; } + +bool operator==(const id_t &lhs, const id_t &rhs) +{ + return (lhs.is_global_ == rhs.is_global_) && (lhs.value_ == rhs.value_); +} + +bool operator==(const id_t &lhs, const uint64_t &v) { return lhs.value_ == v; } + +bool operator!=(const id_t &lhs, const uint64_t &v) +{ + // This is sadly necessary to catch accidental comparisons to empty + // std::optional: + // + // std::optional id{}; + // if(id != 0) { /* id is nullopt, not 0 - so this executes... */ } + // + assert(v != 0); + + return lhs.value_ != v; +} + +bool operator!=(const id_t &lhs, const id_t &rhs) { return !(lhs == rhs); } + +bool operator<(const id_t &lhs, const id_t &rhs) +{ + if (lhs.is_global_ != rhs.is_global_) { + return lhs.value_ < rhs.value_ + 1; + } + + return lhs.value_ < rhs.value_; // Compare values if is_global_ are the same +} + +id_t::type id_t::value() const { return value_; } + +int64_t id_t::ast_local_value() const +{ + assert(!is_global_); + + return static_cast(value_); +} + std::string to_string(const std::string &s) { return s; } std::string to_string(const string_or_regex &sr) { return sr.to_string(); } diff --git a/src/common/types.h b/src/common/types.h index 847283cc..15d78265 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -33,86 +33,32 @@ class id_t { public: using type = uint64_t; - id_t() - : value_{0ULL} - , is_global_{true} - { - } + id_t(); - explicit id_t(int64_t id) - : value_{static_cast(id)} - , is_global_{false} - { - } + explicit id_t(int64_t id); - explicit id_t(type id) - : value_{id} - , is_global_{true} - { - } + explicit id_t(type id); id_t(const id_t &) = default; id_t(id_t &&) noexcept = default; id_t &operator=(const id_t &) = default; id_t &operator=(id_t &&) noexcept = default; - id_t &operator=(int64_t ast_id) - { - assert(!is_global_); - value_ = static_cast(ast_id); - return *this; - } + id_t &operator=(int64_t ast_id); ~id_t() = default; - bool is_global() const { return is_global_; } + bool is_global() const; - friend bool operator==(const id_t &lhs, const id_t &rhs) - { - return (lhs.is_global_ == rhs.is_global_) && (lhs.value_ == rhs.value_); - } + friend bool operator==(const id_t &lhs, const id_t &rhs); + friend bool operator==(const id_t &lhs, const uint64_t &v); + friend bool operator!=(const id_t &lhs, const uint64_t &v); + friend bool operator!=(const id_t &lhs, const id_t &rhs); + friend bool operator<(const id_t &lhs, const id_t &rhs); - friend bool operator==(const id_t &lhs, const uint64_t &v) - { - return lhs.value_ == v; - } + type value() const; - friend bool operator!=(const id_t &lhs, const uint64_t &v) - { - // This is sadly necessary to catch accidental comparisons to empty - // std::optional: - // - // std::optional id{}; - // if(id != 0) { /* id is nullopt, not 0 - so this executes... */ } - // - assert(v != 0); - - return lhs.value_ != v; - } - - friend bool operator!=(const id_t &lhs, const id_t &rhs) - { - return !(lhs == rhs); - } - - friend bool operator<(const id_t &lhs, const id_t &rhs) - { - if (lhs.is_global_ != rhs.is_global_) { - return lhs.value_ < rhs.value_ + 1; - } - - return lhs.value_ < - rhs.value_; // Compare values if is_global_ are the same - } - - type value() const { return value_; } - - int64_t ast_local_value() const - { - assert(!is_global_); - - return static_cast(value_); - } + int64_t ast_local_value() const; private: type value_; diff --git a/src/sequence_diagram/generators/json/sequence_diagram_generator.cc b/src/sequence_diagram/generators/json/sequence_diagram_generator.cc index e62b873c..d401cf35 100644 --- a/src/sequence_diagram/generators/json/sequence_diagram_generator.cc +++ b/src/sequence_diagram/generators/json/sequence_diagram_generator.cc @@ -747,8 +747,8 @@ void generator::generate_diagram(nlohmann::json &parent) const if (to_activity_id == 0) continue; - auto message_chains_unique = - model().get_all_from_to_message_chains(common::id_t{}, *to_activity_id); + auto message_chains_unique = model().get_all_from_to_message_chains( + common::id_t{}, *to_activity_id); nlohmann::json sequence; sequence["to"]["location"] = to_location.location; diff --git a/src/sequence_diagram/generators/mermaid/sequence_diagram_generator.cc b/src/sequence_diagram/generators/mermaid/sequence_diagram_generator.cc index fdf78036..4a8d8e5a 100644 --- a/src/sequence_diagram/generators/mermaid/sequence_diagram_generator.cc +++ b/src/sequence_diagram/generators/mermaid/sequence_diagram_generator.cc @@ -650,7 +650,8 @@ void generator::generate_diagram(std::ostream &ostr) const ostr << indent(1) << from_alias << " " << common::generators::mermaid::to_mermaid( message_t::kReturn) - << " *" << " : "; + << " *" + << " : "; if (config().generate_return_types()) ostr << from.value().return_type(); diff --git a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc index ad1a6ac5..7a3b288c 100644 --- a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc +++ b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc @@ -552,8 +552,8 @@ void generator::generate_diagram(std::ostream &ostr) const if (!to_activity_id) continue; - auto message_chains_unique = - model().get_all_from_to_message_chains(common::id_t{}, *to_activity_id); + auto message_chains_unique = model().get_all_from_to_message_chains( + common::id_t{}, *to_activity_id); bool first_separator_skipped{false}; for (const auto &mc : message_chains_unique) { diff --git a/src/sequence_diagram/model/diagram.cc b/src/sequence_diagram/model/diagram.cc index 98d033b9..c0eb4ffe 100644 --- a/src/sequence_diagram/model/diagram.cc +++ b/src/sequence_diagram/model/diagram.cc @@ -387,7 +387,8 @@ std::vector diagram::get_all_from_to_message_chains( message_chains_unique.end(), mc) != message_chains_unique.end()) continue; - if (from_activity.value() == 0 || (mc.front().from() == from_activity)) { + if (from_activity.value() == 0 || + (mc.front().from() == from_activity)) { message_chains_unique.push_back(mc); } }