-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathpattern_array_static.hpp
More file actions
262 lines (194 loc) · 9.04 KB
/
pattern_array_static.hpp
File metadata and controls
262 lines (194 loc) · 9.04 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#pragma once
#include <pl/patterns/pattern.hpp>
namespace pl::ptrn {
class PatternArrayStatic : public Pattern,
public IInlinable,
public IIndexable {
protected:
PatternArrayStatic(core::Evaluator *evaluator, u64 offset, size_t size, u32 line)
: Pattern(evaluator, offset, size, line) { }
PatternArrayStatic(const PatternArrayStatic &other) : Pattern(other) {}
public:
void post_construct(const PatternArrayStatic &other) {
this->setEntries(other.getTemplate()->clone(), other.getEntryCount());
}
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return create_shared_object<PatternArrayStatic>(*this);
}
[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
std::shared_ptr<Pattern> highlightTemplate = this->m_template->clone();
highlightTemplate->setOffset(this->getOffset() + index * highlightTemplate->getSize());
return highlightTemplate;
}
[[nodiscard]] std::vector<std::shared_ptr<Pattern>> getEntries() override {
return { this->getTemplate()->clone() };
}
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();
};
auto &entry = this->m_template;
const auto count = std::min<u64>(end, this->m_entryCount);
for (u64 index = start; index < count; index += 1) {
entry->clearFormatCache();
entry->clearByteCache();
entry->setArrayIndex(index);
entry->setOffset(this->getOffset() + index * entry->getSize());
evaluator->setCurrentArrayIndex(index);
fn(index, entry.get());
}
}
void setOffset(u64 offset) override {
this->m_template->setOffset(this->m_template->getOffset() - this->getOffset() + offset);
Pattern::setOffset(offset);
}
void setSection(u64 id) override {
if (this->getSection() == id)
return;
this->m_template->setSection(id);
for (auto &highlightTemplate : this->m_highlightTemplates)
highlightTemplate->setSection(id);
Pattern::setSection(id);
}
[[nodiscard]] std::vector<std::pair<u64, Pattern*>> getChildren() override {
if (this->getVisibility() == Visibility::HighlightHidden)
return { };
if (this->isSealed())
return { { this->getOffset(), this } };
else {
std::vector<std::pair<u64, Pattern*>> result;
std::shared_ptr<Pattern> highlightTemplate = this->m_template->clone();
highlightTemplate->setVariableName(this->getVariableName());
highlightTemplate->setOffset(this->getOffset());
const auto &children = this->m_highlightTemplates.emplace_back(std::move(highlightTemplate))->getChildren();
result.reserve(this->getEntryCount() * children.size());
auto templateSize = this->m_template->getSize();
for (size_t i = 0; i < this->getEntryCount(); i++) {
for (const auto &[offset, child] : children) {
result.emplace_back(offset + i * templateSize, child);
}
}
return result;
}
}
void setLocal(bool local) override {
if (this->m_template != nullptr)
this->m_template->setLocal(local);
for (auto &highlightTemplate : this->m_highlightTemplates)
highlightTemplate->setLocal(local);
Pattern::setLocal(local);
}
void setReference(bool reference) override {
if (this->m_template != nullptr)
this->m_template->setReference(reference);
for (auto &highlightTemplate : this->m_highlightTemplates)
highlightTemplate->setReference(reference);
Pattern::setReference(reference);
}
void setColor(u32 color) override {
Pattern::setColor(color);
this->m_template->setColor(color);
for (auto &highlightTemplate : this->m_highlightTemplates)
highlightTemplate->setColor(color);
}
[[nodiscard]] std::string getFormattedName() const override {
return this->m_template->getTypeName() + "[" + std::to_string(this->m_entryCount) + "]";
}
[[nodiscard]] std::string getTypeName() const override {
if (this->m_template == nullptr)
return Pattern::getTypeName();
else
return this->m_template->getTypeName();
}
[[nodiscard]] const std::shared_ptr<Pattern> &getTemplate() const {
return this->m_template;
}
[[nodiscard]] size_t getEntryCount() const override {
return this->m_entryCount;
}
void setEntryCount(size_t count) {
this->m_entryCount = count;
}
void setEntries(std::shared_ptr<Pattern> &&templatePattern, size_t count) {
this->m_template = std::move(templatePattern);
this->m_template->setParent(this->reference());
this->m_highlightTemplates.push_back(this->m_template->clone());
this->m_entryCount = count;
this->m_template->setSection(this->getSection());
this->m_template->setBaseColor(this->getColor());
for (auto &highlightTemplate : this->m_highlightTemplates)
highlightTemplate->setBaseColor(this->getColor());
}
void setEntries(const std::vector<std::shared_ptr<Pattern>> &entries) override {
if (!entries.empty())
this->setEntries(entries[0]->clone(), entries.size());
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!compareCommonProperties<decltype(*this)>(other))
return false;
auto &otherArray = *static_cast<const PatternArrayStatic *>(&other);
return *this->m_template == *otherArray.m_template && this->m_entryCount == otherArray.m_entryCount;
}
void setEndian(std::endian endian) override {
if (this->isLocal()) return;
Pattern::setEndian(endian);
this->m_template->setEndian(endian);
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(this->reference()).value_or("[ ... ]");
}
[[nodiscard]] std::string toString() override {
std::string result;
result += "[ ";
auto entry = this->m_template->clone();
for (size_t index = 0; index < this->m_entryCount; index++) {
if (index > 50) {
result += fmt::format("..., ");
break;
}
entry->setOffset(this->getOffset() + index * this->m_template->getSize());
entry->clearFormatCache();
result += fmt::format("{}, ", entry->toString());
}
if (this->m_entryCount > 0) {
// Remove trailing ", "
result.pop_back();
result.pop_back();
}
result += " ]";
return Pattern::callUserFormatFunc(this->reference(), true).value_or(result);
}
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->m_template->clearFormatCache();
for (auto &highlightTemplate : this->m_highlightTemplates)
highlightTemplate->clearFormatCache();
Pattern::clearFormatCache();
}
private:
std::shared_ptr<Pattern> m_template = nullptr;
mutable std::vector<std::shared_ptr<Pattern>> m_highlightTemplates;
size_t m_entryCount = 0;
BEFRIEND_CREATE_SHARED_OBJECT(PatternArrayStatic)
};
}