xrpld
Loading...
Searching...
No Matches
STExchange.h
1#pragma once
2
3#include <xrpl/basics/Buffer.h>
4#include <xrpl/basics/Slice.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/protocol/SField.h>
7#include <xrpl/protocol/STBase.h>
8#include <xrpl/protocol/STBlob.h>
9#include <xrpl/protocol/STInteger.h>
10#include <xrpl/protocol/STObject.h>
11
12#include <cstddef>
13#include <memory>
14#include <optional>
15#include <stdexcept>
16#include <type_traits>
17#include <utility>
18
19namespace xrpl {
20
24template <class U, class T>
26
27template <class U, class T>
28struct STExchange<STInteger<U>, T>
29{
30 explicit STExchange() = default;
31
32 using value_type = U;
33
34 static void
36 {
37 t = u.value();
38 }
39
41 set(SField const& f, T const& t)
42 {
44 }
45};
46
47template <>
49{
50 explicit STExchange() = default;
51
53
54 static void
56 {
57 t.emplace(u.data(), u.size());
58 }
59
61 set(TypedField<STBlob> const& f, Slice const& t)
62 {
63 return std::make_unique<STBlob>(f, t.data(), t.size());
64 }
65};
66
67template <>
69{
70 explicit STExchange() = default;
71
73
74 static void
76 {
77 t.emplace(u.data(), u.size());
78 }
79
81 set(TypedField<STBlob> const& f, Buffer const& t)
82 {
83 return std::make_unique<STBlob>(f, t.data(), t.size());
84 }
85
88 {
89 return std::make_unique<STBlob>(f, std::move(t));
90 }
91};
92
93//------------------------------------------------------------------------------
94
99template <class T, class U>
101get(STObject const& st, TypedField<U> const& f)
102{
104 STBase const* const b = st.peekAtPField(f);
105 if (!b)
106 return t;
107 auto const id = b->getSType();
108 if (id == STI_NOTPRESENT)
109 return t;
110 auto const u = dynamic_cast<U const*>(b);
111 // This should never happen
112 if (!u)
113 Throw<std::runtime_error>("Wrong field type");
115 return t;
116}
117
118template <class U>
120get(STObject const& st, TypedField<U> const& f)
121{
122 return get<typename U::value_type>(st, f);
123}
124
125
129template <class U, class T>
130void
131set(STObject& st, TypedField<U> const& f, T&& t)
132{
134}
135
139template <class Init>
140void
141set(STObject& st, TypedField<STBlob> const& f, std::size_t size, Init&& init)
142{
143 st.set(std::make_unique<STBlob>(f, size, init));
144}
145
149template <class = void>
150void
151set(STObject& st, TypedField<STBlob> const& f, void const* data, std::size_t size)
152{
153 st.set(std::make_unique<STBlob>(f, data, size));
154}
155
159template <class U>
160void
162{
163 st.makeFieldAbsent(f);
164}
165
166} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:19
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:123
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:148
Identifies fields.
Definition SField.h:132
A type which can be exported to a well known binary format.
Definition STBase.h:129
virtual SerializedTypeID getSType() const
Definition STBase.cpp:54
std::size_t size() const
Definition STBlob.h:94
std::uint8_t const * data() const
Definition STBlob.h:100
value_type value() const noexcept
Definition STInteger.h:132
void set(SOTemplate const &)
Definition STObject.cpp:138
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:437
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:550
An immutable linear range of bytes.
Definition Slice.h:28
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:88
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:70
T emplace(T... args)
T forward(T... args)
T make_unique(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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,...
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition STExchange.h:161
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer const &t)
Definition STExchange.h:81
static void get(std::optional< Buffer > &t, STBlob const &u)
Definition STExchange.h:75
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer &&t)
Definition STExchange.h:87
static void get(std::optional< value_type > &t, STBlob const &u)
Definition STExchange.h:55
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Slice const &t)
Definition STExchange.h:61
static std::unique_ptr< STInteger< U > > set(SField const &f, T const &t)
Definition STExchange.h:41
static void get(std::optional< T > &t, STInteger< U > const &u)
Definition STExchange.h:35
Convert between serialized type U and C++ type T.
Definition STExchange.h:25
A field with a type known at compile time.
Definition SField.h:308