mirror of
https://github.com/qpdf/qpdf.git
synced 2024-09-27 12:39:06 +00:00
Take advantage of unique_ptr and move construction for Buffer
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.
This commit is contained in:
parent
952b00aecd
commit
12967bdf8a
@ -49,6 +49,10 @@ class Buffer
|
|||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
Buffer& operator=(Buffer const&);
|
Buffer& operator=(Buffer const&);
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
|
Buffer(Buffer &&) noexcept;
|
||||||
|
QPDF_DLL
|
||||||
|
Buffer& operator=(Buffer &&) noexcept;
|
||||||
|
QPDF_DLL
|
||||||
size_t getSize() const;
|
size_t getSize() const;
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
unsigned char const* getBuffer() const;
|
unsigned char const* getBuffer() const;
|
||||||
@ -75,7 +79,7 @@ class Buffer
|
|||||||
|
|
||||||
void copy(Buffer const&);
|
void copy(Buffer const&);
|
||||||
|
|
||||||
std::shared_ptr<Members> m;
|
std::unique_ptr<Members> m;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BUFFER_HH
|
#endif // BUFFER_HH
|
||||||
|
@ -48,12 +48,24 @@ Buffer::operator=(Buffer const& rhs)
|
|||||||
return *this;
|
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
|
void
|
||||||
Buffer::copy(Buffer const& rhs)
|
Buffer::copy(Buffer const& rhs)
|
||||||
{
|
{
|
||||||
if (this != &rhs) {
|
if (this != &rhs) {
|
||||||
this->m =
|
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) {
|
if (this->m->size) {
|
||||||
memcpy(this->m->buf, rhs.m->buf, this->m->size);
|
memcpy(this->m->buf, rhs.m->buf, this->m->size);
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,23 @@ main()
|
|||||||
assert(bc2p[1] == 'W');
|
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 {
|
try {
|
||||||
Pl_Discard discard;
|
Pl_Discard discard;
|
||||||
Pl_Count count("count", &discard);
|
Pl_Count count("count", &discard);
|
||||||
|
Loading…
Reference in New Issue
Block a user