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>
527 template <IsArithmetic U>
531 template <IsArithmetic U>
537 template <
typename U>
541 return rhs.
value() == lhs;
569 operator bool() const noexcept;
580 return !lhs.engaged();
608 if (lhs.engaged() != rhs.engaged())
610 return !lhs.engaged() || *lhs == *rhs;
628 return !(lhs == rhs);
634 return !(lhs == rhs);
640 return !(lhs == rhs);
685 Throw<STObject::FieldErr>(
686 "Template field error '" + this->
f_->getName() +
"'");
699 auto const t = find();
704 Throw<STObject::FieldErr>(
"Value requested from invalid STObject.");
708 Throw<STObject::FieldErr>(
709 "Missing field '" + this->f_->getName() +
"'");
718 return this->value();
732 return dynamic_cast<T const*
>(st_->peekAtPField(*f_));
742 st_->makeFieldAbsent(*f_);
747 t =
dynamic_cast<T*
>(st_->getPField(*f_,
true));
749 t =
dynamic_cast<T*
>(st_->makeFieldPresent(*f_));
750 XRPL_ASSERT(t,
"ripple::STObject::Proxy::assign : type cast succeeded");
766template <IsArithmetic U>
770 this->assign(this->value() + u);
775template <IsArithmetic U>
779 this->assign(this->value() - u);
786 return this->value();
805 T>::optional_type()
const
807 return optional_value();
814 return optional_value();
830 this->assign(std::move(*v));
866 return this->style_ ==
soeDEFAULT || this->find() !=
nullptr;
874 Throw<STObject::FieldErr>(
875 "Template field error '" + this->f_->getName() +
"'");
877 this->st_->delField(*this->f_);
879 this->st_->makeFieldAbsent(*this->f_);
888 return this->value();
895 return engaged() ? this->value() : val;
940 return mType ==
nullptr;
960template <
class... Args>
977 return v_[offset].get();
983 return v_[offset].get();
989 return &
v_[offset].get();
995 return &
v_[offset].get();
999typename T::value_type
1027typename T::value_type
1034 Throw<STObject::FieldErr>(
"Missing field: " + f.
getName());
1036 if (
auto const u =
dynamic_cast<T const*
>(b))
1041 "ripple::STObject::at(TypedField auto) : field template non-null");
1043 b->getSType() == STI_NOTPRESENT,
1044 "ripple::STObject::at(TypedField auto) : type not present");
1047 Throw<STObject::FieldErr>(
"Missing optional field: " + f.
getName());
1051 "ripple::STObject::at(TypedField auto) : template style is default");
1066 auto const u =
dynamic_cast<T const*
>(b);
1071 "ripple::STObject::at(OptionaledField auto) : field template "
1074 b->getSType() == STI_NOTPRESENT,
1075 "ripple::STObject::at(OptionaledField auto) : type not present");
1080 "ripple::STObject::at(OptionaledField auto) : template style is "
1082 return typename T::value_type{};
1110 if (rf->
getSType() == STI_NOTPRESENT)
1114 if (
auto cf =
dynamic_cast<Bits*
>(rf))
1117 Throw<std::runtime_error>(
"Wrong field type");
1123 return !(*
this == o);
1126template <
typename T,
typename V>
1137 if (
id == STI_NOTPRESENT)
1140 T
const* cf =
dynamic_cast<T const*
>(rf);
1143 Throw<std::runtime_error>(
"Wrong field type");
1153template <
typename T,
typename V>
1164 if (
id == STI_NOTPRESENT)
1167 T
const* cf =
dynamic_cast<T const*
>(rf);
1170 Throw<std::runtime_error>(
"Wrong field type");
1176template <
typename T,
typename V>
1187 if (rf->
getSType() == STI_NOTPRESENT)
1190 T* cf =
dynamic_cast<T*
>(rf);
1193 Throw<std::runtime_error>(
"Wrong field type");
1195 cf->setValue(std::move(value));
1199template <
typename T>
1208 if (rf->
getSType() == STI_NOTPRESENT)
1211 T* cf =
dynamic_cast<T*
>(rf);
1214 Throw<std::runtime_error>(
"Wrong field type");
1220template <
typename T>
1229 if (rf->
getSType() == STI_NOTPRESENT)
1232 T* cf =
dynamic_cast<T*
>(rf);
1235 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
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_
value_type operator*() const
T const * operator->() const
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
friend bool operator==(U const &lhs, STObject::ValueProxy< T > const &rhs)
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 &)
bool operator!=(STObject const &o) const
void applyTemplate(SOTemplate const &type)
Blob getFieldVL(SField const &field) const
STObject getFieldObject(SField const &field) const
boost::transform_iterator< Transform, STObject::list_type::const_iterator > iterator
bool hasMatchingEntry(STBase const &)
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)
STArray const & getFieldArray(SField const &field) const
uint160 getFieldH160(SField const &field) const
void setFieldCurrency(SField const &field, STCurrency const &)
STBase const * peekAtPField(SField const &field) const
STBase const * peekAtPIndex(int offset) const
void setFieldArray(SField const &field, STArray const &v)
void setFieldPathSet(SField const &field, STPathSet const &)
SField const & getFieldSType(int index) const
void setFieldObject(SField const &field, STObject const &v)
STObject(STObject const &)=default
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
void setFieldI32(SField const &field, std::int32_t)
STBase * copy(std::size_t n, void *buf) const override
void makeFieldAbsent(SField const &field)
STObject & peekFieldObject(SField const &field)
void setFieldU16(SField const &field, std::uint16_t)
Serializer getSerializer() const
std::uint64_t getFieldU64(SField const &field) const
STBase * makeFieldPresent(SField const &field)
void setFieldU8(SField const &field, unsigned char)
void set(SOTemplate 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)
STBase const & peekAtIndex(int offset) const
STBase & getField(SField const &field)
V const & getFieldByConstRef(SField const &field, V const &empty) const
STAmount const & getFieldAmount(SField const &field) const
std::int32_t getFieldI32(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
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
bool isFieldPresent(SField const &field) const
static STObject makeInnerObject(SField const &name)
STCurrency const & getFieldCurrency(SField const &field) const
bool operator==(STObject const &o) 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)
Json::Value getJson(JsonOptions=JsonOptions::none) const override
void setFieldH128(SField const &field, uint128 const &)
void setFieldU64(SField const &field, std::uint64_t)
bool isEquivalent(STBase const &t) const override
void setFieldU32(SField const &field, std::uint32_t)
STBase const & peekAtField(SField const &field) const
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)
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)
STVector256 const & getFieldV256(SField const &field) const
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.
Number operator*(Number const &x, Number const &y)
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.