diff options
author | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 16:49:52 -0500 |
---|---|---|
committer | Jesse Gilles <jgilles@multitech.com> | 2015-04-20 16:49:52 -0500 |
commit | 17b117e73df71925d73ee026b4f54aa1867ce0a5 (patch) | |
tree | 382610c8e598a77a961c5ceb32b9b614ed00e757 /src/MTS_Buffer.cpp | |
download | libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.tar.gz libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.tar.bz2 libmts-17b117e73df71925d73ee026b4f54aa1867ce0a5.zip |
initial commit
Diffstat (limited to 'src/MTS_Buffer.cpp')
-rw-r--r-- | src/MTS_Buffer.cpp | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/MTS_Buffer.cpp b/src/MTS_Buffer.cpp new file mode 100644 index 0000000..91ece42 --- /dev/null +++ b/src/MTS_Buffer.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2015 by Multi-Tech Systems + * + * This file is part of libmts. + * + * libmts 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 of the License, or + * (at your option) any later version. + * + * libmts 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 libmts. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <mts/MTS_Buffer.h> +#include <stdexcept> +#include <string> + +using namespace MTS; + +const uint32_t Buffer::DEFAULT_CAPACITY = 80; + +Buffer::Buffer(uint32_t capacity) { + setCapacity(capacity); +} + +Buffer::Buffer(const uint8_t* bytes, uint32_t count) +: m_vBuffer(bytes, bytes + count) { + +} + +Buffer::Buffer(const Buffer& other) +: m_vBuffer(other.m_vBuffer.begin(), other.m_vBuffer.end()) { + +} + +Buffer::~Buffer() { + +} + +Buffer& +Buffer::operator=(const Buffer& other) { + if (&other == this) { + return *this; + } + m_vBuffer = other.m_vBuffer; + return *this; +} + +const uint8_t* +Buffer::getBuffer() const { + return &m_vBuffer.at(0); +} + +std::string Buffer::str() const { + return std::string(reinterpret_cast<const char*>(&m_vBuffer.at(0)), m_vBuffer.size()); +} + +uint32_t Buffer::getSize() const { + return m_vBuffer.size(); +} + +void Buffer::setSize(uint32_t newSize) { + m_vBuffer.resize(newSize); +} + +uint32_t Buffer::getCapacity() const { + return m_vBuffer.capacity(); +} + +void Buffer::setCapacity(uint32_t newCapacity) { + m_vBuffer.reserve(newCapacity); +} + +void Buffer::clear() { + m_vBuffer.clear(); +} + +void Buffer::compact() { + m_vBuffer.resize(m_vBuffer.size()); +} + +uint8_t Buffer::operator[](uint32_t index) const { + return m_vBuffer[index]; +} + +uint8_t Buffer::operator[](uint32_t index) { + return m_vBuffer[index]; +} + +const uint8_t& +Buffer::at(uint32_t index) const { + return m_vBuffer.at(index); +} + +uint8_t& +Buffer::at(uint32_t index) { + return m_vBuffer.at(index); +} + +Buffer& +Buffer::append(uint8_t byte) { + m_vBuffer.push_back(byte); + return *this; +} + +Buffer& Buffer::append(const uint8_t* bytes, uint32_t count) { + m_vBuffer.resize(m_vBuffer.size() + count); + std::copy(bytes, bytes + count, std::back_inserter(m_vBuffer)); + return *this; +} + +Buffer& +Buffer::insert(uint32_t index, uint8_t byte) { + m_vBuffer.insert(m_vBuffer.begin() + index, byte); + return *this; +} + +Buffer& +Buffer::insert(uint32_t index, const uint8_t* bytes, uint32_t count) { + m_vBuffer.insert(m_vBuffer.begin() + index, bytes, bytes + count); + return *this; +} + +Buffer& +Buffer::remove(uint32_t index) { + m_vBuffer.erase(m_vBuffer.begin() + index); + return *this; +} + +Buffer& +Buffer::remove(uint32_t start, uint32_t end) { + m_vBuffer.erase(m_vBuffer.begin() + start, m_vBuffer.begin() + end); + return *this; +} + +Buffer& +Buffer::replace(uint32_t start, uint32_t end, const uint8_t* bytes, +uint32_t count) { + if (end - start == count) { + if (start > end || end > m_vBuffer.size()) { + throw std::out_of_range("Buffer| index out of bounds"); + } + std::copy(bytes, bytes + count, m_vBuffer.begin()); + return *this; + } + return remove(start, end).insert(start, bytes, count); +} + +Buffer* Buffer::clone() const { + return new Buffer(*this); +} + |