2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-05 03:40:53 +00:00

Merge pull request #756 from jbarlow83/unique-ptr

Use unique_ptr and move constructor for Buffer::Members
This commit is contained in:
Jay Berkenbilt 2022-09-05 17:25:35 -04:00 committed by GitHub
commit 1264f4733e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 2 deletions

View File

@ -49,6 +49,10 @@ class Buffer
QPDF_DLL
Buffer& operator=(Buffer const&);
QPDF_DLL
Buffer(Buffer &&) noexcept;
QPDF_DLL
Buffer& operator=(Buffer &&) noexcept;
QPDF_DLL
size_t getSize() const;
QPDF_DLL
unsigned char const* getBuffer() const;
@ -75,7 +79,7 @@ class Buffer
void copy(Buffer const&);
std::shared_ptr<Members> m;
std::unique_ptr<Members> m;
};
#endif // BUFFER_HH

View File

@ -48,12 +48,24 @@ Buffer::operator=(Buffer const& 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::shared_ptr<Members>(new Members(rhs.m->size, nullptr, true));
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);
}

View File

@ -37,6 +37,23 @@ main()
assert(bc2p[1] == 'W');
}
{
// Test that buffers can be moved.
Buffer bm1(2);
unsigned char* bm1p = bm1.getBuffer();
bm1p[0] = 'Q';
bm1p[1] = 'W';
Buffer bm2(std::move(bm1));
bm1p[0] = 'R';
unsigned char* bm2p = bm2.getBuffer();
assert(bm2p == bm1p);
assert(bm2p[0] == 'R');
Buffer bm3 = std::move(bm2);
unsigned char* bm3p = bm3.getBuffer();
assert(bm3p == bm2p);
}
try {
Pl_Discard discard;
Pl_Count count("count", &discard);