Refactored utils

This commit is contained in:
Bartek Kryza
2022-01-29 21:17:42 +01:00
parent f539569848
commit 5cc986c9d6
9 changed files with 35 additions and 20 deletions

1
.gitignore vendored
View File

@@ -18,6 +18,7 @@ bin/
/debug/ /debug/
/release/ /release/
/.cache /.cache
docs/diagrams
# CLion # CLion

View File

@@ -63,6 +63,7 @@ add_library(clang-umllib OBJECT ${SOURCES})
add_executable(clang-uml ${MAIN_SOURCE_FILE}) add_executable(clang-uml ${MAIN_SOURCE_FILE})
install(TARGETS clang-uml DESTINATION ${CLANG_UML_INSTALL_BIN_DIR}) install(TARGETS clang-uml DESTINATION ${CLANG_UML_INSTALL_BIN_DIR})
target_link_libraries(clang-uml ${LIBCLANG_LIBRARIES} ${YAML_CPP_LIBRARIES} spdlog::spdlog cppast clang-umllib) target_link_libraries(clang-uml ${LIBCLANG_LIBRARIES} ${YAML_CPP_LIBRARIES} spdlog::spdlog cppast clang-umllib)
target_compile_features(clang-uml PRIVATE cxx_std_17)
install( install(
FILES FILES

View File

@@ -44,8 +44,7 @@ void element::add_relationship(relationship &&cr)
LOG_DBG("Adding relationship: '{}' - {} - '{}'", cr.destination(), LOG_DBG("Adding relationship: '{}' - {} - '{}'", cr.destination(),
to_string(cr.type()), full_name(true)); to_string(cr.type()), full_name(true));
auto it = std::find(relationships_.begin(), relationships_.end(), cr); if (!util::contains(relationships_, cr))
if (it == relationships_.end())
relationships_.emplace_back(std::move(cr)); relationships_.emplace_back(std::move(cr));
} }

View File

