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 <cstddef>
7#include <functional>
8#include <initializer_list>
9#include <stdexcept>
10#include <string>
11#include <vector>
12
13namespace xrpl {
14
18// 2026 usages, 129 files
19// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
22 SoeRequired = 0, // required
23 SoeOptional = 1, // optional, may be present with default value
24 SoeDefault = 2, // optional, if present, must not have default value
25 // inner object with the default fields has to be
26 // constructed with STObject::makeInnerObject()
27};
28
29// Part of a Python-parsed DSL (transactions.macro); bare enumerator names required by the parser
33// NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
35
36//------------------------------------------------------------------------------
37
42{
43 // Use std::reference_wrapper so SOElement can be stored in a std::vector.
47
48private:
49 void
50 init(SField const& fieldName) const
51 {
52 if (!sField_.get().isUseful())
53 {
54 auto nm = std::to_string(fieldName.getCode());
55 if (fieldName.hasName())
56 nm += ": '" + fieldName.getName() + "'";
57 Throw<std::runtime_error>("SField (" + nm + ") in SOElement must be useful.");
58 }
59 }
60
61public:
62 SOElement(SField const& fieldName, SOEStyle style) : sField_(fieldName), style_(style)
63 {
64 init(fieldName);
65 }
66
67 template <typename T>
70 TypedField<T> const& fieldName,
73 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
74 {
75 init(fieldName);
76 }
77
78 [[nodiscard]] SField const&
79 sField() const
80 {
81 return sField_.get();
82 }
83
84 [[nodiscard]] SOEStyle
85 style() const
86 {
87 return style_;
88 }
89
90 [[nodiscard]] SOETxMPTIssue
91 supportMPT() const
92 {
93 return supportMpt_;
94 }
95};
96
97//------------------------------------------------------------------------------
98
105{
106public:
107 // Copying vectors is expensive. Make this a move-only type until
108 // there is motivation to change that.
109 SOTemplate(SOTemplate&& other) = default;
111 operator=(SOTemplate&& other) = default;
112
117 SOTemplate(std::vector<SOElement> uniqueFields, std::vector<SOElement> commonFields = {});
118
125 std::initializer_list<SOElement> commonFields = {});
126
127 /* Provide for the enumeration of fields */
128 [[nodiscard]] std::vector<SOElement>::const_iterator
129 begin() const
130 {
131 return elements_.cbegin();
132 }
133
134 [[nodiscard]] std::vector<SOElement>::const_iterator
135 cbegin() const
136 {
137 return begin();
138 }
139
140 [[nodiscard]] std::vector<SOElement>::const_iterator
141 end() const
142 {
143 return elements_.cend();
144 }
145
146 [[nodiscard]] std::vector<SOElement>::const_iterator
147 cend() const
148 {
149 return end();
150 }
151
155 [[nodiscard]] std::size_t
156 size() const
157 {
158 return elements_.size();
159 }
160
164 [[nodiscard]] int
165 getIndex(SField const&) const;
166
167 [[nodiscard]] SOEStyle
168 style(SField const& sf) const
169 {
170 return elements_[indices_[sf.getNum()]].style();
171 }
172
173private:
175 std::vector<int> indices_; // field num -> index
176};
177
178} // namespace xrpl
Identifies fields.
Definition SField.h:132
bool hasName() const
Definition SField.h:204
int getCode() const
Definition SField.h:249
int getNum() const
Definition SField.h:254
std::string const & getName() const
Definition SField.h:198
void init(SField const &fieldName) const
Definition SOTemplate.h:50
SOEStyle style_
Definition SOTemplate.h:45
SOETxMPTIssue supportMPT() const
Definition SOTemplate.h:91
SOETxMPTIssue supportMpt_
Definition SOTemplate.h:46
SField const & sField() const
Definition SOTemplate.h:79
std::reference_wrapper< SField const > sField_
Definition SOTemplate.h:44
SOEStyle style() const
Definition SOTemplate.h:85
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=SoeMptNotSupported)
Definition SOTemplate.h:69
SOElement(SField const &fieldName, SOEStyle style)
Definition SOTemplate.h:62
std::vector< int > indices_
Definition SOTemplate.h:175
int getIndex(SField const &) const
Retrieve the position of a named field.
std::vector< SOElement >::const_iterator end() const
Definition SOTemplate.h:141
SOTemplate(SOTemplate &&other)=default
std::vector< SOElement >::const_iterator cend() const
Definition SOTemplate.h:147
SOEStyle style(SField const &sf) const
Definition SOTemplate.h:168
std::size_t size() const
The number of entries in this template.
Definition SOTemplate.h:156
std::vector< SOElement >::const_iterator cbegin() const
Definition SOTemplate.h:135
std::vector< SOElement >::const_iterator begin() const
Definition SOTemplate.h:129
std::vector< SOElement > elements_
Definition SOTemplate.h:174
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:20
@ SoeDefault
Definition SOTemplate.h:24
@ SoeInvalid
Definition SOTemplate.h:21
@ SoeOptional
Definition SOTemplate.h:23
@ SoeRequired
Definition SOTemplate.h:22
SOETxMPTIssue
Amount fields that can support MPT.
Definition SOTemplate.h:34
@ SoeMptNotSupported
Definition SOTemplate.h:34
@ SoeMptNone
Definition SOTemplate.h:34
@ SoeMptSupported
Definition SOTemplate.h:34
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
A field with a type known at compile time.
Definition SField.h:308
T to_string(T... args)