Skip to content

Commit cb9a59a

Browse files
committed
WIP (#432)
1 parent 5c87a0b commit cb9a59a

File tree

10 files changed

+199
-539
lines changed

10 files changed

+199
-539
lines changed

src/class_diagram/model/diagram.cc

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -313,34 +313,6 @@ bool diagram::has_element(eid_t id) const
313313
[id](const auto &c) { return c.get().id() == id; });
314314
}
315315

316-
std::string diagram::to_alias(eid_t id) const
317-
{
318-
LOG_TRACE("Looking for alias for {}", id);
319-
320-
for (const auto &c : classes()) {
321-
if (c.get().id() == id) {
322-
return c.get().alias();
323-
}
324-
}
325-
326-
for (const auto &e : enums()) {
327-
if (e.get().id() == id)
328-
return e.get().alias();
329-
}
330-
331-
for (const auto &c : concepts()) {
332-
if (c.get().id() == id)
333-
return c.get().alias();
334-
}
335-
336-
for (const auto &c : objc_interfaces()) {
337-
if (c.get().id() == id)
338-
return c.get().alias();
339-
}
340-
341-
throw error::uml_alias_missing(fmt::format("Missing alias for {}", id));
342-
}
343-
344316
void diagram::remove_redundant_dependencies()
345317
{
346318
using common::eid_t;
@@ -376,49 +348,6 @@ void diagram::remove_redundant_dependencies()
376348
});
377349
}
378350

