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  // inner object with the default fields has to be
39  // constructed with STObject::makeInnerObject()
40 };
41 
42 //------------------------------------------------------------------------------
43 
45 class SOElement
46 {
47  // Use std::reference_wrapper so SOElement can be stored in a std::vector.
50 
51 public:
52  SOElement(SField const& fieldName, SOEStyle style)
53  : sField_(fieldName), style_(style)
54  {
55  if (!sField_.get().isUseful())
56  {
57  auto nm = std::to_string(fieldName.getCode());
58  if (fieldName.hasName())
59  nm += ": '" + fieldName.getName() + "'";
60  Throw<std::runtime_error>(
61  "SField (" + nm + ") in SOElement must be useful.");
62  }
63  }
64 
65  SField const&
66  sField() const
67  {
68  return sField_.get();
69  }
70 
71  SOEStyle
72  style() const
73  {
74  return style_;
75  }
76 };
77 
78 //------------------------------------------------------------------------------
79 
85 {
86 public:
87  // Copying vectors is expensive. Make this a move-only type until
88  // there is motivation to change that.
89  SOTemplate(SOTemplate&& other) = default;
90  SOTemplate&
91  operator=(SOTemplate&& other) = default;
92 
97  SOTemplate(
99  std::initializer_list<SOElement> commonFields = {});
100 
101  /* Provide for the enumeration of fields */
103  begin() const
104  {
105  return elements_.cbegin();
106  }
107 
109  cbegin() const
110  {
111  return begin();
112  }
113 
115  end() const
116  {
117  return elements_.cend();
118  }
119 
121  cend() const
122  {
123  return end();
124  }
125 
128  size() const
129  {
130  return elements_.size();
131  }
132 
134  int
135  getIndex(SField const&) const;
136 
137  SOEStyle
138  style(SField const& sf) const
139  {
140  return elements_[indices_[sf.getNum()]].style();
141  }
142 
143 private:
145  std::vector<int> indices_; // field num -> index
146 };
147 
148 } // namespace ripple
149 
150 #endif
ripple::SOTemplate::size
std::size_t size() const
The number of entries in this template.
Definition: SOTemplate.h:128
ripple::SOTemplate::style
SOEStyle style(SField const &sf) const
Definition: SOTemplate.h:138
functional
std::vector
STL class.
ripple::SOTemplate::cbegin
std::vector< SOElement >::const_iterator cbegin() const
Definition: SOTemplate.h:109
ripple::SOElement::style_
SOEStyle style_
Definition: SOTemplate.h:49
ripple::SField::hasName
bool hasName() const
Definition: SField.h:210
ripple::SField::getNum
int getNum() const
Definition: SField.h:255
ripple::soeREQUIRED
@ soeREQUIRED
Definition: SOTemplate.h:35
ripple::SOElement
An element in a SOTemplate.
Definition: SOTemplate.h:45
ripple::SOElement::style
SOEStyle style() const
Definition: SOTemplate.h:72
ripple::SOTemplate::begin
std::vector< SOElement >::const_iterator begin() const
Definition: SOTemplate.h:103
ripple::SOTemplate::elements_
std::vector< SOElement > elements_
Definition: SOTemplate.h:144
ripple::SOElement::SOElement
SOElement(SField const &fieldName, SOEStyle style)
Definition: SOTemplate.h:52
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:84
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:48
ripple::SOTemplate::indices_
std::vector< int > indices_
Definition: SOTemplate.h:145
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:121
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:141
ripple::SField::getName
std::string const & getName() const
Definition: SField.h:204
ripple::SField::getCode
int getCode() const
Definition: SField.h:250
ripple::SOTemplate::end
std::vector< SOElement >::const_iterator end() const
Definition: SOTemplate.h:115
std::size_t
ripple::soeDEFAULT
@ soeDEFAULT
Definition: SOTemplate.h:37
ripple::SOElement::sField
SField const & sField() const
Definition: SOTemplate.h:66
initializer_list