Fixed template instantiation matching

This commit is contained in:
Bartek Kryza
2022-05-21 13:41:58 +02:00
parent 073b3d157d
commit dc26d1354d
8 changed files with 215 additions and 51 deletions

View File

@@ -105,7 +105,7 @@ std::string class_::full_name_no_ns() const
ostr << name();
render_template_params(ostr);
render_template_params(ostr, false);
return ostr.str();
}
@@ -118,7 +118,7 @@ std::string class_::full_name(bool relative) const
std::ostringstream ostr;
ostr << name_and_ns();
render_template_params(ostr);
render_template_params(ostr, relative);
std::string res;
@@ -134,7 +134,7 @@ std::string class_::full_name(bool relative) const
}
std::ostringstream &class_::render_template_params(
std::ostringstream &ostr) const
std::ostringstream &ostr, bool relative) const
{
using clanguml::common::model::namespace_;
@@ -144,8 +144,8 @@ std::ostringstream &class_::render_template_params(
std::transform(templates_.cbegin(), templates_.cend(),
std::back_inserter(tnames),
[ns = using_namespace()](
const auto &tmplt) { return tmplt.to_string(ns); });
[ns = using_namespace(), relative](
const auto &tmplt) { return tmplt.to_string(ns, relative); });
ostr << fmt::format("<{}>", fmt::join(tnames, ","));
}
@@ -160,4 +160,5 @@ bool class_::is_abstract() const
return std::any_of(methods_.begin(), methods_.end(),
[](const auto &method) { return method.is_pure_virtual(); });
}
}

View File

@@ -79,8 +79,13 @@ public:
void is_alias(bool alias) { is_alias_ = alias; }
void find_relationships(
std::vector<std::pair<std::string, common::model::relationship_t>>
&nested_relationships);
private:
std::ostringstream &render_template_params(std::ostringstream &ostr) const;
std::ostringstream &render_template_params(
std::ostringstream &ostr, bool relative) const;
bool is_struct_{false};
bool is_template_{false};

View File

@@ -17,6 +17,7 @@
*/
#include "template_parameter.h"
#include "common/model/enums.h"
#include <common/model/namespace.h>
#include <fmt/format.h>
@@ -81,6 +82,11 @@ void template_parameter::add_template_param(template_parameter &&ct)
template_params_.emplace_back(std::move(ct));
}
void template_parameter::add_template_param(const template_parameter &ct)
{
template_params_.push_back(ct);
}
const std::vector<template_parameter> &
template_parameter::template_params() const
{
@@ -113,28 +119,37 @@ bool operator!=(const template_parameter &l, const template_parameter &r)
}
std::string template_parameter::to_string(
const clanguml::common::model::namespace_ &using_namespace) const
const clanguml::common::model::namespace_ &using_namespace,
bool relative) const
{
using clanguml::common::model::namespace_;
std::string res;
if (!type().empty())
res += namespace_{type()}.relative_to(using_namespace).to_string();
// Render nested template params
if (!template_params_.empty()) {
std::vector<std::string> params;
for (const auto &template_param : template_params_) {
params.push_back(template_param.to_string(using_namespace));
}
res += fmt::format("<{}>", fmt::join(params, ","));
if (!type().empty()) {
if (!relative)
res += namespace_{type()}.to_string();
else
res += namespace_{type()}.relative_to(using_namespace).to_string();
}
if (!name().empty()) {
if (!type().empty())
res += " ";
res += namespace_{name()}.relative_to(using_namespace).to_string();
if (!relative)
res += namespace_{name()}.to_string();
else
res += namespace_{name()}.relative_to(using_namespace).to_string();
}
// Render nested template params
if (!template_params_.empty()) {
std::vector<std::string> params;
for (const auto &template_param : template_params_) {
params.push_back(
template_param.to_string(using_namespace, relative));
}
res += fmt::format("<{}>", fmt::join(params, ","));
}
if (!default_value().empty()) {
@@ -142,7 +157,38 @@ std::string template_parameter::to_string(
res += default_value();
}
// TODO: Refactor this to external configurable class
util::replace_all(res, "std::basic_string<char>", "std::string");
util::replace_all(res, "std::basic_string<wchar_t>", "std::wstring");
return res;
}
void template_parameter::find_nested_relationships(
std::vector<std::pair<std::string, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
std::function<bool(const std::string &full_name)> condition) const
{
// If this type argument should be included in the relationship
// just add it and skip recursion (e.g. this is a user defined type)
if (condition(name())) {
nested_relationships.push_back({to_string({}, false), hint});
}
// Otherwise (e.g. this is a std::shared_ptr) and we're actually
// interested what is stored inside it
else {
for (const auto &template_argument : template_params()) {
if (condition(template_argument.name())) {
nested_relationships.push_back(
{template_argument.to_string({}, false), hint});
}
else {
template_argument.find_nested_relationships(
nested_relationships, hint, condition);
}
}
}
}
}

View File

@@ -17,6 +17,7 @@
*/
#pragma once
#include "common/model/enums.h"
#include "common/model/namespace.h"
#include <string>
@@ -24,10 +25,10 @@
namespace clanguml::class_diagram::model {
/// @brief Represents template parameter or template parameter instantiation
/// @brief Represents template parameter or template argument
///
/// This class can represent both template parameter and template parameter
/// instantiation, including variadic parameters and instantiations with
/// This class can represent both template parameter and template arguments,
/// including variadic parameters and instantiations with
/// nested templates
class template_parameter {
public:
@@ -35,6 +36,8 @@ public:
const std::string &name = "", const std::string &default_value = "",
bool is_variadic = false);
template_parameter(const template_parameter &right) = default;
void set_type(const std::string &type);
std::string type() const;
@@ -72,12 +75,21 @@ public:
}
std::string to_string(
const clanguml::common::model::namespace_ &using_namespace) const;
const clanguml::common::model::namespace_ &using_namespace,
bool relative) const;
void add_template_param(template_parameter &&ct);
void add_template_param(const template_parameter &ct);
const std::vector<template_parameter> &template_params() const;
void find_nested_relationships(
std::vector<std::pair<std::string, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
std::function<bool(const std::string &full_name)> condition) const;
private:
/// Represents the type of non-type template parameters
/// e.g. 'int'