mirror of
https://github.com/qpdf/qpdf.git
synced 2024-11-09 23:00:57 +00:00
12967bdf8a
Since Buffer has always implemented its copy constructor with a deep copy, its Members object will never have multiple owners. Change to unique_ptr. Also implement move constructors for Buffer, since there may be cases where a deep copy is not needed.
92 lines
1.4 KiB
C++
92 lines
1.4 KiB
C++
#include <qpdf/Buffer.hh>
|
|
|
|
#include <cstring>
|
|
|
|
Buffer::Members::Members(size_t size, unsigned char* buf, bool own_memory) :
|
|
own_memory(own_memory),
|
|
size(size),
|
|
buf(nullptr)
|
|
{
|
|
if (own_memory) {
|
|
this->buf = (size ? new unsigned char[size] : nullptr);
|
|
} else {
|
|
this->buf = buf;
|
|
}
|
|
}
|
|
|
|
Buffer::Members::~Members()
|
|
{
|
|
if (this->own_memory) {
|
|
delete[] this->buf;
|
|
}
|
|
}
|
|
|
|
Buffer::Buffer() :
|
|
m(new Members(0, nullptr, true))
|
|
{
|
|
}
|
|
|
|
Buffer::Buffer(size_t size) :
|
|
m(new Members(size, nullptr, true))
|
|
{
|
|
}
|
|
|
|
Buffer::Buffer(unsigned char* buf, size_t size) :
|
|
m(new Members(size, buf, false))
|
|
{
|
|
}
|
|
|
|
Buffer::Buffer(Buffer const& rhs)
|
|
{
|
|
copy(rhs);
|
|
}
|
|
|
|
Buffer&
|
|
Buffer::operator=(Buffer const& rhs)
|
|
{
|
|
copy(rhs);
|
|
return *this;
|
|
}
|
|
|
|
Buffer::Buffer(Buffer&& rhs) noexcept :
|
|
m(std::move(rhs.m))
|
|
{
|
|
}
|
|
|
|
Buffer&
|
|
Buffer::operator=(Buffer&& rhs) noexcept
|
|
{
|
|
std::swap(this->m, rhs.m);
|
|
return *this;
|
|
}
|
|
|
|
void
|
|
Buffer::copy(Buffer const& rhs)
|
|
{
|
|
if (this != &rhs) {
|
|
this->m =
|
|
std::unique_ptr<Members>(new Members(rhs.m->size, nullptr, true));
|
|
if (this->m->size) {
|
|
memcpy(this->m->buf, rhs.m->buf, this->m->size);
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t
|
|
Buffer::getSize() const
|
|
{
|
|
return this->m->size;
|
|
}
|
|
|
|
unsigned char const*
|
|
Buffer::getBuffer() const
|
|
{
|
|
return this->m->buf;
|
|
}
|
|
|
|
unsigned char*
|
|
Buffer::getBuffer()
|
|
{
|
|
return this->m->buf;
|
|
}
|