Refactored id_t to types.cpp

This commit is contained in:
Bartek Kryza
2024-06-04 21:01:45 +02:00
parent a69938f17f
commit 1c9f347c91
9 changed files with 95 additions and 75 deletions

View File

@@ -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)
{

View File

@@ -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<type>(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<uint64_t>(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<id_t>:
//
// std::optional<id_t> 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<int64_t>(value_);
}
std::string to_string(const std::string &s) { return s; }
std::string to_string(const string_or_regex &sr) { return sr.to_string(); }

View File

@@ -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<type>(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<uint64_t>(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<id_t>:
//
// std::optional<id_t> 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<int64_t>(value_);
}
int64_t ast_local_value() const;
private:
type value_;

View File

@@ -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;

View File

@@ -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();

View File

@@ -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) {

View File

@@ -387,7 +387,8 @@ std::vector<message_chain_t> 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);
}
}