rippled
Loading...
Searching...
No Matches
SOTemplate.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
21#define RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
22
23#include <xrpl/basics/contract.h>
24#include <xrpl/protocol/SField.h>
25
26#include <functional>
27#include <initializer_list>
28#include <memory>
29#include <stdexcept>
30
31namespace ripple {
32
36 soeREQUIRED = 0, // required
37 soeOPTIONAL = 1, // optional, may be present with default value
38 soeDEFAULT = 2, // optional, if present, must not have default value
39 // inner object with the default fields has to be
40 // constructed with STObject::makeInnerObject()
41};
42
45
46//------------------------------------------------------------------------------
47
50{
51 // Use std::reference_wrapper so SOElement can be stored in a std::vector.
55
56private:
57 void
58 init(SField const& fieldName) const
59 {
60 if (!sField_.get().isUseful())
61 {
62 auto nm = std::to_string(fieldName.getCode());
63 if (fieldName.hasName())
64 nm += ": '" + fieldName.getName() + "'";
65 Throw<std::runtime_error>(
66 "SField (" + nm + ") in SOElement must be useful.");
67 }
68 }
69
70public:
71 SOElement(SField const& fieldName, SOEStyle style)
72 : sField_(fieldName), style_(style)
73 {
74 init(fieldName);
75 }
76
77 template <typename T>
78 requires(std::is_same_v<T, STAmount> || std::is_same_v<T, STIssue>)
80 TypedField<T> const& fieldName,
83 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
84 {
85 init(fieldName);
86 }
87
88 SField const&
89 sField() const
90 {
91 return sField_.get();
92 }
93
95 style() const
96 {
97 return style_;
98 }
99
102 {
103 return supportMpt_;
104 }
105};
106
107//------------------------------------------------------------------------------
108
114{
115public:
116 // Copying vectors is expensive. Make this a move-only type until
117 // there is motivation to change that.
118 SOTemplate(SOTemplate&& other) = default;
120 operator=(SOTemplate&& other) = default;
121
128 std::initializer_list<SOElement> commonFields = {});
129
130 /* Provide for the enumeration of fields */
132 begin() const
133 {
134 return elements_.cbegin();
135 }
136
138 cbegin() const
139 {
140 return begin();
141 }
142
144 end() const
145 {
146 return elements_.cend();
147 }
148
150 cend() const
151 {
152 return end();
153 }
154
157 size() const
158 {
159 return elements_.size();
160 }
161
163 int
164 getIndex(SField const&) const;
165
167 style(SField const& sf) const
168 {
169 return elements_[indices_[sf.getNum()]].style();
170 }
171
172private:
174 std::vector<int> indices_; // field num -> index
175};
176
177} // namespace ripple
178
179#endif
Identifies fields.
Definition: SField.h:144
int getCode() const
Definition: SField.h:258
bool hasName() const
Definition: SField.h:213
std::string const & getName() const
Definition: SField.h:207
int getNum() const
Definition: SField.h:263
An element in a SOTemplate.
Definition: SOTemplate.h:50
std::reference_wrapper< SField const > sField_
Definition: SOTemplate.h:52
SOEStyle style_
Definition: SOTemplate.h:53
SOETxMPTIssue supportMpt_
Definition: SOTemplate.h:54
void init(SField const &fieldName) const
Definition: SOTemplate.h:58
SField const & sField() const
Definition: SOTemplate.h:89
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=soeMPTNotSupported)
Definition: SOTemplate.h:79
SOETxMPTIssue supportMPT() const
Definition: SOTemplate.h:101
SOElement(SField const &fieldName, SOEStyle style)
Definition: SOTemplate.h:71
SOEStyle style() const
Definition: SOTemplate.h:95
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:114
std::vector< SOElement >::const_iterator cend() const
Definition: SOTemplate.h:150
SOEStyle style(SField const &sf) const
Definition: SOTemplate.h:167
std::vector< SOElement >::const_iterator cbegin() const
Definition: SOTemplate.h:138
std::vector< SOElement >::const_iterator end() const
Definition: SOTemplate.h:144
SOTemplate & operator=(SOTemplate &&other)=default
std::vector< SOElement > elements_
Definition: SOTemplate.h:173
std::vector< SOElement >::const_iterator begin() const
Definition: SOTemplate.h:132
std::vector< int > indices_
Definition: SOTemplate.h:174
std::size_t size() const
The number of entries in this template.
Definition: SOTemplate.h:157
int getIndex(SField const &) const
Retrieve the position of a named field.
Definition: SOTemplate.cpp:62
SOTemplate(SOTemplate &&other)=default
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SOETxMPTIssue
Amount fields that can support MPT.
Definition: SOTemplate.h:44
@ soeMPTNone
Definition: SOTemplate.h:44
@ soeMPTSupported
Definition: SOTemplate.h:44
@ soeMPTNotSupported
Definition: SOTemplate.h:44
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition: SOTemplate.h:34
@ soeOPTIONAL
Definition: SOTemplate.h:37
@ soeREQUIRED
Definition: SOTemplate.h:36
@ soeDEFAULT
Definition: SOTemplate.h:38
@ soeINVALID
Definition: SOTemplate.h:35
A field with a type known at compile time.
Definition: SField.h:315
T to_string(T... args)