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
10namespace xrpl {
11
15 soeREQUIRED = 0, // required
16 soeOPTIONAL = 1, // optional, may be present with default value
17 soeDEFAULT = 2, // optional, if present, must not have default value
18 // inner object with the default fields has to be
19 // constructed with STObject::makeInnerObject()
20};
21
24
25//------------------------------------------------------------------------------
26
29{
30 // Use std::reference_wrapper so SOElement can be stored in a std::vector.
34
35private:
36 void
37 init(SField const& fieldName) const
38 {
39 if (!sField_.get().isUseful())
40 {
41 auto nm = std::to_string(fieldName.getCode());
42 if (fieldName.hasName())
43 nm += ": '" + fieldName.getName() + "'";
44 Throw<std::runtime_error>("SField (" + nm + ") in SOElement must be useful.");
45 }
46 }
47
48public:
49 SOElement(SField const& fieldName, SOEStyle style) : sField_(fieldName), style_(style)
50 {
51 init(fieldName);
52 }
53
54 template <typename T>
57 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
58 {
59 init(fieldName);
60 }
61
62 SField const&
63 sField() const
64 {
65 return sField_.get();
66 }
67
69 style() const
70 {
71 return style_;
72 }
73
75 supportMPT() const
76 {
77 return supportMpt_;
78 }
79};
80
81//------------------------------------------------------------------------------
82
88{
89public:
90 // Copying vectors is expensive. Make this a move-only type until
91 // there is motivation to change that.
92 SOTemplate(SOTemplate&& other) = default;
94 operator=(SOTemplate&& other) = default;
95
101
102 /* Provide for the enumeration of fields */
104 begin() const
105 {
106 return elements_.cbegin();
107 }
108
110 cbegin() const
111 {
112 return begin();
113 }
114
116 end() const
117 {
118 return elements_.cend();
119 }
120
122 cend() const
123 {
124 return end();
125 }
126
129 size() const
130 {
131 return elements_.size();
132 }
133
135 int
136 getIndex(SField const&) const;
137
139 style(SField const& sf) const
140 {
141 return elements_[indices_[sf.getNum()]].style();
142 }
143
144private:
146 std::vector<int> indices_; // field num -> index
147};
148
149} // 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:29
void init(SField const &fieldName) const
Definition SOTemplate.h:37
SOEStyle style_
Definition SOTemplate.h:32
SOETxMPTIssue supportMPT() const
Definition SOTemplate.h:75
SOETxMPTIssue supportMpt_
Definition SOTemplate.h:33
SField const & sField() const
Definition SOTemplate.h:63
std::reference_wrapper< SField const > sField_
Definition SOTemplate.h:31
SOEStyle style() const
Definition SOTemplate.h:69
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=soeMPTNotSupported)
Definition SOTemplate.h:56
SOElement(SField const &fieldName, SOEStyle style)
Definition SOTemplate.h:49
Defines the fields and their attributes within a STObject.
Definition SOTemplate.h:88
std::vector< int > indices_
Definition SOTemplate.h:146
int getIndex(SField const &) const
Retrieve the position of a named field.
std::vector< SOElement >::const_iterator end() const
Definition SOTemplate.h:116
SOTemplate(SOTemplate &&other)=default
std::vector< SOElement >::const_iterator cend() const
Definition SOTemplate.h:122
SOEStyle style(SField const &sf) const
Definition SOTemplate.h:139
std::size_t size() const
The number of entries in this template.
Definition SOTemplate.h:129
std::vector< SOElement >::const_iterator cbegin() const
Definition SOTemplate.h:110
std::vector< SOElement >::const_iterator begin() const
Definition SOTemplate.h:104
std::vector< SOElement > elements_
Definition SOTemplate.h:145
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:13
@ soeINVALID
Definition SOTemplate.h:14
@ soeOPTIONAL
Definition SOTemplate.h:16
@ soeDEFAULT
Definition SOTemplate.h:17
@ soeREQUIRED
Definition SOTemplate.h:15
SOETxMPTIssue
Amount fields that can support MPT.
Definition SOTemplate.h:23
@ soeMPTNotSupported
Definition SOTemplate.h:23
@ soeMPTSupported
Definition SOTemplate.h:23
@ soeMPTNone
Definition SOTemplate.h:23
A field with a type known at compile time.
Definition SField.h:301
T to_string(T... args)