Added style decorators

This commit is contained in:
Bartek Kryza
2021-07-31 20:50:33 +02:00
parent 841f97eeb5
commit ed999d9f5c
10 changed files with 207 additions and 20 deletions

View File

@@ -75,24 +75,24 @@ public:
}
}
std::string to_string(relationship_t r) const
std::string to_string(relationship_t r, std::string style = "") const
{
switch (r) {
case relationship_t::kOwnership:
case relationship_t::kComposition:
return "*--";
return style.empty() ? "*--" : fmt::format("*-[{}]-", style);
case relationship_t::kAggregation:
return "o--";
return style.empty() ? "o--" : fmt::format("o-[{}]-", style);
case relationship_t::kContainment:
return "--+";
return style.empty() ? "--+" : fmt::format("-[{}]-+", style);
case relationship_t::kAssociation:
return "-->";
return style.empty() ? "-->" : fmt::format("-[{}]->", style);
case relationship_t::kInstantiation:
return "..|>";
return style.empty() ? "..|>" : fmt::format(".[{}].|>", style);
case relationship_t::kFriendship:
return "<..";
return style.empty() ? "<.." : fmt::format("<.[{}].", style);
case relationship_t::kDependency:
return "..>";
return style.empty() ? "..>" : fmt::format(".[{}].>", style);
default:
return "";
}
@@ -149,7 +149,12 @@ public:
if (c.is_abstract())
class_type = "abstract";
ostr << class_type << " " << c.alias() << " {" << '\n';
ostr << class_type << " " << c.alias();
if(!c.style.empty())
ostr << " " << c.style;
ostr << " {" << '\n';
//
// Process methods
@@ -228,7 +233,7 @@ public:
if (!r.multiplicity_source.empty())
puml_relation += "\"" + r.multiplicity_source + "\" ";
puml_relation += to_string(r.type);
puml_relation += to_string(r.type, r.style);
if (!r.multiplicity_destination.empty())
puml_relation += " \"" + r.multiplicity_destination + "\"";
@@ -310,7 +315,12 @@ public:
void generate(const enum_ &e, std::ostream &ostr) const
{
ostr << "enum " << e.alias() << " {" << '\n';
ostr << "enum " << e.alias();
if(!e.style.empty())
ostr << " " << e.style;
ostr << " {" << '\n';
for (const auto &enum_constant : e.constants) {
ostr << enum_constant << '\n';

View File

@@ -54,6 +54,10 @@ enum class relationship_t {
std::string to_string(relationship_t r);
struct stylable_element {
std::string style;
};
struct decorated_element {
std::vector<std::shared_ptr<decorators::decorator>> decorators;
@@ -93,6 +97,15 @@ struct decorated_element {
return {relationship_t::kNone, ""};
}
std::string style_spec()
{
for (auto d : decorators)
if (std::dynamic_pointer_cast<decorators::style>(d))
return std::dynamic_pointer_cast<decorators::style>(d)->spec;
return "";
}
};
class element : public decorated_element {
@@ -157,7 +170,7 @@ struct class_parent {
access_t access;
};
struct class_relationship : public decorated_element {
struct class_relationship : public decorated_element, public stylable_element {
relationship_t type{relationship_t::kAssociation};
std::string destination;
std::string multiplicity_source;
@@ -190,7 +203,7 @@ struct type_alias {
std::string underlying_type;
};
class class_ : public element {
class class_ : public element, public stylable_element {
public:
std::string usr;
bool is_struct{false};
@@ -275,7 +288,7 @@ public:
}
};
struct enum_ : public element {
struct enum_ : public element, public stylable_element {
std::vector<std::string> constants;
std::vector<class_relationship> relationships;

View File

@@ -199,6 +199,8 @@ void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
if (e.skip())
return;
e.style = e.style_spec();
// Process enum documentation comment
if (enm.comment().has_value())
e.decorators = decorators::parse(enm.comment().value());
@@ -239,9 +241,6 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
if (cls.comment().has_value())
c.decorators = decorators::parse(cls.comment().value());
if (c.skip())
return;
cppast::cpp_access_specifier_kind last_access_specifier =
cppast::cpp_access_specifier_kind::cpp_private;
@@ -256,6 +255,11 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
c.decorators = decorators::parse(cls.comment().value());
}
if (c.skip())
return;
c.style = c.style_spec();
// Process class child entities
if (c.is_struct)
last_access_specifier = cppast::cpp_access_specifier_kind::cpp_public;
@@ -515,7 +519,7 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
bool tu_visitor::process_field_with_template_instantiation(
const cppast::cpp_member_variable &mv, const cppast::cpp_type &tr,
class_ &c, cppast::cpp_access_specifier_kind as)
class_ &c, class_member &m, cppast::cpp_access_specifier_kind as)
{
LOG_DBG("Processing field with template instatiation type {}",
cppast::to_string(tr));
@@ -572,6 +576,17 @@ bool tu_visitor::process_field_with_template_instantiation(
rr.type = relationship_t::kAggregation;
rr.label = mv.name();
rr.scope = detail::cpp_access_specifier_to_scope(as);
rr.style = m.style_spec();
auto [decorator_rtype, decorator_rmult] = m.relationship();
if (decorator_rtype != relationship_t::kNone) {
rr.type = decorator_rtype;
auto mult = util::split(decorator_rmult, ":");
if (mult.size() == 2) {
rr.multiplicity_source = mult[0];
rr.multiplicity_destination = mult[1];
}
}
LOG_DBG("Adding field instantiation relationship {} {} {} : {}",
rr.destination, model::class_diagram::to_string(rr.type), c.usr,
@@ -623,7 +638,7 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
else if (tr.kind() == cppast::cpp_type_kind::template_instantiation_t) {
template_instantiation_added_as_aggregation =
process_field_with_template_instantiation(
mv, resolve_alias(tr), c, as);
mv, resolve_alias(tr), c, m, as);
}
else if (tr.kind() == cppast::cpp_type_kind::unexposed_t) {
LOG_DBG(
@@ -646,6 +661,7 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
r.type = relationship_type;
r.label = m.name;
r.scope = m.scope;
r.style = m.style_spec();
auto [decorator_rtype, decorator_rmult] = m.relationship();
if (decorator_rtype != relationship_t::kNone) {

View File

@@ -166,6 +166,7 @@ public:
bool process_field_with_template_instantiation(
const cppast::cpp_member_variable &mv, const cppast::cpp_type &tr,
clanguml::model::class_diagram::class_ &c,
clanguml::model::class_diagram::class_member &m,
cppast::cpp_access_specifier_kind as);
void process_static_field(const cppast::cpp_variable &mv,