Line data Source code
1 : #include "../../../inc/tables/typeTable/types/userTypes/userType.h"
2 : #include <memory>
3 :
4 : namespace nicole {
5 :
6 0 : bool UserType::hasAttribute(const std::string &id) const noexcept {
7 0 : if (baseType_) {
8 0 : if (const auto userType = std::dynamic_pointer_cast<UserType>(baseType_)) {
9 0 : return userType->hasAttribute(id);
10 0 : }
11 0 : }
12 0 : if (attrTable_.has(id)) {
13 0 : return true;
14 0 : }
15 0 : return false;
16 0 : }
17 :
18 0 : bool UserType::hasMethod(const Method &id) const noexcept {
19 0 : if (baseType_) {
20 0 : if (const auto userType = std::dynamic_pointer_cast<UserType>(baseType_))
21 0 : return userType->hasMethod(id);
22 0 : }
23 0 : if (methodTable_.getMethods(id.id()).size()) {
24 0 : return true;
25 0 : }
26 0 : return false;
27 0 : }
28 :
29 : const std::expected<Attribute, Error>
30 0 : UserType::getAttribute(const std::string &id) const noexcept {
31 0 : if (attrTable_.has(id)) {
32 0 : return attrTable_.getAttribute(id);
33 0 : }
34 0 : if (baseType_) {
35 0 : if (const auto userType = std::dynamic_pointer_cast<UserType>(baseType_))
36 0 : return userType->getAttribute(id);
37 0 : }
38 0 : return createError(ERROR_TYPE::ATTR,
39 0 : "the attribute: " + id + " does not exist in " + name_);
40 0 : }
41 :
42 : const std::expected<std::vector<Method>, Error>
43 0 : UserType::getMethods(const std::string &id) const noexcept {
44 0 : std::vector<Method> combinedMethods;
45 0 : if (methodTable_.getMethods(id).size()) {
46 0 : auto childRes = methodTable_.getMethods(id);
47 0 : combinedMethods.insert(combinedMethods.end(), childRes.begin(),
48 0 : childRes.end());
49 0 : }
50 0 : if (baseType_) {
51 0 : if (const auto userType = std::dynamic_pointer_cast<UserType>(baseType_)) {
52 0 : auto parentRes = userType->getMethods(id);
53 0 : for (auto &method : parentRes.value()) {
54 0 : method.setInherit(true);
55 0 : }
56 0 : if (parentRes)
57 0 : combinedMethods.insert(combinedMethods.end(), parentRes.value().begin(),
58 0 : parentRes.value().end());
59 0 : }
60 0 : }
61 0 : if (!combinedMethods.empty())
62 0 : return combinedMethods;
63 :
64 0 : return createError(ERROR_TYPE::METHOD,
65 0 : "the method: " + id + " does not exist in " + name_);
66 0 : }
67 :
68 : std::expected<std::monostate, Error>
69 0 : UserType::insertAttr(const Attribute &attr) const noexcept {
70 0 : if (hasAttribute(attr.id())) {
71 0 : return createError(ERROR_TYPE::ATTR,
72 0 : "the attribute: " + attr.id() + " already exists");
73 0 : }
74 0 : return attrTable_.insert(attr);
75 0 : }
76 :
77 0 : void UserType::insertMethod(const Method &method) const noexcept {
78 0 : methodTable_.insert(method);
79 0 : }
80 :
81 : std::expected<std::monostate, Error>
82 0 : UserType::setAttribute(const Attribute &attr) const noexcept {
83 : // Comprobar que el atributo existe en el UserType o en sus bases.
84 0 : if (!attrTable_.has(attr.id())) {
85 0 : return createError(ERROR_TYPE::ATTR, "Attribute: " + attr.id() +
86 0 : " does not exist in " + name_);
87 0 : }
88 : // Delegar la actualización al AttrTable.
89 0 : return attrTable_.setAttribute(attr);
90 0 : }
91 :
92 : bool UserType::isAboveInHearchy(
93 0 : const std::shared_ptr<UserType> &type) const noexcept {
94 0 : auto aux{baseType_};
95 0 : while (aux) {
96 0 : if (aux->name_ == type->name_) {
97 0 : return true;
98 0 : }
99 0 : aux = aux->baseType_;
100 0 : }
101 0 : return false;
102 0 : }
103 :
104 0 : std::string UserType::toString() const noexcept {
105 0 : std::ostringstream oss;
106 0 : oss << name_;
107 0 : if (!genericParams_.empty()) {
108 0 : oss << "<";
109 0 : for (size_t i = 0; i < genericParams_.size(); ++i) {
110 0 : oss << genericParams_[i].name();
111 0 : if (i != genericParams_.size() - 1)
112 0 : oss << ", ";
113 0 : }
114 0 : oss << ">";
115 0 : }
116 0 : if (baseType_) {
117 0 : oss << " : " << baseType_->toString();
118 0 : }
119 0 : return oss.str();
120 0 : }
121 :
122 : } // namespace nicole
|