Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 5 additions & 100 deletions lib/include/pl/patterns/pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace pl::ptrn {
friend class core::Evaluator;
};

class Pattern {
class Pattern : public std::enable_shared_from_this<Pattern> {
public:
constexpr static u64 MainSectionId = 0x0000'0000'0000'0000;
constexpr static u64 HeapSectionId = 0xFFFF'FFFF'FFFF'FFFF;
Expand All @@ -82,7 +82,7 @@ namespace pl::ptrn {

}

Pattern(const Pattern &other) {
Pattern(const Pattern &other) : std::enable_shared_from_this<Pattern>(other) {
this->m_evaluator = other.m_evaluator;
this->m_offset = other.m_offset;
this->m_endian = other.m_endian;
Expand Down Expand Up @@ -115,6 +115,8 @@ namespace pl::ptrn {
}
}

std::shared_ptr<Pattern> get_shared() { return shared_from_this(); }

virtual std::unique_ptr<Pattern> clone() const = 0;

[[nodiscard]] u64 getOffset() const { return this->m_offset; }
Expand Down Expand Up @@ -635,101 +637,4 @@ namespace pl::ptrn {
bool m_manualColor = false;
};

struct PatternRef {
class NoIIndexable {};
class NoIInlinable {};
class NoIIterable {
public:
NoIIterable(auto*){}
};

template<typename T>
class PatternRefIterable : public IIndexable {
public:
PatternRefIterable() = default;
PatternRefIterable(T *refPattern) : m_refPattern(refPattern) {}
PatternRefIterable(const PatternRefIterable &other) {
this->m_refPattern = other.m_refPattern;
}

std::vector<std::shared_ptr<Pattern>> getEntries() override {
return m_refPattern->getEntries();
}

void setEntries(const std::vector<std::shared_ptr<Pattern>> &entries) override {
m_refPattern->setEntries(entries);
}

[[nodiscard]] std::shared_ptr<Pattern> getEntry(size_t index) const override {
return m_refPattern->getEntry(index);
}

void forEachEntry(u64 start, u64 end, const std::function<void(u64, Pattern*)> &callback) override {
m_refPattern->forEachEntry(start, end, callback);
}

[[nodiscard]] size_t getEntryCount() const override {
return m_refPattern->getEntryCount();
}

void addEntry(const std::shared_ptr<Pattern> &entry) override {
return m_refPattern->addEntry(entry);
}

private:
T *m_refPattern;
};

template<typename T>
class PatternRefImpl
: public Pattern,
public std::conditional_t<std::derived_from<T, IInlinable>, IInlinable, NoIInlinable>,
public std::conditional_t<std::derived_from<T, IIterable> || std::derived_from<T, IIndexable>, PatternRefIterable<T>, NoIIterable> {
public:
PatternRefImpl() = default;
PatternRefImpl(T *refPattern)
: Pattern(refPattern->getEvaluator(), refPattern->getOffset(), refPattern->getSize(), refPattern->getLine()),
std::conditional_t<std::derived_from<T, IIterable> || std::derived_from<T, IIndexable>, PatternRefIterable<T>, NoIIterable>(refPattern) {
this->m_refPattern = refPattern;
}

PatternRefImpl(const PatternRefImpl &other)
: Pattern(other),
std::conditional_t<std::derived_from<T, IIterable> || std::derived_from<T, IIndexable>, PatternRefIterable<T>, NoIIterable>(other),
m_refPattern(other.m_refPattern) {}

std::unique_ptr<Pattern> clone() const override {
return std::make_unique<PatternRefImpl>(*this);
}

void accept(PatternVisitor &v) override {
m_refPattern->accept(v);
}

std::string formatDisplayValue() override {
return m_refPattern->formatDisplayValue();
}

std::string getFormattedName() const override {
return m_refPattern->getFormattedName();
}

std::vector<u8> getRawBytes() override {
return m_refPattern->getRawBytes();
}

bool operator==(const Pattern &other) const override {
return *this->m_refPattern == other;
}

private:
T *m_refPattern;
};

template<typename PatternType>
static std::shared_ptr<Pattern> create(PatternType *pattern) {
return std::shared_ptr<Pattern>(new PatternRefImpl<std::remove_cvref_t<PatternType>>(pattern));
}
};

}
} // namespace pl::ptrn
4 changes: 2 additions & 2 deletions lib/include/pl/patterns/pattern_array_dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ namespace pl::ptrn {

result += " ]";

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

[[nodiscard]] bool operator==(const Pattern &other) const override {
Expand Down Expand Up @@ -208,7 +208,7 @@ namespace pl::ptrn {
}

std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or("[ ... ]");
return Pattern::callUserFormatFunc(get_shared()).value_or("[ ... ]");
}

std::vector<u8> getRawBytes() override {
Expand Down
4 changes: 2 additions & 2 deletions lib/include/pl/patterns/pattern_array_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace pl::ptrn {
}

std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or("[ ... ]");
return Pattern::callUserFormatFunc(get_shared()).value_or("[ ... ]");
}

[[nodiscard]] std::string toString() override {
Expand Down Expand Up @@ -220,7 +220,7 @@ namespace pl::ptrn {

result += " ]";

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

std::vector<u8> getRawBytes() override {
Expand Down
10 changes: 5 additions & 5 deletions lib/include/pl/patterns/pattern_bitfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ namespace pl::ptrn {

result += " ]";

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

[[nodiscard]] bool operator==(const Pattern &other) const override {
Expand Down Expand Up @@ -495,7 +495,7 @@ namespace pl::ptrn {
}

std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or("[ ... ]");
return Pattern::callUserFormatFunc(get_shared()).value_or("[ ... ]");
}

void sort(const std::function<bool (const Pattern *, const Pattern *)> &comparator) override {
Expand Down Expand Up @@ -691,7 +691,7 @@ namespace pl::ptrn {

result += " }";

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

std::string formatDisplayValue() override {
Expand Down Expand Up @@ -725,9 +725,9 @@ namespace pl::ptrn {
}

if (valueString.size() > 64)
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or(fmt::format("{{ ... }}", valueString));
return Pattern::callUserFormatFunc(get_shared()).value_or(fmt::format("{{ ... }}", valueString));
else
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or(fmt::format("{{ {} }}", valueString));
return Pattern::callUserFormatFunc(get_shared()).value_or(fmt::format("{{ {} }}", valueString));
}

void setEndian(std::endian endian) override {
Expand Down
2 changes: 1 addition & 1 deletion lib/include/pl/patterns/pattern_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace pl::ptrn {

[[nodiscard]] std::string toString() override {
u128 value = this->getValue().toUnsigned();
return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(getEnumName(this->getTypeName(), value, m_enumValues));
return Pattern::callUserFormatFunc(get_shared(), true).value_or(getEnumName(this->getTypeName(), value, m_enumValues));
}

std::vector<u8> getRawBytes() override {
Expand Down
2 changes: 1 addition & 1 deletion lib/include/pl/patterns/pattern_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace pl::ptrn {
[[nodiscard]] std::string toString() override {
auto result = this->m_pointedAt->toString();

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

std::vector<u8> getRawBytes() override {
Expand Down
4 changes: 2 additions & 2 deletions lib/include/pl/patterns/pattern_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ namespace pl::ptrn {

result += " }";

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

void sort(const std::function<bool (const Pattern *, const Pattern *)> &comparator) override {
Expand Down Expand Up @@ -199,7 +199,7 @@ namespace pl::ptrn {
}

std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or("{ ... }");
return Pattern::callUserFormatFunc(get_shared()).value_or("{ ... }");
}

std::vector<u8> getRawBytes() override {
Expand Down
4 changes: 2 additions & 2 deletions lib/include/pl/patterns/pattern_union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ namespace pl::ptrn {

result += " }";

return Pattern::callUserFormatFunc(PatternRef::create(this), true).value_or(result);
return Pattern::callUserFormatFunc(get_shared(), true).value_or(result);
}

void sort(const std::function<bool (const Pattern *, const Pattern *)> &comparator) override {
Expand Down Expand Up @@ -198,7 +198,7 @@ namespace pl::ptrn {
}

std::string formatDisplayValue() override {
return Pattern::callUserFormatFunc(PatternRef::create(this)).value_or("{ ... }");
return Pattern::callUserFormatFunc(get_shared()).value_or("{ ... }");
}

std::vector<u8> getRawBytes() override {
Expand Down
Loading