@@ -82,9 +82,7 @@ int main(int argc, const char *argv[])
for (const auto &[name, diagram] : config.diagrams) { for (const auto &[name, diagram] : config.diagrams) {
// If there are any specific diagram names provided on the command line, // If there are any specific diagram names provided on the command line,
// and this diagram is not in that list - skip it // and this diagram is not in that list - skip it
if (!diagram_names.empty() && if (!diagram_names.empty() && !util::contains(diagram_names, name))
std::find(diagram_names.begin(), diagram_names.end(), name) ==
diagram_names.end())
continue; continue;
using clanguml::config::class_diagram; using clanguml::config::class_diagram;

View File

@@ -34,7 +34,7 @@ std::string package::full_name(bool relative) const
auto ns = using_namespaces(); auto ns = using_namespaces();
if (relative && (fn.size() >= ns.size())) { if (relative && (fn.size() >= ns.size())) {
if (starts_with(fn, ns)) if (util::starts_with(fn, ns))
fn = std::vector<std::string>(fn.begin() + ns.size(), fn.end()); fn = std::vector<std::string>(fn.begin() + ns.size(), fn.end());
} }

View File

@@ -37,7 +37,7 @@ class package_trait {
public: public:
void add_package(std::unique_ptr<T> p) void add_package(std::unique_ptr<T> p)
{ {
if (!contains(packages_, p.get())) if (!util::contains(packages_, p.get()))
packages_.emplace_back(std::move(p)); packages_.emplace_back(std::move(p));
} }

View File

@@ -97,11 +97,11 @@ void translation_unit_visitor::operator()(const cppast::cpp_entity &file)
auto usn = auto usn =
util::split(ctx.config().using_namespace[0], "::"); util::split(ctx.config().using_namespace[0], "::");
if (!starts_with(usn, package_path)) { if (!util::starts_with(usn, package_path)) {
auto p = std::make_unique<package>( auto p = std::make_unique<package>(
ctx.config().using_namespace); ctx.config().using_namespace);
remove_prefix(package_path, usn); util::remove_prefix(package_path, usn);
remove_prefix(package_parent, usn); util::remove_prefix(package_parent, usn);
p->set_name(e.name()); p->set_name(e.name());
p->set_namespace(package_parent); p->set_namespace(package_parent);
@@ -306,8 +306,8 @@ void translation_unit_visitor::process_class_declaration(
for (const auto &dependency : relationships) { for (const auto &dependency : relationships) {
auto destination = util::split(std::get<0>(dependency), "::"); auto destination = util::split(std::get<0>(dependency), "::");
if (!starts_with(ctx.get_namespace(), destination) && if (!util::starts_with(ctx.get_namespace(), destination) &&
!starts_with(destination, ctx.get_namespace())) { !util::starts_with(destination, ctx.get_namespace())) {
relationship r{ relationship r{
relationship_t::kDependency, std::get<0>(dependency)}; relationship_t::kDependency, std::get<0>(dependency)};
current_package.value().add_relationship(std::move(r)); current_package.value().add_relationship(std::move(r));
@@ -333,8 +333,8 @@ void translation_unit_visitor::process_function(const cppast::cpp_function &f)
for (const auto &dependency : relationships) { for (const auto &dependency : relationships) {
auto destination = util::split(std::get<0>(dependency), "::"); auto destination = util::split(std::get<0>(dependency), "::");
if (!starts_with(ctx.get_namespace(), destination) && if (!util::starts_with(ctx.get_namespace(), destination) &&
!starts_with(destination, ctx.get_namespace())) { !util::starts_with(destination, ctx.get_namespace())) {
relationship r{ relationship r{
relationship_t::kDependency, std::get<0>(dependency)}; relationship_t::kDependency, std::get<0>(dependency)};
current_package.value().add_relationship(std::move(r)); current_package.value().add_relationship(std::move(r));

View File

@@ -146,5 +146,6 @@ bool replace_all(
return replaced; return replaced;
} }
} }
} }

View File

@@ -116,7 +116,6 @@ bool find_element_alias(
*/ */
bool replace_all( bool replace_all(
std::string &input, std::string pattern, std::string replace_with); std::string &input, std::string pattern, std::string replace_with);
}
/** /**
* @brief Appends a vector to a vector. * @brief Appends a vector to a vector.
@@ -148,6 +147,13 @@ bool starts_with(const std::vector<T> &col, const std::vector<T> &prefix)
std::vector<std::string>(col.begin(), col.begin() + prefix.size()); std::vector<std::string>(col.begin(), col.begin() + prefix.size());
} }
/**
* @brief Removes prefix sequence of elements from the beggining of col.
*
* @tparam T
* @param col
* @param prefix
*/
template <typename T> template <typename T>
void remove_prefix(std::vector<T> &col, const std::vector<T> &prefix) void remove_prefix(std::vector<T> &col, const std::vector<T> &prefix)
{ {
@@ -157,18 +163,27 @@ void remove_prefix(std::vector<T> &col, const std::vector<T> &prefix)
col = std::vector<T>(col.begin() + prefix.size(), col.end()); col = std::vector<T>(col.begin() + prefix.size(), col.end());
} }
/**
* Returns true if element exists in container.
*
* @tparam T
* @tparam E
* @param container
* @param element
* @return
*/
template <typename T, typename E> template <typename T, typename E>
bool contains(const T &container, const E &element) bool contains(const T &container, const E &element)
{ {
if constexpr (std::is_pointer_v<E>) { if constexpr (std::is_pointer_v<E>) {
return std::find_if(container.begin(), container.end(), return std::find_if(container.begin(), container.end(),
[&element](const auto &e) { [&element](const auto &e) { return *e == *element; }) !=
return *e == *element; container.end();
}) != container.end();
} }
else { else {
return std::find(container.cbegin(), container.cend(), element) != return std::find(container.cbegin(), container.cend(), element) !=
container.cend(); container.cend();
} }
} }
} } // namespace util
} // namespace clanguml