rippled
SOTemplate.cpp
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 #include <ripple/protocol/SOTemplate.h>
21 
22 namespace ripple {
23 
27  : indices_(SField::getNumFields() + 1, -1) // Unmapped indices == -1
28 {
29  // Add all SOElements.
30  elements_.reserve(uniqueFields.size() + commonFields.size());
31  elements_.assign(uniqueFields);
32  elements_.insert(elements_.end(), commonFields);
33 
34  // Validate and index elements_.
35  for (std::size_t i = 0; i < elements_.size(); ++i)
36  {
37  SField const& sField{elements_[i].sField()};
38 
39  // Make sure the field's index is in range
40  //
41  if (sField.getNum() <= 0 || sField.getNum() >= indices_.size())
42  Throw<std::runtime_error>("Invalid field index for SOTemplate.");
43 
44  // Make sure that this field hasn't already been assigned
45  //
46  if (getIndex(sField) != -1)
47  Throw<std::runtime_error>("Duplicate field index for SOTemplate.");
48 
49  // Add the field to the index mapping table
50  //
51  indices_[sField.getNum()] = i;
52  }
53 }
54 
55 int
56 SOTemplate::getIndex(SField const& sField) const
57 {
58  // The mapping table should be large enough for any possible field
59  //
60  if (sField.getNum() <= 0 || sField.getNum() >= indices_.size())
61  Throw<std::runtime_error>("Invalid field index for getIndex().");
62 
63  return indices_[sField.getNum()];
64 }
65 
66 } // namespace ripple
std::initializer_list::size
T size(T... args)
ripple::SField::getNum
int getNum() const
Definition: SField.h:226
ripple::SOTemplate::elements_
std::vector< SOElement > elements_
Definition: SOTemplate.h:142
ripple::SOTemplate::SOTemplate
SOTemplate(SOTemplate &&other)=default
ripple::SOTemplate::indices_
std::vector< int > indices_
Definition: SOTemplate.h:143
ripple::SOTemplate::getIndex
int getIndex(SField const &) const
Retrieve the position of a named field.
Definition: SOTemplate.cpp:56
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::SField
Identifies fields.
Definition: SField.h:112
std::size_t
std::initializer_list