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 {
35 public:
36  using value_type = Slice;
37 
38  STBlob() = default;
39  STBlob(STBlob const& rhs) : STBase(rhs), value_(rhs.data(), rhs.size())
40  {
41  }
42 
43  STBlob(SField const& f, void const* data, std::size_t size)
44  : STBase(f), value_(data, size)
45  {
46  }
47 
48  STBlob(SField const& f, Buffer&& b) : STBase(f), value_(std::move(b))
49  {
50  }
51 
52  STBlob(SField const& n) : STBase(n)
53  {
54  }
55 
56  STBlob(SerialIter&, SField const& name = sfGeneric);
57 
58  STBase*
59  copy(std::size_t n, void* buf) const override
60  {
61  return emplace(n, buf, *this);
62  }
63 
64  STBase*
65  move(std::size_t n, void* buf) override
66  {
67  return emplace(n, buf, std::move(*this));
68  }
69 
71  size() const
72  {
73  return value_.size();
74  }
75 
76  std::uint8_t const*
77  data() const
78  {
79  return reinterpret_cast<std::uint8_t const*>(value_.data());
80  }
81 
83  getSType() const override
84  {
85  return STI_VL;
86  }
87 
89  getText() const override;
90 
91  void
92  add(Serializer& s) const override
93  {
94  assert(fName->isBinary());
95  assert(
97  s.addVL(value_.data(), value_.size());
98  }
99 
100  STBlob&
101  operator=(Slice const& slice)
102  {
103  value_ = Buffer(slice.data(), slice.size());
104  return *this;
105  }
106 
107  value_type
108  value() const noexcept
109  {
110  return value_;
111  }
112 
113  STBlob&
114  operator=(Buffer&& buffer)
115  {
116  value_ = std::move(buffer);
117  return *this;
118  }
119 
120  void
122  {
123  value_ = std::move(b);
124  }
125 
126  bool
127  isEquivalent(const STBase& t) const override;
128 
129  bool
130  isDefault() const override
131  {
132  return value_.empty();
133  }
134 
135 private:
137 };
138 
139 } // namespace ripple
140 
141 #endif
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:79
ripple::STBlob::STBlob
STBlob()=default
ripple::STBlob::data
std::uint8_t const * data() const
Definition: STBlob.h:77
std::string
STL class.
ripple::STBlob::operator=
STBlob & operator=(Buffer &&buffer)
Definition: STBlob.h:114
cstring
ripple::sfGeneric
const SField sfGeneric(access, 0)
Definition: SField.h:332
ripple::SField::isBinary
bool isBinary() const
Definition: SField.h:210
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STBlob::move
STBase * move(std::size_t n, void *buf) override
Definition: STBlob.h:65
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:96
ripple::Buffer::empty
bool empty() const noexcept
Definition: Buffer.h:132
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::STI_ACCOUNT
@ STI_ACCOUNT
Definition: SField.h:66
ripple::STBlob::value_
Buffer value_
Definition: STBlob.h:136
ripple::STBlob::setValue
void setValue(Buffer &&b)
Definition: STBlob.h:121
ripple::SField::fieldType
const SerializedTypeID fieldType
Definition: SField.h:127
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:149
ripple::STBlob::operator=
STBlob & operator=(Slice const &slice)
Definition: STBlob.h:101
ripple::STBlob::getText
std::string getText() const override
Definition: STBlob.cpp:31
ripple::STBlob::value
value_type value() const noexcept
Definition: STBlob.h:108
ripple::STBlob::value_type
Slice value_type
Definition: STBlob.h:36
ripple::STBlob::getSType
SerializedTypeID getSType() const override
Definition: STBlob.h:83
ripple::STBlob::add
void add(Serializer &s) const override
Definition: STBlob.h:92
ripple::STI_VL
@ STI_VL
Definition: SField.h:65
ripple::STBlob::STBlob
STBlob(STBlob const &rhs)
Definition: STBlob.h:39
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:308
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.h:130
std::uint8_t
ripple::STBlob::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STBlob.h:59
ripple::STBlob::size
std::size_t size() const
Definition: STBlob.h:71
ripple::STBlob::STBlob
STBlob(SField const &f, Buffer &&b)
Definition: STBlob.h:48
memory
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:109
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:62
std
STL namespace.
cassert
ripple::STBlob::isEquivalent
bool isEquivalent(const STBase &t) const override
Definition: STBlob.cpp:37
ripple::Serializer::addVL
int addVL(Blob const &vector)
Definition: Serializer.cpp:192
ripple::STBase::fName
SField const * fName
Definition: STBase.h:145
std::size_t
ripple::STBlob::STBlob
STBlob(SField const &n)
Definition: STBlob.h:52
ripple::STBlob::STBlob
STBlob(SField const &f, void const *data, std::size_t size)
Definition: STBlob.h:43
ripple::STBlob
Definition: STBlob.h:33