rippled
STExchange.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_STEXCHANGE_H_INCLUDED
21 #define RIPPLE_PROTOCOL_STEXCHANGE_H_INCLUDED
22 
23 #include <ripple/basics/Blob.h>
24 #include <ripple/basics/Buffer.h>
25 #include <ripple/basics/Slice.h>
26 #include <ripple/basics/contract.h>
27 #include <ripple/protocol/SField.h>
28 #include <ripple/protocol/STBlob.h>
29 #include <ripple/protocol/STInteger.h>
30 #include <ripple/protocol/STObject.h>
31 #include <boost/optional.hpp>
32 #include <memory>
33 #include <stdexcept>
34 #include <type_traits>
35 #include <utility>
36 
37 namespace ripple {
38 
40 template <class U, class T>
41 struct STExchange;
42 
43 template <class U, class T>
44 struct STExchange<STInteger<U>, T>
45 {
46  explicit STExchange() = default;
47 
48  using value_type = U;
49 
50  static void
51  get(boost::optional<T>& t, STInteger<U> const& u)
52  {
53  t = u.value();
54  }
55 
57  set(SField const& f, T const& t)
58  {
59  return std::make_unique<STInteger<U>>(f, t);
60  }
61 };
62 
63 template <>
65 {
66  explicit STExchange() = default;
67 
68  using value_type = Slice;
69 
70  static void
71  get(boost::optional<value_type>& t, STBlob const& u)
72  {
73  t.emplace(u.data(), u.size());
74  }
75 
77  set(TypedField<STBlob> const& f, Slice const& t)
78  {
79  return std::make_unique<STBlob>(f, t.data(), t.size());
80  }
81 };
82 
83 template <>
85 {
86  explicit STExchange() = default;
87 
88  using value_type = Buffer;
89 
90  static void
91  get(boost::optional<Buffer>& t, STBlob const& u)
92  {
93  t.emplace(u.data(), u.size());
94  }
95 
97  set(TypedField<STBlob> const& f, Buffer const& t)
98  {
99  return std::make_unique<STBlob>(f, t.data(), t.size());
100  }
101 
104  {
105  return std::make_unique<STBlob>(f, std::move(t));
106  }
107 };
108 
109 //------------------------------------------------------------------------------
110 
113 template <class T, class U>
114 boost::optional<T>
115 get(STObject const& st, TypedField<U> const& f)
116 {
117  boost::optional<T> t;
118  STBase const* const b = st.peekAtPField(f);
119  if (!b)
120  return t;
121  auto const id = b->getSType();
122  if (id == STI_NOTPRESENT)
123  return t;
124  auto const u = dynamic_cast<U const*>(b);
125  // This should never happen
126  if (!u)
127  Throw<std::runtime_error>("Wrong field type");
128  STExchange<U, T>::get(t, *u);
129  return t;
130 }
131 
132 template <class U>
133 boost::optional<typename STExchange<U, typename U::value_type>::value_type>
134 get(STObject const& st, TypedField<U> const& f)
135 {
136  return get<typename U::value_type>(st, f);
137 }
141 template <class U, class T>
142 void
143 set(STObject& st, TypedField<U> const& f, T&& t)
144 {
145  st.set(STExchange<U, typename std::decay<T>::type>::set(
146  f, std::forward<T>(t)));
147 }
148 
150 template <class Init>
151 void
152 set(STObject& st, TypedField<STBlob> const& f, std::size_t size, Init&& init)
153 {
154  st.set(std::make_unique<STBlob>(f, size, init));
155 }
156 
158 template <class = void>
159 void
161  TypedField<STBlob> const& f,
162  void const* data,
163  std::size_t size)
164 {
165  st.set(std::make_unique<STBlob>(f, data, size));
166 }
167 
169 template <class U>
170 void
172 {
173  st.makeFieldAbsent(f);
174 }
175 
176 } // namespace ripple
177 
178 #endif
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:79
ripple::STBlob::data
std::uint8_t const * data() const
Definition: STBlob.h:77
ripple::STBase::getSType
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:57
ripple::STObject::makeFieldAbsent
void makeFieldAbsent(SField const &field)
Definition: STObject.cpp:487
utility
ripple::TypedField
A field with a type known at compile time.
Definition: SField.h:281
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::STExchange< STBlob, Slice >::set
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Slice const &t)
Definition: STExchange.h:77
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:96
ripple::STExchange< STBlob, Buffer >::set
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer const &t)
Definition: STExchange.h:97
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::STExchange< STBlob, Slice >::get
static void get(boost::optional< value_type > &t, STBlob const &u)
Definition: STExchange.h:71
ripple::erase
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition: STExchange.h:171
stdexcept
ripple::STExchange< STInteger< U >, T >::value_type
U value_type
Definition: STExchange.h:48
ripple::set
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
Definition: BasicConfig.h:276
ripple::Buffer::size
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition: Buffer.h:126
ripple::STExchange
Convert between serialized type U and C++ type T.
Definition: STExchange.h:41
ripple::Buffer::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Buffer.h:150
ripple::STBlob::size
std::size_t size() const
Definition: STBlob.h:71
ripple::STExchange< STInteger< U >, T >::get
static void get(boost::optional< T > &t, STInteger< U > const &u)
Definition: STExchange.h:51
ripple::STExchange< STBlob, Buffer >::get
static void get(boost::optional< Buffer > &t, STBlob const &u)
Definition: STExchange.h:91
memory
ripple::STInteger
Definition: SField.h:49
std::decay
ripple::STObject
Definition: STObject.h:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STObject::peekAtPField
const STBase * peekAtPField(SField const &field) const
Definition: STObject.cpp:374
ripple::SField
Identifies fields.
Definition: SField.h:109
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:62
ripple::STExchange< STBlob, Buffer >::set
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer &&t)
Definition: STExchange.h:103
ripple::STExchange< STInteger< U >, T >::set
static std::unique_ptr< STInteger< U > > set(SField const &f, T const &t)
Definition: STExchange.h:57
std::size_t
ripple::STInteger::value
value_type value() const noexcept
Definition: STInteger.h:79
std::unique_ptr
STL class.
ripple::STObject::set
void set(const SOTemplate &)
Definition: STObject.cpp:73
ripple::STBlob
Definition: STBlob.h:33
type_traits
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:116
ripple::STI_NOTPRESENT
@ STI_NOTPRESENT
Definition: SField.h:56