rippled
Loading...
Searching...
No Matches
STObject.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/chrono.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/protocol/HashPrefix.h>
9#include <xrpl/protocol/SOTemplate.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/STBase.h>
12#include <xrpl/protocol/STCurrency.h>
13#include <xrpl/protocol/STIssue.h>
14#include <xrpl/protocol/STPathSet.h>
15#include <xrpl/protocol/STVector256.h>
16#include <xrpl/protocol/Units.h>
17#include <xrpl/protocol/detail/STVar.h>
18
19#include <boost/iterator/transform_iterator.hpp>
20
21#include <optional>
22#include <stdexcept>
23#include <type_traits>
24#include <utility>
25
26namespace xrpl {
27
28class STArray;
29
30inline void
32{
33 Throw<std::runtime_error>("Field not found: " + field.getName());
34}
35
36class STObject : public STBase, public CountedObject<STObject>
37{
38 // Proxy value for a STBase derived class
39 template <class T>
40 class Proxy;
41 template <class T>
42 class ValueProxy;
43 template <class T>
44 class OptionalProxy;
45
46 struct Transform
47 {
48 explicit Transform() = default;
49
52
53 STBase const&
54 operator()(detail::STVar const& e) const;
55 };
56
58
60 SOTemplate const* mType{};
61
62public:
63 using iterator = boost::transform_iterator<Transform, STObject::list_type::const_iterator>;
64
65 virtual ~STObject() = default;
66 STObject(STObject const&) = default;
67
68 template <typename F>
69 STObject(SOTemplate const& type, SField const& name, F&& f) : STObject(type, name)
70 {
71 f(*this);
72 }
73
75 operator=(STObject const&) = default;
78 operator=(STObject&& other);
79
80 STObject(SOTemplate const& type, SField const& name);
81 STObject(SOTemplate const& type, SerialIter& sit, SField const& name);
82 STObject(SerialIter& sit, SField const& name, int depth = 0);
83 STObject(SerialIter&& sit, SField const& name);
84 explicit STObject(SField const& name);
85
86 static STObject
87 makeInnerObject(SField const& name);
88
90 begin() const;
91
93 end() const;
94
95 bool
96 empty() const;
97
98 void
100
101 void
102 applyTemplate(SOTemplate const& type);
103
104 void
106
107 bool
108 isFree() const;
109
110 void
111 set(SOTemplate const&);
112
113 bool
114 set(SerialIter& u, int depth = 0);
115
117 getSType() const override;
118
119 bool
120 isEquivalent(STBase const& t) const override;
121
122 bool
123 isDefault() const override;
124
125 void
126 add(Serializer& s) const override;
127
129 getFullText() const override;
130
132 getText() const override;
133
134 // TODO(tom): options should be an enum.
136
137 void
139
141 getSerializer() const;
142
143 template <class... Args>
145 emplace_back(Args&&... args);
146
147 int
148 getCount() const;
149
150 bool
152 bool
154 bool
155 isFlag(std::uint32_t) const;
156
158 getFlags() const;
159
160 uint256
161 getHash(HashPrefix prefix) const;
162
163 uint256
164 getSigningHash(HashPrefix prefix) const;
165
166 STBase const&
167 peekAtIndex(int offset) const;
168
169 STBase&
170 getIndex(int offset);
171
172 STBase const*
173 peekAtPIndex(int offset) const;
174
175 STBase*
176 getPIndex(int offset);
177
178 int
179 getFieldIndex(SField const& field) const;
180
181 SField const&
182 getFieldSType(int index) const;
183
184 STBase const&
185 peekAtField(SField const& field) const;
186
187 STBase&
188 getField(SField const& field);
189
190 STBase const*
191 peekAtPField(SField const& field) const;
192
193 STBase*
194 getPField(SField const& field, bool createOkay = false);
195
196 // these throw if the field type doesn't match, or return default values
197 // if the field is optional but not present
198 unsigned char
199 getFieldU8(SField const& field) const;
201 getFieldU16(SField const& field) const;
203 getFieldU32(SField const& field) const;
205 getFieldU64(SField const& field) const;
206 uint128
207 getFieldH128(SField const& field) const;
208
209 uint160
210 getFieldH160(SField const& field) const;
211 uint192
212 getFieldH192(SField const& field) const;
213 uint256
214 getFieldH256(SField const& field) const;
216 getFieldI32(SField const& field) const;
218 getAccountID(SField const& field) const;
219
220 Blob
221 getFieldVL(SField const& field) const;
222 STAmount const&
223 getFieldAmount(SField const& field) const;
224 STPathSet const&
225 getFieldPathSet(SField const& field) const;
226 STVector256 const&
227 getFieldV256(SField const& field) const;
228 // If not found, returns an object constructed with the given field
230 getFieldObject(SField const& field) const;
231 STArray const&
232 getFieldArray(SField const& field) const;
233 STCurrency const&
234 getFieldCurrency(SField const& field) const;
235 STNumber const&
236 getFieldNumber(SField const& field) const;
237
245 template <class T>
246 typename T::value_type
247 operator[](TypedField<T> const& f) const;
248
257 template <class T>
259 operator[](OptionaledField<T> const& of) const;
260
268 template <class T>
271
281 template <class T>
284
292 template <class T>
293 typename T::value_type
294 at(TypedField<T> const& f) const;
295
304 template <class T>
306 at(OptionaledField<T> const& of) const;
307
315 template <class T>
317 at(TypedField<T> const& f);
318
328 template <class T>
331
335 void
337
338 void
339 set(STBase&& v);
340
341 void
342 setFieldU8(SField const& field, unsigned char);
343 void
344 setFieldU16(SField const& field, std::uint16_t);
345 void
346 setFieldU32(SField const& field, std::uint32_t);
347 void
348 setFieldU64(SField const& field, std::uint64_t);
349 void
350 setFieldH128(SField const& field, uint128 const&);
351 void
352 setFieldH256(SField const& field, uint256 const&);
353 void
354 setFieldI32(SField const& field, std::int32_t);
355 void
356 setFieldVL(SField const& field, Blob const&);
357 void
358 setFieldVL(SField const& field, Slice const&);
359
360 void
361 setAccountID(SField const& field, AccountID const&);
362
363 void
364 setFieldAmount(SField const& field, STAmount const&);
365 void
366 setFieldIssue(SField const& field, STIssue const&);
367 void
368 setFieldCurrency(SField const& field, STCurrency const&);
369 void
370 setFieldNumber(SField const& field, STNumber const&);
371 void
372 setFieldPathSet(SField const& field, STPathSet const&);
373 void
374 setFieldV256(SField const& field, STVector256 const& v);
375 void
376 setFieldArray(SField const& field, STArray const& v);
377 void
378 setFieldObject(SField const& field, STObject const& v);
379
380 template <class Tag>
381 void
382 setFieldH160(SField const& field, base_uint<160, Tag> const& v);
383
384 STObject&
385 peekFieldObject(SField const& field);
386 STArray&
387 peekFieldArray(SField const& field);
388
389 bool
390 isFieldPresent(SField const& field) const;
391 STBase*
392 makeFieldPresent(SField const& field);
393 void
394 makeFieldAbsent(SField const& field);
395 bool
396 delField(SField const& field);
397 void
398 delField(int index);
399
401 getStyle(SField const& field) const;
402
403 bool
404 hasMatchingEntry(STBase const&) const;
405
406 bool
407 operator==(STObject const& o) const;
408 bool
409 operator!=(STObject const& o) const;
410
411 class FieldErr;
412
413private:
414 enum WhichFields : bool {
415 // These values are carefully chosen to do the right thing if passed
416 // to SField::shouldInclude (bool)
418 withAllFields = true
419 };
420
421 void
422 add(Serializer& s, WhichFields whichFields) const;
423
424 // Sort the entries in an STObject into the order that they will be
425 // serialized. Note: they are not sorted into pointer value order, they
426 // are sorted by SField::fieldCode.
428 getSortedFields(STObject const& objToSort, WhichFields whichFields);
429
430 // Implementation for getting (most) fields that return by value.
431 //
432 // The remove_cv and remove_reference are necessitated by the STBitString
433 // types. Their value() returns by const ref. We return those types
434 // by value.
435 template <
436 typename T,
437 typename V = typename std::remove_cv<
438 typename std::remove_reference<decltype(std::declval<T>().value())>::type>::type>
439 V
440 getFieldByValue(SField const& field) const;
441
442 // Implementations for getting (most) fields that return by const reference.
443 //
444 // If an absent optional field is deserialized we don't have anything
445 // obvious to return. So we insist on having the call provide an
446 // 'empty' value we return in that circumstance.
447 template <typename T, typename V>
448 V const&
449 getFieldByConstRef(SField const& field, V const& empty) const;
450
451 // Implementation for setting most fields with a setValue() method.
452 template <typename T, typename V>
453 void
454 setFieldUsingSetValue(SField const& field, V value);
455
456 // Implementation for setting fields using assignment
457 template <typename T>
458 void
459 setFieldUsingAssignment(SField const& field, T const& value);
460
461 // Implementation for peeking STObjects and STArrays
462 template <typename T>
463 T&
464 peekField(SField const& field);
465
466 STBase*
467 copy(std::size_t n, void* buf) const override;
468 STBase*
469 move(std::size_t n, void* buf) override;
470
471 friend class detail::STVar;
472};
473
474//------------------------------------------------------------------------------
475
476template <class T>
478{
479public:
480 using value_type = typename T::value_type;
481
483 value() const;
484
486 operator*() const;
487
490 T const*
491 operator->() const;
492
493protected:
497
498 Proxy(Proxy const&) = default;
499
500 Proxy(STObject* st, TypedField<T> const* f);
501
502 T const*
503 find() const;
504
505 template <class U>
506 void
507 assign(U&& u);
508};
509
510// Constraint += and -= ValueProxy operators
511// to value types that support arithmetic operations
512template <typename U>
515template <
516 typename U,
517 typename Value = typename U::value_type,
518 typename Unit = typename U::unit_type>
521template <typename U, typename Value = typename U::value_type>
523template <typename U>
525
526template <class T, class U>
527concept Addable = requires(T t, U u) { t = t + u; };
528template <typename T, typename U>
531
532template <class T>
534{
535private:
536 using value_type = typename T::value_type;
537
538public:
539 ValueProxy(ValueProxy const&) = default;
541 operator=(ValueProxy const&) = delete;
542
543 template <class U>
545 operator=(U&& u);
546
547 // Convenience operators for value types supporting
548 // arithmetic operations
549 template <IsArithmetic U>
552 operator+=(U const& u);
553
554 template <IsArithmetic U>
557 operator-=(U const& u);
558
559 operator value_type() const;
560
561 template <typename U>
562 friend bool
563 operator==(U const& lhs, STObject::ValueProxy<T> const& rhs)
564 {
565 return rhs.value() == lhs;
566 }
567
568private:
569 friend class STObject;
570
571 ValueProxy(STObject* st, TypedField<T> const* f);
572};
573
574template <class T>
576{
577private:
578 using value_type = typename T::value_type;
579
581
582public:
583 OptionalProxy(OptionalProxy const&) = default;
585 operator=(OptionalProxy const&) = delete;
586
592 explicit
593 operator bool() const noexcept;
594
595 operator optional_type() const;
596
599 operator~() const;
600
601 friend bool
602 operator==(OptionalProxy const& lhs, std::nullopt_t) noexcept
603 {
604 return !lhs.engaged();
605 }
606
607 friend bool
609 {
610 return rhs == std::nullopt;
611 }
612
613 friend bool
614 operator==(OptionalProxy const& lhs, optional_type const& rhs) noexcept
615 {
616 if (!lhs.engaged())
617 return !rhs;
618 if (!rhs)
619 return false;
620 return *lhs == *rhs;
621 }
622
623 friend bool
624 operator==(optional_type const& lhs, OptionalProxy const& rhs) noexcept
625 {
626 return rhs == lhs;
627 }
628
629 friend bool
630 operator==(OptionalProxy const& lhs, OptionalProxy const& rhs) noexcept
631 {
632 if (lhs.engaged() != rhs.engaged())
633 return false;
634 return !lhs.engaged() || *lhs == *rhs;
635 }
636
637 friend bool
639 {
640 return !(lhs == std::nullopt);
641 }
642
643 friend bool
645 {
646 return !(rhs == std::nullopt);
647 }
648
649 friend bool
650 operator!=(OptionalProxy const& lhs, optional_type const& rhs) noexcept
651 {
652 return !(lhs == rhs);
653 }
654
655 friend bool
656 operator!=(optional_type const& lhs, OptionalProxy const& rhs) noexcept
657 {
658 return !(lhs == rhs);
659 }
660
661 friend bool
662 operator!=(OptionalProxy const& lhs, OptionalProxy const& rhs) noexcept
663 {
664 return !(lhs == rhs);
665 }
666
667 // Emulate std::optional::value_or
669 value_or(value_type val) const;
670
674 operator=(optional_type&& v); // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
676 operator=(optional_type const& v);
677
678 template <class U>
680 operator=(U&& u);
681
682private:
683 friend class STObject;
684
685 OptionalProxy(STObject* st, TypedField<T> const* f);
686
687 bool
688 engaged() const noexcept;
689
690 void
691 disengage();
692
694 optional_value() const;
695};
696
697class STObject::FieldErr : public std::runtime_error
698{
700};
701
702template <class T>
704{
705 if (st_->mType)
706 {
707 // STObject has associated template
708 if (!st_->peekAtPField(*f_))
709 Throw<STObject::FieldErr>("Template field error '" + this->f_->getName() + "'");
710 style_ = st_->mType->style(*f_);
711 }
712 else
713 {
715 }
716}
717
718template <class T>
719auto
721{
722 auto const t = find();
723 if (t)
724 return t->value();
725 if (style_ == soeINVALID)
726 {
727 Throw<STObject::FieldErr>("Value requested from invalid STObject.");
728 }
729 if (style_ != soeDEFAULT)
730 {
731 Throw<STObject::FieldErr>("Missing field '" + this->f_->getName() + "'");
732 }
733 return value_type{};
734}
735
736template <class T>
737auto
739{
740 return this->value();
741}
742
745template <class T>
746T const*
748{
749 return this->find();
750}
751
752template <class T>
753inline T const*
755{
756 return dynamic_cast<T const*>(st_->peekAtPField(*f_));
757}
758
759template <class T>
760template <class U>
761void
763{
764 if (style_ == soeDEFAULT && u == value_type{})
765 {
766 st_->makeFieldAbsent(*f_);
767 return;
768 }
769 T* t = nullptr;
770 if (style_ == soeINVALID)
771 t = dynamic_cast<T*>(st_->getPField(*f_, true));
772 else
773 t = dynamic_cast<T*>(st_->makeFieldPresent(*f_));
774 XRPL_ASSERT(t, "xrpl::STObject::Proxy::assign : type cast succeeded");
775 *t = std::forward<U>(u);
776}
777
778//------------------------------------------------------------------------------
779
780template <class T>
781template <class U>
784{
785 this->assign(std::forward<U>(u));
786 return *this;
787}
788
789template <typename T>
790template <IsArithmetic U>
794{
795 this->assign(this->value() + u);
796 return *this;
797}
798
799template <class T>
800template <IsArithmetic U>
804{
805 this->assign(this->value() - u);
806 return *this;
807}
808
809template <class T>
811operator value_type() const
812{
813 return this->value();
814}
815
816template <class T>
820
821//------------------------------------------------------------------------------
822
823template <class T>
825operator bool() const noexcept
826{
827 return engaged();
828}
829
830template <class T>
832operator typename STObject::OptionalProxy<T>::optional_type() const
833{
834 return optional_value();
835}
836
837template <class T>
840{
841 return optional_value();
842}
843
844template <class T>
845auto
847{
848 disengage();
849 return *this;
850}
851
852template <class T>
853auto
855 optional_type&& v) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
856 -> OptionalProxy&
857{
858 if (v)
859 this->assign(std::move(*v));
860 else
861 disengage();
862 return *this;
863}
864
865template <class T>
866auto
868{
869 if (v)
870 this->assign(*v);
871 else
872 disengage();
873 return *this;
874}
875
876template <class T>
877template <class U>
880{
881 this->assign(std::forward<U>(u));
882 return *this;
883}
884
885template <class T>
889
890template <class T>
891bool
893{
894 return this->style_ == soeDEFAULT || this->find() != nullptr;
895}
896
897template <class T>
898void
900{
901 if (this->style_ == soeREQUIRED || this->style_ == soeDEFAULT)
902 Throw<STObject::FieldErr>("Template field error '" + this->f_->getName() + "'");
903 if (this->style_ == soeINVALID)
904 this->st_->delField(*this->f_);
905 else
906 this->st_->makeFieldAbsent(*this->f_);
907}
908
909template <class T>
910auto
912{
913 if (!engaged())
914 return std::nullopt;
915 return this->value();
916}
917
918template <class T>
921{
922 return engaged() ? this->value() : val;
923}
924
925//------------------------------------------------------------------------------
926
927inline STBase const&
929{
930 return e.get();
931}
932
933//------------------------------------------------------------------------------
934
935// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
936inline STObject::STObject(SerialIter&& sit, SField const& name) : STObject(sit, name)
937{
938}
939
942{
943 return iterator(v_.begin());
944}
945
948{
949 return iterator(v_.end());
950}
951
952inline bool
954{
955 return v_.empty();
956}
957
958inline void
963
964inline bool
966{
967 return mType == nullptr;
968}
969
970inline void
975
976// VFALCO NOTE does this return an expensive copy of an object with a
977// dynamic buffer?
978// VFALCO TODO Remove this function and fix the few callers.
979inline Serializer
981{
982 Serializer s;
983 add(s, withAllFields);
984 return s;
985}
986
987template <class... Args>
988inline std::size_t
990{
992 return v_.size() - 1;
993}
994
995inline int
997{
998 return v_.size();
999}
1000
1001inline STBase const&
1002STObject::peekAtIndex(int offset) const
1003{
1004 return v_[offset].get();
1005}
1006
1007inline STBase&
1009{
1010 return v_[offset].get();
1011}
1012
1013inline STBase const*
1014STObject::peekAtPIndex(int offset) const
1015{
1016 return &v_[offset].get();
1017}
1018
1019inline STBase*
1021{
1022 return &v_[offset].get();
1023}
1024
1025template <class T>
1026typename T::value_type
1028{
1029 return at(f);
1030}
1031
1032template <class T>
1035{
1036 return at(of);
1037}
1038
1039template <class T>
1040inline auto
1042{
1043 return at(f);
1044}
1045
1046template <class T>
1047inline auto
1049{
1050 return at(of);
1051}
1052
1053template <class T>
1054typename T::value_type
1056{
1057 auto const b = peekAtPField(f);
1058 if (!b)
1059 // This is a free object (no constraints)
1060 // with no template
1061 Throw<STObject::FieldErr>("Missing field: " + f.getName());
1062
1063 if (auto const u = dynamic_cast<T const*>(b))
1064 return u->value();
1065
1066 XRPL_ASSERT(mType, "xrpl::STObject::at(TypedField auto) : field template non-null");
1067 XRPL_ASSERT(
1068 b->getSType() == STI_NOTPRESENT, "xrpl::STObject::at(TypedField auto) : type not present");
1069
1070 if (mType->style(f) == soeOPTIONAL)
1071 Throw<STObject::FieldErr>("Missing optional field: " + f.getName());
1072
1073 XRPL_ASSERT(
1074 mType->style(f) == soeDEFAULT,
1075 "xrpl::STObject::at(TypedField auto) : template style is default");
1076
1077 // Used to help handle the case where value_type is a const reference,
1078 // otherwise we would return the address of a temporary.
1079 static std::decay_t<typename T::value_type> const dv{};
1080 return dv;
1081}
1082
1083template <class T>
1086{
1087 auto const b = peekAtPField(*of.f);
1088 if (!b)
1089 return std::nullopt;
1090 auto const u = dynamic_cast<T const*>(b);
1091 if (!u)
1092 {
1093 XRPL_ASSERT(
1094 mType,
1095 "xrpl::STObject::at(OptionaledField auto) : field template "
1096 "non-null");
1097 XRPL_ASSERT(
1098 b->getSType() == STI_NOTPRESENT,
1099 "xrpl::STObject::at(OptionaledField auto) : type not present");
1100 if (mType->style(*of.f) == soeOPTIONAL)
1101 return std::nullopt;
1102 XRPL_ASSERT(
1103 mType->style(*of.f) == soeDEFAULT,
1104 "xrpl::STObject::at(OptionaledField auto) : template style is "
1105 "default");
1106 return typename T::value_type{};
1107 }
1108 return u->value();
1109}
1110
1111template <class T>
1112inline auto
1114{
1115 return ValueProxy<T>(this, &f);
1116}
1117
1118template <class T>
1119inline auto
1121{
1122 return OptionalProxy<T>(this, of.f);
1123}
1124
1125template <class Tag>
1126void
1128{
1129 STBase* rf = getPField(field, true);
1130
1131 if (!rf)
1132 throwFieldNotFound(field);
1133
1134 if (rf->getSType() == STI_NOTPRESENT)
1135 rf = makeFieldPresent(field);
1136
1137 using Bits = STBitString<160>;
1138 if (auto cf = dynamic_cast<Bits*>(rf))
1139 cf->setValue(v);
1140 else
1141 Throw<std::runtime_error>("Wrong field type");
1142}
1143
1144inline bool
1146{
1147 return !(*this == o);
1148}
1149
1150template <typename T, typename V>
1151V
1153{
1154 STBase const* rf = peekAtPField(field);
1155
1156 if (!rf)
1157 throwFieldNotFound(field);
1158
1159 SerializedTypeID const id = rf->getSType();
1160
1161 if (id == STI_NOTPRESENT)
1162 return V(); // optional field not present
1163
1164 T const* cf = dynamic_cast<T const*>(rf);
1165
1166 if (!cf)
1167 Throw<std::runtime_error>("Wrong field type");
1168
1169 return cf->value();
1170}
1171
1172// Implementations for getting (most) fields that return by const reference.
1173//
1174// If an absent optional field is deserialized we don't have anything
1175// obvious to return. So we insist on having the call provide an
1176// 'empty' value we return in that circumstance.
1177template <typename T, typename V>
1178V const&
1179STObject::getFieldByConstRef(SField const& field, V const& empty) const
1180{
1181 STBase const* rf = peekAtPField(field);
1182
1183 if (!rf)
1184 throwFieldNotFound(field);
1185
1186 SerializedTypeID const id = rf->getSType();
1187
1188 if (id == STI_NOTPRESENT)
1189 return empty; // optional field not present
1190
1191 T const* cf = dynamic_cast<T const*>(rf);
1192
1193 if (!cf)
1194 Throw<std::runtime_error>("Wrong field type");
1195
1196 return *cf;
1197}
1198
1199// Implementation for setting most fields with a setValue() method.
1200template <typename T, typename V>
1201void
1203{
1204 static_assert(!std::is_lvalue_reference<V>::value, "");
1205
1206 STBase* rf = getPField(field, true);
1207
1208 if (!rf)
1209 throwFieldNotFound(field);
1210
1211 if (rf->getSType() == STI_NOTPRESENT)
1212 rf = makeFieldPresent(field);
1213
1214 T* cf = dynamic_cast<T*>(rf);
1215
1216 if (!cf)
1217 Throw<std::runtime_error>("Wrong field type");
1218
1219 cf->setValue(std::move(value));
1220}
1221
1222// Implementation for setting fields using assignment
1223template <typename T>
1224void
1225STObject::setFieldUsingAssignment(SField const& field, T const& value)
1226{
1227 STBase* rf = getPField(field, true);
1228
1229 if (!rf)
1230 throwFieldNotFound(field);
1231
1232 if (rf->getSType() == STI_NOTPRESENT)
1233 rf = makeFieldPresent(field);
1234
1235 T* cf = dynamic_cast<T*>(rf);
1236
1237 if (!cf)
1238 Throw<std::runtime_error>("Wrong field type");
1239
1240 (*cf) = value;
1241}
1242
1243// Implementation for peeking STObjects and STArrays
1244template <typename T>
1245T&
1247{
1248 STBase* rf = getPField(field, true);
1249
1250 if (!rf)
1251 throwFieldNotFound(field);
1252
1253 if (rf->getSType() == STI_NOTPRESENT)
1254 rf = makeFieldPresent(field);
1255
1256 T* cf = dynamic_cast<T*>(rf);
1257
1258 if (!cf)
1259 Throw<std::runtime_error>("Wrong field type");
1260
1261 return *cf;
1262}
1263
1264} // namespace xrpl
T begin(T... args)
Represents a JSON value.
Definition json_value.h:130
Tracks the number of instances of an object.
Identifies fields.
Definition SField.h:126
std::string const & getName() const
Definition SField.h:193
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:92
SOEStyle style(SField const &sf) const
Definition SOTemplate.h:149
A type which can be exported to a well known binary format.
Definition STBase.h:115
virtual SerializedTypeID getSType() const
Definition STBase.cpp:59
void setValue(base_uint< Bits, Tag > const &v)
A serializable number.
Definition STNumber.h:35
friend bool operator!=(OptionalProxy const &lhs, optional_type const &rhs) noexcept
Definition STObject.h:650
friend bool operator==(OptionalProxy const &lhs, optional_type const &rhs) noexcept
Definition STObject.h:614
friend bool operator==(optional_type const &lhs, OptionalProxy const &rhs) noexcept
Definition STObject.h:624
friend bool operator==(OptionalProxy const &lhs, OptionalProxy const &rhs) noexcept
Definition STObject.h:630
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.
Definition STObject.h:839
std::optional< typename std::decay< value_type >::type > optional_type
Definition STObject.h:580
typename T::value_type value_type
Definition STObject.h:578
friend bool operator!=(OptionalProxy const &lhs, std::nullopt_t) noexcept
Definition STObject.h:638
friend bool operator!=(std::nullopt_t, OptionalProxy const &rhs) noexcept
Definition STObject.h:644
OptionalProxy & operator=(OptionalProxy const &)=delete
bool engaged() const noexcept
Definition STObject.h:892
friend bool operator!=(optional_type const &lhs, OptionalProxy const &rhs) noexcept
Definition STObject.h:656
friend bool operator==(std::nullopt_t, OptionalProxy const &rhs) noexcept
Definition STObject.h:608
value_type value_or(value_type val) const
Definition STObject.h:920
optional_type optional_value() const
Definition STObject.h:911
friend bool operator!=(OptionalProxy const &lhs, OptionalProxy const &rhs) noexcept
Definition STObject.h:662
void assign(U &&u)
Definition STObject.h:762
Proxy(Proxy const &)=default
value_type operator*() const
Definition STObject.h:738
T const * operator->() const
Do not use operator->() unless the field is required, or you've checked that it's set.
Definition STObject.h:747
value_type value() const
Definition STObject.h:720
T const * find() const
Definition STObject.h:754
typename T::value_type value_type
Definition STObject.h:480
TypedField< T > const * f_
Definition STObject.h:496
ValueProxy(ValueProxy const &)=default
ValueProxy & operator=(ValueProxy const &)=delete
typename T::value_type value_type
Definition STObject.h:536
ValueProxy & operator+=(U const &u)
ValueProxy & operator-=(U const &u)
friend bool operator==(U const &lhs, STObject::ValueProxy< T > const &rhs)
Definition STObject.h:563
std::enable_if_t< std::is_assignable_v< T, U >, ValueProxy & > operator=(U &&u)
void setFieldU8(SField const &field, unsigned char)
Definition STObject.cpp:723
SField const & getFieldSType(int index) const
Definition STObject.cpp:423
uint192 getFieldH192(SField const &field) const
Definition STObject.cpp:617
bool isFree() const
Definition STObject.h:965
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1055
STBase const * peekAtPIndex(int offset) const
Definition STObject.h:1014
Json::Value getJson(JsonOptions=JsonOptions::none) const override
Definition STObject.cpp:831
STCurrency const & getFieldCurrency(SField const &field) const
Definition STObject.cpp:687
Blob getFieldVL(SField const &field) const
Definition STObject.cpp:641
void addWithoutSigningFields(Serializer &s) const
Definition STObject.h:971
void setFieldIssue(SField const &field, STIssue const &)
Definition STObject.cpp:801
uint128 getFieldH128(SField const &field) const
Definition STObject.cpp:605
iterator begin() const
Definition STObject.h:941
bool operator==(STObject const &o) const
Definition STObject.cpp:844
bool isEquivalent(STBase const &t) const override
Definition STObject.cpp:342
bool empty() const
Definition STObject.h:953
void setFieldNumber(SField const &field, STNumber const &)
Definition STObject.cpp:807
void setFieldV256(SField const &field, STVector256 const &v)
Definition STObject.cpp:765
void setFieldU64(SField const &field, std::uint64_t)
Definition STObject.cpp:741
unsigned char getFieldU8(SField const &field) const
Definition STObject.cpp:581
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:593
STBase const & peekAtIndex(int offset) const
Definition STObject.h:1002
V const & getFieldByConstRef(SField const &field, V const &empty) const
STNumber const & getFieldNumber(SField const &field) const
Definition STObject.cpp:694
OptionalProxy< T > at(OptionaledField< T > const &of)
Return a modifiable field value as std::optional.
void setFieldVL(SField const &field, Blob const &)
Definition STObject.cpp:777
void reserve(std::size_t n)
Definition STObject.h:959
T::value_type operator[](TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1027
std::size_t emplace_back(Args &&... args)
Definition STObject.h:989
iterator end() const
Definition STObject.h:947
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:154
int getFieldIndex(SField const &field) const
Definition STObject.cpp:385
uint256 getHash(HashPrefix prefix) const
Definition STObject.cpp:367
void setFieldI32(SField const &field, std::int32_t)
Definition STObject.cpp:759
virtual ~STObject()=default
std::string getFullText() const override
Definition STObject.cpp:286
SOTemplate const * mType
Definition STObject.h:60
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:735
list_type v_
Definition STObject.h:59
STArray & peekFieldArray(SField const &field)
Definition STObject.cpp:473
std::string getText() const override
Definition STObject.cpp:323
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:680
void setFieldArray(SField const &field, STArray const &v)
Definition STObject.cpp:819
STObject & peekFieldObject(SField const &field)
Definition STObject.cpp:467
SOEStyle getStyle(SField const &field) const
Definition STObject.cpp:575
STBase & getField(SField const &field)
Definition STObject.cpp:412
void setFieldAmount(SField const &field, STAmount const &)
Definition STObject.cpp:789
void add(Serializer &s) const override
Definition STObject.cpp:119
Serializer getSerializer() const
Definition STObject.h:980
bool isFlag(std::uint32_t) const
Definition STObject.cpp:503
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:456
STObject & operator=(STObject const &)=default
STObject(STObject const &)=default
int getCount() const
Definition STObject.h:996
ValueProxy< T > operator[](TypedField< T > const &f)
Get a modifiable field value.
SerializedTypeID getSType() const override
Definition STObject.cpp:107
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:729
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:623
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:73
std::int32_t getFieldI32(SField const &field) const
Definition STObject.cpp:629
STBase const & peekAtField(SField const &field) const
Definition STObject.cpp:401
void set(SOTemplate const &)
Definition STObject.cpp:134
STObject(SOTemplate const &type, SField const &name, F &&f)
Definition STObject.h:69
uint256 getSigningHash(HashPrefix prefix) const
Definition STObject.cpp:376
bool clearFlag(std::uint32_t)
Definition STObject.cpp:491
void setFieldCurrency(SField const &field, STCurrency const &)
Definition STObject.cpp:795
std::uint64_t getFieldU64(SField const &field) const
Definition STObject.cpp:599
void setFieldPathSet(SField const &field, STPathSet const &)
Definition STObject.cpp:813
STBase * move(std::size_t n, void *buf) override
Definition STObject.cpp:101
static std::vector< STBase const * > getSortedFields(STObject const &objToSort, WhichFields whichFields)
Definition STObject.cpp:908
STBase * getPField(SField const &field, bool createOkay=false)
Definition STObject.cpp:440
STBase * makeFieldPresent(SField const &field)
Definition STObject.cpp:520
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:429
void applyTemplateFromSField(SField const &)
Definition STObject.cpp:204
bool setFlag(std::uint32_t)
Definition STObject.cpp:479
void setFieldObject(SField const &field, STObject const &v)
Definition STObject.cpp:825
bool operator!=(STObject const &o) const
Definition STObject.h:1145
void setFieldH128(SField const &field, uint128 const &)
Definition STObject.cpp:747
STObject getFieldObject(SField const &field) const
Definition STObject.cpp:670
T & peekField(SField const &field)
Definition STObject.h:1246
void setFieldH160(SField const &field, base_uint< 160, Tag > const &v)
Definition STObject.h:1127
void setFieldUsingSetValue(SField const &field, V value)
Definition STObject.h:1202
void setAccountID(SField const &field, AccountID const &)
Definition STObject.cpp:771
void setFieldUsingAssignment(SField const &field, T const &value)
Definition STObject.h:1225
bool delField(SField const &field)
Definition STObject.cpp:557
STBase & getIndex(int offset)
Definition STObject.h:1008
uint160 getFieldH160(SField const &field) const
Definition STObject.cpp:611
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:635
STBase * getPIndex(int offset)
Definition STObject.h:1020
STVector256 const & getFieldV256(SField const &field) const
Definition STObject.cpp:663
STPathSet const & getFieldPathSet(SField const &field) const
Definition STObject.cpp:656
bool isDefault() const override
Definition STObject.cpp:113
V getFieldByValue(SField const &field) const
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:542
ValueProxy< T > at(TypedField< T > const &f)
Get a modifiable field value.
bool hasMatchingEntry(STBase const &) const
Definition STObject.cpp:275
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:753
boost::transform_iterator< Transform, STObject::list_type::const_iterator > iterator
Definition STObject.h:63
OptionalProxy< T > operator[](OptionaledField< T > const &of)
Return a modifiable field value as std::optional.
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:587
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:649
std::uint32_t getFlags() const
Definition STObject.cpp:509
STBase * copy(std::size_t n, void *buf) const override
Definition STObject.cpp:95
An immutable linear range of bytes.
Definition Slice.h:26
STBase & get()
Definition STVar.h:68
T emplace_back(T... args)
T empty(T... args)
T end(T... args)
T is_same_v
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void throwFieldNotFound(SField const &field)
Definition STObject.h:31
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition SOTemplate.h:14
@ soeINVALID
Definition SOTemplate.h:15
@ soeOPTIONAL
Definition SOTemplate.h:17
@ soeDEFAULT
Definition SOTemplate.h:18
@ soeREQUIRED
Definition SOTemplate.h:16
Number operator*(Number const &x, Number const &y)
Definition Number.h:656
SerializedTypeID
Definition SField.h:90
HashPrefix
Prefix for hashing functions.
Definition HashPrefix.h:34
T reserve(T... args)
T runtime_error(T... args)
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
Indicate std::optional field semantics.
Definition SField.h:311
TypedField< T > const * f
Definition SField.h:312
STBase const & operator()(detail::STVar const &e) const
Definition STObject.h:928
A field with a type known at compile time.
Definition SField.h:301