blob.hpp 5.94 KB
Newer Older
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 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
25 26 27 28 29 30 31 32

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ZMQ_BLOB_HPP_INCLUDED__
#define __ZMQ_BLOB_HPP_INCLUDED__

33
#include "macros.hpp"
34 35
#include "err.hpp"

36
#include <stdlib.h>
37
#include <string.h>
38 39
#include <algorithm>

40
#if __cplusplus >= 201103L || defined(_MSC_VER) && _MSC_VER > 1700
41
#define ZMQ_HAS_MOVE_SEMANTICS
42
#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) emplace (k, v)
43 44 45
#define ZMQ_PUSH_OR_EMPLACE_BACK emplace_back
#define ZMQ_MOVE(x) std::move (x)
#else
46
#if defined __SUNPRO_CC
47 48 49 50 51 52 53 54
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
55
#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) insert (std::make_pair (k, v))
56 57
#endif

58 59 60
#define ZMQ_PUSH_OR_EMPLACE_BACK push_back
#define ZMQ_MOVE(x) (x)
#endif
61

62 63
namespace zmq
{
64 65 66
struct reference_tag_t
{
};
67

68 69 70 71 72 73 74
//  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.
75
    blob_t () : _data (0), _size (0), _owned (true) {}
76 77

    //  Creates a blob_t of a given size, with uninitialized content.
78
    explicit blob_t (const size_t size_) :
79 80 81
        _data (static_cast<unsigned char *> (malloc (size_))),
        _size (size_),
        _owned (true)
82
    {
83
        alloc_assert (_data);
84 85 86 87
    }

    //  Creates a blob_t of a given size, an initializes content by copying
    // from another buffer.
88
    blob_t (const unsigned char *const data_, const size_t size_) :
89 90 91
        _data (static_cast<unsigned char *> (malloc (size_))),
        _size (size_),
        _owned (true)
92
    {
93 94
        alloc_assert (_data);
        memcpy (_data, data_, size_);
95 96 97 98 99 100
    }

    //  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.
101
    blob_t (unsigned char *const data_, const size_t size_, reference_tag_t) :
102 103 104
        _data (data_),
        _size (size_),
        _owned (false)
105 106
    {
    }
107

108
    //  Returns the size of the blob_t.
109
    size_t size () const { return _size; }
110

111
    //  Returns a pointer to the data of the blob_t.
112
    const unsigned char *data () const { return _data; }
113

114
    //  Returns a pointer to the data of the blob_t.
115
    unsigned char *data () { return _data; }
116

117
    //  Defines an order relationship on blob_t.
118
    bool operator< (blob_t const &other_) const
119
    {
120
        const int cmpres =
121 122
          memcmp (_data, other_._data, std::min (_size, other_._size));
        return cmpres < 0 || (cmpres == 0 && _size < other_._size);
123
    }
124

125
    //  Sets a blob_t to a deep copy of another blob_t.
126
    void set_deep_copy (blob_t const &other_)
127 128
    {
        clear ();
129 130 131 132 133
        _data = static_cast<unsigned char *> (malloc (other_._size));
        alloc_assert (_data);
        _size = other_._size;
        _owned = true;
        memcpy (_data, other_._data, _size);
134 135 136
    }

    //  Sets a blob_t to a copy of a given buffer.
137
    void set (const unsigned char *const data_, const size_t size_)
138 139
    {
        clear ();
140 141 142 143 144
        _data = static_cast<unsigned char *> (malloc (size_));
        alloc_assert (_data);
        _size = size_;
        _owned = true;
        memcpy (_data, data_, size_);
145 146 147 148 149
    }

    //  Empties a blob_t.
    void clear ()
    {
150 151
        if (_owned) {
            free (_data);
152
        }
153 154
        _data = 0;
        _size = 0;
155
    }
156

157 158
    ~blob_t ()
    {
159 160
        if (_owned) {
            free (_data);
161
        }
162
    }
163 164

#ifdef ZMQ_HAS_MOVE_SEMANTICS
165 166 167
    blob_t (const blob_t &) = delete;
    blob_t &operator= (const blob_t &) = delete;

168 169 170
    blob_t (blob_t &&other_) ZMQ_NOEXCEPT : _data (other_._data),
                                            _size (other_._size),
                                            _owned (other_._owned)
171
    {
172
        other_._owned = false;
173
    }
174
    blob_t &operator= (blob_t &&other_) ZMQ_NOEXCEPT
175
    {
176
        if (this != &other_) {
177
            clear ();
178 179 180 181
            _data = other_._data;
            _size = other_._size;
            _owned = other_._owned;
            other_._owned = false;
182
        }
183 184
        return *this;
    }
185
#else
186
    blob_t (const blob_t &other) : _owned (false) { set_deep_copy (other); }
187 188 189 190
    blob_t &operator= (const blob_t &other)
    {
        if (this != &other) {
            clear ();
191 192
            set_deep_copy (other);
        }
193 194
        return *this;
    }
195 196
#endif

197
  private:
198 199 200
    unsigned char *_data;
    size_t _size;
    bool _owned;
201
};
202 203 204
}

#endif