rippled
Loading...
Searching...
No Matches
STExchange.h
1#ifndef XRPL_PROTOCOL_STEXCHANGE_H_INCLUDED
2#define XRPL_PROTOCOL_STEXCHANGE_H_INCLUDED
3
4#include <xrpl/basics/Blob.h>
5#include <xrpl/basics/Buffer.h>
6#include <xrpl/basics/Slice.h>
7#include <xrpl/basics/contract.h>
8#include <xrpl/protocol/SField.h>
9#include <xrpl/protocol/STBlob.h>
10#include <xrpl/protocol/STInteger.h>
11#include <xrpl/protocol/STObject.h>
12
13#include <memory>
14#include <optional>
15#include <stdexcept>
16#include <type_traits>
17#include <utility>
18
19namespace ripple {
20
22template <class U, class T>
24
25template <class U, class T>
26struct STExchange<STInteger<U>, T>
27{
28 explicit STExchange() = default;
29
30 using value_type = U;
31
32 static void
34 {
35 t = u.value();
36 }
37
39 set(SField const& f, T const& t)
40 {
42 }
43};
44
45template <>
47{
48 explicit STExchange() = default;
49
51
52 static void
54 {
55 t.emplace(u.data(), u.size());
56 }
57
59 set(TypedField<STBlob> const& f, Slice const& t)
60 {
61 return std::make_unique<STBlob>(f, t.data(), t.size());
62 }
63};
64
65template <>
67{
68 explicit STExchange() = default;
69
71
72 static void
74 {
75 t.emplace(u.data(), u.size());
76 }
77
79 set(TypedField<STBlob> const& f, Buffer const& t)
80 {
81 return std::make_unique<STBlob>(f, t.data(), t.size());
82 }
83
86 {
87 return std::make_unique<STBlob>(f, std::move(t));
88 }
89};
90
91//------------------------------------------------------------------------------
92
95template <class T, class U>
97get(STObject const& st, TypedField<U> const& f)
98{
100 STBase const* const b = st.peekAtPField(f);
101 if (!b)
102 return t;
103 auto const id = b->getSType();
104 if (id == STI_NOTPRESENT)
105 return t;
106 auto const u = dynamic_cast<U const*>(b);
107 // This should never happen
108 if (!u)
109 Throw<std::runtime_error>("Wrong field type");
111 return t;
112}
113
114template <class U>
116get(STObject const& st, TypedField<U> const& f)
117{
118 return get<typename U::value_type>(st, f);
119}
123template <class U, class T>
124void
125set(STObject& st, TypedField<U> const& f, T&& t)
126{
127 st.set(STExchange<U, typename std::decay<T>::type>::set(
128 f, std::forward<T>(t)));
129}
130
132template <class Init>
133void
134set(STObject& st, TypedField<STBlob> const& f, std::size_t size, Init&& init)
135{
136 st.set(std::make_unique<STBlob>(f, size, init));
137}
138
140template <class = void>
141void
143 TypedField<STBlob> const& f,
144 void const* data,
145 std::size_t size)
146{
147 st.set(std::make_unique<STBlob>(f, data, size));
148}
149
151template <class U>
152void
154{
155 st.makeFieldAbsent(f);
156}
157
158} // namespace ripple
159
160#endif
Like std::vector<char> but better.
Definition Buffer.h:17
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:108
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:132
Identifies fields.
Definition SField.h:127
A type which can be exported to a well known binary format.
Definition STBase.h:116
virtual SerializedTypeID getSType() const
Definition STBase.cpp:56
std::uint8_t const * data() const
Definition STBlob.h:98
std::size_t size() const
Definition STBlob.h:92
value_type value() const noexcept
Definition STInteger.h:129
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:438
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:551
void set(SOTemplate const &)
Definition STObject.cpp:137
An immutable linear range of bytes.
Definition Slice.h:27
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:79
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:62
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:6
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:153
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:79
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer &&t)
Definition STExchange.h:85
static void get(std::optional< Buffer > &t, STBlob const &u)
Definition STExchange.h:73
static void get(std::optional< value_type > &t, STBlob const &u)
Definition STExchange.h:53
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Slice const &t)
Definition STExchange.h:59
static void get(std::optional< T > &t, STInteger< U > const &u)
Definition STExchange.h:33
static std::unique_ptr< STInteger< U > > set(SField const &f, T const &t)
Definition STExchange.h:39
Convert between serialized type U and C++ type T.
Definition STExchange.h:23
A field with a type known at compile time.
Definition SField.h:301