Refactored class diagram generator to use identifiers for types
This commit is contained in:
@@ -95,38 +95,15 @@ public:
|
||||
|
||||
void generate(const class_ &c, std::ostream &ostr) const
|
||||
{
|
||||
std::string class_type{"class"};
|
||||
if (c.is_abstract())
|
||||
ostr << "abstract ";
|
||||
else
|
||||
ostr << "class ";
|
||||
class_type = "abstract";
|
||||
|
||||
ostr << ns_relative(m_config.using_namespace, c.name);
|
||||
ostr << class_type << " \"" << c.full_name(m_config.using_namespace);
|
||||
|
||||
if (!c.templates.empty()) {
|
||||
std::vector<std::string> tnames;
|
||||
std::transform(c.templates.cbegin(), c.templates.cend(),
|
||||
std::back_inserter(tnames), [this](const auto &tmplt) {
|
||||
std::vector<std::string> res;
|
||||
ostr << "\" as " << c.alias() << std::endl;
|
||||
|
||||
if (!tmplt.type.empty())
|
||||
res.push_back(
|
||||
ns_relative(m_config.using_namespace, tmplt.type));
|
||||
|
||||
if (!tmplt.name.empty())
|
||||
res.push_back(
|
||||
ns_relative(m_config.using_namespace, tmplt.name));
|
||||
|
||||
if (!tmplt.default_value.empty()) {
|
||||
res.push_back("=");
|
||||
res.push_back(tmplt.default_value);
|
||||
}
|
||||
|
||||
return fmt::format("{}", fmt::join(res, " "));
|
||||
});
|
||||
ostr << fmt::format("<{}>", fmt::join(tnames, ", "));
|
||||
}
|
||||
|
||||
ostr << " {" << std::endl;
|
||||
ostr << class_type << " " << c.alias() << " {" << std::endl;
|
||||
|
||||
//
|
||||
// Process methods
|
||||
@@ -173,14 +150,20 @@ public:
|
||||
ostr << "}" << std::endl;
|
||||
|
||||
for (const auto &b : c.bases) {
|
||||
ostr << ns_relative(m_config.using_namespace, b.name) << " <|-- "
|
||||
<< ns_relative(m_config.using_namespace, c.name) << std::endl;
|
||||
ostr << m_model.to_alias(m_config.using_namespace,
|
||||
ns_relative(m_config.using_namespace, b.name))
|
||||
<< " <|-- "
|
||||
<< m_model.to_alias(m_config.using_namespace,
|
||||
ns_relative(m_config.using_namespace, c.name))
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
for (const auto &r : c.relationships) {
|
||||
ostr << ns_relative(m_config.using_namespace, c.name) << " "
|
||||
<< to_string(r.type) << " "
|
||||
<< ns_relative(m_config.using_namespace, r.destination);
|
||||
ostr << m_model.to_alias(m_config.using_namespace,
|
||||
ns_relative(m_config.using_namespace, c.name))
|
||||
<< " " << to_string(r.type) << " "
|
||||
<< m_model.to_alias(m_config.using_namespace,
|
||||
ns_relative(m_config.using_namespace, r.destination));
|
||||
if (!r.label.empty())
|
||||
ostr << " : " << r.label;
|
||||
|
||||
|
||||
27
src/uml/class_diagram_model.cc
Normal file
27
src/uml/class_diagram_model.cc
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* src/uml/class_diagram_model.cc
|
||||
*
|
||||
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "class_diagram_model.h"
|
||||
|
||||
namespace clanguml {
|
||||
namespace model {
|
||||
namespace class_diagram {
|
||||
std::atomic_uint64_t element::m_nextId = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,12 +17,16 @@
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "util/util.h"
|
||||
|
||||
#include <clang-c/CXCompilationDatabase.h>
|
||||
#include <clang-c/Index.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
@@ -42,9 +46,22 @@ enum class relationship_t {
|
||||
kAssociation
|
||||
};
|
||||
|
||||
struct element {
|
||||
class element {
|
||||
public:
|
||||
element()
|
||||
: m_id{m_nextId++}
|
||||
{
|
||||
}
|
||||
std::string name;
|
||||
std::vector<std::string> namespace_;
|
||||
|
||||
std::string alias() const { return fmt::format("C_{:010}", m_id); }
|
||||
|
||||
protected:
|
||||
const uint64_t m_id{0};
|
||||
|
||||
private:
|
||||
static std::atomic_uint64_t m_nextId;
|
||||
};
|
||||
|
||||
struct class_element {
|
||||
@@ -93,7 +110,8 @@ struct class_template {
|
||||
std::string default_value;
|
||||
};
|
||||
|
||||
struct class_ : public element {
|
||||
class class_ : public element {
|
||||
public:
|
||||
bool is_struct{false};
|
||||
bool is_template{false};
|
||||
std::vector<class_member> members;
|
||||
@@ -103,6 +121,42 @@ struct class_ : public element {
|
||||
std::vector<class_relationship> relationships;
|
||||
std::vector<class_template> templates;
|
||||
|
||||
std::string full_name(
|
||||
const std::vector<std::string> &using_namespaces) const
|
||||
{
|
||||
using namespace clanguml::util;
|
||||
|
||||
std::ostringstream ostr;
|
||||
ostr << ns_relative(using_namespaces, name);
|
||||
|
||||
if (!templates.empty()) {
|
||||
std::vector<std::string> tnames;
|
||||
std::transform(templates.cbegin(), templates.cend(),
|
||||
std::back_inserter(tnames),
|
||||
[&using_namespaces](const auto &tmplt) {
|
||||
std::vector<std::string> res;
|
||||
|
||||
if (!tmplt.type.empty())
|
||||
res.push_back(
|
||||
ns_relative(using_namespaces, tmplt.type));
|
||||
|
||||
if (!tmplt.name.empty())
|
||||
res.push_back(
|
||||
ns_relative(using_namespaces, tmplt.name));
|
||||
|
||||
if (!tmplt.default_value.empty()) {
|
||||
res.push_back("=");
|
||||
res.push_back(tmplt.default_value);
|
||||
}
|
||||
|
||||
return fmt::format("{}", fmt::join(res, " "));
|
||||
});
|
||||
ostr << fmt::format("<{}>", fmt::join(tnames, ", "));
|
||||
}
|
||||
|
||||
return ostr.str();
|
||||
}
|
||||
|
||||
bool is_abstract() const
|
||||
{
|
||||
// TODO check if all base abstract methods are overriden
|
||||
@@ -120,6 +174,18 @@ struct diagram {
|
||||
std::string name;
|
||||
std::vector<class_> classes;
|
||||
std::vector<enum_> enums;
|
||||
|
||||
std::string to_alias(const std::vector<std::string> &using_namespaces,
|
||||
const std::string &full_name) const
|
||||
{
|
||||
for (const auto &c : classes) {
|
||||
if (c.full_name(using_namespaces) == full_name) {
|
||||
return c.alias();
|
||||
}
|
||||
}
|
||||
|
||||
return full_name;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,23 @@
|
||||
|
||||
namespace clanguml {
|
||||
namespace util {
|
||||
|
||||
const std::string WHITESPACE = " \n\r\t\f\v";
|
||||
|
||||
std::string ltrim(const std::string &s)
|
||||
{
|
||||
size_t start = s.find_first_not_of(WHITESPACE);
|
||||
return (start == std::string::npos) ? "" : s.substr(start);
|
||||
}
|
||||
|
||||
std::string rtrim(const std::string &s)
|
||||
{
|
||||
size_t end = s.find_last_not_of(WHITESPACE);
|
||||
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
|
||||
}
|
||||
|
||||
std::string trim(const std::string &s) { return rtrim(ltrim(s)); }
|
||||
|
||||
std::vector<std::string> split(std::string str, std::string delimiter)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
namespace clanguml {
|
||||
namespace util {
|
||||
|
||||
std::string ltrim(const std::string &s);
|
||||
std::string rtrim(const std::string &s);
|
||||
std::string trim(const std::string &s);
|
||||
|
||||
/**
|
||||
* @brief Split a string using delimiter
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user