1515#include < string>
1616
1717namespace pl ::ptrn {
18-
1918 using namespace ::std::literals::string_literals;
2019
2120 class IInlinable {
@@ -264,7 +263,7 @@ namespace pl::ptrn {
264263 }
265264 }
266265
267- [[nodiscard]] virtual std::string toString () const {
266+ [[nodiscard]] virtual std::string toString () {
268267 auto result = fmt::format (" {} {} @ 0x{:X}" , this ->getTypeName (), this ->getVariableName (), this ->getOffset ());
269268
270269 return this ->callUserFormatFunc (this ->getValue (), true ).value_or (result);
@@ -544,6 +543,8 @@ namespace pl::ptrn {
544543 m_arrayIndex = index;
545544 }
546545
546+ [[nodiscard]] virtual std::string formatDisplayValue () = 0;
547+
547548 protected:
548549 std::optional<std::endian> m_endian;
549550
@@ -561,8 +562,6 @@ namespace pl::ptrn {
561562 return value;
562563 }
563564
564- [[nodiscard]] virtual std::string formatDisplayValue () = 0;
565-
566565 /* *
567566 * @brief Calls an user-defined PL function to format the given literal (pattern), if existing. Else, returns defaultValue
568567 */
@@ -636,4 +635,101 @@ namespace pl::ptrn {
636635 bool m_manualColor = false ;
637636 };
638637
638+ struct PatternRef {
639+ class NoIIndexable {};
640+ class NoIInlinable {};
641+ class NoIIterable {
642+ public:
643+ NoIIterable (auto *){}
644+ };
645+
646+ template <typename T>
647+ class PatternRefIterable : public IIndexable {
648+ public:
649+ PatternRefIterable () = default ;
650+ PatternRefIterable (T *refPattern) : m_refPattern(refPattern) {}
651+ PatternRefIterable (const PatternRefIterable &other) {
652+ this ->m_refPattern = other.m_refPattern ;
653+ }
654+
655+ std::vector<std::shared_ptr<Pattern>> getEntries () override {
656+ return m_refPattern->getEntries ();
657+ }
658+
659+ void setEntries (const std::vector<std::shared_ptr<Pattern>> &entries) override {
660+ m_refPattern->setEntries (entries);
661+ }
662+
663+ [[nodiscard]] std::shared_ptr<Pattern> getEntry (size_t index) const override {
664+ return m_refPattern->getEntry (index);
665+ }
666+
667+ void forEachEntry (u64 start, u64 end, const std::function<void (u64 , Pattern*)> &callback) override {
668+ m_refPattern->forEachEntry (start, end, callback);
669+ }
670+
671+ [[nodiscard]] size_t getEntryCount () const override {
672+ return m_refPattern->getEntryCount ();
673+ }
674+
675+ void addEntry (const std::shared_ptr<Pattern> &entry) override {
676+ return m_refPattern->addEntry (entry);
677+ }
678+
679+ private:
680+ T *m_refPattern;
681+ };
682+
683+ template <typename T>
684+ class PatternRefImpl
685+ : public Pattern,
686+ public std::conditional_t <std::derived_from<T, IInlinable>, IInlinable, NoIInlinable>,
687+ public std::conditional_t <std::derived_from<T, IIterable> || std::derived_from<T, IIndexable>, PatternRefIterable<T>, NoIIterable> {
688+ public:
689+ PatternRefImpl () = default ;
690+ PatternRefImpl (T *refPattern)
691+ : Pattern(refPattern->getEvaluator (), refPattern->getOffset(), refPattern->getSize(), refPattern->getLine()),
692+ std::conditional_t<std::derived_from<T, IIterable> || std::derived_from<T, IIndexable>, PatternRefIterable<T>, NoIIterable>(refPattern) {
693+ this ->m_refPattern = refPattern;
694+ }
695+
696+ PatternRefImpl (const PatternRefImpl &other)
697+ : Pattern(other),
698+ std::conditional_t<std::derived_from<T, IIterable> || std::derived_from<T, IIndexable>, PatternRefIterable<T>, NoIIterable>(other),
699+ m_refPattern(other.m_refPattern) {}
700+
701+ std::unique_ptr<Pattern> clone () const override {
702+ return std::make_unique<PatternRefImpl>(*this );
703+ }
704+
705+ void accept (PatternVisitor &v) override {
706+ m_refPattern->accept (v);
707+ }
708+
709+ std::string formatDisplayValue () override {
710+ return m_refPattern->formatDisplayValue ();
711+ }
712+
713+ std::string getFormattedName () const override {
714+ return m_refPattern->getFormattedName ();
715+ }
716+
717+ std::vector<u8 > getRawBytes () override {
718+ return m_refPattern->getRawBytes ();
719+ }
720+
721+ bool operator ==(const Pattern &other) const override {
722+ return *this ->m_refPattern == other;
723+ }
724+
725+ private:
726+ T *m_refPattern;
727+ };
728+
729+ template <typename PatternType>
730+ static std::shared_ptr<Pattern> create (PatternType *pattern) {
731+ return std::shared_ptr<Pattern>(new PatternRefImpl<std::remove_cvref_t <PatternType>>(pattern));
732+ }
733+ };
734+
639735}
0 commit comments