1#ifndef XRPL_PROTOCOL_STOBJECT_H_INCLUDED
2#define XRPL_PROTOCOL_STOBJECT_H_INCLUDED
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/basics/chrono.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/beast/utility/instrumentation.h>
9#include <xrpl/protocol/HashPrefix.h>
10#include <xrpl/protocol/SOTemplate.h>
11#include <xrpl/protocol/STAmount.h>
12#include <xrpl/protocol/STBase.h>
13#include <xrpl/protocol/STCurrency.h>
14#include <xrpl/protocol/STIssue.h>
15#include <xrpl/protocol/STPathSet.h>
16#include <xrpl/protocol/STVector256.h>
17#include <xrpl/protocol/Units.h>
18#include <xrpl/protocol/detail/STVar.h>
20#include <boost/iterator/transform_iterator.hpp>
34 Throw<std::runtime_error>(
"Field not found: " + field.getName());
65 transform_iterator<Transform, STObject::list_type::const_iterator>;
146 template <
class... Args>
246 typename T::value_type
293 typename T::value_type
444 template <
typename T,
typename V>
449 template <
typename T,
typename V>
454 template <
typename T>
459 template <
typename T>
514 typename Value =
typename U::value_type,
515 typename Unit =
typename U::unit_type>
519template <
typename U,
typename Value =
typename U::value_type>
525template <
class T,
class U>
526concept Addable =
requires(T t, U u) { t = t + u; };
527template <
typename T,
typename U>
548 template <IsArithmetic U>
553 template <IsArithmetic U>
560 template <
typename U>
564 return rhs.
value() == lhs;
592 operator bool() const noexcept;
603 return !lhs.engaged();
631 if (lhs.engaged() != rhs.engaged())
633 return !lhs.engaged() || *lhs == *rhs;
651 return !(lhs == rhs);
657 return !(lhs == rhs);
663 return !(lhs == rhs);
708 Throw<STObject::FieldErr>(
709 "Template field error '" + this->
f_->getName() +
"'");
722 auto const t = find();
727 Throw<STObject::FieldErr>(
"Value requested from invalid STObject.");
731 Throw<STObject::FieldErr>(
732 "Missing field '" + this->f_->getName() +
"'");
741 return this->value();
757 return dynamic_cast<T const*
>(st_->peekAtPField(*f_));
767 st_->makeFieldAbsent(*f_);
772 t =
dynamic_cast<T*
>(st_->getPField(*f_,
true));
774 t =
dynamic_cast<T*
>(st_->makeFieldPresent(*f_));
775 XRPL_ASSERT(t,
"xrpl::STObject::Proxy::assign : type cast succeeded");
791template <IsArithmetic U>
796 this->assign(this->value() + u);
801template <IsArithmetic U>
806 this->assign(this->value() - u);
813 return this->value();
832 T>::optional_type()
const
834 return optional_value();
841 return optional_value();
857 this->assign(std::move(*v));
893 return this->style_ ==
soeDEFAULT || this->find() !=
nullptr;
901 Throw<STObject::FieldErr>(
902 "Template field error '" + this->f_->getName() +
"'");
904 this->st_->delField(*this->f_);
906 this->st_->makeFieldAbsent(*this->f_);
915 return this->value();
922 return engaged() ? this->value() : val;
967 return mType ==
nullptr;
987template <
class... Args>
1004 return v_[offset].get();
1010 return v_[offset].get();
1016 return &
v_[offset].get();
1022 return &
v_[offset].get();
1026typename T::value_type
1054typename T::value_type
1061 Throw<STObject::FieldErr>(
"Missing field: " + f.
getName());
1063 if (
auto const u =
dynamic_cast<T const*
>(b))
1067 mType,
"xrpl::STObject::at(TypedField auto) : field template non-null");
1069 b->getSType() == STI_NOTPRESENT,
1070 "xrpl::STObject::at(TypedField auto) : type not present");
1073 Throw<STObject::FieldErr>(
"Missing optional field: " + f.
getName());
1077 "xrpl::STObject::at(TypedField auto) : template style is default");
1092 auto const u =
dynamic_cast<T const*
>(b);
1097 "xrpl::STObject::at(OptionaledField auto) : field template "
1100 b->getSType() == STI_NOTPRESENT,
1101 "xrpl::STObject::at(OptionaledField auto) : type not present");
1106 "xrpl::STObject::at(OptionaledField auto) : template style is "
1108 return typename T::value_type{};
1136 if (rf->
getSType() == STI_NOTPRESENT)
1140 if (
auto cf =
dynamic_cast<Bits*
>(rf))
1143 Throw<std::runtime_error>(
"Wrong field type");
1149 return !(*
this == o);
1152template <
typename T,
typename V>
1163 if (
id == STI_NOTPRESENT)
1166 T
const* cf =
dynamic_cast<T const*
>(rf);
1169 Throw<std::runtime_error>(
"Wrong field type");
1179template <
typename T,
typename V>
1190 if (
id == STI_NOTPRESENT)
1193 T
const* cf =
dynamic_cast<T const*
>(rf);
1196 Throw<std::runtime_error>(
"Wrong field type");
1202template <
typename T,
typename V>
1213 if (rf->
getSType() == STI_NOTPRESENT)
1216 T* cf =
dynamic_cast<T*
>(rf);
1219 Throw<std::runtime_error>(
"Wrong field type");
1221 cf->setValue(std::move(value));
1225template <
typename T>
1234 if (rf->
getSType() == STI_NOTPRESENT)
1237 T* cf =
dynamic_cast<T*
>(rf);
1240 Throw<std::runtime_error>(
"Wrong field type");
1246template <
typename T>
1255 if (rf->
getSType() == STI_NOTPRESENT)
1258 T* cf =
dynamic_cast<T*
>(rf);
1261 Throw<std::runtime_error>(
"Wrong field type");
Tracks the number of instances of an object.
std::string const & getName() const
Defines the fields and their attributes within a STObject.
SOEStyle style(SField const &sf) const
A type which can be exported to a well known binary format.
virtual SerializedTypeID getSType() const
void setValue(base_uint< Bits, Tag > const &v)
friend bool operator!=(OptionalProxy const &lhs, optional_type const &rhs) noexcept
friend bool operator==(OptionalProxy const &lhs, optional_type const &rhs) noexcept
friend bool operator==(optional_type const &lhs, OptionalProxy const &rhs) noexcept
friend bool operator==(OptionalProxy const &lhs, OptionalProxy const &rhs) noexcept
std::enable_if_t< std::is_assignable_v< T, U >, OptionalProxy & > operator=(U &&u)
OptionalProxy(OptionalProxy const &)=default
optional_type operator~() const
Explicit conversion to std::optional.
std::optional< typename std::decay< value_type >::type > optional_type
typename T::value_type value_type
friend bool operator!=(OptionalProxy const &lhs, std::nullopt_t) noexcept
friend bool operator!=(std::nullopt_t, OptionalProxy const &rhs) noexcept
OptionalProxy & operator=(OptionalProxy const &)=delete
bool engaged() const noexcept
friend bool operator!=(optional_type const &lhs, OptionalProxy const &rhs) noexcept
friend bool operator==(std::nullopt_t, OptionalProxy const &rhs) noexcept
value_type value_or(value_type val) const
optional_type optional_value() const
friend bool operator!=(OptionalProxy const &lhs, OptionalProxy const &rhs) noexcept
Proxy(Proxy const &)=default
value_type operator*() const
T const * operator->() const
Do not use operator->() unless the field is required, or you've checked that it's set.
typename T::value_type value_type
TypedField< T > const * f_
ValueProxy(ValueProxy const &)=default
ValueProxy & operator=(ValueProxy const &)=delete
typename T::value_type value_type
ValueProxy & operator+=(U const &u)
ValueProxy & operator-=(U const &u)
friend bool operator==(U const &lhs, STObject::ValueProxy< T > const &rhs)
std::enable_if_t< std::is_assignable_v< T, U >, ValueProxy & > operator=(U &&u)
void setFieldU8(SField const &field, unsigned char)
SField const & getFieldSType(int index) const
uint192 getFieldH192(SField const &field) const
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
STBase const * peekAtPIndex(int offset) const
Json::Value getJson(JsonOptions=JsonOptions::none) const override
STCurrency const & getFieldCurrency(SField const &field) const
Blob getFieldVL(SField const &field) const
void addWithoutSigningFields(Serializer &s) const
void setFieldIssue(SField const &field, STIssue const &)
uint128 getFieldH128(SField const &field) const
bool operator==(STObject const &o) const
bool isEquivalent(STBase const &t) const override
void setFieldNumber(SField const &field, STNumber const &)
void setFieldV256(SField const &field, STVector256 const &v)
void setFieldU64(SField const &field, std::uint64_t)
unsigned char getFieldU8(SField const &field) const
std::uint32_t getFieldU32(SField const &field) const
STBase const & peekAtIndex(int offset) const
bool hasMatchingEntry(STBase const &)
V const & getFieldByConstRef(SField const &field, V const &empty) const
STNumber const & getFieldNumber(SField const &field) const
OptionalProxy< T > at(OptionaledField< T > const &of)
Return a modifiable field value as std::optional.
void setFieldVL(SField const &field, Blob const &)
void reserve(std::size_t n)
T::value_type operator[](TypedField< T > const &f) const
Get the value of a field.
std::size_t emplace_back(Args &&... args)
boost::transform_iterator< Transform, STObject::list_type::const_iterator > iterator
void applyTemplate(SOTemplate const &type)
int getFieldIndex(SField const &field) const
uint256 getHash(HashPrefix prefix) const
void setFieldI32(SField const &field, std::int32_t)
virtual ~STObject()=default
std::string getFullText() const override
void setFieldU32(SField const &field, std::uint32_t)
STArray & peekFieldArray(SField const &field)
std::string getText() const override
STArray const & getFieldArray(SField const &field) const
void setFieldArray(SField const &field, STArray const &v)
STObject & peekFieldObject(SField const &field)
STBase & getField(SField const &field)
void setFieldAmount(SField const &field, STAmount const &)
void add(Serializer &s) const override
Serializer getSerializer() const
bool isFlag(std::uint32_t) const
bool isFieldPresent(SField const &field) const
STObject & operator=(STObject const &)=default
STObject(STObject const &)=default
ValueProxy< T > operator[](TypedField< T > const &f)
Get a modifiable field value.
SerializedTypeID getSType() const override
void setFieldU16(SField const &field, std::uint16_t)
uint256 getFieldH256(SField const &field) const
static STObject makeInnerObject(SField const &name)
std::int32_t getFieldI32(SField const &field) const
STBase const & peekAtField(SField const &field) const
void set(SOTemplate const &)
STObject(SOTemplate const &type, SField const &name, F &&f)
uint256 getSigningHash(HashPrefix prefix) const
bool clearFlag(std::uint32_t)
void setFieldCurrency(SField const &field, STCurrency const &)
std::uint64_t getFieldU64(SField const &field) const
void setFieldPathSet(SField const &field, STPathSet const &)
STBase * move(std::size_t n, void *buf) override
static std::vector< STBase const * > getSortedFields(STObject const &objToSort, WhichFields whichFields)
STBase * getPField(SField const &field, bool createOkay=false)
STBase * makeFieldPresent(SField const &field)
STBase const * peekAtPField(SField const &field) const
void applyTemplateFromSField(SField const &)
bool setFlag(std::uint32_t)
void setFieldObject(SField const &field, STObject const &v)
bool operator!=(STObject const &o) const
void setFieldH128(SField const &field, uint128 const &)
STObject getFieldObject(SField const &field) const
T & peekField(SField const &field)
void setFieldH160(SField const &field, base_uint< 160, Tag > const &v)
void setFieldUsingSetValue(SField const &field, V value)
void setAccountID(SField const &field, AccountID const &)
void setFieldUsingAssignment(SField const &field, T const &value)
bool delField(SField const &field)
STBase & getIndex(int offset)
uint160 getFieldH160(SField const &field) const
AccountID getAccountID(SField const &field) const
STBase * getPIndex(int offset)
STVector256 const & getFieldV256(SField const &field) const
STPathSet const & getFieldPathSet(SField const &field) const
bool isDefault() const override
V getFieldByValue(SField const &field) const
void makeFieldAbsent(SField const &field)
ValueProxy< T > at(TypedField< T > const &f)
Get a modifiable field value.
void setFieldH256(SField const &field, uint256 const &)
OptionalProxy< T > operator[](OptionaledField< T > const &of)
Return a modifiable field value as std::optional.
std::uint16_t getFieldU16(SField const &field) const
STAmount const & getFieldAmount(SField const &field) const
std::uint32_t getFlags() const
STBase * copy(std::size_t n, void *buf) const override
An immutable linear range of bytes.
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void throwFieldNotFound(SField const &field)
SOEStyle
Kind of element in each entry of an SOTemplate.
Number operator*(Number const &x, Number const &y)
HashPrefix
Prefix for hashing functions.
T runtime_error(T... args)
Note, should be treated as flags that can be | and &.
Indicate std::optional field semantics.
TypedField< T > const * f
A field with a type known at compile time.