Refactored template argument relationship hints to configuration file option

This commit is contained in:
Bartek Kryza
2022-05-21 20:36:35 +02:00
parent 315c1d26e6
commit ec97414870
5 changed files with 146 additions and 33 deletions

View File

@@ -120,6 +120,34 @@ common::model::diagram_t include_diagram::type() const
return common::model::diagram_t::kInclude;
}
void class_diagram::initialize_relationship_hints()
{
using common::model::relationship_t;
if (!relationship_hints().count("std::vector")) {
relationship_hints().insert({"std::vector", {}});
}
if (!relationship_hints().count("std::unique_ptr")) {
relationship_hints().insert({"std::unique_ptr", {}});
}
if (!relationship_hints().count("std::shared_ptr")) {
relationship_hints().insert(
{"std::shared_ptr", {relationship_t::kAssociation}});
}
if (!relationship_hints().count("std::weak_ptr")) {
relationship_hints().insert(
{"std::weak_ptr", {relationship_t::kAssociation}});
}
if (!relationship_hints().count("std::tuple")) {
relationship_hints().insert({"std::tuple", {}});
}
if (!relationship_hints().count("std::map")) {
relationship_hint_t hint{relationship_t::kNone};
hint.argument_hints.insert({1, relationship_t::kAggregation});
relationship_hints().insert({"std::tuple", std::move(hint)});
}
}
template <> void append_value<plantuml>(plantuml &l, const plantuml &r)
{
l.append(r);
@@ -141,6 +169,7 @@ using clanguml::config::layout_hint;
using clanguml::config::method_arguments;
using clanguml::config::package_diagram;
using clanguml::config::plantuml;
using clanguml::config::relationship_hint_t;
using clanguml::config::sequence_diagram;
using clanguml::config::source_location;
@@ -459,6 +488,9 @@ template <> struct convert<class_diagram> {
get_option(node, rhs.include_relations_also_as_members);
get_option(node, rhs.generate_method_arguments);
get_option(node, rhs.generate_packages);
get_option(node, rhs.relationship_hints);
rhs.initialize_relationship_hints();
return true;
}
@@ -551,6 +583,42 @@ template <> struct convert<layout_hint> {
}
};
//
// relationship_hint_t Yaml decoder
//
template <> struct convert<relationship_hint_t> {
static bool decode(const Node &node, relationship_hint_t &rhs)
{
assert(node.Type() == NodeType::Map || node.Type() == NodeType::Scalar);
if (node.Type() == NodeType::Scalar) {
// This will be default relationship hint for all arguments
// of this template (useful for instance for tuples)
rhs.default_hint = node.as<relationship_t>();
}
else {
for (const auto &it : node) {
auto key = it.first.as<std::string>();
if (key == "default") {
rhs.default_hint = node["default"].as<relationship_t>();
}
else {
try {
auto index = stoul(key);
rhs.argument_hints[index] =
it.second.as<relationship_t>();
}
catch (std::exception &e) {
return false;
}
}
}
}
return true;
}
};
//
// config Yaml decoder
//

View File

@@ -95,6 +95,27 @@ struct git_config {
std::string toplevel;
};
struct relationship_hint_t {
std::map<unsigned int, common::model::relationship_t> argument_hints;
common::model::relationship_t default_hint;
relationship_hint_t(common::model::relationship_t def =
common::model::relationship_t::kAggregation)
: default_hint{def}
{
}
common::model::relationship_t get(unsigned int argument_index) const
{
if (argument_hints.count(argument_index) > 0)
return argument_hints.at(argument_index);
return default_hint;
}
};
using relationship_hints_t = std::map<std::string, relationship_hint_t>;
std::string to_string(const hint_t t);
struct inheritable_diagram_options {
@@ -113,6 +134,7 @@ struct inheritable_diagram_options {
option<std::filesystem::path> base_directory{"__parent_path"};
option<std::filesystem::path> relative_to{"relative_to"};
option<bool> generate_system_headers{"generate_system_headers", false};
option<relationship_hints_t> relationship_hints{"relationship_hints"};
void inherit(const inheritable_diagram_options &parent);
};
@@ -139,7 +161,7 @@ struct class_diagram : public diagram {
option<std::vector<std::string>> classes{"classes"};
option<layout_hints> layout{"layout"};
bool has_class(std::string clazz);
void initialize_relationship_hints();
};
struct sequence_diagram : public diagram {