rippled
SOTemplate.h
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 #ifndef RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
21 #define RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
22 
23 #include <ripple/basics/contract.h>
24 #include <ripple/protocol/SField.h>
25 #include <functional>
26 #include <initializer_list>
27 #include <memory>
28 
29 namespace ripple {
30 
32 enum SOEStyle {
33  soeINVALID = -1,
34  soeREQUIRED = 0, // required
35  soeOPTIONAL = 1, // optional, may be present with default value
36  soeDEFAULT = 2, // optional, if present, must not have default value
37 };
38 
39 //------------------------------------------------------------------------------
40 
42 class SOElement
43 {
44  // Use std::reference_wrapper so SOElement can be stored in a std::vector.
47 
48 public:
49  SOElement(SField const& fieldName, SOEStyle style)
50  : sField_(fieldName), style_(style)
51  {
52  if (!sField_.get().isUseful())
53  {
54  auto nm = std::to_string(fieldName.getCode());
55  if (fieldName.hasName())
56  nm += ": '" + fieldName.getName() + "'";
57  Throw<std::runtime_error>(
58  "SField (" + nm + ") in SOElement must be useful.");
59  }
60  }
61 
62  SField const&
63  sField() const
64  {
65  return sField_.get();
66  }
67 
68  SOEStyle
69  style() const
70  {
71  return style_;
72  }
73 };
74 
75 //------------------------------------------------------------------------------
76 
82 {
83 public:
84  // Copying vectors is expensive. Make this a move-only type until
85  // there is motivation to change that.
86  SOTemplate(SOTemplate&& other) = default;
87  SOTemplate&
88  operator=(SOTemplate&& other) = default;
89 
94  SOTemplate(
96  std::initializer_list<SOElement> commonFields = {});
97 
98  /* Provide for the enumeration of fields */
100  begin() const
101  {
102  return elements_.cbegin();
103  }
104 
106  cbegin() const
107  {
108  return begin();
109  }
110 
112  end() const
113  {
114  return elements_.cend();
115  }
116 
118  cend() const
119  {
120  return end();
121  }
122 
125  size() const
126  {
127  return elements_.size();
128  }
129 
131  int
132  getIndex(SField const&) const;
133 
134  SOEStyle
135  style(SField const& sf) const
136  {
137  return elements_[indices_[sf.getNum()]].style();
138  }
139 
140 private:
142  std::vector<int> indices_; // field num -> index
143 };
144 
145 } // namespace ripple
146 
147 #endif
ripple::SOTemplate::size
std::size_t size() const
The number of entries in this template.
Definition: SOTemplate.h:125
ripple::SOTemplate::style
SOEStyle style(SField const &sf) const
Definition: SOTemplate.h:135
functional
std::vector
STL class.
ripple::SOTemplate::cbegin
std::vector< SOElement >::const_iterator cbegin() const
Definition: SOTemplate.h:106
ripple::SOElement::style_
SOEStyle style_
Definition: SOTemplate.h:46
ripple::SField::hasName
bool hasName() const
Definition: SField.h:178
ripple::SField::getNum
int getNum() const
Definition: SField.h:231
ripple::soeREQUIRED
@ soeREQUIRED
Definition: SOTemplate.h:34
ripple::SOElement
An element in a SOTemplate.
Definition: SOTemplate.h:42
ripple::SOElement::style
SOEStyle style() const
Definition: SOTemplate.h:69
ripple::SOTemplate::begin
std::vector< SOElement >::const_iterator begin() const
Definition: SOTemplate.h:100
ripple::SOTemplate::elements_
std::vector< SOElement > elements_
Definition: SOTemplate.h:141
ripple::SOElement::SOElement
SOElement(SField const &fieldName, SOEStyle style)
Definition: SOTemplate.h:49
ripple::SOTemplate::operator=
SOTemplate & operator=(SOTemplate &&other)=default
std::reference_wrapper
ripple::SOTemplate
Defines the fields and their attributes within a STObject.
Definition: SOTemplate.h:81
ripple::soeOPTIONAL
@ soeOPTIONAL
Definition: SOTemplate.h:35
ripple::SOEStyle
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition: SOTemplate.h:32
std::to_string
T to_string(T... args)
ripple::soeINVALID
@ soeINVALID
Definition: SOTemplate.h:33
ripple::SOTemplate::SOTemplate
SOTemplate(SOTemplate &&other)=default
ripple::SOElement::sField_
std::reference_wrapper< SField const > sField_
Definition: SOTemplate.h:45
ripple::SOTemplate::indices_
std::vector< int > indices_
Definition: SOTemplate.h:142
memory
ripple::SOTemplate::getIndex
int getIndex(SField const &) const
Retrieve the position of a named field.
Definition: SOTemplate.cpp:56
ripple::SOTemplate::cend
std::vector< SOElement >::const_iterator cend() const
Definition: SOTemplate.h:118
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:109
ripple::SField::getName
std::string const & getName() const
Definition: SField.h:172
ripple::SField::getCode
int getCode() const
Definition: SField.h:226
ripple::SOTemplate::end
std::vector< SOElement >::const_iterator end() const
Definition: SOTemplate.h:112
std::size_t
ripple::soeDEFAULT
@ soeDEFAULT
Definition: SOTemplate.h:36
ripple::SOElement::sField
SField const & sField() const
Definition: SOTemplate.h:63
initializer_list