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

3
.gitignore vendored
View File

@@ -18,9 +18,10 @@ bin/
/debug/
/release/
/.cache
docs/diagrams
# CLion
.idea/
cmake-build-debug/
cmake-build-debug/

View File

@@ -63,6 +63,7 @@ add_library(clang-umllib OBJECT ${SOURCES})
add_executable(clang-uml ${MAIN_SOURCE_FILE})
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_compile_features(clang-uml PRIVATE cxx_std_17)
install(
FILES

View File

@@ -44,8 +44,7 @@ void element::add_relationship(relationship &&cr)
LOG_DBG("Adding relationship: '{}' - {} - '{}'", cr.destination(),
to_string(cr.type()), full_name(true));
auto it = std::find(relationships_.begin(), relationships_.end(), cr);
if (it == relationships_.end())
if (!util::contains(relationships_, 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) {
// If there are any specific diagram names provided on the command line,
// and this diagram is not in that list - skip it
if (!diagram_names.empty() &&
std::find(diagram_names.begin(), diagram_names.end(), name) ==
diagram_names.end())
if (!diagram_names.empty() && !util::contains(diagram_names, name))
continue;
using clanguml::config::class_diagram;

View File

@@ -34,7 +34,7 @@ std::string package::full_name(bool relative) const
auto ns = using_namespaces();
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());
}

View File

@@ -37,7 +37,7 @@ class package_trait {
public:
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));
}

View File

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

View File

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

View File

@@ -116,7 +116,6 @@ bool find_element_alias(
*/
bool replace_all(
std::string &input, std::string pattern, std::string replace_with);
}
/**
* @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());
}
/**
* @brief Removes prefix sequence of elements from the beggining of col.
*
* @tparam T
* @param col
* @param prefix
*/
template <typename T>
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());
}
/**
* Returns true if element exists in container.
*
* @tparam T
* @tparam E
* @param container
* @param element
* @return
*/
template <typename T, typename E>
bool contains(const T &container, const E &element)
{
if constexpr (std::is_pointer_v<E>) {
return std::find_if(container.begin(), container.end(),
[&element](const auto &e) {
return *e == *element;
}) != container.end();
[&element](const auto &e) { return *e == *element; }) !=
container.end();
}
else {
return std::find(container.cbegin(), container.cend(), element) !=
container.cend();
}
}
}
} // namespace util
} // namespace clanguml