-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathpattern_boolean.hpp
More file actions
68 lines (51 loc) · 2.23 KB
/
pattern_boolean.hpp
File metadata and controls
68 lines (51 loc) · 2.23 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
#pragma once
#include <pl/patterns/pattern.hpp>
namespace pl::ptrn {
class PatternBoolean : public Pattern {
public:
explicit PatternBoolean(core::Evaluator *evaluator, u64 offset, u32 line)
: Pattern(evaluator, offset, 1, line) { }
[[nodiscard]] std::shared_ptr<Pattern> clone() const override {
return std::shared_ptr<Pattern>(new PatternBoolean(*this));
}
[[nodiscard]] core::Token::Literal getValue() const override {
bool boolean = false;
this->getEvaluator()->readData(this->getOffset(), &boolean, 1, this->getSection());
return transformValue(boolean);
}
std::vector<u8> getBytesOf(const core::Token::Literal &value) const override {
if (auto boolValue = std::get_if<bool>(&value); boolValue != nullptr)
return wolv::util::toContainer<std::vector<u8>>(wolv::util::toBytes(*boolValue));
else
return { };
}
[[nodiscard]] std::string getFormattedName() const override {
return "bool";
}
std::string formatDisplayValue() override {
auto value = this->getValue().toUnsigned();
if (value == 0)
return "false";
if (value == 1)
return "true";
return "true*";
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return compareCommonProperties<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
[[nodiscard]] std::string toString() override {
auto value = this->getValue();
auto result = fmt::format("{}", value.toBoolean() ? "true" : "false");
return Pattern::callUserFormatFunc(value, true).value_or(result);
}
std::vector<u8> getRawBytes() override {
std::vector<u8> result;
result.resize(this->getSize());
this->getEvaluator()->readData(this->getOffset(), result.data(), result.size(), this->getSection());
if (this->getEndian() != std::endian::native)
std::reverse(result.begin(), result.end());
return result;
}
};
}