blob.hpp 5.9 KB

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