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