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 
33 {
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)
52  , style_ (style)
53  {
54  if (! sField_.get().isUseful())
55  Throw<std::runtime_error> ("SField in SOElement must be useful.");
56  }
57 
58  SField const& sField () const
59  {
60  return sField_.get();
61  }
62 
63  SOEStyle style () const
64  {
65  return style_;
66  }
67 };
68 
69 //------------------------------------------------------------------------------
70 
76 {
77 public:
78  // Copying vectors is expensive. Make this a move-only type until
79  // there is motivation to change that.
80  SOTemplate(SOTemplate&& other) = default;
81  SOTemplate& operator=(SOTemplate&& other) = default;
82 
88  std::initializer_list<SOElement> commonFields = {});
89 
90  /* Provide for the enumeration of fields */
92  {
93  return elements_.cbegin();
94  }
95 
97  {
98  return begin();
99  }
100 
102  {
103  return elements_.cend();
104  }
105 
107  {
108  return end();
109  }
110 
112  std::size_t size () const
113  {
114  return elements_.size ();
115  }
116 
118  int getIndex (SField const&) const;
119 
120  SOEStyle
121  style(SField const& sf) const
122  {
123  return elements_[indices_[sf.getNum()]].style();
124  }
125 
126 private:
128  std::vector<int> indices_; // field num -> index
129 };
130 
131 } // ripple
132 
133 #endif
ripple::SOTemplate::size
std::size_t size() const
The number of entries in this template.
Definition: SOTemplate.h:112
ripple::SOTemplate::style
SOEStyle style(SField const &sf) const
Definition: SOTemplate.h:121
functional
std::vector
STL class.
ripple::SOTemplate::cbegin
std::vector< SOElement >::const_iterator cbegin() const
Definition: SOTemplate.h:96
ripple::SOElement::style_
SOEStyle style_
Definition: SOTemplate.h:47
ripple::SField::getNum
int getNum() const
Definition: SField.h:217
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:63
ripple::SOTemplate::begin
std::vector< SOElement >::const_iterator begin() const
Definition: SOTemplate.h:91
ripple::SOTemplate::elements_
std::vector< SOElement > elements_
Definition: SOTemplate.h:127
ripple::SOElement::SOElement
SOElement(SField const &fieldName, SOEStyle style)
Definition: SOTemplate.h:50
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:75
ripple::soeOPTIONAL
@ soeOPTIONAL
Definition: SOTemplate.h:36
ripple::SOEStyle
SOEStyle
Kind of element in each entry of an SOTemplate.
Definition: SOTemplate.h:32
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:128
memory
ripple::SOTemplate::getIndex
int getIndex(SField const &) const
Retrieve the position of a named field.
Definition: SOTemplate.cpp:55
ripple::SOTemplate::cend
std::vector< SOElement >::const_iterator cend() const
Definition: SOTemplate.h:106
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::SOTemplate::end
std::vector< SOElement >::const_iterator end() const
Definition: SOTemplate.h:101
std::size_t
ripple::soeDEFAULT
@ soeDEFAULT
Definition: SOTemplate.h:37
ripple::SOElement::sField
SField const & sField() const
Definition: SOTemplate.h:58
initializer_list