rippled
Loading...
Searching...
No Matches
SOTemplate.h
1#ifndef XRPL_PROTOCOL_SOTEMPLATE_H_INCLUDED
2#define XRPL_PROTOCOL_SOTEMPLATE_H_INCLUDED
3
4#include <xrpl/basics/contract.h>
5#include <xrpl/protocol/SField.h>
6
7#include <functional>
8#include <initializer_list>
9#include <stdexcept>
10
11namespace ripple {
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>(
46 "SField (" + nm + ") in SOElement must be useful.");
47 }
48 }
49
50public:
51 SOElement(SField const& fieldName, SOEStyle style)
52 : sField_(fieldName), style_(style)
53 {
54 init(fieldName);
55 }
56
57 template <typename T>
60 TypedField<T> const& fieldName,
63 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
64 {
65 init(fieldName);
66 }
67
68 SField const&
69 sField() const
70 {
71 return sField_.get();
72 }
73
75 style() const
76 {
77 return style_;
78 }
79
81 supportMPT() const
82 {
83 return supportMpt_;
84 }
85};
86
87//------------------------------------------------------------------------------
88
94{
95public:
96 // Copying vectors is expensive. Make this a move-only type until
97 // there is motivation to change that.
98 SOTemplate(SOTemplate&& other) = default;
100 operator=(SOTemplate&& other) = default;
101
108 std::initializer_list<SOElement> commonFields = {});
109
110 /* Provide for the enumeration of fields */
112 begin() const
113 {
114 return elements_.cbegin();
115 }
116
118 cbegin() const
119 {
120 return begin();
121 }
122
124 end() const
125 {
126 return elements_.cend();
127 }
128
130 cend() const
131 {
132 return end();
133 }
134
137 size() const
138 {
139 return elements_.size();
140 }
141
143 int
144 getIndex(SField const&) const;
145
147 style(SField const& sf) const
148 {
149 return elements_[indices_[sf.getNum()]].style();
150 }
151
152private:
154 std::vector<int> indices_; // field num -> index
155};
156
157} // namespace ripple
158
159#endif
Identifies fields.
Definition SField.h:127
int getCode() const
Definition SField.h:243
bool hasName() const
Definition SField.h:198
std::string const & getName() const
Definition SField.h:192
int getNum() const
Definition SField.h:248
An element in a SOTemplate.
Definition SOTemplate.h:30
std::reference_wrapper< SField const > sField_
Definition SOTemplate.h:32
SOETxMPTIssue supportMpt_
Definition SOTemplate.h:34
void init(SField const &fieldName) const
Definition SOTemplate.h:38
SField const & sField() const
Definition SOTemplate.h:69
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=soeMPTNotSupported)
Definition SOTemplate.h:59
SOETxMPTIssue supportMPT() const
Definition SOTemplate.h:81
SOElement(SField const &fieldName, SOEStyle style)
Definition SOTemplate.h:51
SOEStyle style() const
Definition SOTemplate.h:75
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:94
std::vector< SOElement >::const_iterator cend() const
Definition SOTemplate.h:130
SOEStyle style(SField const &sf) const
Definition SOTemplate.h:147
std::vector< SOElement >::const_iterator cbegin() const
Definition SOTemplate.h:118
std::vector< SOElement >::const_iterator end() const
Definition SOTemplate.h:124
SOTemplate & operator=(SOTemplate &&other)=default
std::vector< SOElement > elements_
Definition SOTemplate.h:153
std::vector< SOElement >::const_iterator begin() const
Definition SOTemplate.h:112
std::vector< int > indices_
Definition SOTemplate.h:154
std::size_t size() const
The number of entries in this template.
Definition SOTemplate.h:137
int getIndex(SField const &) const
Retrieve the position of a named field.
SOTemplate(SOTemplate &&other)=default
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
SOETxMPTIssue
Amount fields that can support MPT.
Definition SOTemplate.h:24
@ soeMPTNone
Definition SOTemplate.h:24
@ soeMPTSupported
Definition SOTemplate.h:24
@ soeMPTNotSupported
Definition SOTemplate.h:24
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition SOTemplate.h:14
@ soeOPTIONAL
Definition SOTemplate.h:17
@ soeREQUIRED
Definition SOTemplate.h:16
@ soeDEFAULT
Definition SOTemplate.h:18
@ soeINVALID
Definition SOTemplate.h:15
A field with a type known at compile time.
Definition SField.h:301
T to_string(T... args)