20#ifndef RIPPLE_PROTOCOL_STOBJECT_H_INCLUDED
21#define RIPPLE_PROTOCOL_STOBJECT_H_INCLUDED
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/basics/Slice.h>
25#include <xrpl/basics/chrono.h>
26#include <xrpl/basics/contract.h>
27#include <xrpl/beast/utility/instrumentation.h>
28#include <xrpl/protocol/FeeUnits.h>
29#include <xrpl/protocol/HashPrefix.h>
30#include <xrpl/protocol/SOTemplate.h>
31#include <xrpl/protocol/STAmount.h>
32#include <xrpl/protocol/STBase.h>
33#include <xrpl/protocol/STCurrency.h>
34#include <xrpl/protocol/STIssue.h>
35#include <xrpl/protocol/STPathSet.h>
36#include <xrpl/protocol/STVector256.h>
37#include <xrpl/protocol/detail/STVar.h>
38#include <boost/iterator/transform_iterator.hpp>
51 Throw<std::runtime_error>(
"Field not found: " + field.getName());
82 transform_iterator<Transform, STObject::list_type::const_iterator>;
164 template <
class... Args>
259 typename T::value_type
306 typename T::value_type
444 decltype(std::declval<T>().value())>::type>::type>
453 template <
typename T,
typename V>
458 template <
typename T,
typename V>
463 template <
typename T>
468 template <
typename T>
510concept IsArithmetic = std::is_arithmetic_v<U> || std::is_same_v<U, STAmount>;
529 template <IsArithmetic U>
533 template <IsArithmetic U>
564 operator bool() const noexcept;
584 return !lhs.engaged();
590 return rhs == std::nullopt;
612 if (lhs.engaged() != rhs.engaged())
614 return !lhs.engaged() || *lhs == *rhs;
620 return !(lhs == std::nullopt);
626 return !(rhs == std::nullopt);
632 return !(lhs == rhs);
638 return !(lhs == rhs);
644 return !(lhs == rhs);
689 Throw<STObject::FieldErr>(
690 "Template field error '" + this->
f_->getName() +
"'");
703 auto const t = find();
708 Throw<STObject::FieldErr>(
"Value requested from invalid STObject.");
712 Throw<STObject::FieldErr>(
713 "Missing field '" + this->f_->getName() +
"'");
722 return dynamic_cast<T const*
>(st_->peekAtPField(*f_));
732 st_->makeFieldAbsent(*f_);
737 t =
dynamic_cast<T*
>(st_->getPField(*f_,
true));
739 t =
dynamic_cast<T*
>(st_->makeFieldPresent(*f_));
740 XRPL_ASSERT(t,
"ripple::STObject::Proxy::assign : type cast succeeded");
741 *t = std::forward<U>(u);
751 this->assign(std::forward<U>(u));
756template <IsArithmetic U>
760 this->assign(this->value() + u);
765template <IsArithmetic U>
769 this->assign(this->value() - u);
776 return this->value();
797 return this->value();
802 T>::optional_type()
const
804 return optional_value();
811 return optional_value();
827 this->assign(std::move(*v));
849 this->assign(std::forward<U>(u));
863 return this->style_ ==
soeDEFAULT || this->find() !=
nullptr;
871 Throw<STObject::FieldErr>(
872 "Template field error '" + this->f_->getName() +
"'");
874 this->st_->delField(*this->f_);
876 this->st_->makeFieldAbsent(*this->f_);
885 return this->value();
892 return engaged() ? this->value() : val;
937 return mType ==
nullptr;
957template <
class... Args>
974 return v_[offset].get();
980 return v_[offset].get();
986 return &
v_[offset].get();
992 return &
v_[offset].get();
996typename T::value_type
1024typename T::value_type
1031 Throw<STObject::FieldErr>(
"Missing field: " + f.
getName());
1033 if (
auto const u =
dynamic_cast<T const*
>(b))
1038 "ripple::STObject::at(TypedField auto) : field template non-null");
1040 b->getSType() == STI_NOTPRESENT,
1041 "ripple::STObject::at(TypedField auto) : type not present");
1044 Throw<STObject::FieldErr>(
"Missing optional field: " + f.
getName());
1048 "ripple::STObject::at(TypedField auto) : template style is default");
1062 return std::nullopt;
1063 auto const u =
dynamic_cast<T const*
>(b);
1068 "ripple::STObject::at(OptionaledField auto) : field template "
1071 b->getSType() == STI_NOTPRESENT,
1072 "ripple::STObject::at(OptionaledField auto) : type not present");
1074 return std::nullopt;
1077 "ripple::STObject::at(OptionaledField auto) : template style is "
1079 return typename T::value_type{};
1107 if (rf->
getSType() == STI_NOTPRESENT)
1111 if (
auto cf =
dynamic_cast<Bits*
>(rf))
1114 Throw<std::runtime_error>(
"Wrong field type");
1120 return !(*
this == o);
1123template <
typename T,
typename V>
1134 if (
id == STI_NOTPRESENT)
1137 const T* cf =
dynamic_cast<const T*
>(rf);
1140 Throw<std::runtime_error>(
"Wrong field type");
1150template <
typename T,
typename V>
1161 if (
id == STI_NOTPRESENT)
1164 const T* cf =
dynamic_cast<const T*
>(rf);
1167 Throw<std::runtime_error>(
"Wrong field type");
1173template <
typename T,
typename V>
1184 if (rf->
getSType() == STI_NOTPRESENT)
1187 T* cf =
dynamic_cast<T*
>(rf);
1190 Throw<std::runtime_error>(
"Wrong field type");
1192 cf->setValue(std::move(value));
1196template <
typename T>
1205 if (rf->
getSType() == STI_NOTPRESENT)
1208 T* cf =
dynamic_cast<T*
>(rf);
1211 Throw<std::runtime_error>(
"Wrong field type");
1217template <
typename T>
1226 if (rf->
getSType() == STI_NOTPRESENT)
1229 T* cf =
dynamic_cast<T*
>(rf);
1232 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)
value_type operator*() const
Return the contained value.
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
optional_type operator~() const
Explicit conversion to std::optional.
OptionalProxy(OptionalProxy const &)=default
value_type value_or(value_type val) const
friend bool operator!=(OptionalProxy const &lhs, std::nullopt_t) noexcept
friend bool operator!=(std::nullopt_t, OptionalProxy const &rhs) noexcept
std::enable_if_t< std::is_assignable_v< T, U >, OptionalProxy & > operator=(U &&u)
typename T::value_type value_type
OptionalProxy & operator=(OptionalProxy const &)=delete
optional_type optional_value() const
std::optional< typename std::decay< value_type >::type > optional_type
friend bool operator!=(optional_type const &lhs, OptionalProxy const &rhs) noexcept
friend bool operator==(std::nullopt_t, OptionalProxy const &rhs) noexcept
bool engaged() const noexcept
friend bool operator!=(OptionalProxy const &lhs, OptionalProxy const &rhs) noexcept
typename T::value_type value_type
Proxy(Proxy const &)=default
TypedField< T > const * f_
ValueProxy & operator-=(U const &u)
typename T::value_type value_type
ValueProxy & operator+=(U const &u)
ValueProxy & operator=(ValueProxy const &)=delete
std::enable_if_t< std::is_assignable_v< T, U >, ValueProxy & > operator=(U &&u)
ValueProxy(ValueProxy const &)=default
STPathSet const & getFieldPathSet(SField const &field) const
unsigned char getFieldU8(SField const &field) const
uint192 getFieldH192(SField const &field) const
STBase * getPField(SField const &field, bool createOkay=false)
void setFieldIssue(SField const &field, STIssue const &)
Blob getFieldVL(SField const &field) const
boost::transform_iterator< Transform, STObject::list_type::const_iterator > iterator
bool clearFlag(std::uint32_t)
AccountID getAccountID(SField const &field) const
static std::vector< STBase const * > getSortedFields(STObject const &objToSort, WhichFields whichFields)
T & peekField(SField const &field)
uint160 getFieldH160(SField const &field) const
void setFieldCurrency(SField const &field, STCurrency const &)
const STArray & getFieldArray(SField const &field) const
void setFieldArray(SField const &field, STArray const &v)
const STBase * peekAtPField(SField const &field) const
void setFieldPathSet(SField const &field, STPathSet const &)
SField const & getFieldSType(int index) const
STObject(STObject const &)=default
bool isEquivalent(const STBase &t) const override
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
bool isFlag(std::uint32_t) const
std::uint16_t getFieldU16(SField const &field) const
virtual ~STObject()=default
void setFieldH256(SField const &field, uint256 const &)
void setFieldNumber(SField const &field, STNumber const &)
bool setFlag(std::uint32_t)
std::uint32_t getFieldU32(SField const &field) const
const STVector256 & getFieldV256(SField const &field) const
STBase * copy(std::size_t n, void *buf) const override
void makeFieldAbsent(SField const &field)
const STBase & peekAtIndex(int offset) const
STObject & peekFieldObject(SField const &field)
void setFieldU16(SField const &field, std::uint16_t)
const STBase * peekAtPIndex(int offset) const
Serializer getSerializer() const
std::uint64_t getFieldU64(SField const &field) const
STBase * makeFieldPresent(SField const &field)
void setFieldU8(SField const &field, unsigned char)
bool operator==(const STObject &o) const
void add(Serializer &s) const override
STObject(SOTemplate const &type, SField const &name, F &&f)
void setFieldAmount(SField const &field, STAmount const &)
bool delField(SField const &field)
bool hasMatchingEntry(const STBase &)
STBase & getField(SField const &field)
V const & getFieldByConstRef(SField const &field, V const &empty) const
STAmount const & getFieldAmount(SField const &field) const
uint256 getSigningHash(HashPrefix prefix) const
T::value_type operator[](TypedField< T > const &f) const
Get the value of a field.
uint256 getHash(HashPrefix prefix) const
void set(const SOTemplate &)
int getFieldIndex(SField const &field) const
STBase & getIndex(int offset)
ValueProxy< T > operator[](TypedField< T > const &f)
Get a modifiable field value.
void setFieldUsingAssignment(SField const &field, T const &value)
SerializedTypeID getSType() const override
std::string getFullText() const override
std::string getText() const override
const STBase & peekAtField(SField const &field) const
bool isFieldPresent(SField const &field) const
static STObject makeInnerObject(SField const &name)
const STCurrency & getFieldCurrency(SField const &field) const
void setAccountID(SField const &field, AccountID const &)
uint128 getFieldH128(SField const &field) const
void applyTemplateFromSField(SField const &)
OptionalProxy< T > at(OptionaledField< T > const &of)
Return a modifiable field value as std::optional.
ValueProxy< T > at(TypedField< T > const &f)
Get a modifiable field value.
void setFieldV256(SField const &field, STVector256 const &v)
void setFieldUsingSetValue(SField const &field, V value)
void setFieldH128(SField const &field, uint128 const &)
void setFieldU64(SField const &field, std::uint64_t)
bool operator!=(const STObject &o) const
Json::Value getJson(JsonOptions options) const override
void setFieldU32(SField const &field, std::uint32_t)
V getFieldByValue(SField const &field) const
STBase * move(std::size_t n, void *buf) override
OptionalProxy< T > operator[](OptionaledField< T > const &of)
Return a modifiable field value as std::optional.
std::size_t emplace_back(Args &&... args)
STBase * getPIndex(int offset)
void applyTemplate(const SOTemplate &type)
STArray & peekFieldArray(SField const &field)
STNumber const & getFieldNumber(SField const &field) const
void reserve(std::size_t n)
STObject & operator=(STObject const &)=default
void addWithoutSigningFields(Serializer &s) const
void setFieldVL(SField const &field, Blob const &)
std::uint32_t getFlags() const
uint256 getFieldH256(SField const &field) const
void setFieldH160(SField const &field, base_uint< 160, Tag > const &v)
bool isDefault() 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.
HashPrefix
Prefix for hashing functions.
SOEStyle
Kind of element in each entry of an SOTemplate.
void throwFieldNotFound(SField const &field)
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.