-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathpattern_array_dynamic.hpp
More file actions
247 lines (184 loc) · 7.77 KB
/
pattern_array_dynamic.hpp
File metadata and controls
247 lines (184 loc) · 7.77 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#pragma once
#include <pl/patterns/pattern.hpp>
namespace pl::ptrn {
class PatternArrayDynamic : public Pattern,
public IInlinable,
public IIndexable {
protected:
PatternArrayDynamic(core::Evaluator *evaluator, u64 offset, size_t size, u32 line)
: Pattern(evaluator, offset, size, line) { }
PatternArrayDynamic(const PatternArrayDynamic &other) : Pattern(other) {}
public:
void post_construct(const PatternArrayDynamic &other) {
std::vector<std::shared_ptr<Pattern>> entries;
for (const auto &entry : other.m_entries)
entries.push_back(entry->clone());
this->setEntries(entries);
}
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return create_shared_object<PatternArrayDynamic>(*this);
}
void setColor(u32 color) override {
Pattern::setColor(color);
for (auto &entry : this->m_entries) {
if (!entry->hasOverriddenColor())
entry->setColor(color);
}
}
[[nodiscard]] std::string getFormattedName() const override {
if (this->m_entries.empty())
return "???";
return this->m_entries.front()->getTypeName() + "[" + std::to_string(this->m_entries.size()) + "]";
}
[[nodiscard]] std::string getTypeName() const override {
if (this->m_entries.empty())
return "???";
return this->m_entries.front()->getTypeName();
}
void setOffset(u64 offset) override {
for (auto &entry : this->m_entries) {
if (entry->getSection() == this->getSection()) {
if (entry->getSection() != ptrn::Pattern::PatternLocalSectionId)
entry->setOffset(entry->getOffset() - this->getOffset() + offset);
else
entry->setOffset(offset);
}
}
Pattern::setOffset(offset);
}
void setSection(u64 id) override {
if (this->getSection() == id)
return;
for (auto &entry : this->m_entries)
entry->setSection(id);
Pattern::setSection(id);
}
[[nodiscard]] std::vector<std::pair<u64, Pattern*>> getChildren() override {
if (this->getVisibility() == Visibility::HighlightHidden)
return { };
std::vector<std::pair<u64, Pattern*>> result;
for (const auto &entry : this->m_entries) {
auto children = entry->getChildren();
std::move(children.begin(), children.end(), std::back_inserter(result));
}
return result;
}
void setLocal(bool local) override {
for (auto &pattern : this->m_entries)
pattern->setLocal(local);
Pattern::setLocal(local);
}
void setReference(bool reference) override {
for (auto &pattern : this->m_entries)
pattern->setReference(reference);
Pattern::setReference(reference);
}
[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
return this->m_entries[index];
}
[[nodiscard]] size_t getEntryCount() const override {
return this->m_entries.size();
}
[[nodiscard]] std::vector<std::shared_ptr<Pattern>> getEntries() override {
return this->m_entries;
}
void forEachEntry(u64 start, u64 end, const std::function<void(u64, Pattern*)>& fn) override {
auto evaluator = this->getEvaluator();
auto startArrayIndex = evaluator->getCurrentArrayIndex();
ON_SCOPE_EXIT {
if (startArrayIndex.has_value())
evaluator->setCurrentArrayIndex(*startArrayIndex);
else
evaluator->clearCurrentArrayIndex();
};
for (u64 i = start; i < std::min<u64>(end, this->m_entries.size()); i++) {
evaluator->setCurrentArrayIndex(i);
auto &entry = this->m_entries[i];
if (!entry->isPatternLocal() || entry->hasAttribute("export"))
fn(i, entry.get());
}
}
void addEntry(const std::shared_ptr<Pattern> &entry) override {
if (entry == nullptr) return;
if (!entry->hasOverriddenColor())
entry->setBaseColor(this->getColor());
entry->setParent(this->reference());
this->m_entries.emplace_back(entry);
}
void setEntries(const std::vector<std::shared_ptr<Pattern>> &entries) override {
this->m_entries.clear();
for (const auto &entry : entries) {
addEntry(entry);
}
if (!this->m_entries.empty())
this->setBaseColor(this->m_entries.front()->getColor());
}
[[nodiscard]] std::string toString() override {
std::string result;
result += "[ ";
size_t entryCount = 0;
for (const auto &entry : this->m_entries) {
if (entryCount > 50) {
result += fmt::format("..., ");
break;
}
result += fmt::format("{}, ", entry->toString());
entryCount++;
}
if (entryCount > 0) {
// Remove trailing ", "
result.pop_back();
result.pop_back();
}
result += " ]";
return Pattern::callUserFormatFunc(this->reference(), true).value_or(result);
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherArray = *static_cast<const PatternArrayDynamic *>(&other);
if (this->m_entries.size() != otherArray.m_entries.size())
return false;
for (u64 i = 0; i < this->m_entries.size(); i++) {
if (*this->m_entries[i] != *otherArray.m_entries[i])
return false;
}
return true;
}
void setEndian(std::endian endian) override {
if (this->isLocal()) return;
Pattern::setEndian(endian);
for (auto &entry : this->m_entries) {
entry->setEndian(endian);
}
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(this->reference()).value_or("[ ... ]");
}
std::vector<u8> getRawBytes() override {
std::vector<u8> result;
if (this->isSealed()) {
result.resize(this->getSize());
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
} else {
this->forEachEntry(0, this->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) {
auto bytes = entry->getBytes();
std::copy(bytes.begin(), bytes.end(), std::back_inserter(result));
});
}
return result;
}
void clearFormatCache() override {
this->forEachEntry(0, this->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) {
entry->clearFormatCache();
});
Pattern::clearFormatCache();
}
private:
std::vector<std::shared_ptr<Pattern>> m_entries;
BEFRIEND_CREATE_SHARED_OBJECT(PatternArrayDynamic)
};
}