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