summaryrefslogtreecommitdiff
path: root/src/buildtool/build_engine/expression/configuration.hpp
blob: 5d0b9c2a83b2d6efe7c8730af5d883c363fcd0f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_EXPRESSION_CONFIGURATION_HPP
#define INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_EXPRESSION_CONFIGURATION_HPP

#include <algorithm>
#include <sstream>
#include <string>

#include "gsl-lite/gsl-lite.hpp"
#include "src/buildtool/build_engine/expression/expression.hpp"
#include "src/utils/cpp/concepts.hpp"

// Decorator for Expression containing a map. Adds Prune() and Update().
class Configuration {
  public:
    explicit Configuration(ExpressionPtr expr) noexcept
        : expr_{std::move(expr)} {
        gsl_ExpectsAudit(expr_->IsMap());
    }
    explicit Configuration(Expression::map_t const& map) noexcept
        : expr_{ExpressionPtr{map}} {}

    Configuration() noexcept = default;
    ~Configuration() noexcept = default;
    Configuration(Configuration const&) noexcept = default;
    Configuration(Configuration&&) noexcept = default;
    auto operator=(Configuration const&) noexcept -> Configuration& = default;
    auto operator=(Configuration&&) noexcept -> Configuration& = default;

    [[nodiscard]] auto operator[](std::string const& key) const
        -> ExpressionPtr {
        return expr_->Get(key, Expression::none_t{});
    }
    [[nodiscard]] auto operator[](ExpressionPtr const& key) const
        -> ExpressionPtr {
        return expr_->Get(key->String(), Expression::none_t{});
    }
    [[nodiscard]] auto ToString() const -> std::string {
        return expr_->ToString();
    }
    [[nodiscard]] auto ToJson() const -> nlohmann::json {
        return expr_->ToJson();
    }
    [[nodiscard]] auto Enumerate(const std::string& prefix, size_t width) const
        -> std::string {
        std::stringstream ss{};
        if (width > prefix.size()) {
            size_t actual_width = width - prefix.size();
            for (auto const& [key, value] : expr_->Map()) {
                std::string key_str = Expression{key}.ToString();
                std::string val_str = value->ToString();
                if (actual_width > key_str.size() + 3) {
                    ss << prefix << key_str << " : ";
                    size_t remain = actual_width - key_str.size() - 3;
                    if (val_str.size() >= remain) {
                        ss << val_str.substr(0, remain - 3) << "...";
                    }
                    else {
                        ss << val_str;
                    }
                }
                else {
                    ss << prefix << key_str.substr(0, actual_width);
                }
                ss << std::endl;
            }
        }
        return ss.str();
    }

    [[nodiscard]] auto operator==(const Configuration& other) const -> bool {
        return expr_ == other.expr_;
    }

    [[nodiscard]] auto hash() const noexcept -> std::size_t {
        return std::hash<ExpressionPtr>{}(expr_);
    }

    template <InputIterableStringContainer T>
    [[nodiscard]] auto Prune(T const& vars) const -> Configuration {
        auto subset = Expression::map_t::underlying_map_t{};
        std::for_each(vars.begin(), vars.end(), [&](auto const& k) {
            auto const& map = expr_->Map();
            auto v = map.Find(k);
            if (v) {
                subset.emplace(k, v->get());
            }
            else {
                subset.emplace(k, Expression::kNone);
            }
        });
        return Configuration{Expression::map_t{subset}};
    }

    [[nodiscard]] auto Prune(ExpressionPtr const& vars) const -> Configuration {
        auto subset = Expression::map_t::underlying_map_t{};
        auto const& list = vars->List();
        std::for_each(list.begin(), list.end(), [&](auto const& k) {
            auto const& map = expr_->Map();
            auto const key = k->String();
            auto v = map.Find(key);
            if (v) {
                subset.emplace(key, v->get());
            }
            else {
                subset.emplace(key, ExpressionPtr{Expression::none_t{}});
            }
        });
        return Configuration{Expression::map_t{subset}};
    }

    template <class T>
    requires(Expression::IsValidType<T>() or std::is_same_v<T, ExpressionPtr>)
        [[nodiscard]] auto Update(std::string const& name, T const& value) const
        -> Configuration {
        auto update = Expression::map_t::underlying_map_t{};
        update.emplace(name, value);
        return Configuration{Expression::map_t{expr_, update}};
    }

    [[nodiscard]] auto Update(
        Expression::map_t::underlying_map_t const& map) const -> Configuration {
        if (map.empty()) {
            return *this;
        }
        return Configuration{Expression::map_t{expr_, map}};
    }

    [[nodiscard]] auto Update(ExpressionPtr const& map) const -> Configuration {
        gsl_ExpectsAudit(map->IsMap());
        if (map->Map().empty()) {
            return *this;
        }
        return Configuration{Expression::map_t{expr_, map}};
    }

    [[nodiscard]] auto VariableFixed(std::string const& x) const -> bool {
        return expr_->Map().Find(x).has_value();
    }

  private:
    ExpressionPtr expr_{Expression::kEmptyMap};
};

namespace std {
template <>
struct hash<Configuration> {
    [[nodiscard]] auto operator()(Configuration const& p) const noexcept
        -> std::size_t {
        return p.hash();
    }
};
}  // namespace std

#endif  // INCLUDED_SRC_BUILDTOOL_BUILD_ENGINE_EXPRESSION_CONFIGURATION_HPP