blob.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_BLOB_HPP_INCLUDED__
4 #define __ZMQ_BLOB_HPP_INCLUDED__
5 
6 #include "macros.hpp"
7 #include "err.hpp"
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <algorithm>
12 #include <ios>
13 
14 #if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER > 1700
15 #define ZMQ_HAS_MOVE_SEMANTICS
16 #define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) emplace (k, v)
17 #define ZMQ_PUSH_OR_EMPLACE_BACK emplace_back
18 #define ZMQ_MOVE(x) std::move (x)
19 #else
20 #if defined __SUNPRO_CC
21 template <typename K, typename V>
22 std::pair<const K, V> make_pair_fix_const (const K &k, const V &v)
23 {
24  return std::pair<const K, V> (k, v);
25 }
26 
27 #define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) insert (make_pair_fix_const (k, v))
28 #else
29 #define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) insert (std::make_pair (k, v))
30 #endif
31 
32 #define ZMQ_PUSH_OR_EMPLACE_BACK push_back
33 #define ZMQ_MOVE(x) (x)
34 #endif
35 
36 namespace zmq
37 {
39 {
40 };
41 
42 // Object to hold dynamically allocated opaque binary data.
43 // On modern compilers, it will be movable but not copyable. Copies
44 // must be explicitly created by set_deep_copy.
45 // On older compilers, it is copyable for syntactical reasons.
46 struct blob_t
47 {
48  // Creates an empty blob_t.
49  blob_t () : _data (0), _size (0), _owned (true) {}
50 
51  // Creates a blob_t of a given size, with uninitialized content.
52  explicit blob_t (const size_t size_) :
53  _data (static_cast<unsigned char *> (malloc (size_))),
54  _size (size_),
55  _owned (true)
56  {
57  alloc_assert (!_size || _data);
58  }
59 
60  // Creates a blob_t of a given size, an initializes content by copying
61  // from another buffer.
62  blob_t (const unsigned char *const data_, const size_t size_) :
63  _data (static_cast<unsigned char *> (malloc (size_))),
64  _size (size_),
65  _owned (true)
66  {
67  alloc_assert (!size_ || _data);
68  if (size_ && _data) {
69  memcpy (_data, data_, size_);
70  }
71  }
72 
73  // Creates a blob_t for temporary use that only references a
74  // pre-allocated block of data.
75  // Use with caution and ensure that the blob_t will not outlive
76  // the referenced data.
77  blob_t (unsigned char *const data_, const size_t size_, reference_tag_t) :
78  _data (data_), _size (size_), _owned (false)
79  {
80  }
81 
82  // Returns the size of the blob_t.
83  size_t size () const { return _size; }
84 
85  // Returns a pointer to the data of the blob_t.
86  const unsigned char *data () const { return _data; }
87 
88  // Returns a pointer to the data of the blob_t.
89  unsigned char *data () { return _data; }
90 
91  // Defines an order relationship on blob_t.
92  bool operator<(blob_t const &other_) const
93  {
94  const int cmpres =
95  memcmp (_data, other_._data, std::min (_size, other_._size));
96  return cmpres < 0 || (cmpres == 0 && _size < other_._size);
97  }
98 
99  // Sets a blob_t to a deep copy of another blob_t.
100  void set_deep_copy (blob_t const &other_)
101  {
102  clear ();
103  _data = static_cast<unsigned char *> (malloc (other_._size));
104  alloc_assert (!other_._size || _data);
105  _size = other_._size;
106  _owned = true;
107  if (_size && _data) {
108  memcpy (_data, other_._data, _size);
109  }
110  }
111 
112  // Sets a blob_t to a copy of a given buffer.
113  void set (const unsigned char *const data_, const size_t size_)
114  {
115  clear ();
116  _data = static_cast<unsigned char *> (malloc (size_));
117  alloc_assert (!size_ || _data);
118  _size = size_;
119  _owned = true;
120  if (size_ && _data) {
121  memcpy (_data, data_, size_);
122  }
123  }
124 
125  // Empties a blob_t.
126  void clear ()
127  {
128  if (_owned) {
129  free (_data);
130  }
131  _data = 0;
132  _size = 0;
133  }
134 
136  {
137  if (_owned) {
138  free (_data);
139  }
140  }
141 
142 #ifdef ZMQ_HAS_MOVE_SEMANTICS
143  blob_t (const blob_t &) = delete;
144  blob_t &operator= (const blob_t &) = delete;
145 
146  blob_t (blob_t &&other_) ZMQ_NOEXCEPT : _data (other_._data),
147  _size (other_._size),
148  _owned (other_._owned)
149  {
150  other_._owned = false;
151  }
153  {
154  if (this != &other_) {
155  clear ();
156  _data = other_._data;
157  _size = other_._size;
158  _owned = other_._owned;
159  other_._owned = false;
160  }
161  return *this;
162  }
163 #else
164  blob_t (const blob_t &other) : _owned (false)
165  {
166  set_deep_copy (other);
167  }
168  blob_t &operator= (const blob_t &other)
169  {
170  if (this != &other) {
171  clear ();
172  set_deep_copy (other);
173  }
174  return *this;
175  }
176 #endif
177 
178  private:
179  unsigned char *_data;
180  size_t _size;
181  bool _owned;
182 };
183 }
184 
185 #endif
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
K
#define K(t)
Definition: sha1.c:43
zmq::blob_t::blob_t
blob_t(const unsigned char *const data_, const size_t size_)
Definition: blob.hpp:62
zmq::blob_t::blob_t
blob_t(const size_t size_)
Definition: blob.hpp:52
zmq::blob_t::data
unsigned char * data()
Definition: blob.hpp:89
zmq::blob_t::_data
unsigned char * _data
Definition: blob.hpp:179
zmq::blob_t::_owned
bool _owned
Definition: blob.hpp:181
zmq
Definition: zmq.hpp:229
alloc_assert
#define alloc_assert(x)
Definition: err.hpp:146
ZMQ_NOEXCEPT
#define ZMQ_NOEXCEPT
Definition: macros.hpp:19
macros.hpp
zmq::blob_t::blob_t
blob_t()
Definition: blob.hpp:49
zmq::blob_t::data
const unsigned char * data() const
Definition: blob.hpp:86
zmq::blob_t::size
size_t size() const
Definition: blob.hpp:83
zmq::blob_t::set_deep_copy
void set_deep_copy(blob_t const &other_)
Definition: blob.hpp:100
zmq::blob_t::set
void set(const unsigned char *const data_, const size_t size_)
Definition: blob.hpp:113
zmq::blob_t::operator<
bool operator<(blob_t const &other_) const
Definition: blob.hpp:92
zmq::blob_t::operator=
blob_t & operator=(const blob_t &other)
Definition: blob.hpp:168
zmq::blob_t::clear
void clear()
Definition: blob.hpp:126
zmq::reference_tag_t
Definition: blob.hpp:38
zmq::blob_t
Definition: blob.hpp:46
zmq::blob_t::blob_t
blob_t(unsigned char *const data_, const size_t size_, reference_tag_t)
Definition: blob.hpp:77
v
const GLdouble * v
Definition: glcorearb.h:3106
zmq::blob_t::_size
size_t _size
Definition: blob.hpp:180
zmq::blob_t::~blob_t
~blob_t()
Definition: blob.hpp:135
err.hpp
true
#define true
Definition: cJSON.c:65
zmq::blob_t::blob_t
blob_t(const blob_t &other)
Definition: blob.hpp:164
false
#define false
Definition: cJSON.c:70


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:48