diff options
author | Jay Berkenbilt <ejb@ql.org> | 2019-06-22 03:32:47 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2019-06-22 16:13:27 +0200 |
commit | 79f6b4823b95b65290a045a59fdd4a8d9b302708 (patch) | |
tree | b94d273dd51e64a7144b669cf5ed91d826cc1387 /libqpdf/Buffer.cc | |
parent | 864a546af6e1c17e0de2dc2be6da105f454b6e54 (diff) | |
download | qpdf-79f6b4823b95b65290a045a59fdd4a8d9b302708.tar.zst |
Convert remaining public classes to use Members pattern
Have classes contain only a single private member of type
PointerHolder<Members>. This makes it safe to change the structure of
the Members class without breaking binary compatibility. Many of the
classes already follow this pattern quite successfully. This brings in
the rest of the class that are part of the public API.
Diffstat (limited to 'libqpdf/Buffer.cc')
-rw-r--r-- | libqpdf/Buffer.cc | 82 |
1 files changed, 36 insertions, 46 deletions
diff --git a/libqpdf/Buffer.cc b/libqpdf/Buffer.cc index 94e69a56..a15f73e8 100644 --- a/libqpdf/Buffer.cc +++ b/libqpdf/Buffer.cc @@ -1,53 +1,55 @@ #include <qpdf/Buffer.hh> -#include <string.h> +#include <cstring> -Buffer::Buffer() +Buffer::Members::Members(size_t size, unsigned char* buf, bool own_memory) : + own_memory(own_memory), + size(size), + buf(0) { - init(0, 0, true); + if (own_memory) + { + this->buf = (size ? new unsigned char[size] : 0); + } + else + { + this->buf = buf; + } } -Buffer::Buffer(size_t size) +Buffer::Members::~Members() { - init(size, 0, true); + if (this->own_memory) + { + delete [] this->buf; + } } -Buffer::Buffer(unsigned char* buf, size_t size) +Buffer::Buffer() : + m(new Members(0, 0, true)) { - init(size, buf, false); } -Buffer::Buffer(Buffer const& rhs) +Buffer::Buffer(size_t size) : + m(new Members(size, 0, true)) { - init(0, 0, true); - copy(rhs); } -Buffer& -Buffer::operator=(Buffer const& rhs) +Buffer::Buffer(unsigned char* buf, size_t size) : + m(new Members(size, buf, false)) { - copy(rhs); - return *this; } -Buffer::~Buffer() +Buffer::Buffer(Buffer const& rhs) { - destroy(); + copy(rhs); } -void -Buffer::init(size_t size, unsigned char* buf, bool own_memory) +Buffer& +Buffer::operator=(Buffer const& rhs) { - this->own_memory = own_memory; - this->size = size; - if (own_memory) - { - this->buf = (size ? new unsigned char[size] : 0); - } - else - { - this->buf = buf; - } + copy(rhs); + return *this; } void @@ -55,40 +57,28 @@ Buffer::copy(Buffer const& rhs) { if (this != &rhs) { - this->destroy(); - this->init(rhs.size, 0, true); - if (this->size) + this->m = new Members(rhs.m->size, 0, true); + if (this->m->size) { - memcpy(this->buf, rhs.buf, this->size); + memcpy(this->m->buf, rhs.m->buf, this->m->size); } } } -void -Buffer::destroy() -{ - if (this->own_memory) - { - delete [] this->buf; - } - this->size = 0; - this->buf = 0; -} - size_t Buffer::getSize() const { - return this->size; + return this->m->size; } unsigned char const* Buffer::getBuffer() const { - return this->buf; + return this->m->buf; } unsigned char* Buffer::getBuffer() { - return this->buf; + return this->m->buf; } |