rippled
Loading...
Searching...
No Matches
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 <xrpl/basics/Blob.h>
24#include <xrpl/basics/Buffer.h>
25#include <xrpl/basics/Slice.h>
26#include <xrpl/basics/contract.h>
27#include <xrpl/protocol/SField.h>
28#include <xrpl/protocol/STBlob.h>
29#include <xrpl/protocol/STInteger.h>
30#include <xrpl/protocol/STObject.h>
31
32#include <memory>
33#include <optional>
34#include <stdexcept>
35#include <type_traits>
36#include <utility>
37
38namespace ripple {
39
41template <class U, class T>
43
44template <class U, class T>
45struct STExchange<STInteger<U>, T>
46{
47 explicit STExchange() = default;
48
49 using value_type = U;
50
51 static void
53 {
54 t = u.value();
55 }
56
58 set(SField const& f, T const& t)
59 {
61 }
62};
63
64template <>
66{
67 explicit STExchange() = default;
68
70
71 static void
73 {
74 t.emplace(u.data(), u.size());
75 }
76
78 set(TypedField<STBlob> const& f, Slice const& t)
79 {
80 return std::make_unique<STBlob>(f, t.data(), t.size());
81 }
82};
83
84template <>
86{
87 explicit STExchange() = default;
88
90
91 static void
93 {
94 t.emplace(u.data(), u.size());
95 }
96
98 set(TypedField<STBlob> const& f, Buffer const& t)
99 {
100 return std::make_unique<STBlob>(f, t.data(), t.size());
101 }
102
105 {
106 return std::make_unique<STBlob>(f, std::move(t));
107 }
108};
109
110//------------------------------------------------------------------------------
111
114template <class T, class U>
116get(STObject const& st, TypedField<U> const& f)
117{
119 STBase const* const b = st.peekAtPField(f);
120 if (!b)
121 return t;
122 auto const id = b->getSType();
123 if (id == STI_NOTPRESENT)
124 return t;
125 auto const u = dynamic_cast<U const*>(b);
126 // This should never happen
127 if (!u)
128 Throw<std::runtime_error>("Wrong field type");
130 return t;
131}
132
133template <class U>
135get(STObject const& st, TypedField<U> const& f)
136{
137 return get<typename U::value_type>(st, f);
138}
142template <class U, class T>
143void
144set(STObject& st, TypedField<U> const& f, T&& t)
145{
146 st.set(STExchange<U, typename std::decay<T>::type>::set(
147 f, std::forward<T>(t)));
148}
149
151template <class Init>
152void
153set(STObject& st, TypedField<STBlob> const& f, std::size_t size, Init&& init)
154{
155 st.set(std::make_unique<STBlob>(f, size, init));
156}
157
159template <class = void>
160void
162 TypedField<STBlob> const& f,
163 void const* data,
164 std::size_t size)
165{
166 st.set(std::make_unique<STBlob>(f, data, size));
167}
168
170template <class U>
171void
173{
174 st.makeFieldAbsent(f);
175}
176
177} // namespace ripple
178
179#endif
Like std::vector<char> but better.
Definition Buffer.h:36
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:127
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:151
Identifies fields.
Definition SField.h:146
A type which can be exported to a well known binary format.
Definition STBase.h:135
virtual SerializedTypeID getSType() const
Definition STBase.cpp:75
std::uint8_t const * data() const
Definition STBlob.h:117
std::size_t size() const
Definition STBlob.h:111
value_type value() const noexcept
Definition STInteger.h:148
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:457
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:570
void set(SOTemplate const &)
Definition STObject.cpp:156
An immutable linear range of bytes.
Definition Slice.h:46
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:98
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:81
T emplace(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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,...
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition STExchange.h:172
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer const &t)
Definition STExchange.h:98
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer &&t)
Definition STExchange.h:104
static void get(std::optional< Buffer > &t, STBlob const &u)
Definition STExchange.h:92
static void get(std::optional< value_type > &t, STBlob const &u)
Definition STExchange.h:72
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Slice const &t)
Definition STExchange.h:78
static void get(std::optional< T > &t, STInteger< U > const &u)
Definition STExchange.h:52
static std::unique_ptr< STInteger< U > > set(SField const &f, T const &t)
Definition STExchange.h:58
Convert between serialized type U and C++ type T.
Definition STExchange.h:42
A field with a type known at compile time.
Definition SField.h:320