/** * src/uml/class_diagram_model.h * * Copyright (c) 2021 Bartek Kryza * * 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 "util/util.h" #include #include #include #include #include #include #include #include #include namespace clanguml { namespace model { namespace class_diagram { enum class scope_t { kPublic, kProtected, kPrivate }; enum class relationship_t { kNone, kExtension, kComposition, kAggregation, kContainment, kOwnership, kAssociation, kInstantiation, kFriendship }; class element { public: element() : m_id{m_nextId++} { } std::string name; std::vector namespace_; std::string alias() const { return fmt::format("C_{:010}", m_id); } protected: const uint64_t m_id{0}; private: static std::atomic_uint64_t m_nextId; }; struct class_element { scope_t scope; std::string name; std::string type; }; struct class_member : public class_element { bool is_relationship{false}; bool is_static{false}; }; struct method_argument { std::string type; std::string name; }; struct class_method : public class_element { std::vector arguments; bool is_pure_virtual{false}; bool is_virtual{false}; bool is_const{false}; bool is_defaulted{false}; bool is_static{false}; }; struct class_parent { enum class access_t { kPublic, kProtected, kPrivate }; std::string name; bool is_virtual{false}; access_t access; }; struct class_relationship { relationship_t type{relationship_t::kAssociation}; std::string destination; std::string cardinality_source; std::string cardinality_destination; std::string label; }; struct class_template { std::string name; std::string type; std::string default_value; }; class class_ : public element { public: std::string usr; bool is_struct{false}; bool is_template{false}; bool is_template_instantiation{false}; std::vector members; std::vector methods; std::vector bases; std::vector inner_classes; std::vector relationships; std::vector templates; std::string base_template_usr; std::string full_name( const std::vector &using_namespaces) const { using namespace clanguml::util; std::ostringstream ostr; ostr << ns_relative(using_namespaces, name); if (!templates.empty()) { std::vector tnames; std::transform(templates.cbegin(), templates.cend(), std::back_inserter(tnames), [&using_namespaces](const auto &tmplt) { std::vector res; if (!tmplt.type.empty()) res.push_back( ns_relative(using_namespaces, tmplt.type)); if (!tmplt.name.empty()) res.push_back( ns_relative(using_namespaces, tmplt.name)); if (!tmplt.default_value.empty()) { res.push_back("="); res.push_back(tmplt.default_value); } return fmt::format("{}", fmt::join(res, " ")); }); ostr << fmt::format("<{}>", fmt::join(tnames, ", ")); } return ostr.str(); } bool is_abstract() const { // TODO check if all base abstract methods are overriden // with non-abstract methods return std::any_of(methods.begin(), methods.end(), [](const auto &method) { return method.is_pure_virtual; }); } }; struct enum_ : public element { std::vector constants; }; struct diagram { std::string name; std::vector classes; std::vector enums; std::string to_alias(const std::vector &using_namespaces, const std::string &full_name) const { for (const auto &c : classes) { if (c.full_name(using_namespaces) == full_name) { return c.alias(); } } return full_name; } std::string usr_to_name(const std::vector &using_namespaces, const std::string &usr) const { if (usr.empty()) throw std::runtime_error("Empty USR"); for (const auto &c : classes) { if (c.usr == usr) return c.full_name(using_namespaces); } return usr; } }; } } }