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
447 template <
typename T,
typename V>
452 template <
typename T,
typename V>
457 template <
typename T>
462 template <
typename T>
517 typename Value =
typename U::value_type,
518 typename Unit =
typename U::unit_type>
522template <
typename U,
typename Value =
typename U::value_type>
528template <
class T,
class U>
529concept Addable =
requires(T t, U u) { t = t + u; };
530template <
typename T,
typename U>
551 template <IsArithmetic U>
556 template <IsArithmetic U>
563 template <
typename U>
567 return rhs.
value() == lhs;
595 operator bool() const noexcept;
606 return !lhs.engaged();
634 if (lhs.engaged() != rhs.engaged())
636 return !lhs.engaged() || *lhs == *rhs;
654 return !(lhs == rhs);
660 return !(lhs == rhs);
666 return !(lhs == rhs);
711 Throw<STObject::FieldErr>(
712 "Template field error '" + this->
f_->getName() +
"'");
725 auto const t = find();
730 Throw<STObject::FieldErr>(
"Value requested from invalid STObject.");
734 Throw<STObject::FieldErr>(
735 "Missing field '" + this->f_->getName() +
"'");
744 return this->value();
760 return dynamic_cast<T const*
>(st_->peekAtPField(*f_));
770 st_->makeFieldAbsent(*f_);
775 t =
dynamic_cast<T*
>(st_->getPField(*f_,
true));
777 t =
dynamic_cast<T*
>(st_->makeFieldPresent(*f_));
778 XRPL_ASSERT(t,
"xrpl::STObject::Proxy::assign : type cast succeeded");
794template <IsArithmetic U>
799 this->assign(this->value() + u);
804template <IsArithmetic U>
809 this->assign(this->value() - u);
816 return this->value();
835 T>::optional_type()
const
837 return optional_value();
844 return optional_value();
860 this->assign(std::move(*v));
896 return this->style_ ==
soeDEFAULT || this->find() !=
nullptr;
904 Throw<STObject::FieldErr>(
905 "Template field error '" + this->f_->getName() +
"'");
907 this->st_->delField(*this->f_);
909 this->st_->makeFieldAbsent(*this->f_);
918 return this->value();
925 return engaged() ? this->value() : val;
970 return mType ==
nullptr;
990template <
class... Args>
1007 return v_[offset].get();
1013 return v_[offset].get();
1019 return &
v_[offset].get();
1025 return &
v_[offset].get();
1029typename T::value_type
1057typename T::value_type
1064 Throw<STObject::FieldErr>(
"Missing field: " + f.
getName());
1066 if (
auto const u =
dynamic_cast<T const*
>(b))
1070 mType,
"xrpl::STObject::at(TypedField auto) : field template non-null");
1072 b->getSType() == STI_NOTPRESENT,
1073 "xrpl::STObject::at(TypedField auto) : type not present");
1076 Throw<STObject::FieldErr>(
"Missing optional field: " + f.
getName());
1080 "xrpl::STObject::at(TypedField auto) : template style is default");
1095 auto const u =
dynamic_cast<T const*
>(b);
1100 "xrpl::STObject::at(OptionaledField auto) : field template "
1103 b->getSType() == STI_NOTPRESENT,
1104 "xrpl::STObject::at(OptionaledField auto) : type not present");
1109 "xrpl::STObject::at(OptionaledField auto) : template style is "
1111 return typename T::value_type{};
1139 if (rf->
getSType() == STI_NOTPRESENT)
1143 if (
auto cf =
dynamic_cast<Bits*
>(rf))
1146 Throw<std::runtime_error>(
"Wrong field type");
1152 return !(*
this == o);
1155template <
typename T,
typename V>
1166 if (
id == STI_NOTPRESENT)
1169 T
const* cf =
dynamic_cast<T const*
>(rf);
1172 Throw<std::runtime_error>(
"Wrong field type");
1182template <
typename T,
typename V>
1193 if (
id == STI_NOTPRESENT)
1196 T
const* cf =
dynamic_cast<T const*
>(rf);
1199 Throw<std::runtime_error>(
"Wrong field type");
1205template <
typename T,
typename V>
1216 if (rf->
getSType() == STI_NOTPRESENT)
1219 T* cf =
dynamic_cast<T*
>(rf);
1222 Throw<std::runtime_error>(
"Wrong field type");
1224 cf->setValue(std::move(value));
1228template <
typename T>
1237 if (rf->
getSType() == STI_NOTPRESENT)
1240 T* cf =
dynamic_cast<T*
>(rf);
1243 Throw<std::runtime_error>(
"Wrong field type");
1249template <
typename T>
1258 if (rf->
getSType() == STI_NOTPRESENT)
1261 T* cf =
dynamic_cast<T*
>(rf);
1264 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)
SOEStyle getStyle(SField const &field) const
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.