blob.hpp 5.96 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
#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 45 46
#define ZMQ_PUSH_OR_EMPLACE_BACK emplace_back
#define ZMQ_MOVE(x) std::move (x)
#else
47
#if defined __SUNPRO_CC
48 49 50 51 52 53 54 55
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
56
#define ZMQ_MAP_INSERT_OR_EMPLACE(k, v) insert (std::make_pair (k, v))
57 58
#endif

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

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

69 70 71 72 73 74 75
//  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.
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 81 82
        _data (static_cast<unsigned char *> (malloc (size_))),
        _size (size_),
        _owned (true)
83
    {
84
        alloc_assert (_data);
85 86 87 88
    }

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

    //  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.
102
    blob_t (unsigned char *const data_, const size_t size_, reference_tag_t) :
103 104 105
        _data (data_),
        _size (size_),
        _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 123
          memcmp (_data, other_._data, std::min (_size, other_._size));
        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 131 132 133 134
        _data = static_cast<unsigned char *> (malloc (other_._size));
        alloc_assert (_data);
        _size = other_._size;
        _owned = true;
        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 142 143 144 145
        _data = static_cast<unsigned char *> (malloc (size_));
        alloc_assert (_data);
        _size = size_;
        _owned = true;
        memcpy (_data, data_, size_);
146 147 148 149 150
    }

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

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

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

169 170 171
    blob_t (blob_t &&other_) ZMQ_NOEXCEPT : _data (other_._data),
                                            _size (other_._size),
                                            _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 180 181 182
            _data = other_._data;
            _size = other_._size;
            _owned = other_._owned;
            other_._owned = false;
183
        }
184 185
        return *this;
    }
186
#else
187
    blob_t (const blob_t &other) : _owned (false) { set_deep_copy (other); }
188 189 190 191
    blob_t &operator= (const blob_t &other)
    {
        if (this != &other) {
            clear ();
192 193
            set_deep_copy (other);
        }
194 195
        return *this;
    }
196 197
#endif

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

#endif