diff --git a/include/xrpl/protocol/TypedLedgerEntries.h b/include/xrpl/protocol/TypedLedgerEntries.h index 0b7ea19674..7d389acb28 100644 --- a/include/xrpl/protocol/TypedLedgerEntries.h +++ b/include/xrpl/protocol/TypedLedgerEntries.h @@ -30,8 +30,7 @@ namespace ripple { -enum class LedgerEntryConstness -{ +enum class LedgerEntryConstness { NonConst, Const, }; @@ -390,15 +389,11 @@ template using AddConstAwareness = std::conditional_t< Constness == LedgerEntryConstness::NonConst, T, - std::add_const_t ->; + std::add_const_t>; template -using MatchConstness = std::conditional_t< - std::is_const_v, - std::add_const_t, - Type ->; +using MatchConstness = std:: + conditional_t, std::add_const_t, Type>; template concept STObjectLike = std::same_as, STObject>; @@ -802,8 +797,7 @@ struct GetFieldValue< #pragma pop_macro("INNER_OBJECT_BEGIN") #pragma pop_macro("INNER_OBJECT_END") -template +template struct LedgerEntry { }; @@ -853,9 +847,9 @@ struct LedgerEntry soeStyle, \ detail::GetFieldType::Value, \ InnerObjectTypesArray>:: \ - get(std::declval&>(), field)); \ + get(std::declval< \ + AddConstAwareness&>(), \ + field)); \ Field_##field##Type f##field() \ { \ return GetFieldValue< \ @@ -875,130 +869,147 @@ struct LedgerEntry InnerObjectTypesArray>::get(*getObject(), field); \ } -#define LEDGER_ENTRY(tag, value, name, rpcName, fields) \ - template \ - struct LedgerEntry \ - { \ - private: \ - using CLS = LedgerEntry; \ - std::variant< \ - std::shared_ptr>, \ - AddConstAwareness*> object_; \ - LedgerEntry(AddConstAwareness& object) \ - : object_(&object) \ - { \ - } \ - LedgerEntry( \ - std::shared_ptr< \ - AddConstAwareness \ - > const& object) \ - : object_(object) \ - { \ - } \ - \ - public: \ - bool \ - isValid() const \ - { \ - return getObject() != nullptr; \ - } \ - static void \ - ensureType(STLedgerEntry const& object) \ - { \ - if (tag != object.getType()) \ - { \ - Throw("Object type mismatch!"); \ - } \ - } \ - AddConstAwareness* \ - getObject() \ - { \ - if (std::holds_alternative< \ - std::shared_ptr< \ - AddConstAwareness \ - >>( \ - object_)) \ - { \ - return std::get< \ - std::shared_ptr< \ - AddConstAwareness \ - >>(object_).get(); \ - } \ - else \ - { \ - return std::get< \ - AddConstAwareness* \ - >(object_); \ - } \ - } \ - STLedgerEntry const* \ - getObject() const \ - { \ - if (std::holds_alternative< \ - std::shared_ptr< \ - AddConstAwareness> \ - >(object_)) \ - { \ - return std::get< \ - std::shared_ptr< \ - AddConstAwareness> \ - >(object_).get(); \ - } \ - else \ - { \ - return std::get< \ - AddConstAwareness* \ - >(object_); \ - } \ - } \ - operator std::shared_ptr< \ - AddConstAwareness>&() \ - { \ - return std::get< \ - std::shared_ptr< \ - AddConstAwareness> \ - >(object_); \ - } \ - operator std::shared_ptr< \ - AddConstAwareness \ - > const&() const \ - { \ - return std::get< \ - std::shared_ptr< \ - AddConstAwareness> \ - >(object_); \ - } \ - operator bool() const { return isValid(); } \ - bool operator==(std::nullptr_t) const { return !isValid(); } \ - bool operator!=(std::nullptr_t) const { return isValid(); } \ - bool operator!() const { return !isValid(); } \ - auto operator->() const { return getObject(); } \ - auto operator->() { return getObject(); } \ - auto& operator*() { return *getObject(); } \ - auto const& operator*() const { return *getObject(); } \ - static LedgerEntry \ - fromObject(AddConstAwareness& object) \ - { \ - ensureType(object); \ - return LedgerEntry{object}; \ - } \ - static LedgerEntry \ - fromObject(std::shared_ptr< \ - AddConstAwareness \ - > const& object) \ - { \ - ensureType(*object); \ - return LedgerEntry{object}; \ - } \ - static LedgerEntry \ - create(uint256 const& key) \ - { \ - return LedgerEntry{ \ - std::make_shared< \ - AddConstAwareness \ - >(tag, key)}; \ - } \ - fields \ +#define LEDGER_ENTRY(tag, value, name, rpcName, fields) \ + template \ + struct LedgerEntry \ + { \ + private: \ + using CLS = LedgerEntry; \ + std::variant< \ + std::shared_ptr>, \ + AddConstAwareness*> \ + object_; \ + LedgerEntry(AddConstAwareness& object) \ + : object_(&object) \ + { \ + } \ + LedgerEntry( \ + std::shared_ptr< \ + AddConstAwareness> const& object) \ + : object_(object) \ + { \ + } \ + \ + public: \ + bool \ + isValid() const \ + { \ + return getObject() != nullptr; \ + } \ + static void \ + ensureType(STLedgerEntry const& object) \ + { \ + if (tag != object.getType()) \ + { \ + Throw("Object type mismatch!"); \ + } \ + } \ + AddConstAwareness* \ + getObject() \ + { \ + if (std::holds_alternative>>(object_)) \ + { \ + return std::get>>(object_) \ + .get(); \ + } \ + else \ + { \ + return std::get*>( \ + object_); \ + } \ + } \ + STLedgerEntry const* \ + getObject() const \ + { \ + if (std::holds_alternative>>(object_)) \ + { \ + return std::get>>(object_) \ + .get(); \ + } \ + else \ + { \ + return std::get*>( \ + object_); \ + } \ + } \ + operator std::shared_ptr< \ + AddConstAwareness>&() \ + { \ + return std::get< \ + std::shared_ptr>>( \ + object_); \ + } \ + operator std::shared_ptr< \ + AddConstAwareness> const&() const \ + { \ + return std::get< \ + std::shared_ptr>>( \ + object_); \ + } \ + operator bool() const \ + { \ + return isValid(); \ + } \ + bool \ + operator==(std::nullptr_t) const \ + { \ + return !isValid(); \ + } \ + bool \ + operator!=(std::nullptr_t) const \ + { \ + return isValid(); \ + } \ + bool \ + operator!() const \ + { \ + return !isValid(); \ + } \ + auto \ + operator->() const \ + { \ + return getObject(); \ + } \ + auto \ + operator->() \ + { \ + return getObject(); \ + } \ + auto& \ + operator*() \ + { \ + return *getObject(); \ + } \ + auto const& \ + operator*() const \ + { \ + return *getObject(); \ + } \ + static LedgerEntry \ + fromObject(AddConstAwareness& object) \ + { \ + ensureType(object); \ + return LedgerEntry{object}; \ + } \ + static LedgerEntry \ + fromObject(std::shared_ptr< \ + AddConstAwareness> const& object) \ + { \ + ensureType(*object); \ + return LedgerEntry{object}; \ + } \ + static LedgerEntry \ + create(uint256 const& key) \ + { \ + return LedgerEntry{ \ + std::make_shared>( \ + tag, key)}; \ + } \ + fields \ }; #define LEDGER_ENTRY_DUPLICATE(...) @@ -1027,9 +1038,11 @@ using InnerObjectType = typename detail:: // This gives a ledger entry type that the LedgerEntryType corresponds to. template -using LedgerObjectType = detail::LedgerEntry; +using LedgerObjectType = + detail::LedgerEntry; template -using ConstLedgerObjectType = detail::LedgerEntry; +using ConstLedgerObjectType = + detail::LedgerEntry; } // namespace ripple #endif // TYPED_LEDGER_ENTRIES_H