Refactoring template_parameter model

This commit is contained in:
Bartek Kryza
2023-03-16 01:53:10 +01:00
parent 3a01d8689c
commit 6d4533018b
10 changed files with 393 additions and 254 deletions

View File

@@ -26,30 +26,105 @@
namespace clanguml::common::model {
/// @brief Represents template parameter or template argument
enum class template_parameter_kind_t {
template_type,
template_template_type,
non_type_template,
argument, // a.k.a. type parameter specialization
concept_constraint
};
std::string to_string(template_parameter_kind_t k);
/// @brief Represents template parameter, template arguments or concept
/// constraints
///
/// This class can represent both template parameter and template arguments,
/// including variadic parameters and instantiations with
/// nested templates
class template_parameter {
public:
template_parameter(const std::string &type = "",
const std::string &name = "", std::string default_value = "",
bool is_variadic = false);
template_parameter() = default;
template_parameter(const template_parameter &right) = default;
public:
static template_parameter make_template_type(std::string name,
const std::optional<std::string> &default_value = {},
bool is_variadic = false)
{
template_parameter p;
p.set_kind(template_parameter_kind_t::template_type);
p.set_name(std::move(name));
p.is_variadic(is_variadic);
if (default_value)
p.set_default_value(default_value.value());
return p;
}
static template_parameter make_template_template_type(std::string name,
const std::optional<std::string> &default_value = {},
bool is_variadic = false)
{
template_parameter p;
p.set_kind(template_parameter_kind_t::template_template_type);
p.set_name(name + "<>");
if (default_value)
p.set_default_value(default_value.value());
p.is_variadic(is_variadic);
return p;
}
static template_parameter make_non_type_template(std::string type,
const std::optional<std::string> &name,
const std::optional<std::string> &default_value = {},
bool is_variadic = false)
{
template_parameter p;
p.set_kind(template_parameter_kind_t::non_type_template);
p.set_type(std::move(type));
if (name)
p.set_name(name.value());
if (default_value)
p.set_default_value(default_value.value());
p.is_variadic(is_variadic);
return p;
}
static template_parameter make_argument(
std::string type, const std::optional<std::string> &default_value = {})
{
template_parameter p;
p.set_kind(template_parameter_kind_t::argument);
p.set_type(std::move(type));
if (default_value)
p.set_default_value(default_value.value());
return p;
}
static template_parameter make_unexposed_argument(
std::string type, const std::optional<std::string> &default_value = {})
{
template_parameter p = make_argument(std::move(type), default_value);
p.set_unexposed(true);
return p;
}
// template_parameter(const std::optional<std::string> &type = {},
// const std::optional<std::string> &name = {},
// const std::optional<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;
std::optional<std::string> type() const;
void set_id(const int64_t id) { id_ = id; }
std::optional<int64_t> id() const { return id_; }
const std::optional<int64_t> &id() const { return id_; }
void set_name(const std::string &name);
std::string name() const;
std::optional<std::string> name() const;
void set_default_value(const std::string &value);
std::string default_value() const;
const std::optional<std::string> &default_value() const;
void is_variadic(bool is_variadic) noexcept;
bool is_variadic() const noexcept;
@@ -101,18 +176,29 @@ public:
const;
void set_concept_constraint(std::string constraint);
const std::optional<std::string> &concept_constraint() const;
template_parameter_kind_t kind() const { return kind_; }
void set_kind(template_parameter_kind_t kind) { kind_ = kind; }
bool is_unexposed() const { return is_unexposed_; }
void set_unexposed(bool unexposed) { is_unexposed_ = unexposed; }
private:
template_parameter_kind_t kind_;
/// Represents the type of non-type template parameters
/// e.g. 'int'
std::string type_;
/// e.g. 'int' or type of template arguments
std::optional<std::string> type_;
/// The name of the parameter (e.g. 'T' or 'N')
std::string name_;
std::optional<std::string> name_;
/// Default value of the template parameter
std::string default_value_;
std::optional<std::string> default_value_;
/// Whether the template parameter is a regular template parameter
/// When false, it is a non-type template parameter
@@ -136,5 +222,7 @@ private:
std::vector<template_parameter> template_params_;
std::optional<int64_t> id_;
bool is_unexposed_{false};
};
} // namespace clanguml::common::model