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 : elements_(std::move(uniqueFields))
25 , indices_(SField::getNumFields() + 1, -1) // Unmapped indices == -1
26{
27 // Add all SOElements.
28 //
29
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
54int
55SOTemplate::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
T back_inserter(T... args)
Identifies fields.
Definition SField.h:132
int getNum() const
Definition SField.h:254
std::vector< int > indices_
Definition SOTemplate.h:175
int getIndex(SField const &) const
Retrieve the position of a named field.
SOTemplate(SOTemplate &&other)=default
std::vector< SOElement > elements_
Definition SOTemplate.h:174
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:52