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