xrpld
Loading...
Searching...
No Matches
STObject.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/Number.h>
6#include <xrpl/basics/Slice.h>
7#include <xrpl/basics/base_uint.h>
8#include <xrpl/basics/contract.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/json/json_value.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/HashPrefix.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/SOTemplate.h>
15#include <xrpl/protocol/STAccount.h> // IWYU pragma: keep
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STBase.h>
18#include <xrpl/protocol/STBitString.h>
19#include <xrpl/protocol/STCurrency.h>
20#include <xrpl/protocol/STIssue.h>
21#include <xrpl/protocol/STPathSet.h>
22#include <xrpl/protocol/STVector256.h>
23#include <xrpl/protocol/Serializer.h>
24#include <xrpl/protocol/Units.h>
25#include <xrpl/protocol/detail/STVar.h>
26
27#include <boost/iterator/transform_iterator.hpp>
28
29#include <cstddef>
30#include <cstdint>
31#include <memory>
32#include <optional>
33#include <stdexcept>
34#include <string>
35#include <type_traits>
36#include <utility>
37#include <vector>
38
39namespace xrpl {
40
41class STArray;
42
43inline void
45{
46 Throw<std::runtime_error>("Field not found: " + field.getName());
47}
48
49class STObject : public STBase, public CountedObject<STObject>
50{
51 // Proxy value for a STBase derived class
52 template <class T>
53 class Proxy;
54 template <class T>
55 class ValueProxy;
56 template <class T>
57 class OptionalProxy;
58
59 struct Transform
60 {
61 explicit Transform() = default;
62
65
66 STBase const&
67 operator()(detail::STVar const& e) const;
68 };
69
71
73 SOTemplate const* type_{};
74
75public:
76 using iterator = boost::transform_iterator<Transform, STObject::ListType::const_iterator>;
77
78 ~STObject() override = default;
79 STObject(STObject const&) = default;
80
81 template <typename F>
82 STObject(SOTemplate const& type, SField const& name, F&& f) : STObject(type, name)
83 {
84 f(*this);
85 }
86
88 operator=(STObject const&) = default;
91 operator=(STObject&& other);
92
93 STObject(SOTemplate const& type, SField const& name);
95 SOTemplate const& type,
96 SerialIter& sit,
97 SField const& name,
98 bool requireCanonicalOrder = false);
99 STObject(SerialIter& sit, SField const& name, int depth = 0);
100 STObject(SerialIter&& sit, SField const& name);
101 explicit STObject(SField const& name);
102
103 static STObject
104 makeInnerObject(SField const& name);
105
106 [[nodiscard]] iterator
107 begin() const;
108
109 [[nodiscard]] iterator
110 end() const;
111
112 [[nodiscard]] bool
113 empty() const;
114
115 void
117
118 void
119 applyTemplate(SOTemplate const& type);
120
121 void
123
124 [[nodiscard]] bool
125 isFree() const;
126
127 void
128 set(SOTemplate const&);
129
130 bool
131 set(SerialIter& u, int depth = 0, bool requireCanonicalOrder = false);
132
133 [[nodiscard]] SerializedTypeID
134 getSType() const override;
135
136 [[nodiscard]] bool
137 isEquivalent(STBase const& t) const override;
138
139 [[nodiscard]] bool
140 isDefault() const override;
141
142 void
143 add(Serializer& s) const override;
144
145 [[nodiscard]] std::string
146 getFullText() const override;
147
148 [[nodiscard]] std::string
149 getText() const override;
150
151 // TODO(tom): options should be an enum.
152 [[nodiscard]] json::Value getJson(JsonOptions = JsonOptions::Values::None) const override;
153
154 void
156
157 [[nodiscard]] Serializer
158 getSerializer() const;
159
160 template <class... Args>
162 emplaceBack(Args&&... args);
163
164 [[nodiscard]] int
165 getCount() const;
166
167 bool
169 bool
171 [[nodiscard]] bool
172 isFlag(std::uint32_t) const;
173
174 [[nodiscard]] std::uint32_t
175 getFlags() const;
176
177 [[nodiscard]] UInt256
178 getHash(HashPrefix prefix) const;
179
180 [[nodiscard]] UInt256
181 getSigningHash(HashPrefix prefix) const;
182
183 [[nodiscard]] STBase const&
184 peekAtIndex(int offset) const;
185
186 STBase&
187 getIndex(int offset);
188
189 [[nodiscard]] STBase const*
190 peekAtPIndex(int offset) const;
191
192 STBase*
193 getPIndex(int offset);
194
195 [[nodiscard]] int
196 getFieldIndex(SField const& field) const;
197
198 [[nodiscard]] SField const&
199 getFieldSType(int index) const;
200
201 [[nodiscard]] STBase const&
202 peekAtField(SField const& field) const;
203
204 STBase&
205 getField(SField const& field);
206
207 [[nodiscard]] STBase const*
208 peekAtPField(SField const& field) const;
209
210 STBase*
211 getPField(SField const& field, bool createOkay = false);
212
213 // these throw if the field type doesn't match, or return default values
214 // if the field is optional but not present
215 [[nodiscard]] unsigned char
216 getFieldU8(SField const& field) const;
217 [[nodiscard]] std::uint16_t
218 getFieldU16(SField const& field) const;
219 [[nodiscard]] std::uint32_t
220 getFieldU32(SField const& field) const;
221 [[nodiscard]] std::uint64_t
222 getFieldU64(SField const& field) const;
223 [[nodiscard]] UInt128
224 getFieldH128(SField const& field) const;
225
226 [[nodiscard]] UInt160
227 getFieldH160(SField const& field) const;
228 [[nodiscard]] UInt192
229 getFieldH192(SField const& field) const;
230 [[nodiscard]] UInt256
231 getFieldH256(SField const& field) const;
232 [[nodiscard]] std::int32_t
233 getFieldI32(SField const& field) const;
234 [[nodiscard]] AccountID
235 getAccountID(SField const& field) const;
236
237 [[nodiscard]] Blob
238 getFieldVL(SField const& field) const;
239 [[nodiscard]] STAmount const&
240 getFieldAmount(SField const& field) const;
241 [[nodiscard]] STPathSet const&
242 getFieldPathSet(SField const& field) const;
243 [[nodiscard]] STVector256 const&
244 getFieldV256(SField const& field) const;
245 // If not found, returns an object constructed with the given field
246 [[nodiscard]] STObject
247 getFieldObject(SField const& field) const;
248 [[nodiscard]] STArray const&
249 getFieldArray(SField const& field) const;
250 [[nodiscard]] STCurrency const&
251 getFieldCurrency(SField const& field) const;
252 [[nodiscard]] STNumber const&
253 getFieldNumber(SField const& field) const;
254
263 template <class T>
264 T::value_type
265 operator[](TypedField<T> const& f) const;
266
276 template <class T>
278 operator[](OptionaledField<T> const& of) const;
279
288 template <class T>
290 operator[](TypedField<T> const& f);
291
302 template <class T>
305
314 template <class T>
315 [[nodiscard]] T::value_type
316 at(TypedField<T> const& f) const;
317
327 template <class T>
329 at(OptionaledField<T> const& of) const;
330
339 template <class T>
341 at(TypedField<T> const& f);
342
353 template <class T>
355 at(OptionaledField<T> const& of);
356
361 void
363
364 void
365 set(STBase&& v);
366
367 void
368 setFieldU8(SField const& field, unsigned char);
369 void
370 setFieldU16(SField const& field, std::uint16_t);
371 void
372 setFieldU32(SField const& field, std::uint32_t);
373 void
374 setFieldU64(SField const& field, std::uint64_t);
375 void
376 setFieldH128(SField const& field, UInt128 const&);
377 void
378 setFieldH192(SField const& field, UInt192 const&);
379 void
380 setFieldH256(SField const& field, UInt256 const&);
381 void
382 setFieldI32(SField const& field, std::int32_t);
383 void
384 setFieldVL(SField const& field, Blob const&);
385 void
386 setFieldVL(SField const& field, Slice const&);
387
388 void
389 setAccountID(SField const& field, AccountID const&);
390
391 void
392 setFieldAmount(SField const& field, STAmount const&);
393 void
394 setFieldIssue(SField const& field, STIssue const&);
395 void
396 setFieldCurrency(SField const& field, STCurrency const&);
397 void
398 setFieldNumber(SField const& field, STNumber const&);
399 void
400 setFieldPathSet(SField const& field, STPathSet const&);
401 void
402 setFieldV256(SField const& field, STVector256 const& v);
403 void
404 setFieldArray(SField const& field, STArray const& v);
405 void
406 setFieldObject(SField const& field, STObject const& v);
407
408 template <class Tag>
409 void
410 setFieldH160(SField const& field, BaseUInt<160, Tag> const& v);
411
412 STObject&
413 peekFieldObject(SField const& field);
414 STArray&
415 peekFieldArray(SField const& field);
416
417 [[nodiscard]] bool
418 isFieldPresent(SField const& field) const;
419 STBase*
420 makeFieldPresent(SField const& field);
421 void
422 makeFieldAbsent(SField const& field);
423 bool
424 delField(SField const& field);
425 void
426 delField(int index);
427
428 [[nodiscard]] SOEStyle
429 getStyle(SField const& field) const;
430
431 [[nodiscard]] bool
432 hasMatchingEntry(STBase const&) const;
433
434 bool
435 operator==(STObject const& o) const;
436
437 class FieldErr;
438
439private:
440 enum class WhichFields : bool {
441 // These values are carefully chosen to do the right thing if passed
442 // to SField::shouldInclude (bool)
445 };
446
447 void
448 add(Serializer& s, WhichFields whichFields) const;
449
450 // Sort the entries in an STObject into the order that they will be
451 // serialized. Note: they are not sorted into pointer value order, they
452 // are sorted by SField::fieldCode.
454 getSortedFields(STObject const& objToSort, WhichFields whichFields);
455
456 // Implementation for getting (most) fields that return by value.
457 //
458 // The remove_cv and remove_reference are necessitated by the STBitString
459 // types. Their value() returns by const ref. We return those types
460 // by value.
461 template <typename T, typename V = std::remove_cvref_t<decltype(std::declval<T>().value())>>
462 V
463 getFieldByValue(SField const& field) const;
464
465 // Implementations for getting (most) fields that return by const reference.
466 //
467 // If an absent optional field is deserialized we don't have anything
468 // obvious to return. So we insist on having the call provide an
469 // 'empty' value we return in that circumstance.
470 template <typename T, typename V>
471 V const&
472 getFieldByConstRef(SField const& field, V const& empty) const;
473
474 // Implementation for setting most fields with a setValue() method.
475 template <typename T, typename V>
476 void
477 setFieldUsingSetValue(SField const& field, V value);
478
479 // Implementation for setting fields using assignment
480 template <typename T>
481 void
482 setFieldUsingAssignment(SField const& field, T const& value);
483
484 // Implementation for peeking STObjects and STArrays
485 template <typename T>
486 T&
487 peekField(SField const& field);
488
489 STBase*
490 copy(std::size_t n, void* buf) const override;
491 STBase*
492 move(std::size_t n, void* buf) override;
493
494 friend class detail::STVar;
495};
496
497//------------------------------------------------------------------------------
498
499template <class T>
501{
502public:
503 using value_type = T::value_type;
504
505 [[nodiscard]] value_type
506 value() const;
507
509 operator*() const;
510
515 T const*
516 operator->() const;
517
518protected:
522
523 Proxy(Proxy const&) = default;
524
525 Proxy(STObject* st, TypedField<T> const* f);
526
527 [[nodiscard]] T const*
528 find() const;
529
530 template <class U>
531 void
532 assign(U&& u);
533};
534
535// Constraint += and -= ValueProxy operators
536// to value types that support arithmetic operations
537template <typename U>
540template <typename U, typename Value = U::value_type, typename Unit = U::UnitType>
543template <typename U, typename Value = U::value_type>
545template <typename U>
547
548template <class T, class U>
549concept Addable = requires(T t, U u) { t = t + u; };
550template <typename T, typename U>
553
554template <class T>
556{
557private:
558 using value_type = T::value_type;
559
560public:
561 ValueProxy(ValueProxy const&) = default;
563 operator=(ValueProxy const&) = delete;
564
565 // Write-through proxy: assignment sets the referenced field to the given
566 // value, so it intentionally takes the assigned value rather than a
567 // ValueProxy.
568 template <class U>
569 // NOLINTNEXTLINE(misc-unconventional-assign-operator)
573
574 // Convenience operators for value types supporting
575 // arithmetic operations
576 template <IsArithmetic U>
579 operator+=(U const& u);
580
581 template <IsArithmetic U>
584 operator-=(U const& u);
585
586 operator value_type() const;
587
588 template <typename U>
589 friend bool
590 operator==(U const& lhs, STObject::ValueProxy<T> const& rhs)
591 {
592 return rhs.value() == lhs;
593 }
594
595private:
596 friend class STObject;
597
598 ValueProxy(STObject* st, TypedField<T> const* f);
599};
600
601template <class T>
603{
604private:
605 using value_type = T::value_type;
606
608
609public:
610 OptionalProxy(OptionalProxy const&) = default;
612 operator=(OptionalProxy const&) = delete;
613
620 explicit
621 operator bool() const noexcept;
622
623 operator OptionalType() const;
624
629 operator~() const;
630
631 friend bool
632 operator==(OptionalProxy const& lhs, std::nullopt_t) noexcept
633 {
634 return !lhs.engaged();
635 }
636
637 friend bool
639 {
640 return rhs == std::nullopt;
641 }
642
643 friend bool
644 operator==(OptionalProxy const& lhs, OptionalType const& rhs) noexcept
645 {
646 if (!lhs.engaged())
647 return !rhs;
648 if (!rhs)
649 return false;
650 return *lhs == *rhs;
651 }
652
653 friend bool
654 operator==(OptionalType const& lhs, OptionalProxy const& rhs) noexcept
655 {
656 return rhs == lhs;
657 }
658
659 friend bool
660 operator==(OptionalProxy const& lhs, OptionalProxy const& rhs) noexcept
661 {
662 if (lhs.engaged() != rhs.engaged())
663 return false;
664 return !lhs.engaged() || *lhs == *rhs;
665 }
666
667 // Emulate std::optional::value_or
668 [[nodiscard]] value_type
669 valueOr(value_type val) const;
670
674 operator=(OptionalType&& v); // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
676 operator=(OptionalType const& v);
677
678 template <class U>
682
683private:
684 friend class STObject;
685
686 OptionalProxy(STObject* st, TypedField<T> const* f);
687
688 [[nodiscard]] bool
689 engaged() const noexcept;
690
691 void
692 disengage();
693
694 [[nodiscard]] OptionalType
695 optionalValue() const;
696};
697
702
703template <class T>
705{
706 if (st_->type_ != nullptr)
707 {
708 // STObject has associated template
709 if (!st_->peekAtPField(*f_))
710 Throw<STObject::FieldErr>("Template field error '" + this->f_->getName() + "'");
711 style_ = st_->type_->style(*f_);
712 }
713 else
714 {
715 style_ = SoeInvalid;
716 }
717}
718
719template <class T>
720auto
722{
723 auto const t = find();
724 if (t)
725 return t->value();
726 if (style_ == SoeInvalid)
727 {
728 Throw<STObject::FieldErr>("Value requested from invalid STObject.");
729 }
730 if (style_ != SoeDefault)
731 {
732 Throw<STObject::FieldErr>("Missing field '" + this->f_->getName() + "'");
733 }
734 return value_type{};
735}
736
737template <class T>
738auto
740{
741 return this->value();
742}
743
748template <class T>
749T const*
751{
752 return this->find();
753}
754
755template <class T>
756inline T const*
758{
759 return dynamic_cast<T const*>(st_->peekAtPField(*f_));
760}
761
762template <class T>
763template <class U>
764void
766{
767 if (style_ == SoeDefault && u == value_type{})
768 {
769 st_->makeFieldAbsent(*f_);
770 return;
771 }
772 T* t = nullptr;
773 if (style_ == SoeInvalid)
774 {
775 t = dynamic_cast<T*>(st_->getPField(*f_, true));
776 }
777 else
778 {
779 t = dynamic_cast<T*>(st_->makeFieldPresent(*f_));
780 }
781 XRPL_ASSERT(t, "xrpl::STObject::Proxy::assign : type cast succeeded");
782 *t = std::forward<U>(u);
783}
784
785//------------------------------------------------------------------------------
786
787template <class T>
788template <class U>
789// NOLINTNEXTLINE(misc-unconventional-assign-operator)
793{
794 this->assign(std::forward<U>(u));
795 return *this;
796}
797
798template <typename T>
799template <IsArithmetic U>
803{
804 this->assign(this->value() + u);
805 return *this;
806}
807
808template <class T>
809template <IsArithmetic U>
813{
814 this->assign(this->value() - u);
815 return *this;
816}
817
818template <class T>
820operator value_type() const
821{
822 return this->value();
823}
824
825template <class T>
829
830//------------------------------------------------------------------------------
831
832template <class T>
834operator bool() const noexcept
835{
836 return engaged();
837}
838
839template <class T>
841operator typename STObject::OptionalProxy<T>::OptionalType() const
842{
843 return optionalValue();
844}
845
846template <class T>
852
853template <class T>
854auto
860
861template <class T>
862auto
864 OptionalType&& v) // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
865 -> OptionalProxy&
866{
867 if (v)
868 {
869 this->assign(std::move(*v));
870 }
871 else
872 {
873 disengage();
874 }
875 return *this;
876}
877
878template <class T>
879auto
881{
882 if (v)
883 {
884 this->assign(*v);
885 }
886 else
887 {
888 disengage();
889 }
890 return *this;
891}
892
893template <class T>
894template <class U>
898{
899 this->assign(std::forward<U>(u));
900 return *this;
901}
902
903template <class T>
907
908template <class T>
909bool
911{
912 return this->style_ == SoeDefault || this->find() != nullptr;
913}
914
915template <class T>
916void
918{
919 if (this->style_ == SoeRequired || this->style_ == SoeDefault)
920 Throw<STObject::FieldErr>("Template field error '" + this->f_->getName() + "'");
921 if (this->style_ == SoeInvalid)
922 {
923 this->st_->delField(*this->f_);
924 }
925 else
926 {
927 this->st_->makeFieldAbsent(*this->f_);
928 }
929}
930
931template <class T>
932auto
934{
935 if (!engaged())
936 return std::nullopt;
937 return this->value();
938}
939
940template <class T>
943{
944 return engaged() ? this->value() : val;
945}
946
947//------------------------------------------------------------------------------
948
949inline STBase const&
951{
952 return e.get();
953}
954
955//------------------------------------------------------------------------------
956
957// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
958inline STObject::STObject(SerialIter&& sit, SField const& name) : STObject(sit, name)
959{
960}
961
964{
965 return iterator(v_.begin());
966}
967
970{
971 return iterator(v_.end());
972}
973
974inline bool
976{
977 return v_.empty();
978}
979
980inline void
982{
983 v_.reserve(n);
984}
985
986inline bool
988{
989 return type_ == nullptr;
990}
991
992inline void
997
998// VFALCO NOTE does this return an expensive copy of an object with a
999// dynamic buffer?
1000// VFALCO TODO Remove this function and fix the few callers.
1001inline Serializer
1003{
1004 Serializer s;
1006 return s;
1007}
1008
1009template <class... Args>
1010inline std::size_t
1012{
1013 v_.emplace_back(std::forward<Args>(args)...);
1014 return v_.size() - 1;
1015}
1016
1017inline int
1019{
1020 return v_.size();
1021}
1022
1023inline STBase const&
1024STObject::peekAtIndex(int offset) const
1025{
1026 return v_[offset].get();
1027}
1028
1029inline STBase&
1031{
1032 return v_[offset].get();
1033}
1034
1035inline STBase const*
1036STObject::peekAtPIndex(int offset) const
1037{
1038 return &v_[offset].get();
1039}
1040
1041inline STBase*
1043{
1044 return &v_[offset].get();
1045}
1046
1047template <class T>
1048T::value_type
1050{
1051 return at(f);
1052}
1053
1054template <class T>
1057{
1058 return at(of);
1059}
1060
1061template <class T>
1062inline auto
1064{
1065 return at(f);
1066}
1067
1068template <class T>
1069inline auto
1071{
1072 return at(of);
1073}
1074
1075template <class T>
1076[[nodiscard]] T::value_type
1078{
1079 auto const b = peekAtPField(f);
1080 if (!b)
1081 {
1082 // This is a free object (no constraints)
1083 // with no template
1084 Throw<STObject::FieldErr>("Missing field: " + f.getName());
1085 }
1086
1087 if (auto const u = dynamic_cast<T const*>(b))
1088 return u->value();
1089
1090 XRPL_ASSERT(type_, "xrpl::STObject::at(TypedField auto) : field template non-null");
1091 XRPL_ASSERT(
1092 b->getSType() == STI_NOTPRESENT, "xrpl::STObject::at(TypedField auto) : type not present");
1093
1094 if (type_->style(f) == SoeOptional)
1095 Throw<STObject::FieldErr>("Missing optional field: " + f.getName());
1096
1097 XRPL_ASSERT(
1098 type_->style(f) == SoeDefault,
1099 "xrpl::STObject::at(TypedField auto) : template style is default");
1100
1101 // Used to help handle the case where value_type is a const reference,
1102 // otherwise we would return the address of a temporary.
1103 static std::decay_t<typename T::value_type> const kDV{};
1104 return kDV;
1105}
1106
1107template <class T>
1110{
1111 auto const b = peekAtPField(*of.f);
1112 if (!b)
1113 return std::nullopt;
1114 auto const u = dynamic_cast<T const*>(b);
1115 if (!u)
1116 {
1117 XRPL_ASSERT(
1118 type_,
1119 "xrpl::STObject::at(OptionaledField auto) : field template "
1120 "non-null");
1121 XRPL_ASSERT(
1122 b->getSType() == STI_NOTPRESENT,
1123 "xrpl::STObject::at(OptionaledField auto) : type not present");
1124 if (type_->style(*of.f) == SoeOptional)
1125 return std::nullopt;
1126 XRPL_ASSERT(
1127 type_->style(*of.f) == SoeDefault,
1128 "xrpl::STObject::at(OptionaledField auto) : template style is "
1129 "default");
1130 return typename T::value_type{};
1131 }
1132 return u->value();
1133}
1134
1135template <class T>
1136inline auto
1138{
1139 return ValueProxy<T>(this, &f);
1140}
1141
1142template <class T>
1143inline auto
1145{
1146 return OptionalProxy<T>(this, of.f);
1147}
1148
1149template <class Tag>
1150void
1152{
1153 STBase* rf = getPField(field, true);
1154
1155 if (!rf)
1156 throwFieldNotFound(field);
1157
1158 if (rf->getSType() == STI_NOTPRESENT)
1159 rf = makeFieldPresent(field);
1160
1161 using Bits = STBitString<160>;
1162 if (auto cf = dynamic_cast<Bits*>(rf))
1163 {
1164 cf->setValue(v);
1165 }
1166 else
1167 {
1168 Throw<std::runtime_error>("Wrong field type");
1169 }
1170}
1171
1172template <typename T, typename V>
1173V
1175{
1176 STBase const* rf = peekAtPField(field);
1177
1178 if (!rf)
1179 throwFieldNotFound(field);
1180
1181 SerializedTypeID const id = rf->getSType();
1182
1183 if (id == STI_NOTPRESENT)
1184 return V(); // optional field not present
1185
1186 T const* cf = dynamic_cast<T const*>(rf);
1187
1188 if (!cf)
1189 Throw<std::runtime_error>("Wrong field type");
1190
1191 return cf->value();
1192}
1193
1194// Implementations for getting (most) fields that return by const reference.
1195//
1196// If an absent optional field is deserialized we don't have anything
1197// obvious to return. So we insist on having the call provide an
1198// 'empty' value we return in that circumstance.
1199template <typename T, typename V>
1200V const&
1201STObject::getFieldByConstRef(SField const& field, V const& empty) const
1202{
1203 STBase const* rf = peekAtPField(field);
1204
1205 if (!rf)
1206 throwFieldNotFound(field);
1207
1208 SerializedTypeID const id = rf->getSType();
1209
1210 if (id == STI_NOTPRESENT)
1211 {
1212 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
1213 return empty; // optional field not present
1214 }
1215
1216 T const* cf = dynamic_cast<T const*>(rf);
1217
1218 if (!cf)
1219 Throw<std::runtime_error>("Wrong field type");
1220
1221 return *cf;
1222}
1223
1224// Implementation for setting most fields with a setValue() method.
1225template <typename T, typename V>
1226void
1228{
1229 static_assert(!std::is_lvalue_reference_v<V>);
1230
1231 STBase* rf = getPField(field, true);
1232
1233 if (!rf)
1234 throwFieldNotFound(field);
1235
1236 if (rf->getSType() == STI_NOTPRESENT)
1237 rf = makeFieldPresent(field);
1238
1239 T* cf = dynamic_cast<T*>(rf);
1240
1241 if (!cf)
1242 Throw<std::runtime_error>("Wrong field type");
1243
1244 cf->setValue(std::move(value));
1245}
1246
1247// Implementation for setting fields using assignment
1248template <typename T>
1249void
1250STObject::setFieldUsingAssignment(SField const& field, T const& value)
1251{
1252 STBase* rf = getPField(field, true);
1253
1254 if (!rf)
1255 throwFieldNotFound(field);
1256
1257 if (rf->getSType() == STI_NOTPRESENT)
1258 rf = makeFieldPresent(field);
1259
1260 T* cf = dynamic_cast<T*>(rf);
1261
1262 if (!cf)
1263 Throw<std::runtime_error>("Wrong field type");
1264
1265 (*cf) = value;
1266}
1267
1268// Implementation for peeking STObjects and STArrays
1269template <typename T>
1270T&
1272{
1273 STBase* rf = getPField(field, true);
1274
1275 if (!rf)
1276 throwFieldNotFound(field);
1277
1278 if (rf->getSType() == STI_NOTPRESENT)
1279 rf = makeFieldPresent(field);
1280
1281 T* cf = dynamic_cast<T*>(rf);
1282
1283 if (!cf)
1284 Throw<std::runtime_error>("Wrong field type");
1285
1286 return *cf;
1287}
1288
1289} // namespace xrpl
Represents a JSON value.
Definition json_value.h:121
Integers of any length that is a multiple of 32-bits.
Definition base_uint.h:82
Identifies fields.
Definition SField.h:131
std::string const & getName() const
Definition SField.h:197
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:105
A type which can be exported to a well known binary format.
Definition STBase.h:129
virtual SerializedTypeID getSType() const
Definition STBase.cpp:54
A serializable number.
Definition STNumber.h:42
T runtime_error(T... args)
OptionalType operator~() const
Explicit conversion to std::optional.
Definition STObject.h:848
friend bool operator==(OptionalProxy const &lhs, OptionalProxy const &rhs) noexcept
Definition STObject.h:660
OptionalProxy(OptionalProxy const &)=default
value_type valueOr(value_type val) const
Definition STObject.h:942
OptionalProxy & operator=(U &&u)
std::optional< std::decay_t< value_type > > OptionalType
Definition STObject.h:607
OptionalProxy & operator=(OptionalProxy const &)=delete
OptionalType optionalValue() const
Definition STObject.h:933
friend bool operator==(OptionalType const &lhs, OptionalProxy const &rhs) noexcept
Definition STObject.h:654
bool engaged() const noexcept
Definition STObject.h:910
friend bool operator==(OptionalProxy const &lhs, OptionalType const &rhs) noexcept
Definition STObject.h:644
friend bool operator==(std::nullopt_t, OptionalProxy const &rhs) noexcept
Definition STObject.h:638
void assign(U &&u)
Definition STObject.h:765
Proxy(Proxy const &)=default
T::value_type value_type
Definition STObject.h:503
value_type operator*() const
Definition STObject.h:739
T const * operator->() const
Do not use operator->() unless the field is required, or you've checked that it's set.
Definition STObject.h:750
value_type value() const
Definition STObject.h:721
T const * find() const
Definition STObject.h:757
TypedField< T > const * f_
Definition STObject.h:521
ValueProxy(ValueProxy const &)=default
ValueProxy & operator=(ValueProxy const &)=delete
ValueProxy & operator=(U &&u)
ValueProxy & operator+=(U const &u)
ValueProxy & operator-=(U const &u)
friend bool operator==(U const &lhs, STObject::ValueProxy< T > const &rhs)
Definition STObject.h:590
T::value_type value_type
Definition STObject.h:558
void setFieldU8(SField const &field, unsigned char)
Definition STObject.cpp:731
SField const & getFieldSType(int index) const
Definition STObject.cpp:431
bool isFree() const
Definition STObject.h:987
T::value_type at(TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1077
STBase const * peekAtPIndex(int offset) const
Definition STObject.h:1036
STCurrency const & getFieldCurrency(SField const &field) const
Definition STObject.cpp:695
Blob getFieldVL(SField const &field) const
Definition STObject.cpp:649
void addWithoutSigningFields(Serializer &s) const
Definition STObject.h:993
void setFieldIssue(SField const &field, STIssue const &)
Definition STObject.cpp:815
iterator begin() const
Definition STObject.h:963
bool operator==(STObject const &o) const
Definition STObject.cpp:858
bool isEquivalent(STBase const &t) const override
Definition STObject.cpp:351
UInt256 getFieldH256(SField const &field) const
Definition STObject.cpp:631
bool empty() const
Definition STObject.h:975
void setFieldNumber(SField const &field, STNumber const &)
Definition STObject.cpp:821
void setFieldV256(SField const &field, STVector256 const &v)
Definition STObject.cpp:779
UInt160 getFieldH160(SField const &field) const
Definition STObject.cpp:619
boost::transform_iterator< Transform, STObject::ListType::const_iterator > iterator
Definition STObject.h:76
void setFieldU64(SField const &field, std::uint64_t)
Definition STObject.cpp:749
UInt256 getSigningHash(HashPrefix prefix) const
Definition STObject.cpp:384
unsigned char getFieldU8(SField const &field) const
Definition STObject.cpp:589
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
STBase const & peekAtIndex(int offset) const
Definition STObject.h:1024
V const & getFieldByConstRef(SField const &field, V const &empty) const
STNumber const & getFieldNumber(SField const &field) const
Definition STObject.cpp:702
void setFieldVL(SField const &field, Blob const &)
Definition STObject.cpp:791
void reserve(std::size_t n)
Definition STObject.h:981
T::value_type operator[](TypedField< T > const &f) const
Get the value of a field.
Definition STObject.h:1049
UInt192 getFieldH192(SField const &field) const
Definition STObject.cpp:625
iterator end() const
Definition STObject.h:969
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:158
int getFieldIndex(SField const &field) const
Definition STObject.cpp:393
void setFieldI32(SField const &field, std::int32_t)
Definition STObject.cpp:773
std::string getFullText() const override
Definition STObject.cpp:295
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:743
STArray & peekFieldArray(SField const &field)
Definition STObject.cpp:481
std::string getText() const override
Definition STObject.cpp:332
~STObject() override=default
std::size_t emplaceBack(Args &&... args)
Definition STObject.h:1011
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:688
void setFieldArray(SField const &field, STArray const &v)
Definition STObject.cpp:833
STObject & peekFieldObject(SField const &field)
Definition STObject.cpp:475
SOEStyle getStyle(SField const &field) const
Definition STObject.cpp:583
STBase & getField(SField const &field)
Definition STObject.cpp:420
void setFieldAmount(SField const &field, STAmount const &)
Definition STObject.cpp:803
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
Definition STObject.cpp:845
void add(Serializer &s) const override
Definition STObject.cpp:123
Serializer getSerializer() const
Definition STObject.h:1002
bool isFlag(std::uint32_t) const
Definition STObject.cpp:511
bool isFieldPresent(SField const &field) const
Definition STObject.cpp:464
STObject & operator=(STObject const &)=default
STObject(STObject const &)=default
int getCount() const
Definition STObject.h:1018
SerializedTypeID getSType() const override
Definition STObject.cpp:111
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:737
static STObject makeInnerObject(SField const &name)
Definition STObject.cpp:79
SOTemplate const * type_
Definition STObject.h:73
std::int32_t getFieldI32(SField const &field) const
Definition STObject.cpp:637
STBase const & peekAtField(SField const &field) const
Definition STObject.cpp:409
void set(SOTemplate const &)
Definition STObject.cpp:138
STObject(SOTemplate const &type, SField const &name, F &&f)
Definition STObject.h:82
bool clearFlag(std::uint32_t)
Definition STObject.cpp:499
void setFieldH128(SField const &field, UInt128 const &)
Definition STObject.cpp:755
void setFieldH192(SField const &field, UInt192 const &)
Definition STObject.cpp:761
void setFieldCurrency(SField const &field, STCurrency const &)
Definition STObject.cpp:809
std::uint64_t getFieldU64(SField const &field) const
Definition STObject.cpp:607
void setFieldPathSet(SField const &field, STPathSet const &)
Definition STObject.cpp:827
UInt256 getHash(HashPrefix prefix) const
Definition STObject.cpp:375
STBase * move(std::size_t n, void *buf) override
Definition STObject.cpp:105
static std::vector< STBase const * > getSortedFields(STObject const &objToSort, WhichFields whichFields)
Definition STObject.cpp:926
STBase * getPField(SField const &field, bool createOkay=false)
Definition STObject.cpp:448
STBase * makeFieldPresent(SField const &field)
Definition STObject.cpp:528
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:437
void applyTemplateFromSField(SField const &)
Definition STObject.cpp:207
bool setFlag(std::uint32_t)
Definition STObject.cpp:487
void setFieldObject(SField const &field, STObject const &v)
Definition STObject.cpp:839
STObject getFieldObject(SField const &field) const
Definition STObject.cpp:678
T & peekField(SField const &field)
Definition STObject.h:1271
void setFieldUsingSetValue(SField const &field, V value)
Definition STObject.h:1227
void setAccountID(SField const &field, AccountID const &)
Definition STObject.cpp:785
void setFieldUsingAssignment(SField const &field, T const &value)
Definition STObject.h:1250
bool delField(SField const &field)
Definition STObject.cpp:565
STBase & getIndex(int offset)
Definition STObject.h:1030
void setFieldH256(SField const &field, UInt256 const &)
Definition STObject.cpp:767
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
STBase * getPIndex(int offset)
Definition STObject.h:1042
V getFieldByValue(SField const &field) const
Definition STObject.h:1174
STVector256 const & getFieldV256(SField const &field) const
Definition STObject.cpp:671
STPathSet const & getFieldPathSet(SField const &field) const
Definition STObject.cpp:664
ListType v_
Definition STObject.h:72
bool isDefault() const override
Definition STObject.cpp:117
UInt128 getFieldH128(SField const &field) const
Definition STObject.cpp:613
void setFieldH160(SField const &field, BaseUInt< 160, Tag > const &v)
Definition STObject.h:1151
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:550
bool hasMatchingEntry(STBase const &) const
Definition STObject.cpp:284
std::vector< detail::STVar > ListType
Definition STObject.h:70
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:595
STAmount const & getFieldAmount(SField const &field) const
Definition STObject.cpp:657
std::uint32_t getFlags() const
Definition STObject.cpp:517
STBase * copy(std::size_t n, void *buf) const override
Definition STObject.cpp:99
An immutable linear range of bytes.
Definition Slice.h:28
STBase & get()
Definition STVar.h:68
T forward(T... args)
T is_arithmetic_v
T is_assignable_v
T is_class_v
T is_lvalue_reference_v
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:44
BaseUInt< 128 > UInt128
Definition base_uint.h:579
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition SOTemplate.h:20
@ SoeDefault
Definition SOTemplate.h:24
@ SoeInvalid
Definition SOTemplate.h:21
@ SoeOptional
Definition SOTemplate.h:23
@ SoeRequired
Definition SOTemplate.h:22
BaseUInt< 160 > UInt160
Definition base_uint.h:580
BaseUInt< 192 > UInt192
Definition base_uint.h:582
BaseUInt< 256 > UInt256
Definition base_uint.h:581
SerializedTypeID
Definition SField.h:93
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
HashPrefix
Prefix for hashing functions.
Definition HashPrefix.h:35
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T runtime_error(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:22
Indicate std::optional field semantics.
Definition SField.h:319
TypedField< T > const * f
Definition SField.h:320
STBase const & operator()(detail::STVar const &e) const
Definition STObject.h:950
detail::STVar argument_type
Definition STObject.h:63
A field with a type known at compile time.
Definition SField.h:307