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