379-
void diagram::apply_filter()
380-
{
381-
common::model::apply_filter(relationships(), filter());
382-
383-
// First find all element ids which should be removed
384-
std::set<eid_t> to_remove;
385-
386-
while (true) {
387-
for_all_elements([&](auto &&elements_view) mutable {
388-
for (const auto &el : elements_view)
389-
if (!filter().should_include(el.get()))
390-
to_remove.emplace(el.get().id());
391-
});
392-
393-
if (to_remove.empty())
394-
break;
395-
396-
element_view<class_>::remove(to_remove);
397-
element_view<enum_>::remove(to_remove);
398-
element_view<concept_>::remove(to_remove);
399-
element_view<objc_interface>::remove(to_remove);
400-
401-
nested_trait_ns::remove(to_remove);
402-
403-
to_remove.clear();
404-
405-
filter().reset();
406-
}
407-
408-
for_all_elements([&](auto &&elements_view) mutable {
409-
for (const auto &el : elements_view)
410-
el.get().apply_filter(filter(), to_remove);
411-
});
412-
413-
auto &rels = relationships();
414-
rels.erase(std::remove_if(std::begin(rels), std::end(rels),
415-
[&to_remove](auto &&r) {
416-
return to_remove.count(r.source()) > 0 ||
417-
to_remove.count(r.destination()) > 0;
418-
}),
419-
std::end(rels));
420-
}
421-
422351
bool diagram::is_empty() const
423352
{
424353
return element_view<class_>::is_empty() &&

src/class_diagram/model/diagram.h

Lines changed: 21 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "class.h"
2121
#include "common/model/diagram.h"
2222
#include "common/model/element_view.h"
23+
#include "common/model/filters/diagram_filter.h"
2324
#include "common/model/nested_trait.h"
2425
#include "common/model/package.h"
2526
#include "common/types.h"
@@ -44,21 +45,16 @@ using common::model::element_views;
4445
using common::model::path;
4546
using common::model::path_type;
4647

47-
using nested_trait_ns =
48-
clanguml::common::model::nested_trait<clanguml::common::model::element,
49-
clanguml::common::model::namespace_>;
50-
5148
/**
5249
* @brief Class representing a class diagram.
5350
*/
54-
class diagram : public common::model::diagram,
55-
public element_views<class_, enum_, concept_, objc_interface>,
56-
public nested_trait_ns {
51+
class diagram : public common::model::hierarchical_diagram<class_, enum_,
52+
concept_, objc_interface> {
5753
public:
58-
using nested_trait_t = nested_trait_ns;
59-
6054
diagram(const config::class_diagram &config)
61-
: clanguml::common::model::diagram{config}, config_{config}
55+
: common::model::hierarchical_diagram<class_, enum_, concept_,
56+
objc_interface>(config)
57+
, config_{config}
6258
{
6359
}
6460

@@ -76,11 +72,6 @@ class diagram : public common::model::diagram,
7672
*/
7773
diagram_t type() const override;
7874

79-
/**
80-
* Inherit the should_include methods from the common diagram model.
81-
*/
82-
using common::model::diagram::should_include;
83-
8475
/**
8576
* @brief Whether a class_member should be included in the diagram.
8677
*
@@ -243,37 +234,21 @@ class diagram : public common::model::diagram,
243234
void add_concept(std::unique_ptr<concept_> &&c);
244235

245236
void add_objc_interface(std::unique_ptr<objc_interface> &&c);
246-
247-
template <typename ElementT> void move(eid_t id, const path &parent_path)
248-
{
249-
LOG_DBG("Moving element {} to package {}", id.value(),
250-
parent_path.to_string());
251-
252-
auto e = nested_trait_ns::get_and_remove<ElementT>(id);
253-
assert(e);
254-
255-
element_view<ElementT>::remove({id});
256-
added_elements_.erase(id);
257-
258-
this->add<ElementT>(parent_path, std::move(e));
259-
}
260-
261-
template <typename ElementT> void remove(eid_t id)
262-
{
263-
nested_trait_ns::remove({id});
264-
element_view<ElementT>::remove({id});
265-
}
266-
267-
/**
268-
* @brief Convert element id to PlantUML alias.
269-
*
270-
* @todo This method does not belong here - refactor to PlantUML specific
271-
* code.
272-
*
273-
* @param id Id of the diagram element.
274-
* @return PlantUML alias.
275-
*/
276-
std::string to_alias(eid_t id) const;
237+
//
238+
// template <typename ElementT> void move(eid_t id, const path
239+
// &parent_path)
240+
// {
241+
// LOG_DBG("Moving element {} to package {}", id.value(),
242+
// parent_path.to_string());
243+
//
244+
// auto e = nested_trait_ns::get_and_remove<ElementT>(id);
245+
// assert(e);
246+
//
247+
// element_view<ElementT>::remove({id});
248+
// added_elements_.erase(id);
249+
//
250+
// this->add<ElementT>(parent_path, std::move(e));
251+
// }
277252

278253
/**
279254
* @brief Given an initial set of classes, add all their parents to the
@@ -312,17 +287,6 @@ class diagram : public common::model::diagram,
312287
const config::class_diagram &config_;
313288

314289
std::set<eid_t> added_elements_;
315-
316-
template <typename ElementT>
317-
bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);
318-
319-
template <typename ElementT>
320-
bool add_with_module_path(
321-
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
322-
323-
template <typename ElementT>
324-
bool add_with_filesystem_path(
325-
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
326290
};
327291

328292
template <typename ElementT> bool diagram::contains(const ElementT &element)
@@ -333,143 +297,6 @@ template <typename ElementT> bool diagram::contains(const ElementT &element)
333297
const auto &element_opt) { return element_opt.get() == element; });
334298
}
335299

336-
template <typename ElementT>
337-
bool diagram::add_with_namespace_path(std::unique_ptr<ElementT> &&e)
338-
{
339-
if (added_elements_.count(e->id()) > 0)
340-
return true;
341-
342-
added_elements_.emplace(e->id());
343-
344-
const auto base_name = e->name();
345-
const auto full_name = e->full_name(false);
346-
const auto element_type = e->type_name();
347-
348-
LOG_DBG("Adding {}: {}::{}, {} [{}]", element_type,
349-
e->get_namespace().to_string(), base_name, full_name, e->id().usr());
350-
351-
if (util::contains(base_name, "::"))
352-
throw std::runtime_error("Name cannot contain namespace: " + base_name);
353-
354-
const auto ns = e->get_relative_namespace();
355-
356-
auto name = base_name;
357-
auto name_and_ns = ns | name;
358-
auto &e_ref = *e;
359-
auto id = e_ref.id();
360-
361-
try {
362-
if (!contains(e_ref)) {
363-
if (add_element(ns, std::move(e)))
364-
element_view<ElementT>::add(std::ref(e_ref));
365-
366-
#if !defined(NDEBUG)
367-
const auto maybe_el = get_element<ElementT>(name_and_ns);
368-
const auto &el = maybe_el.value();
369-
370-
if ((el.name() != name) || !(el.get_relative_namespace() == ns))
371-
throw std::runtime_error(
372-
"Invalid element stored in the diagram tree");
373-
374-
LOG_DBG("Added {} {} ({} - [{}])", element_type, base_name,
375-
full_name, id);
376-
#endif
377-
378-
return true;
379-
}
380-
}
381-
catch (const std::runtime_error &e) {
382-
LOG_WARN("Cannot add {} {} with id {} due to: {}", element_type, name,
383-
id, e.what());
384-
return false;
385-
}
386-
387-
LOG_DBG("{} {} ({} - [{}]) already in the model", element_type, base_name,
388-
full_name, id);
389-
390-
return false;
391-
}
392-
393-
template <typename ElementT>
394-
bool diagram::add_with_module_path(
395-
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e)
396-
{
397-
if (added_elements_.count(e->id()) > 0)
398-
return true;
399-
400-
added_elements_.emplace(e->id());
401-
402-
const auto element_type = e->type_name();
403-
404-
// Make sure all parent modules are already packages in the
405-
// model
406-
for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
407-
auto pkg = std::make_unique<common::model::package>(
408-
e->using_namespace(), parent_path.type());
409-
pkg->set_name(*it);
410-
auto ns =
411-
common::model::path(parent_path.begin(), it, parent_path.type());
412-
// ns.pop_back();
413-
pkg->set_namespace(ns);
414-
pkg->set_id(common::to_id(*pkg));
415-
416-
add(ns, std::move(pkg));
417-
}
418-
419-
const auto base_name = e->name();
420-
const auto full_name = e->full_name(false);
421-
auto &e_ref = *e;
422-
423-
if (add_element(parent_path, std::move(e))) {
424-
element_view<ElementT>::add(std::ref(e_ref));
425-
return true;
426-
}
427-
428-
return false;
429-
}
430-
431-
template <typename ElementT>
432-
bool diagram::add_with_filesystem_path(
433-
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e)
434-
{
435-
if (added_elements_.count(e->id()) > 0)
436-
return false;
437-
438-
LOG_DBG("Adding element {} at path {}", e->full_name(false),
439-
parent_path.to_string());
440-
441-
const auto element_type = e->type_name();
442-
443-
// Make sure all parent modules are already packages in the
444-
// model
445-
for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
446-
auto pkg = std::make_unique<common::model::package>(
447-
e->using_namespace(), parent_path.type());
448-
pkg->set_name(*it);
449-
auto package_path =
450-
common::model::path(parent_path.begin(), it, parent_path.type());
451-
pkg->set_namespace(package_path);
452-
pkg->set_id(common::to_id(*pkg));
453-
454-
LOG_DBG("Adding filesystem package {} at path {}", pkg->name(),
455-
package_path.to_string());
456-
457-
add(package_path, std::move(pkg));
458-
}
459-
460-
const auto base_name = e->name();
461-
const auto full_name = e->full_name(false);
462-
auto &e_ref = *e;
463-
464-
if (add_element(parent_path, std::move(e))) {
465-
added_elements_.emplace(e_ref.id());
466-
element_view<ElementT>::add(std::ref(e_ref));
467-
return true;
468-
}
469-
470-
return false;
471-
}
472-
473300
template <typename ElementT>
474301
opt_ref<ElementT> diagram::find(const std::string &name) const
475302
{
@@ -523,23 +350,6 @@ const common::reference_vector<ElementT> &diagram::elements() const
523350
return element_view<ElementT>::view();
524351
}
525352

526-
//
527-
// Template method specialization pre-declarations...
528-
//
529-
template <>
530-
bool diagram::add_with_namespace_path<common::model::package>(
531-
std::unique_ptr<common::model::package> &&p);
532-
533-
template <>
534-
bool diagram::add_with_module_path<common::model::package>(
535-
const common::model::path &parent_path,
536-
std::unique_ptr<common::model::package> &&p);
537-
538-
template <>
539-
bool diagram::add_with_filesystem_path<common::model::package>(
540-
const common::model::path &parent_path,
541-
std::unique_ptr<common::model::package> &&p);
542-
543353
} // namespace clanguml::class_diagram::model
544354

545355
namespace clanguml::common::model {

src/class_diagram/visitor/translation_unit_visitor.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -674,20 +674,7 @@ bool translation_unit_visitor::add_or_update(
674674
set_source_location(*cls, class_model);
675675
}
676676

677-
if (is_complete_definition) {
678-
if (maybe_existing_model &&
679-
config().package_type() == config::package_type_t::kDirectory) {
680-
// Move the class model to current filesystem path
681-
// Eventually, this should be refactored so that it's
682-
// not needed
683-
const auto file = config().make_path_relative(class_model.file());
684-
common::model::path p{
685-
file.string(), common::model::path_type::kFilesystem};
686-
p.pop_back();
687-
diagram().move<ElementT>(id, p);
688-
}
689-
}
690-
else {
677+
if (!is_complete_definition) {
691678
forward_declarations_.get<ElementT>().emplace(id, std::move(c_ptr));
692679
return true;
693680
}

0 commit comments

Comments
 (0)