2014-08-01 17:55:07 +08:00
|
|
|
/**
|
|
|
|
|
* ByteByffer.h
|
|
|
|
|
*
|
|
|
|
|
* Very simple implementation of the buffer class that simply wraps
|
|
|
|
|
* around a buffer of bytes
|
|
|
|
|
*
|
|
|
|
|
* @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
|
2018-05-11 17:58:55 +08:00
|
|
|
* @copyright 2014 - 2018 Copernica BV
|
2014-08-01 17:55:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Include guard
|
|
|
|
|
*/
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class definition
|
|
|
|
|
*/
|
|
|
|
|
class ByteBuffer : public Buffer
|
|
|
|
|
{
|
2016-06-16 01:32:30 +08:00
|
|
|
protected:
|
2014-08-01 17:55:07 +08:00
|
|
|
/**
|
|
|
|
|
* 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) {}
|
|
|
|
|
|
2016-06-16 01:32:30 +08:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 17:55:07 +08:00
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
virtual ~ByteBuffer() {}
|
|
|
|
|
|
2016-06-16 01:32:30 +08:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-13 19:01:27 +08:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
{
|
2018-05-11 17:58:55 +08:00
|
|
|
// make sure compilers dont complain about unused parameters
|
2018-05-11 08:52:43 +08:00
|
|
|
(void) size;
|
2018-05-11 17:58:55 +08:00
|
|
|
|
|
|
|
|
// expose the data
|
2014-08-13 19:01:27 +08:00
|
|
|
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);
|
|
|
|
|
}
|
2014-08-01 17:55:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|