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#include <memory>
32#include <optional>
33#include <stdexcept>
34#include <type_traits>
35#include <utility>
36
37namespace ripple {
38
40template <class U, class T>
42
43template <class U, class T>
44struct STExchange<STInteger<U>, T>
45{
46 explicit STExchange() = default;
47
48 using value_type = U;
49
50 static void
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
63template <>
65{
66 explicit STExchange() = default;
67
69
70 static void
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
83template <>
85{
86 explicit STExchange() = default;
87
89
90 static void
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
113template <class T, class U>
115get(STObject const& st, TypedField<U> const& f)
116{
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");
129 return t;
130}
131
132template <class U>
134get(STObject const& st, TypedField<U> const& f)
135{
136 return get<typename U::value_type>(st, f);
137}
141template <class U, class T>
142void
143set(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
150template <class Init>
151void
152set(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
158template <class = void>
159void
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
169template <class U>
170void
172{
173 st.makeFieldAbsent(f);
174}
175
176} // namespace ripple
177
178#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:144
A type which can be exported to a well known binary format.
Definition: STBase.h:124
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:68
std::uint8_t const * data() const
Definition: STBlob.h:118
std::size_t size() const
Definition: STBlob.h:112
value_type value() const noexcept
Definition: STInteger.h:146
const STBase * peekAtPField(SField const &field) const
Definition: STObject.cpp:427
void makeFieldAbsent(SField const &field)
Definition: STObject.cpp:540
void set(const SOTemplate &)
Definition: STObject.cpp:126
An immutable linear range of bytes.
Definition: Slice.h:45
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
T emplace(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:316
void erase(STObject &st, TypedField< U > const &f)
Remove a field in an STObject.
Definition: STExchange.h:171
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
Definition: BasicConfig.h:356
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer const &t)
Definition: STExchange.h:97
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Buffer &&t)
Definition: STExchange.h:103
static void get(std::optional< Buffer > &t, STBlob const &u)
Definition: STExchange.h:91
static void get(std::optional< value_type > &t, STBlob const &u)
Definition: STExchange.h:71
static std::unique_ptr< STBlob > set(TypedField< STBlob > const &f, Slice const &t)
Definition: STExchange.h:77
static void get(std::optional< T > &t, STInteger< U > const &u)
Definition: STExchange.h:51
static std::unique_ptr< STInteger< U > > set(SField const &f, T const &t)
Definition: STExchange.h:57
Convert between serialized type U and C++ type T.
Definition: STExchange.h:41
A field with a type known at compile time.
Definition: SField.h:315