Initial support for concept dependency relationships in class diagrams
This commit is contained in:
@@ -79,9 +79,6 @@ public:
|
||||
|
||||
bool is_abstract() const;
|
||||
|
||||
bool is_concept() const { return is_concept_; }
|
||||
void is_concept(bool concept_) { is_concept_ = concept_; }
|
||||
|
||||
void find_relationships(
|
||||
std::vector<std::pair<std::string, common::model::relationship_t>>
|
||||
&nested_relationships);
|
||||
@@ -95,7 +92,6 @@ private:
|
||||
bool is_template_instantiation_{false};
|
||||
bool is_alias_{false};
|
||||
bool is_union_{false};
|
||||
bool is_concept_{false};
|
||||
std::vector<class_member> members_;
|
||||
std::vector<class_method> methods_;
|
||||
std::vector<class_parent> bases_;
|
||||
|
||||
72
src/class_diagram/model/concept.cc
Normal file
72
src/class_diagram/model/concept.cc
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* src/class_diagram/model/concept.cc
|
||||
*
|
||||
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "concept.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace clanguml::class_diagram::model {
|
||||
|
||||
concept_::concept_(const common::model::namespace_ &using_namespace)
|
||||
: element{using_namespace}
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(const concept_ &l, const concept_ &r)
|
||||
{
|
||||
return l.id() == r.id();
|
||||
}
|
||||
|
||||
std::string concept_::full_name_no_ns() const
|
||||
{
|
||||
using namespace clanguml::util;
|
||||
|
||||
std::ostringstream ostr;
|
||||
|
||||
ostr << name();
|
||||
|
||||
render_template_params(ostr, using_namespace(), false);
|
||||
|
||||
return ostr.str();
|
||||
}
|
||||
|
||||
std::string concept_::full_name(bool relative) const
|
||||
{
|
||||
using namespace clanguml::util;
|
||||
using clanguml::common::model::namespace_;
|
||||
|
||||
std::ostringstream ostr;
|
||||
|
||||
ostr << name_and_ns();
|
||||
|
||||
render_template_params(ostr, using_namespace(), relative);
|
||||
|
||||
std::string res;
|
||||
|
||||
if (relative)
|
||||
res = using_namespace().relative(ostr.str());
|
||||
else
|
||||
res = ostr.str();
|
||||
|
||||
if (res.empty())
|
||||
return "<<anonymous>>";
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
59
src/class_diagram/model/concept.h
Normal file
59
src/class_diagram/model/concept.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* src/class_diagram/model/concept.h
|
||||
*
|
||||
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "common/model/element.h"
|
||||
#include "common/model/stylable_element.h"
|
||||
#include "common/model/template_parameter.h"
|
||||
#include "common/model/template_trait.h"
|
||||
#include "common/types.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace clanguml::class_diagram::model {
|
||||
|
||||
struct requires_expression {
|
||||
common::model::template_parameter parameter;
|
||||
std::vector<std::string> requirements;
|
||||
};
|
||||
|
||||
class concept_ : public common::model::element,
|
||||
public common::model::stylable_element,
|
||||
public common::model::template_trait {
|
||||
public:
|
||||
concept_(const common::model::namespace_ &using_namespace);
|
||||
|
||||
concept_(const concept_ &) = delete;
|
||||
concept_(concept_ &&) noexcept = default;
|
||||
concept_ &operator=(const concept_ &) = delete;
|
||||
concept_ &operator=(concept_ &&) = delete;
|
||||
|
||||
std::string type_name() const override { return "concept"; }
|
||||
|
||||
friend bool operator==(const concept_ &l, const concept_ &r);
|
||||
|
||||
std::string full_name(bool relative = true) const override;
|
||||
|
||||
std::string full_name_no_ns() const override;
|
||||
|
||||
std::vector<std::string> requires_expression_;
|
||||
|
||||
std::string full_name_;
|
||||
};
|
||||
}
|
||||
@@ -32,6 +32,11 @@ const common::reference_vector<class_> &diagram::classes() const
|
||||
|
||||
const common::reference_vector<enum_> &diagram::enums() const { return enums_; }
|
||||
|
||||
const common::reference_vector<concept_> &diagram::concepts() const
|
||||
{
|
||||
return concepts_;
|
||||
}
|
||||
|
||||
common::model::diagram_t diagram::type() const
|
||||
{
|
||||
return common::model::diagram_t::kClass;
|
||||
@@ -48,6 +53,11 @@ common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
|
||||
|
||||
res = get_enum(full_name);
|
||||
|
||||
if (res.has_value())
|
||||
return res;
|
||||
|
||||
res = get_concept(full_name);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -63,6 +73,11 @@ common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
|
||||
|
||||
res = get_enum(id);
|
||||
|
||||
if (res.has_value())
|
||||
return res;
|
||||
|
||||
res = get_concept(id);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -78,6 +93,12 @@ bool diagram::has_enum(const enum_ &e) const
|
||||
[&e](const auto &ee) { return ee.get().full_name() == e.full_name(); });
|
||||
}
|
||||
|
||||
bool diagram::has_concept(const concept_ &c) const
|
||||
{
|
||||
return std::any_of(concepts_.cbegin(), concepts_.cend(),
|
||||
[&c](const auto &cc) { return cc.get() == c; });
|
||||
}
|
||||
|
||||
common::optional_ref<class_> diagram::get_class(const std::string &name) const
|
||||
{
|
||||
for (const auto &c : classes_) {
|
||||
@@ -126,6 +147,32 @@ common::optional_ref<enum_> diagram::get_enum(
|
||||
return {};
|
||||
}
|
||||
|
||||
common::optional_ref<concept_> diagram::get_concept(
|
||||
const std::string &name) const
|
||||
{
|
||||
for (const auto &c : concepts_) {
|
||||
const auto full_name = c.get().full_name(false);
|
||||
|
||||
if (full_name == name) {
|
||||
return {c};
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
common::optional_ref<concept_> diagram::get_concept(
|
||||
clanguml::common::model::diagram_element::id_t id) const
|
||||
{
|
||||
for (const auto &c : concepts_) {
|
||||
if (c.get().id() == id) {
|
||||
return {c};
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
bool diagram::add_package(std::unique_ptr<common::model::package> &&p)
|
||||
{
|
||||
LOG_DBG("Adding namespace package: {}, {}", p->name(), p->full_name(true));
|
||||
@@ -173,8 +220,8 @@ bool diagram::add_class(std::unique_ptr<class_> &&c)
|
||||
}
|
||||
}
|
||||
catch (const std::runtime_error &e) {
|
||||
LOG_WARN("Cannot add template specialization {} with id {} due to: {}",
|
||||
name, id, e.what());
|
||||
LOG_WARN(
|
||||
"Cannot add concept {} with id {} due to: {}", name, id, e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -207,6 +254,55 @@ bool diagram::add_enum(std::unique_ptr<enum_> &&e)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool diagram::add_concept(std::unique_ptr<concept_> &&c)
|
||||
{
|
||||
const auto base_name = c->name();
|
||||
const auto full_name = c->full_name(false);
|
||||
|
||||
LOG_DBG("Adding concept: {}::{}, {}", c->get_namespace().to_string(),
|
||||
base_name, full_name);
|
||||
|
||||
if (util::contains(base_name, "::"))
|
||||
throw std::runtime_error("Name cannot contain namespace: " + base_name);
|
||||
|
||||
if (util::contains(base_name, "*"))
|
||||
throw std::runtime_error("Name cannot contain *: " + base_name);
|
||||
|
||||
const auto ns = c->get_relative_namespace();
|
||||
auto name = base_name;
|
||||
auto name_with_ns = c->name_and_ns();
|
||||
auto name_and_ns = ns | name;
|
||||
auto &cc = *c;
|
||||
auto id = cc.id();
|
||||
|
||||
try {
|
||||
if (!has_concept(cc)) {
|
||||
if (add_element(ns, std::move(c)))
|
||||
concepts_.push_back(std::ref(cc));
|
||||
|
||||
const auto &el = get_element<concept_>(name_and_ns).value();
|
||||
|
||||
if ((el.name() != name) || !(el.get_relative_namespace() == ns))
|
||||
throw std::runtime_error(
|
||||
"Invalid element stored in the diagram tree");
|
||||
|
||||
LOG_DBG("Added concept {} ({} - [{}])", base_name, full_name, id);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (const std::runtime_error &e) {
|
||||
LOG_WARN(
|
||||
"Cannot add concept {} with id {} due to: {}", name, id, e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_DBG("Concept {} ({} - [{}]) already in the model", base_name, full_name,
|
||||
id);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void diagram::get_parents(
|
||||
clanguml::common::reference_set<class_> &parents) const
|
||||
{
|
||||
@@ -257,6 +353,11 @@ std::string diagram::to_alias(
|
||||
return e.get().alias();
|
||||
}
|
||||
|
||||
for (const auto &c : concepts_) {
|
||||
if (c.get().id() == id)
|
||||
return c.get().alias();
|
||||
}
|
||||
|
||||
throw error::uml_alias_missing(fmt::format("Missing alias for {}", id));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "common/model/nested_trait.h"
|
||||
#include "common/model/package.h"
|
||||
#include "common/types.h"
|
||||
#include "concept.h"
|
||||
#include "enum.h"
|
||||
|
||||
#include <string>
|
||||
@@ -54,10 +55,14 @@ public:
|
||||
|
||||
const common::reference_vector<enum_> &enums() const;
|
||||
|
||||
const common::reference_vector<concept_> &concepts() const;
|
||||
|
||||
bool has_class(const class_ &c) const;
|
||||
|
||||
bool has_enum(const enum_ &e) const;
|
||||
|
||||
bool has_concept(const concept_ &e) const;
|
||||
|
||||
common::optional_ref<class_> get_class(const std::string &name) const;
|
||||
|
||||
common::optional_ref<class_> get_class(
|
||||
@@ -68,10 +73,17 @@ public:
|
||||
common::optional_ref<enum_> get_enum(
|
||||
clanguml::common::model::diagram_element::id_t id) const;
|
||||
|
||||
common::optional_ref<concept_> get_concept(const std::string &name) const;
|
||||
|
||||
common::optional_ref<concept_> get_concept(
|
||||
clanguml::common::model::diagram_element::id_t id) const;
|
||||
|
||||
bool add_class(std::unique_ptr<class_> &&c);
|
||||
|
||||
bool add_enum(std::unique_ptr<enum_> &&e);
|
||||
|
||||
bool add_concept(std::unique_ptr<concept_> &&e);
|
||||
|
||||
bool add_package(std::unique_ptr<common::model::package> &&p);
|
||||
|
||||
std::string to_alias(
|
||||
@@ -90,6 +102,8 @@ private:
|
||||
common::reference_vector<class_> classes_;
|
||||
|
||||
common::reference_vector<enum_> enums_;
|
||||
|
||||
common::reference_vector<concept_> concepts_;
|
||||
};
|
||||
} // namespace clanguml::class_diagram::model
|
||||
|
||||
|
||||
Reference in New Issue
Block a user