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 <stdexcept>
29
30namespace ripple {
31
35 soeREQUIRED = 0, // required
36 soeOPTIONAL = 1, // optional, may be present with default value
37 soeDEFAULT = 2, // optional, if present, must not have default value
38 // inner object with the default fields has to be
39 // constructed with STObject::makeInnerObject()
40};
41
44
45//------------------------------------------------------------------------------
46
49{
50 // Use std::reference_wrapper so SOElement can be stored in a std::vector.
54
55private:
56 void
57 init(SField const& fieldName) const
58 {
59 if (!sField_.get().isUseful())
60 {
61 auto nm = std::to_string(fieldName.getCode());
62 if (fieldName.hasName())
63 nm += ": '" + fieldName.getName() + "'";
64 Throw<std::runtime_error>(
65 "SField (" + nm + ") in SOElement must be useful.");
66 }
67 }
68
69public:
70 SOElement(SField const& fieldName, SOEStyle style)
71 : sField_(fieldName), style_(style)
72 {
73 init(fieldName);
74 }
75
76 template <typename T>
77 requires(std::is_same_v<T, STAmount> || std::is_same_v<T, STIssue>)
79 TypedField<T> const& fieldName,
82 : sField_(fieldName), style_(style), supportMpt_(supportMpt)
83 {
84 init(fieldName);
85 }
86
87 SField const&
88 sField() const
89 {
90 return sField_.get();
91 }
92
94 style() const
95 {
96 return style_;
97 }
98
101 {
102 return supportMpt_;
103 }
104};
105
106//------------------------------------------------------------------------------
107
113{
114public:
115 // Copying vectors is expensive. Make this a move-only type until
116 // there is motivation to change that.
117 SOTemplate(SOTemplate&& other) = default;
119 operator=(SOTemplate&& other) = default;
120
127 std::initializer_list<SOElement> commonFields = {});
128
129 /* Provide for the enumeration of fields */
131 begin() const
132 {
133 return elements_.cbegin();
134 }
135
137 cbegin() const
138 {
139 return begin();
140 }
141
143 end() const
144 {
145 return elements_.cend();
146 }
147
149 cend() const
150 {
151 return end();
152 }
153
156 size() const
157 {
158 return elements_.size();
159 }
160
162 int
163 getIndex(SField const&) const;
164
166 style(SField const& sf) const
167 {
168 return elements_[indices_[sf.getNum()]].style();
169 }
170
171private:
173 std::vector<int> indices_; // field num -> index
174};
175
176} // namespace ripple
177
178#endif
Identifies fields.
Definition: SField.h:143
int getCode() const
Definition: SField.h:257
bool hasName() const
Definition: SField.h:212
std::string const & getName() const
Definition: SField.h:206
int getNum() const
Definition: SField.h:262
An element in a SOTemplate.
Definition: SOTemplate.h:49
std::reference_wrapper< SField const > sField_
Definition: SOTemplate.h:51
SOEStyle style_
Definition: SOTemplate.h:52
SOETxMPTIssue supportMpt_
Definition: SOTemplate.h:53
void init(SField const &fieldName) const
Definition: SOTemplate.h:57
SField const & sField() const
Definition: SOTemplate.h:88
SOElement(TypedField< T > const &fieldName, SOEStyle style, SOETxMPTIssue supportMpt=soeMPTNotSupported)
Definition: SOTemplate.h:78
SOETxMPTIssue supportMPT() const
Definition: SOTemplate.h:100
SOElement(SField const &fieldName, SOEStyle style)
Definition: SOTemplate.h:70
SOEStyle style() const
Definition: SOTemplate.h:94
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:113
std::vector< SOElement >::const_iterator cend() const
Definition: SOTemplate.h:149
SOEStyle style(SField const &sf) const
Definition: SOTemplate.h:166
std::vector< SOElement >::const_iterator cbegin() const
Definition: SOTemplate.h:137
std::vector< SOElement >::const_iterator end() const
Definition: SOTemplate.h:143
SOTemplate & operator=(SOTemplate &&other)=default
std::vector< SOElement > elements_
Definition: SOTemplate.h:172
std::vector< SOElement >::const_iterator begin() const
Definition: SOTemplate.h:131
std::vector< int > indices_
Definition: SOTemplate.h:173
std::size_t size() const
The number of entries in this template.
Definition: SOTemplate.h:156
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:25
SOETxMPTIssue
Amount fields that can support MPT.
Definition: SOTemplate.h:43
@ soeMPTNone
Definition: SOTemplate.h:43
@ soeMPTSupported
Definition: SOTemplate.h:43
@ soeMPTNotSupported
Definition: SOTemplate.h:43
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition: SOTemplate.h:33
@ soeOPTIONAL
Definition: SOTemplate.h:36
@ soeREQUIRED
Definition: SOTemplate.h:35
@ soeDEFAULT
Definition: SOTemplate.h:37
@ soeINVALID
Definition: SOTemplate.h:34
A field with a type known at compile time.
Definition: SField.h:314
T to_string(T... args)