1/*
2 Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifndef __ZMQ_BLOB_HPP_INCLUDED__
31#define __ZMQ_BLOB_HPP_INCLUDED__
32
33#include "macros.hpp"
34#include "err.hpp"
35
36#include <stdlib.h>
37#include <string.h>
38#include <algorithm>
39#include <ios>
40
41#if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER > 1700
42#define ZMQ_HAS_MOVE_SEMANTICS
43#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) emplace (k, v)
44#define ZMQ_PUSH_OR_EMPLACE_BACK emplace_back
45#define ZMQ_MOVE(x) std::move (x)
46#else
47#if defined __SUNPRO_CC
48template <typename K, typename V>
49std::pair<const K, V> make_pair_fix_const (const K &k, const V &v)
50{
51 return std::pair<const K, V> (k, v);
52}
53
54#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) insert (make_pair_fix_const (k, v))
55#else
56#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) insert (std::make_pair (k, v))
57#endif
58
59#define ZMQ_PUSH_OR_EMPLACE_BACK push_back
60#define ZMQ_MOVE(x) (x)
61#endif
62
63namespace zmq
64{
65struct reference_tag_t
66{
67};
68
69// Object to hold dynamically allocated opaque binary data.
70// On modern compilers, it will be movable but not copyable. Copies
71// must be explicitly created by set_deep_copy.
72// On older compilers, it is copyable for syntactical reasons.
73struct blob_t
74{
75 // Creates an empty blob_t.
76 blob_t () : _data (0), _size (0), _owned (true) {}
77
78 // Creates a blob_t of a given size, with uninitialized content.
79 explicit blob_t (const size_t size_) :
80 _data (static_cast<unsigned char *> (malloc (size_))),
81 _size (size_),
82 _owned (true)
83 {
84 alloc_assert (_data);
85 }
86
87 // Creates a blob_t of a given size, an initializes content by copying
88 // from another buffer.
89 blob_t (const unsigned char *const data_, const size_t size_) :
90 _data (static_cast<unsigned char *> (malloc (size_))),
91 _size (size_),
92 _owned (true)
93 {
94 alloc_assert (_data);
95 memcpy (_data, data_, size_);
96 }
97
98 // Creates a blob_t for temporary use that only references a
99 // pre-allocated block of data.
100 // Use with caution and ensure that the blob_t will not outlive
101 // the referenced data.
102 blob_t (unsigned char *const data_, const size_t size_, reference_tag_t) :
103 _data (data_),
104 _size (size_),
105 _owned (false)
106 {
107 }
108
109 // Returns the size of the blob_t.
110 size_t size () const { return _size; }
111
112 // Returns a pointer to the data of the blob_t.
113 const unsigned char *data () const { return _data; }
114
115 // Returns a pointer to the data of the blob_t.
116 unsigned char *data () { return _data; }
117
118 // Defines an order relationship on blob_t.
119 bool operator< (blob_t const &other_) const
120 {
121 const int cmpres =
122 memcmp (_data, other_._data, std::min (_size, other_._size));
123 return cmpres < 0 || (cmpres == 0 && _size < other_._size);
124 }
125
126 // Sets a blob_t to a deep copy of another blob_t.
127 void set_deep_copy (blob_t const &other_)
128 {
129 clear ();
130 _data = static_cast<unsigned char *> (malloc (other_._size));
131 alloc_assert (_data);
132 _size = other_._size;
133 _owned = true;
134 memcpy (_data, other_._data, _size);
135 }
136
137 // Sets a blob_t to a copy of a given buffer.
138 void set (const unsigned char *const data_, const size_t size_)
139 {
140 clear ();
141 _data = static_cast<unsigned char *> (malloc (size_));
142 alloc_assert (_data);
143 _size = size_;
144 _owned = true;
145 memcpy (_data, data_, size_);
146 }
147
148 // Empties a blob_t.
149 void clear ()
150 {
151 if (_owned) {
152 free (_data);
153 }
154 _data = 0;
155 _size = 0;
156 }
157
158 ~blob_t ()
159 {
160 if (_owned) {
161 free (_data);
162 }
163 }
164
165#ifdef ZMQ_HAS_MOVE_SEMANTICS
166 blob_t (const blob_t &) = delete;
167 blob_t &operator= (const blob_t &) = delete;
168
169 blob_t (blob_t &&other_) ZMQ_NOEXCEPT : _data (other_._data),
170 _size (other_._size),
171 _owned (other_._owned)
172 {
173 other_._owned = false;
174 }
175 blob_t &operator= (blob_t &&other_) ZMQ_NOEXCEPT
176 {
177 if (this != &other_) {
178 clear ();
179 _data = other_._data;
180 _size = other_._size;
181 _owned = other_._owned;
182 other_._owned = false;
183 }
184 return *this;
185 }
186#else
187 blob_t (const blob_t &other) : _owned (false) { set_deep_copy (other); }
188 blob_t &operator= (const blob_t &other)
189 {
190 if (this != &other) {
191 clear ();
192 set_deep_copy (other);
193 }
194 return *this;
195 }
196#endif
197
198 private:
199 unsigned char *_data;
200 size_t _size;
201 bool _owned;
202};
203}
204
205#endif
206