Added regex support to parents filter

This commit is contained in:
Bartek Kryza
2023-06-08 00:03:27 +02:00
parent ad2fc3f8a6
commit b3b95efb65
11 changed files with 306 additions and 139 deletions

View File

@@ -1,5 +1,5 @@
/**
* src/class_diagram/visitor/translation_unit_visitor.h
* src/common/types.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
@@ -20,15 +20,21 @@
#include <cassert>
#include <cstdint>
#include <optional>
#include <regex>
#include <unordered_set>
#include <variant>
#include <vector>
#include "model/namespace.h"
namespace clanguml::common {
using id_t = int64_t;
enum class generator_type_t { plantuml, json };
std::string to_string(const std::string &s);
template <typename T> class optional_ref {
public:
using optional_type = T;
@@ -137,4 +143,76 @@ using reference_vector = std::vector<std::reference_wrapper<T>>;
template <typename T>
using reference_set = std::unordered_set<std::reference_wrapper<T>>;
/**
* @brief Wrapper around std::regex, which contains original pattern
*/
struct regex {
regex(std::regex r, std::string p)
: regexp{std::move(r)}
, pattern{std::move(p)}
{
}
[[nodiscard]] bool operator==(const std::string &v) const
{
return std::regex_match(v, regexp);
}
std::regex regexp;
std::string pattern;
};
template <typename T> struct or_regex {
or_regex() = default;
or_regex(T v)
: value_{std::move(v)}
{
}
or_regex(std::regex r, std::string p)
: value_{regex{std::move(r), std::move(p)}}
{
}
or_regex &operator=(const T &v)
{
value_ = v;
return *this;
}
or_regex &operator=(const regex &v)
{
value_ = v;
return *this;
}
[[nodiscard]] bool operator==(const T &v) const
{
if (std::holds_alternative<regex>(value_))
return std::regex_match(v, std::get<regex>(value_).regexp);
return std::get<T>(value_) == v;
}
std::string to_string() const
{
if (std::holds_alternative<regex>(value_))
return std::get<regex>(value_).pattern;
return clanguml::common::to_string(std::get<T>(value_));
}
const std::variant<T, regex> &value() const { return value_; }
private:
std::variant<T, regex> value_;
};
using string_or_regex = or_regex<std::string>;
std::string to_string(string_or_regex sr);
using namespace_or_regex = common::or_regex<common::model::namespace_>;
} // namespace clanguml::common