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