rippled
Loading...
Searching...
No Matches
STExchange.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/Buffer.h>
5#include <xrpl/basics/Slice.h>
6#include <xrpl/basics/contract.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STBlob.h>
9#include <xrpl/protocol/STInteger.h>
10#include <xrpl/protocol/STObject.h>
11
12#include <memory>
13#include <optional>
14#include <stdexcept>
15#include <type_traits>
16#include <utility>
17
18namespace xrpl {
19
21template <class U, class T>
23
24template <class U, class T>
25struct STExchange<STInteger<U>, T>
26{
27 explicit STExchange() = default;
28
29 using value_type = U;
30
31 static void
33 {
34 t = u.value();
35 }
36
38 set(SField const& f, T const& t)
39 {
41 }
42};
43
44template <>
46{
47 explicit STExchange() = default;
48
50
51 static void
53 {
54 t.emplace(u.data(), u.size());
55 }
56
58 set(TypedField<STBlob> const& f, Slice const& t)
59 {
60 return std::make_unique<STBlob>(f, t.data(), t.size());
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, Buffer const& t)
79 {
80 return std::make_unique<STBlob>(f, t.data(), t.size());
81 }
82
85 {
86 return std::make_unique<STBlob>(f, std::move(t));
87 }
88};
89
90//------------------------------------------------------------------------------
91
94template <class T, class U>
96get(STObject const& st, TypedField<U> const& f)
97{
99 STBase const* const b = st.peekAtPField(f);
100 if (!b)
101 return t;
102 auto const id = b->getSType();
103 if (id == STI_NOTPRESENT)
104 return t;
105 auto const u = dynamic_cast<U const*>(b);
106 // This should never happen
107 if (!u)
108 Throw<std::runtime_error>("Wrong field type");
110 return t;
111}
112
113template <class U>
115get(STObject const& st, TypedField<U> const& f)
116{
117 return get<typename U::value_type>(st, f);
118}
122template <class U, class T>
123void
124set(STObject& st, TypedField<U> const& f, T&& t)
125{
126 st.set(STExchange<U, typename std::decay<T>::type>::set(f, std::forward<T>(t)));
127}
128
130template <class Init>
131void
132set(STObject& st, TypedField<STBlob> const& f, std::size_t size, Init&& init)
133{
134 st.set(std::make_unique<STBlob>(f, size, init));
135}
136
138template <class = void>
139void
140set(STObject& st, TypedField<STBlob> const& f, void const* data, std::size_t size)
141{
142 st.set(std::make_unique<STBlob>(f, data, size));
143}
144
146template <class U>
147void
149{
150 st.makeFieldAbsent(f);
151}
152
153} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:16
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:104
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:128
Identifies fields.
Definition SField.h:126
A type which can be exported to a well known binary format.
Definition STBase.h:115
virtual SerializedTypeID getSType() const
Definition STBase.cpp:56
std::size_t size() const
Definition STBlob.h:88
std::uint8_t const * data() const
Definition STBlob.h:94
value_type value() const noexcept
Definition STInteger.h:124
void set(SOTemplate const &)
Definition STObject.cpp:132
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:412
void makeFieldAbsent(SField const &field)
Definition STObject.cpp:525
An immutable linear range of bytes.
Definition Slice.h:26
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:77
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:60
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: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:148
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer const &t)
Definition STExchange.h:78
static void get(std::optional< Buffer > &t, STBlob const &u)
Definition STExchange.h:72
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer &&t)
Definition STExchange.h:84
static void get(std::optional< value_type > &t, STBlob const &u)
Definition STExchange.h:52
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Slice const &t)
Definition STExchange.h:58
static std::unique_ptr< STInteger< U > > set(SField const &f, T const &t)
Definition STExchange.h:38
static void get(std::optional< T > &t, STInteger< U > const &u)
Definition STExchange.h:32
Convert between serialized type U and C++ type T.
Definition STExchange.h:22
A field with a type known at compile time.
Definition SField.h:301