xrpld
Loading...
Searching...
No Matches
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
14namespace xrpl {
15
19 : SOTemplate(std::vector(uniqueFields), std::vector(commonFields))
20{
21}
22
24 : indices_(SField::getNumFields() + 1, -1) // Unmapped indices == -1
25{
26 // Add all SOElements.
27 //
28 elements_ = std::move(uniqueFields);
30
31 // Validate and index elements_.
32 //
33 for (std::size_t i = 0; i < elements_.size(); ++i)
34 {
35 SField const& sField{elements_[i].sField()};
36
37 // Make sure the field's index is in range
38 //
39 if (sField.getNum() <= 0 || sField.getNum() >= indices_.size())
40 Throw<std::runtime_error>("Invalid field index for SOTemplate.");
41
42 // Make sure that this field hasn't already been assigned
43 //
44 if (getIndex(sField) != -1)
45 Throw<std::runtime_error>("Duplicate field index for SOTemplate.");
46
47 // Add the field to the index mapping table
48 //
49 indices_[sField.getNum()] = i;
50 }
51}
52
53int
54SOTemplate::getIndex(SField const& sField) const
55{
56 // The mapping table should be large enough for any possible field
57 //
58 if (sField.getNum() <= 0 || sField.getNum() >= indices_.size())
59 Throw<std::runtime_error>("Invalid field index for getIndex().");
60
61 return indices_[sField.getNum()];
62}
63
64} // namespace xrpl
T back_inserter(T... args)
Identifies fields.
Definition SField.h:130
int getNum() const
Definition SField.h:252
std::vector< int > indices_
Definition SOTemplate.h:160
int getIndex(SField const &) const
Retrieve the position of a named field.
SOTemplate(SOTemplate &&other)=default
std::vector< SOElement > elements_
Definition SOTemplate.h:159
T move(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49