File: bytebuffer.h

package info (click to toggle)
amqp-cpp 4.3.27-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,384 kB
  • sloc: cpp: 10,021; ansic: 191; makefile: 95
file content (140 lines) | stat: -rw-r--r-- 2,818 bytes parent folder | download
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
/**
 *  ByteByffer.h
 *
 *  Very simple implementation of the buffer class that simply wraps
 *  around a buffer of bytes
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 - 2018 Copernica BV
 */

/**
 *  Include guard
 */
#pragma once

/**
 *  Open namespace
 */
namespace AMQP {

/**
 *  Class definition
 */
class ByteBuffer : public Buffer
{
protected:
    /**
     *  The actual byte buffer
     *  @var const char *
     */
    const char *_data;
    
    /**
     *  Size of the buffer
     *  @var size_t
     */
    size_t _size;

public:
    /**
     *  Constructor
     *  @param  data
     *  @param  size
     */
    ByteBuffer(const char *data, size_t size) : _data(data), _size(size) {}
    
    /**
     *  No copy'ing
     *  @param  that
     */
    ByteBuffer(const ByteBuffer &that) = delete;
    
    /**
     *  Move constructor
     *  @param  that
     */
    ByteBuffer(ByteBuffer &&that) : _data(that._data), _size(that._size)
    {
        // reset other object
        that._data = nullptr;
        that._size = 0;
    }
    
    /**
     *  Destructor
     */
    virtual ~ByteBuffer() {}

    /**
     *  Move assignment operator
     *  @param  that
     */
    ByteBuffer &operator=(ByteBuffer &&that)
    {
        // skip self-assignment
        if (this == &that) return *this;
        
        // copy members
        _data = that._data;
        _size = that._size;
        
        // reset other object
        that._data = nullptr;
        that._size = 0;
        
        // done
        return *this;
    }

    /**
     *  Total size of the buffer
     *  @return size_t
     */
    virtual size_t size() const override
    {
        return _size;
    }

    /**
     *  Get access to a single byte
     *  @param  pos         position in the buffer
     *  @return char        value of the byte in the buffer
     */
    virtual char byte(size_t pos) const override
    {
        return _data[pos];
    }

    /**
     *  Get access to the raw data
     *  @param  pos         position in the buffer
     *  @param  size        number of continuous bytes
     *  @return char*
     */
    virtual const char *data(size_t pos, size_t size) const override
    {
        // make sure compilers dont complain about unused parameters
        (void) size;
        
        // expose the data
        return _data + pos;
    }
    
    /**
     *  Copy bytes to a buffer
     *  @param  pos         position in the buffer
     *  @param  size        number of bytes to copy
     *  @param  buffer      buffer to copy into
     *  @return size_t      pointer to buffer
     */
    virtual void *copy(size_t pos, size_t size, void *buffer) const override
    {
        return memcpy(buffer, _data + pos, size);
    }
};

/**
 *  End namespace
 */
}