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
|
#ifndef CLUNK_BUFFER_H__
#define CLUNK_BUFFER_H__
/* libClunk - cross-platform 3D audio API built on top SDL library
* Copyright (C) 2007-2008 Netive Media Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/types.h>
#include <string>
#include "export_clunk.h"
namespace clunk {
/*!
\brief Memory buffer
This class contains single memory buffer, allocated by malloc.
It auto frees it when it goes out of scope.
*/
class CLUNKAPI Buffer {
public:
//! Default ctor, empty buffer.
inline Buffer(): ptr(NULL), size(0) {}
//! Copy ctor
inline Buffer(const Buffer& c) : ptr(NULL), size(0) { *this = c; }
/*!
\brief Instantly allocates 'size' memory
\param[in] size size of the memory buffer
*/
inline Buffer(const int size): ptr(NULL), size(0) { set_size(size); }
//! Destructor, deallocates buffer if needed
inline ~Buffer() { free(); }
//! Gets pointer to the memory buffer
inline void *get_ptr() const { return ptr; }
//! Gets pointer to the memory buffer (const)
inline const size_t get_size() const { return size; }
/*!
\brief Tests if buffer was empty
\return returns true if the buffer is empty or deallocated.
*/
inline bool empty() const { return ptr == NULL; }
/*!
\brief Leaks buffer's content. Use it with care.
Leaks buffer's content.
Useful for exception-safe passing of malloc'ed memory to some library function which later deallocates it.
*/
inline void unlink() { ptr = NULL; size = 0; }
//! Default operator=
const Buffer& operator=(const Buffer& c);
//! Frees buffer. you do not have to call it exlicitly everywhere!
void free();
/*!
\brief Sets size of the buffer
reallocates buffer to the given size. May throw exception!
\param[in] s size of the new buffer.
*/
void set_size(size_t s);
/*! \brief Sets buffer content to a given data.
Copies given data to the buffer. Note, that functions allocates memory for a new buffer. Do not forget to deallocate 'p' if needed.
\param[in] p source pointer
\param[in] s size of the data to be copied.
*/
void set_data(const void *p, const size_t s);
/*! \brief Sets buffer content to a given data.
Copies given data to the buffer. Note, that functions allocates memory for a new buffer if 'own' == false. Do not forget to deallocate 'p' if needed.
\param[in] p source pointer
\param[in] s size of the data to be copied.
\param[in] own grab pointer and deallocate it later automatically.
*/
void set_data(void *p, const size_t s, const bool own = false);
//! Fills contents of the buffer with given byte.
void fill(const int b);
//! Appends given buffer.
void append(const Buffer &other);
//! Appends given buffer.
void append(const void *data, const size_t size);
/*!
\brief Add more bytes to the end of the buffer.
\param[in] more number of bytes to be allocated.
*/
void *reserve(const int more);
//! Returns nice string representation for the buffer. Useful for debugging.
const std::string dump() const;
//! Pops n bytes from the front
void pop(size_t n);
protected:
void *ptr;
size_t size;
};
}
#endif
|