diff --git a/include/xrpl/ledger/helpers/WrappedSLEBase.h b/include/xrpl/ledger/helpers/WrappedSLEBase.h new file mode 100644 index 0000000000..12b63636d6 --- /dev/null +++ b/include/xrpl/ledger/helpers/WrappedSLEBase.h @@ -0,0 +1,109 @@ +#pragma once + +#include + +#include +#include + +namespace xrpl { + +class ReadView; +class ApplyView; + +/** + * Base class for all ledger entry view classes. + * + * Provides common functionality for existence checking and raw SLE access. + * Supports both read-only (ReadView) and read-write (ApplyView) contexts. + * + * Derived classes should provide domain-specific accessors that hide + * implementation details of the underlying ledger entry format. + */ +class WrappedSLEBase +{ +public: + virtual ~WrappedSLEBase() = default; + + /** Returns true if the ledger entry exists */ + bool + exists() const + { + return sle_ != nullptr; + } + + /** Explicit conversion to bool for convenient existence checking */ + explicit + operator bool() const + { + return exists(); + } + + /** Returns the underlying SLE for raw access when needed */ + std::shared_ptr const& + sle() const + { + return sle_; + } + + /** Returns the read view (always available) */ + ReadView const* + readView() const + { + return readView_; + } + + /** Returns true if this wrapper supports write operations */ + bool + canModify() const + { + return applyView_ != nullptr; + } + + /** Returns the apply view for write operations + * + * @throws std::logic_error if called in a read-only context + */ + ApplyView* + applyView() const + { + if (!applyView_) + { + throw std::logic_error("Cannot access ApplyView in read-only context"); + } + return applyView_; + } + + STLedgerEntry const* + operator->() const + { + return sle_.get(); + } + + STLedgerEntry const& + operator*() const + { + return *sle_; + } + +protected: + WrappedSLEBase() = default; + + /** Constructor for read-only context (ReadView) */ + explicit WrappedSLEBase(std::shared_ptr sle, ReadView const* view) + : sle_(std::move(sle)), readView_(view), applyView_(nullptr) + { + } + + /** Constructor for read-write context (ApplyView) */ + explicit WrappedSLEBase(std::shared_ptr sle, ApplyView* view) + : sle_(std::move(sle)), readView_(view), applyView_(view) + { + // ApplyView inherits from ReadView, so we can use it for both + } + + std::shared_ptr sle_; + ReadView const* readView_; + ApplyView* applyView_; // nullptr for read-only contexts +}; + +} // namespace xrpl