Line data Source code
1 : #ifndef GENERIC_INSTANCE_TYPE_TYPE_H
2 : #define GENERIC_INSTANCE_TYPE_TYPE_H
3 :
4 : #include "userType.h"
5 : #include <expected>
6 : #include <memory>
7 : #include <sstream>
8 : #include <string>
9 : #include <variant>
10 : #include <vector>
11 :
12 : namespace nicole {
13 :
14 : class GenericInstanceType final : public UserType {
15 : private:
16 : mutable std::vector<std::shared_ptr<Type>> typeArgs_;
17 :
18 : public:
19 : GenericInstanceType(const std::shared_ptr<UserType> &genericType,
20 : const std::vector<std::shared_ptr<Type>> &args) noexcept
21 0 : : UserType(*genericType),
22 0 : typeArgs_{args} {}
23 :
24 : [[nodiscard]] const std::vector<std::shared_ptr<Type>> &
25 0 : typeArgs() const noexcept {
26 0 : return typeArgs_;
27 0 : }
28 :
29 : [[nodiscard]] std::expected<std::monostate, Error>
30 : setGenericReplacement(const std::size_t pos,
31 0 : const std::shared_ptr<Type> &type) const noexcept {
32 0 : if (pos >= typeArgs_.size()) {
33 0 : return createError(
34 0 : ERROR_TYPE::TYPE,
35 0 : "trying to access a invalid position in a replacement list");
36 0 : }
37 0 : typeArgs_[pos] = type;
38 0 : return {};
39 0 : }
40 :
41 0 : [[nodiscard]] std::string toString() const noexcept override {
42 0 : std::ostringstream oss;
43 0 : oss << UserType::toString() << "<";
44 0 : for (size_t i = 0; i < typeArgs_.size(); ++i) {
45 0 : oss << typeArgs_[i]->toString();
46 0 : if (i != typeArgs_.size() - 1)
47 0 : oss << ", ";
48 0 : }
49 0 : oss << ">";
50 0 : return oss.str();
51 0 : }
52 : };
53 :
54 : } // namespace nicole
55 :
56 : #endif
|