Refactored include diagram generator to util::for_each

This commit is contained in:
Bartek Kryza
2022-04-23 17:58:44 +02:00
parent cc22494c79
commit 8ba5689662
2 changed files with 40 additions and 20 deletions

View File

@@ -30,28 +30,31 @@ generator::generator(diagram_config &config, diagram_model &model)
void generator::generate_relationships( void generator::generate_relationships(
const source_file &f, std::ostream &ostr) const const source_file &f, std::ostream &ostr) const
{ {
if (!util::contains(m_generated_aliases, f.alias()))
return;
LOG_DBG("Generating relationships for file {}", f.full_name(true)); LOG_DBG("Generating relationships for file {}", f.full_name(true));
namespace plantuml_common = clanguml::common::generators::plantuml; namespace plantuml_common = clanguml::common::generators::plantuml;
if (f.type() == common::model::source_file_t::kDirectory) { if (f.type() == common::model::source_file_t::kDirectory) {
for (const auto &file : f) { util::for_each(f, [this, &ostr](const auto &file) {
generate_relationships( generate_relationships(
dynamic_cast<const source_file &>(*file), ostr); dynamic_cast<const source_file &>(*file), ostr);
} });
} }
else { else {
for (const auto &r : f.relationships()) { util::for_each_if(
if (m_model.should_include(r.type()) && f.relationships(),
// make sure we only generate relationships for elements [this, &f](const auto &r) {
// included in the diagram return m_model.should_include(r.type()) &&
util::contains(m_generated_aliases, r.destination()) && util::contains(m_generated_aliases, r.destination());
util::contains(m_generated_aliases, f.alias())) { },
[this, &f, &ostr](const auto &r) {
ostr << f.alias() << " " ostr << f.alias() << " "
<< plantuml_common::to_plantuml(r.type(), r.style()) << " " << plantuml_common::to_plantuml(r.type(), r.style()) << " "
<< r.destination() << '\n'; << r.destination() << '\n';
} });
}
} }
} }
@@ -63,9 +66,11 @@ void generator::generate(const source_file &f, std::ostream &ostr) const
ostr << "folder \"" << f.name(); ostr << "folder \"" << f.name();
ostr << "\" as " << f.alias(); ostr << "\" as " << f.alias();
ostr << " {\n"; ostr << " {\n";
for (const auto &file : f) {
util::for_each(f, [this, &ostr](const auto &file) {
generate(dynamic_cast<const source_file &>(*file), ostr); generate(dynamic_cast<const source_file &>(*file), ostr);
} });
ostr << "}" << '\n'; ostr << "}" << '\n';
m_generated_aliases.emplace(f.alias()); m_generated_aliases.emplace(f.alias());
@@ -92,16 +97,20 @@ void generator::generate(std::ostream &ostr) const
generate_plantuml_directives(ostr, m_config.puml().before); generate_plantuml_directives(ostr, m_config.puml().before);
// Generate files and folders // Generate files and folders
for (const auto &p : m_model) { util::for_each_if(
if (p->type() == common::model::source_file_t::kDirectory || m_model,
m_model.should_include(*p)) [this](const auto &f) {
generate(dynamic_cast<source_file &>(*p), ostr); return f->type() == common::model::source_file_t::kDirectory ||
} m_model.should_include(*f);
},
[this, &ostr](const auto &f) {
generate(dynamic_cast<source_file &>(*f), ostr);
});
// Process file include relationships // Process file include relationships
for (const auto &p : m_model) { util::for_each(m_model, [this, &ostr](const auto &f) {
generate_relationships(dynamic_cast<source_file &>(*p), ostr); generate_relationships(dynamic_cast<source_file &>(*f), ostr);
} });
generate_config_layout_hints(ostr); generate_config_layout_hints(ostr);

View File

@@ -224,5 +224,16 @@ template <typename T, typename F> void for_each(const T &collection, F &&func)
std::forward<decltype(func)>(func)); std::forward<decltype(func)>(func));
} }
template <typename T, typename C, typename F>
void for_each_if(const T &collection, C &&cond, F &&func)
{
std::for_each(std::begin(collection), std::end(collection),
[cond = std::forward<decltype(cond)>(cond),
func = std::forward<decltype(func)>(func)](const auto &e) {
if (cond(e))
func(e);
});
}
} // namespace util } // namespace util
} // namespace clanguml } // namespace clanguml