rippled
Loading...
Searching...
No Matches
STBlob.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_STBLOB_H_INCLUDED
21#define RIPPLE_PROTOCOL_STBLOB_H_INCLUDED
22
23#include <xrpl/basics/Buffer.h>
24#include <xrpl/basics/CountedObject.h>
25#include <xrpl/basics/Slice.h>
26#include <xrpl/beast/utility/instrumentation.h>
27#include <xrpl/protocol/STBase.h>
28
29#include <cstring>
30
31namespace ripple {
32
33// variable length byte string
34class STBlob : public STBase, public CountedObject<STBlob>
35{
37
38public:
40
41 STBlob() = default;
42 STBlob(STBlob const& rhs);
43
44 STBlob(SField const& f, void const* data, std::size_t size);
45 STBlob(SField const& f, Buffer&& b);
46 STBlob(SField const& n);
47 STBlob(SerialIter&, SField const& name = sfGeneric);
48
50 size() const;
51
52 std::uint8_t const*
53 data() const;
54
56 getSType() const override;
57
59 getText() const override;
60
61 void
62 add(Serializer& s) const override;
63
64 bool
65 isEquivalent(STBase const& t) const override;
66
67 bool
68 isDefault() const override;
69
70 STBlob&
71 operator=(Slice const& slice);
72
74 value() const noexcept;
75
76 STBlob&
77 operator=(Buffer&& buffer);
78
79 void
80 setValue(Buffer&& b);
81
82private:
83 STBase*
84 copy(std::size_t n, void* buf) const override;
85 STBase*
86 move(std::size_t n, void* buf) override;
87
88 friend class detail::STVar;
89};
90
91inline STBlob::STBlob(STBlob const& rhs)
92 : STBase(rhs), CountedObject<STBlob>(rhs), value_(rhs.data(), rhs.size())
93{
94}
95
96inline STBlob::STBlob(SField const& f, void const* data, std::size_t size)
97 : STBase(f), value_(data, size)
98{
99}
100
101inline STBlob::STBlob(SField const& f, Buffer&& b)
102 : STBase(f), value_(std::move(b))
103{
104}
105
106inline STBlob::STBlob(SField const& n) : STBase(n)
107{
108}
109
110inline std::size_t
112{
113 return value_.size();
114}
115
116inline std::uint8_t const*
118{
119 return reinterpret_cast<std::uint8_t const*>(value_.data());
120}
121
122inline STBlob&
124{
125 value_ = Buffer(slice.data(), slice.size());
126 return *this;
127}
128
130STBlob::value() const noexcept
131{
132 return value_;
133}
134
135inline STBlob&
137{
138 value_ = std::move(buffer);
139 return *this;
140}
141
142inline void
144{
145 value_ = std::move(b);
146}
147
148} // namespace ripple
149
150#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
Tracks the number of instances of an object.
Identifies fields.
Definition SField.h:143
A type which can be exported to a well known binary format.
Definition STBase.h:135
SerializedTypeID getSType() const override
Definition STBlob.cpp:51
STBlob & operator=(Slice const &slice)
Definition STBlob.h:123
value_type value() const noexcept
Definition STBlob.h:130
std::uint8_t const * data() const
Definition STBlob.h:117
void setValue(Buffer &&b)
Definition STBlob.h:143
friend class detail::STVar
Definition STBlob.h:88
bool isEquivalent(STBase const &t) const override
Definition STBlob.cpp:74
std::size_t size() const
Definition STBlob.h:111
STBase * move(std::size_t n, void *buf) override
Definition STBlob.cpp:45
STBlob()=default
void add(Serializer &s) const override
Definition STBlob.cpp:63
Buffer value_
Definition STBlob.h:36
STBase * copy(std::size_t n, void *buf) const override
Definition STBlob.cpp:39
bool isDefault() const override
Definition STBlob.cpp:81
std::string getText() const override
Definition STBlob.cpp:57
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
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
SerializedTypeID
Definition SField.h:107
SField const sfGeneric
STL namespace.