xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
SOTemplate.cpp
1
#include <xrpl/protocol/SOTemplate.h>
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/protocol/SField.h>
5
6
#include <
algorithm
>
7
#include <
cstddef
>
8
#include <
initializer_list
>
9
#include <
iterator
>
10
#include <
stdexcept
>
11
#include <
utility
>
12
#include <
vector
>
13
14
namespace
xrpl
{
15
16
SOTemplate::SOTemplate
(
17
std::initializer_list<SOElement>
uniqueFields,
18
std::initializer_list<SOElement>
commonFields)
19
:
SOTemplate
(
std
::vector(uniqueFields),
std
::vector(commonFields))
20
{
21
}
22
23
SOTemplate::SOTemplate
(
std::vector<SOElement>
uniqueFields,
std::vector<SOElement>
commonFields)
24
:
elements_
(
std
::move(uniqueFields))
25
,
indices_
(
SField
::getNumFields() + 1, -1)
// Unmapped indices == -1
26
{
27
// Add all SOElements.
28
//
29
30
std::ranges::move
(commonFields,
std::back_inserter
(
elements_
));
31
32
// Validate and index elements_.
33
//
34
for
(
std::size_t
i = 0; i <
elements_
.size(); ++i)
35
{
36
SField
const
& sField{
elements_
[i].sField()};
37
38
// Make sure the field's index is in range
39
//
40
if
(sField.
getNum
() <= 0 || sField.
getNum
() >=
indices_
.size())
41
Throw<std::runtime_error>
(
"Invalid field index for SOTemplate."
);
42
43
// Make sure that this field hasn't already been assigned
44
//
45
if
(
getIndex
(sField) != -1)
46
Throw<std::runtime_error>
(
"Duplicate field index for SOTemplate."
);
47
48
// Add the field to the index mapping table
49
//
50
indices_
[sField.
getNum
()] = i;
51
}
52
}
53
54
int
55
SOTemplate::getIndex
(
SField
const
& sField)
const
56
{
57
// The mapping table should be large enough for any possible field
58
//
59
if
(sField.
getNum
() <= 0 || sField.
getNum
() >=
indices_
.size())
60
Throw<std::runtime_error>
(
"Invalid field index for getIndex()."
);
61
62
return
indices_
[sField.
getNum
()];
63
}
64
65
}
// namespace xrpl
algorithm
std::back_inserter
T back_inserter(T... args)
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::SField::getNum
int getNum() const
Definition
SField.h:254
xrpl::SOTemplate::indices_
std::vector< int > indices_
Definition
SOTemplate.h:175
xrpl::SOTemplate::getIndex
int getIndex(SField const &) const
Retrieve the position of a named field.
Definition
SOTemplate.cpp:55
xrpl::SOTemplate::SOTemplate
SOTemplate(SOTemplate &&other)=default
xrpl::SOTemplate::elements_
std::vector< SOElement > elements_
Definition
SOTemplate.h:174
cstddef
initializer_list
iterator
std::ranges::move
T move(T... args)
std
STL namespace.
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::size_t
stdexcept
utility
vector
Generated by
1.17.0