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/Pl_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/Pl_Buffer.cc')
-rw-r--r-- | libqpdf/Pl_Buffer.cc | 60 |
1 files changed, 29 insertions, 31 deletions
diff --git a/libqpdf/Pl_Buffer.cc b/libqpdf/Pl_Buffer.cc index d11959f4..70d906ea 100644 --- a/libqpdf/Pl_Buffer.cc +++ b/libqpdf/Pl_Buffer.cc @@ -4,13 +4,22 @@ #include <assert.h> #include <string.h> -Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) : - Pipeline(identifier, next), +Pl_Buffer::Members::Members() : ready(true), total_size(0) { } +Pl_Buffer::Members::~Members() +{ +} + +Pl_Buffer::Pl_Buffer(char const* identifier, Pipeline* next) : + Pipeline(identifier, next), + m(new Members()) +{ +} + Pl_Buffer::~Pl_Buffer() { } @@ -18,32 +27,25 @@ Pl_Buffer::~Pl_Buffer() void Pl_Buffer::write(unsigned char* buf, size_t len) { - PointerHolder<Buffer> cur_buf; - size_t cur_size = 0; - if (! this->data.empty()) + if (this->m->data.getPointer() == 0) { - cur_buf = this->data.back(); - cur_size = cur_buf->getSize(); + this->m->data = new Buffer(len); } - size_t left = cur_size - this->total_size; + size_t cur_size = this->m->data->getSize(); + size_t left = cur_size - this->m->total_size; if (left < len) { - size_t new_size = std::max(this->total_size + len, 2 * cur_size); - Buffer* b = new Buffer(new_size); - if (cur_buf.getPointer()) - { - memcpy(b->getBuffer(), cur_buf->getBuffer(), this->total_size); - } - this->data.clear(); - cur_buf = b; - this->data.push_back(cur_buf); + size_t new_size = std::max(this->m->total_size + len, 2 * cur_size); + PointerHolder<Buffer> b = new Buffer(new_size); + memcpy(b->getBuffer(), this->m->data->getBuffer(), this->m->total_size); + this->m->data = b; } if (len) { - memcpy(cur_buf->getBuffer() + this->total_size, buf, len); - this->total_size += len; + memcpy(this->m->data->getBuffer() + this->m->total_size, buf, len); + this->m->total_size += len; } - this->ready = false; + this->m->ready = false; if (getNext(true)) { @@ -54,7 +56,7 @@ Pl_Buffer::write(unsigned char* buf, size_t len) void Pl_Buffer::finish() { - this->ready = true; + this->m->ready = true; if (getNext(true)) { getNext()->finish(); @@ -64,21 +66,17 @@ Pl_Buffer::finish() Buffer* Pl_Buffer::getBuffer() { - if (! this->ready) + if (! this->m->ready) { throw std::logic_error("Pl_Buffer::getBuffer() called when not ready"); } - Buffer* b = new Buffer(this->total_size); - unsigned char* p = b->getBuffer(); - if (! this->data.empty()) + Buffer* b = new Buffer(this->m->total_size); + if (this->m->total_size > 0) { - PointerHolder<Buffer> bp = this->data.back(); - this->data.clear(); - memcpy(p, bp->getBuffer(), this->total_size); + unsigned char* p = b->getBuffer(); + memcpy(p, this->m->data->getBuffer(), this->m->total_size); } - this->total_size = 0; - this->ready = false; - + this->m = new Members(); return b; } |