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