xrpld
Loading...
Searching...
No Matches
SOTemplate.h
1#pragma once
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/protocol/SField.h>
5
6#include <functional>
7#include <initializer_list>
8#include <stdexcept>
9#include <vector>
10
11namespace xrpl {
12
14// 2026 usages, 129 files
15// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
18 SoeRequired = 0, // required
19 SoeOptional = 1, // optional, may be present with default value
20 SoeDefault = 2, // optional, if present, must not have default value
21 // inner object with the default fields has to be
22 // constructed with STObject::makeInnerObject()
23};
24
25// Part of a Python-parsed DSL (transactions.macro); bare enumerator names required by the parser
27// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
29
30//------------------------------------------------------------------------------
31
34{
35 // Use std::reference_wrapper so SOElement can be stored in a std::vector.
39
40private:
41 void
42 init(SField const& fieldName) const
43 {
44 if (!sField_.get().isUseful())
45 {
46 auto nm = std::to_string(fieldName.getCode());
47 if (fieldName.hasName())
48 nm += ": '" + fieldName.getName() + "'";
49 Throw<std::runtime_error>("SField (" + nm + ") in SOElement must be useful.");
50 }
51 }
52
53public:
54 SOElement(SField const& fieldName, SOEStyle style) : sField_(fieldName), style_(style)
55 {
56 init(fieldName);
57 }
58
59 template <typename T>
62 TypedField<T> const& fieldName,
65 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
66 {
67 init(fieldName);
68 }
69
70 [[nodiscard]] SField const&
71 sField() const
72 {
73 return sField_.get();
74 }
75
76 [[nodiscard]] SOEStyle
77 style() const
78 {
79 return style_;
80 }
81
82 [[nodiscard]] SOETxMPTIssue
83 supportMPT() const
84 {
85 return supportMpt_;
86 }
87};
88
89//------------------------------------------------------------------------------
90
96{
97public:
98 // Copying vectors is expensive. Make this a move-only type until
99 // there is motivation to change that.
100 SOTemplate(SOTemplate&& other) = default;
102 operator=(SOTemplate&& other) = default;
103
107 SOTemplate(std::vector<SOElement> uniqueFields, std::vector<SOElement> commonFields = {});
108
114 std::initializer_list<SOElement> commonFields = {});
115
116 /* Provide for the enumeration of fields */
117 [[nodiscard]] std::vector<SOElement>::const_iterator
118 begin() const
119 {
120 return elements_.cbegin();
121 }
122
123 [[nodiscard]] std::vector<SOElement>::const_iterator
124 cbegin() const
125 {
126 return begin();
127 }
128
129 [[nodiscard]] std::vector<SOElement>::const_iterator
130 end() const
131 {
132 return elements_.cend();
133 }
134
135 [[nodiscard]] std::vector<SOElement>::const_iterator
136 cend() const
137 {
138 return end();
139 }
140
142 [[nodiscard]] std::size_t
143 size() const
144 {
145 return elements_.size();
146 }
147
149 [[nodiscard]] int
150 getIndex(SField const&) const;
151
152 [[nodiscard]] SOEStyle
153 style(SField const& sf) const
154 {
155 return elements_[indices_[sf.getNum()]].style();
156 }
157
158private:
160 std::vector<int> indices_; // field num -> index
161};
162
163} // namespace xrpl
Identifies fields.
Definition SField.h:130
bool hasName() const
Definition SField.h:202
int getCode() const
Definition SField.h:247
int getNum() const
Definition SField.h:252
std::string const & getName() const
Definition SField.h:196
void init(SField const &fieldName) const
Definition SOTemplate.h:42
SOEStyle style_
Definition SOTemplate.h:37
SOETxMPTIssue supportMPT() const
Definition SOTemplate.h:83
SOETxMPTIssue supportMpt_
Definition SOTemplate.h:38
SField const & sField() const
Definition SOTemplate.h:71
std::reference_wrapper< SField const > sField_
Definition SOTemplate.h:36
SOEStyle style() const
Definition SOTemplate.h:77
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=SoeMptNotSupported)
Definition SOTemplate.h:61
SOElement(SField const &fieldName, SOEStyle style)
Definition SOTemplate.h:54
std::vector< int > indices_
Definition SOTemplate.h:160
int getIndex(SField const &) const
Retrieve the position of a named field.
std::vector< SOElement >::const_iterator end() const
Definition SOTemplate.h:130
SOTemplate(SOTemplate &&other)=default
std::vector< SOElement >::const_iterator cend() const
Definition SOTemplate.h:136
SOEStyle style(SField const &sf) const
Definition SOTemplate.h:153
std::size_t size() const
The number of entries in this template.
Definition SOTemplate.h:143
std::vector< SOElement >::const_iterator cbegin() const
Definition SOTemplate.h:124
std::vector< SOElement >::const_iterator begin() const
Definition SOTemplate.h:118
std::vector< SOElement > elements_
Definition SOTemplate.h:159
SOTemplate & operator=(SOTemplate &&other)=default
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition SOTemplate.h:16
@ SoeDefault
Definition SOTemplate.h:20
@ SoeInvalid
Definition SOTemplate.h:17
@ SoeOptional
Definition SOTemplate.h:19
@ SoeRequired
Definition SOTemplate.h:18
SOETxMPTIssue
Amount fields that can support MPT.
Definition SOTemplate.h:28
@ SoeMptNotSupported
Definition SOTemplate.h:28
@ SoeMptNone
Definition SOTemplate.h:28
@ SoeMptSupported
Definition SOTemplate.h:28
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
A field with a type known at compile time.
Definition SField.h:304
T to_string(T... args)