// Copyright (c) 2013 by István Váradi // This file is part of VSCPL, a simple cross-platform utility library // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // The views and conclusions contained in the software and documentation are those // of the authors and should not be interpreted as representing official policies, // either expressed or implied, of the FreeBSD Project. #ifndef HU_VARADIISTVAN_SCPL_IO_BUFFER_H #define HU_VARADIISTVAN_SCPL_IO_BUFFER_H //------------------------------------------------------------------------------ #include #include #include //------------------------------------------------------------------------------ namespace hu { namespace varadiistvan { namespace scpl { namespace io { //------------------------------------------------------------------------------ /** * Base class for buffers. It maintains a memory area of a certain * capacity and the length of the data in the buffer. */ class Buffer { private: /** * The data itself. */ unsigned char* data; /** * The capacity of the buffer. */ size_t capacity; /** * The amount of data in the buffer. */ size_t length; public: /** * Construct the buffer with the given capacity. */ Buffer(size_t capacity); /** * Destroy the buffer. */ ~Buffer(); /** * Get the capacity of the buffer. */ size_t getCapacity() const; /** * Get the pointer to the data. */ const unsigned char* getData() const; /** * Get the pointer to the data for modification. */ unsigned char* getData(); /** * Get the length of the data. */ size_t getLength() const; /** * Get whether the buffer is empty or not. */ bool isEmpty() const; /** * Get the available capacity in the buffer. */ size_t getAvailable() const; /** * Reset the buffer. */ void reset(); /** * Add the given amount to the length. Should not exceed the * capacity! * * @return the actual amount added. */ size_t addLength(size_t n); /** * Append the given data to the contents of the given buffer. * * @return the number of bytes actually appended */ size_t append(const void* src, size_t size); /** * Extract data into the given buffer from the given offset. * * @return the number of bytes actually extracted. */ size_t extract(void* dest, size_t size, size_t offset = 0) const; }; //------------------------------------------------------------------------------ // Inline definitions //------------------------------------------------------------------------------ inline Buffer::Buffer(size_t capacity) : data(new unsigned char[capacity]), capacity(capacity), length(0) { } //------------------------------------------------------------------------------ inline Buffer::~Buffer() { delete[] data; } //------------------------------------------------------------------------------ inline size_t Buffer::getCapacity() const { return capacity; } //------------------------------------------------------------------------------ inline const unsigned char* Buffer::getData() const { return data; } //------------------------------------------------------------------------------ inline unsigned char* Buffer::getData() { return data; } //------------------------------------------------------------------------------ inline size_t Buffer::getLength() const { return length; } //------------------------------------------------------------------------------ inline bool Buffer::isEmpty() const { return length==0; } //------------------------------------------------------------------------------ inline size_t Buffer::getAvailable() const { return capacity - length; } //------------------------------------------------------------------------------ inline void Buffer::reset() { length = 0; } //------------------------------------------------------------------------------ inline size_t Buffer::addLength(size_t n) { size_t toAdd = std::min(n, getAvailable()); length += toAdd; return toAdd; } //------------------------------------------------------------------------------ inline size_t Buffer::append(const void* src, size_t size) { size_t toCopy = std::min(size, getAvailable()); memcpy(data + length, src, toCopy); length += toCopy; return toCopy; } //------------------------------------------------------------------------------ inline size_t Buffer::extract(void* dest, size_t size, size_t offset) const { size_t toCopy = (offset>=length) ? 0 : std::min(size, length - offset); memcpy(dest, data + offset, toCopy); return toCopy; } //------------------------------------------------------------------------------ } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */ //------------------------------------------------------------------------------ #endif // HU_VARADIISTVAN_SCPL_IO_BUFFER_H // Local Variables: // mode: C++ // c-basic-offset: 4 // indent-tabs-mode: nil // End: