rippled
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
16 soeREQUIRED = 0, // required
17 soeOPTIONAL = 1, // optional, may be present with default value
18 soeDEFAULT = 2, // optional, if present, must not have default value
19 // inner object with the default fields has to be
20 // constructed with STObject::makeInnerObject()
21};
22
25
26//------------------------------------------------------------------------------
27
30{
31 // Use std::reference_wrapper so SOElement can be stored in a std::vector.
35
36private:
37 void
38 init(SField const& fieldName) const
39 {
40 if (!sField_.get().isUseful())
41 {
42 auto nm = std::to_string(fieldName.getCode());
43 if (fieldName.hasName())
44 nm += ": '" + fieldName.getName() + "'";
45 Throw<std::runtime_error>("SField (" + nm + ") in SOElement must be useful.");
46 }
47 }
48
49public:
50 SOElement(SField const& fieldName, SOEStyle style) : sField_(fieldName), style_(style)
51 {
52 init(fieldName);
53 }
54
55 template <typename T>
58 TypedField<T> const& fieldName,
61 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
62 {
63 init(fieldName);
64 }
65
66 SField const&
67 sField() const
68 {
69 return sField_.get();
70 }
71
73 style() const
74 {
75 return style_;
76 }
77
79 supportMPT() const
80 {
81 return supportMpt_;
82 }
83};
84
85//------------------------------------------------------------------------------
86
92{
93public:
94 // Copying vectors is expensive. Make this a move-only type until
95 // there is motivation to change that.
96 SOTemplate(SOTemplate&& other) = default;
98 operator=(SOTemplate&& other) = default;
99
103 SOTemplate(std::vector<SOElement> uniqueFields, std::vector<SOElement> commonFields = {});
104
110 std::initializer_list<SOElement> commonFields = {});
111
112 /* Provide for the enumeration of fields */
114 begin() const
115 {
116 return elements_.cbegin();
117 }
118
120 cbegin() const
121 {
122 return begin();
123 }
124
126 end() const
127 {
128 return elements_.cend();
129 }
130
132 cend() const
133 {
134 return end();
135 }
136
139 size() const
140 {
141 return elements_.size();
142 }
143
145 int
146 getIndex(SField const&) const;
147
149 style(SField const& sf) const
150 {
151 return elements_[indices_[sf.getNum()]].style();
152 }
153
154private:
156 std::vector<int> indices_; // field num -> index
157};
158
159} // namespace xrpl
Identifies fields.
Definition SField.h:126
bool hasName() const
Definition SField.h:199
int getCode() const
Definition SField.h:244
int getNum() const
Definition SField.h:249
std::string const & getName() const
Definition SField.h:193
An element in a SOTemplate.
Definition SOTemplate.h:30
void init(SField const &fieldName) const
Definition SOTemplate.h:38
SOEStyle style_
Definition SOTemplate.h:33
SOETxMPTIssue supportMPT() const
Definition SOTemplate.h:79
SOETxMPTIssue supportMpt_
Definition SOTemplate.h:34
SField const & sField() const
Definition SOTemplate.h:67
std::reference_wrapper< SField const > sField_
Definition SOTemplate.h:32
SOEStyle style() const
Definition SOTemplate.h:73
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=soeMPTNotSupported)
Definition SOTemplate.h:57
SOElement(SField const &fieldName, SOEStyle style)
Definition SOTemplate.h:50
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:92
std::vector< int > indices_
Definition SOTemplate.h:156
int getIndex(SField const &) const
Retrieve the position of a named field.
std::vector< SOElement >::const_iterator end() const
Definition SOTemplate.h:126
SOTemplate(SOTemplate &&other)=default
std::vector< SOElement >::const_iterator cend() const
Definition SOTemplate.h:132
SOEStyle style(SField const &sf) const
Definition SOTemplate.h:149
std::size_t size() const
The number of entries in this template.
Definition SOTemplate.h:139
std::vector< SOElement >::const_iterator cbegin() const
Definition SOTemplate.h:120
std::vector< SOElement >::const_iterator begin() const
Definition SOTemplate.h:114
std::vector< SOElement > elements_
Definition SOTemplate.h:155
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:14
@ soeINVALID
Definition SOTemplate.h:15
@ soeOPTIONAL
Definition SOTemplate.h:17
@ soeDEFAULT
Definition SOTemplate.h:18
@ soeREQUIRED
Definition SOTemplate.h:16
SOETxMPTIssue
Amount fields that can support MPT.
Definition SOTemplate.h:24
@ soeMPTNotSupported
Definition SOTemplate.h:24
@ soeMPTSupported
Definition SOTemplate.h:24
@ soeMPTNone
Definition SOTemplate.h:24
A field with a type known at compile time.
Definition SField.h:301
T to_string(T... args)