1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include "utils/Buffer.h"
- #include <stdlib.h>
- #include <string.h>
- #include "utils/Utility.h"
- Buffer::Buffer(int initialSize)
- : length(0), capacity(initialSize <= 0 ? 1 : initialSize),
- buffer(static_cast<char*>(malloc(static_cast<unsigned int>(capacity)))) {
- }
- Buffer::Buffer(const Buffer& other)
- : length(other.length), capacity(other.capacity),
- buffer(static_cast<char*>(malloc(static_cast<unsigned int>(capacity)))) {
- memcpy(buffer, other.buffer, static_cast<unsigned int>(length));
- }
- Buffer::Buffer(Buffer&& other) : length(0), capacity(0), buffer(nullptr) {
- swap(other);
- }
- Buffer::~Buffer() {
- free(buffer);
- }
- Buffer& Buffer::operator=(Buffer other) {
- swap(other);
- return *this;
- }
- Buffer& Buffer::add(const void* data, int size) {
- while(length + size > capacity) {
- capacity *= 2;
- buffer = static_cast<char*>(
- realloc(buffer, static_cast<unsigned int>(capacity)));
- }
- memcpy(buffer + length, data, static_cast<unsigned int>(size));
- length += size;
- return *this;
- }
- int Buffer::getLength() const {
- return length;
- }
- Buffer::operator const char*() const {
- return buffer;
- }
- void Buffer::swap(Buffer& other) {
- Core::swap(length, other.length);
- Core::swap(capacity, other.capacity);
- Core::swap(buffer, other.buffer);
- }
- void Buffer::clear() {
- length = 0;
- }
|