Refactored decorated_element

This commit is contained in:
Bartek Kryza
2021-09-27 00:05:47 +02:00
parent d6b88e68ec
commit e615f3b869
4 changed files with 45 additions and 23 deletions

View File

@@ -273,7 +273,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
// //
// Process notes // Process notes
// //
for (auto decorator : c.decorators) { for (auto decorator : c.decorators()) {
auto note = std::dynamic_pointer_cast<decorators::note>(decorator); auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
if (note && note->applies_to_diagram(m_config.name)) { if (note && note->applies_to_diagram(m_config.name)) {
ostr << "note " << note->position << " of " << c.alias() << '\n' ostr << "note " << note->position << " of " << c.alias() << '\n'
@@ -334,7 +334,7 @@ void generator::generate(const enum_ &e, std::ostream &ostr) const
// //
// Process notes // Process notes
// //
for (auto decorator : e.decorators) { for (auto decorator : e.decorators()) {
auto note = std::dynamic_pointer_cast<decorators::note>(decorator); auto note = std::dynamic_pointer_cast<decorators::note>(decorator);
if (note && note->applies_to_diagram(m_config.name)) { if (note && note->applies_to_diagram(m_config.name)) {
ostr << "note " << note->position << " of " << e.alias() << '\n' ostr << "note " << note->position << " of " << e.alias() << '\n'

View File

@@ -64,7 +64,7 @@ std::string stylable_element::style() const { return style_; }
bool decorated_element::skip() const bool decorated_element::skip() const
{ {
for (auto d : decorators) for (auto d : decorators_)
if (std::dynamic_pointer_cast<decorators::skip>(d)) if (std::dynamic_pointer_cast<decorators::skip>(d))
return true; return true;
@@ -73,7 +73,7 @@ bool decorated_element::skip() const
bool decorated_element::skip_relationship() const bool decorated_element::skip_relationship() const
{ {
for (auto d : decorators) for (auto d : decorators_)
if (std::dynamic_pointer_cast<decorators::skip_relationship>(d)) if (std::dynamic_pointer_cast<decorators::skip_relationship>(d))
return true; return true;
@@ -82,7 +82,7 @@ bool decorated_element::skip_relationship() const
std::pair<relationship_t, std::string> decorated_element::relationship() const std::pair<relationship_t, std::string> decorated_element::relationship() const
{ {
for (auto &d : decorators) for (auto &d : decorators_)
if (std::dynamic_pointer_cast<decorators::association>(d)) if (std::dynamic_pointer_cast<decorators::association>(d))
return {relationship_t::kAssociation, return {relationship_t::kAssociation,
std::dynamic_pointer_cast<decorators::relationship>(d) std::dynamic_pointer_cast<decorators::relationship>(d)
@@ -101,13 +101,27 @@ std::pair<relationship_t, std::string> decorated_element::relationship() const
std::string decorated_element::style_spec() std::string decorated_element::style_spec()
{ {
for (auto d : decorators) for (auto d : decorators_)
if (std::dynamic_pointer_cast<decorators::style>(d)) if (std::dynamic_pointer_cast<decorators::style>(d))
return std::dynamic_pointer_cast<decorators::style>(d)->spec; return std::dynamic_pointer_cast<decorators::style>(d)->spec;
return ""; return "";
} }
const std::vector<std::shared_ptr<decorators::decorator>> &
decorated_element::decorators() const
{
return decorators_;
}
void decorated_element::add_decorators(
const std::vector<std::shared_ptr<decorators::decorator>> &decorators)
{
for (auto d : decorators) {
decorators_.push_back(d);
}
}
// //
// element // element
// //

View File

@@ -63,9 +63,8 @@ private:
std::string style_; std::string style_;
}; };
struct decorated_element { class decorated_element {
std::vector<std::shared_ptr<decorators::decorator>> decorators; public:
bool skip() const; bool skip() const;
bool skip_relationship() const; bool skip_relationship() const;
@@ -73,6 +72,15 @@ struct decorated_element {
std::pair<relationship_t, std::string> relationship() const; std::pair<relationship_t, std::string> relationship() const;
std::string style_spec(); std::string style_spec();
const std::vector<std::shared_ptr<decorators::decorator>> &
decorators() const;
void add_decorators(
const std::vector<std::shared_ptr<decorators::decorator>> &decorators);
private:
std::vector<std::shared_ptr<decorators::decorator>> decorators_;
}; };
struct class_element : public decorated_element { struct class_element : public decorated_element {

View File

@@ -283,7 +283,7 @@ void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
e.set_name(cx::util::full_name(ctx.namespace_, enm)); e.set_name(cx::util::full_name(ctx.namespace_, enm));
if (enm.comment().has_value()) if (enm.comment().has_value())
e.decorators = decorators::parse(enm.comment().value()); e.add_decorators(decorators::parse(enm.comment().value()));
if (e.skip()) if (e.skip())
return; return;
@@ -292,7 +292,7 @@ void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
// Process enum documentation comment // Process enum documentation comment
if (enm.comment().has_value()) if (enm.comment().has_value())
e.decorators = decorators::parse(enm.comment().value()); e.add_decorators(decorators::parse(enm.comment().value()));
for (const auto &ev : enm) { for (const auto &ev : enm) {
if (ev.kind() == cppast::cpp_entity_kind::enum_value_t) { if (ev.kind() == cppast::cpp_entity_kind::enum_value_t) {
@@ -328,7 +328,7 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
c.set_name(cx::util::full_name(ctx.namespace_, cls)); c.set_name(cx::util::full_name(ctx.namespace_, cls));
if (cls.comment().has_value()) if (cls.comment().has_value())
c.decorators = decorators::parse(cls.comment().value()); c.add_decorators(decorators::parse(cls.comment().value()));
cppast::cpp_access_specifier_kind last_access_specifier = cppast::cpp_access_specifier_kind last_access_specifier =
cppast::cpp_access_specifier_kind::cpp_private; cppast::cpp_access_specifier_kind::cpp_private;
@@ -336,12 +336,12 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
// Process class documentation comment // Process class documentation comment
if (cppast::is_templated(cls)) { if (cppast::is_templated(cls)) {
if (cls.parent().value().comment().has_value()) if (cls.parent().value().comment().has_value())
c.decorators = c.add_decorators(
decorators::parse(cls.parent().value().comment().value()); decorators::parse(cls.parent().value().comment().value()));
} }
else { else {
if (cls.comment().has_value()) if (cls.comment().has_value())
c.decorators = decorators::parse(cls.comment().value()); c.add_decorators(decorators::parse(cls.comment().value()));
} }
if (c.skip()) if (c.skip())
@@ -675,7 +675,7 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
m.is_static = false; m.is_static = false;
if (mv.comment().has_value()) if (mv.comment().has_value())
m.decorators = decorators::parse(mv.comment().value()); m.add_decorators(decorators::parse(mv.comment().value()));
if (m.skip()) if (m.skip())
return; return;
@@ -767,7 +767,7 @@ void tu_visitor::process_static_field(const cppast::cpp_variable &mv, class_ &c,
m.is_static = true; m.is_static = true;
if (mv.comment().has_value()) if (mv.comment().has_value())
m.decorators = decorators::parse(mv.comment().value()); m.add_decorators(decorators::parse(mv.comment().value()));
if (m.skip()) if (m.skip())
return; return;
@@ -789,7 +789,7 @@ void tu_visitor::process_method(const cppast::cpp_member_function &mf,
m.scope = detail::cpp_access_specifier_to_scope(as); m.scope = detail::cpp_access_specifier_to_scope(as);
if (mf.comment().has_value()) if (mf.comment().has_value())
m.decorators = decorators::parse(mf.comment().value()); m.add_decorators(decorators::parse(mf.comment().value()));
if (m.skip()) if (m.skip())
return; return;
@@ -824,7 +824,7 @@ void tu_visitor::process_template_method(
m.scope = detail::cpp_access_specifier_to_scope(as); m.scope = detail::cpp_access_specifier_to_scope(as);
if (mf.comment().has_value()) if (mf.comment().has_value())
m.decorators = decorators::parse(mf.comment().value()); m.add_decorators(decorators::parse(mf.comment().value()));
if (m.skip()) if (m.skip())
return; return;
@@ -856,7 +856,7 @@ void tu_visitor::process_static_method(const cppast::cpp_function &mf,
m.scope = detail::cpp_access_specifier_to_scope(as); m.scope = detail::cpp_access_specifier_to_scope(as);
if (mf.comment().has_value()) if (mf.comment().has_value())
m.decorators = decorators::parse(mf.comment().value()); m.add_decorators(decorators::parse(mf.comment().value()));
if (m.skip()) if (m.skip())
return; return;
@@ -883,7 +883,7 @@ void tu_visitor::process_constructor(const cppast::cpp_constructor &mf,
m.scope = detail::cpp_access_specifier_to_scope(as); m.scope = detail::cpp_access_specifier_to_scope(as);
if (mf.comment().has_value()) if (mf.comment().has_value())
m.decorators = decorators::parse(mf.comment().value()); m.add_decorators(decorators::parse(mf.comment().value()));
if (m.skip()) if (m.skip())
return; return;
@@ -918,7 +918,7 @@ void tu_visitor::process_function_parameter(
mp.name = param.name(); mp.name = param.name();
if (param.comment().has_value()) if (param.comment().has_value())
m.decorators = decorators::parse(param.comment().value()); m.add_decorators(decorators::parse(param.comment().value()));
if (mp.skip()) if (mp.skip())
return; return;
@@ -1109,7 +1109,7 @@ void tu_visitor::process_friend(const cppast::cpp_friend &f, class_ &parent)
r.label = "<<friend>>"; r.label = "<<friend>>";
if (f.comment().has_value()) if (f.comment().has_value())
r.decorators = decorators::parse(f.comment().value()); r.add_decorators(decorators::parse(f.comment().value()));
if (r.skip() || r.skip_relationship()) if (r.skip() || r.skip_relationship())
return; return;