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/CountedObject.h>
25 #include <ripple/basics/Slice.h>
26 #include <ripple/protocol/STBase.h>
27 
28 #include <cassert>
29 #include <cstring>
30 #include <memory>
31 
32 namespace ripple {
33 
34 // variable length byte string
35 class STBlob : public STBase, public CountedObject<STBlob>
36 {
38 
39 public:
40  using value_type = Slice;
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 
83 private:
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 
92 inline STBlob::STBlob(STBlob const& rhs)
93  : STBase(rhs), CountedObject<STBlob>(rhs), value_(rhs.data(), rhs.size())
94 {
95 }
96 
97 inline STBlob::STBlob(SField const& f, void const* data, std::size_t size)
98  : STBase(f), value_(data, size)
99 {
100 }
101 
102 inline STBlob::STBlob(SField const& f, Buffer&& b)
103  : STBase(f), value_(std::move(b))
104 {
105 }
106 
107 inline STBlob::STBlob(SField const& n) : STBase(n)
108 {
109 }
110 
111 inline std::size_t
113 {
114  return value_.size();
115 }
116 
117 inline std::uint8_t const*
119 {
120  return reinterpret_cast<std::uint8_t const*>(value_.data());
121 }
122 
123 inline STBlob&
125 {
126  value_ = Buffer(slice.data(), slice.size());
127  return *this;
128 }
129 
130 inline STBlob::value_type
131 STBlob::value() const noexcept
132 {
133  return value_;
134 }
135 
136 inline STBlob&
138 {
139  value_ = std::move(buffer);
140  return *this;
141 }
142 
143 inline void
145 {
146  value_ = std::move(b);
147 }
148 
149 } // namespace ripple
150 
151 #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
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
std::string
STL class.
cstring
ripple::sfGeneric
const SField sfGeneric(access, 0)
Definition: SField.h:356
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::STBlob::value
value_type value() const noexcept
Definition: STBlob.h:131
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:104
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:124
ripple::STBlob::value_
Buffer value_
Definition: STBlob.h:37
ripple::STBlob::setValue
void setValue(Buffer &&b)
Definition: STBlob.h:144
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:311
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:112
memory
ripple::STBlob::move
STBase * move(std::size_t n, void *buf) override
Definition: STBlob.cpp:37
ripple::Serializer
Definition: Serializer.h:40
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:139
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:121
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:35
ripple::STBlob::data
std::uint8_t const * data() const
Definition: STBlob.h:118