diff --git a/ChangeLog b/ChangeLog index 0c129e04..705cc99a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2022-02-06 Jay Berkenbilt + * Pl_Buffer and QPDFWriter: add getBufferSharedPointer(), which + turns a PointerHolder but will return a + std::shared_ptr in qpdf 11. + * From m-holger: add getKeyIfDict(), which calls getKey for dictionaries and returns null if called on null. This is for easier access to optional, lower-level dictionaries. diff --git a/TODO b/TODO index 5388860a..aa057c1d 100644 --- a/TODO +++ b/TODO @@ -420,6 +420,8 @@ auto x = std::make_unique(5) PointerHolder in public API: + PointerHolder Pl_Buffer::getBufferSharedPointer(); + PointerHolder QPDFWriter::getBufferSharedPointer(); QUtil::read_file_into_memory( char const*, PointerHolder&, unsigned long&) QPDFObjectHandle::addContentTokenFilter( diff --git a/include/qpdf/Pl_Buffer.hh b/include/qpdf/Pl_Buffer.hh index 656c755c..98c87622 100644 --- a/include/qpdf/Pl_Buffer.hh +++ b/include/qpdf/Pl_Buffer.hh @@ -51,10 +51,14 @@ class Pl_Buffer: public Pipeline // Each call to getBuffer() resets this object -- see notes above. // The caller is responsible for deleting the returned Buffer - // object. + // object. See also getBufferSharedPointer() and getMallocBuffer(). QPDF_DLL Buffer* getBuffer(); + // Same as getBuffer but wraps the result in a shared pointer. + QPDF_DLL + PointerHolder getBufferSharedPointer(); + // getMallocBuffer behaves in the same was as getBuffer except the // buffer is allocated with malloc(), making it suitable for use // when calling from other languages. If there is no data, *buf is diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh index 76f50c96..cb6585f0 100644 --- a/include/qpdf/QPDFWriter.hh +++ b/include/qpdf/QPDFWriter.hh @@ -118,13 +118,18 @@ class QPDFWriter QPDF_DLL void setOutputMemory(); - // Return the buffer object containing the PDF file. If + // Return the buffer object containing the PDF file. If // setOutputMemory() has been called, this method may be called - // exactly one time after write() has returned. The caller is - // responsible for deleting the buffer when done. + // exactly one time after write() has returned. The caller is + // responsible for deleting the buffer when done. See also + // getBufferSharedPointer(). QPDF_DLL Buffer* getBuffer(); + // Return getBuffer() in a shared pointer. + QPDF_DLL + PointerHolder getBufferSharedPointer(); + // Supply your own pipeline object. Output will be written to // this pipeline, and QPDFWriter will call finish() on the // pipeline. It is the caller's responsibility to manage the diff --git a/libqpdf/Pl_Buffer.cc b/libqpdf/Pl_Buffer.cc index 73db555a..3a41c579 100644 --- a/libqpdf/Pl_Buffer.cc +++ b/libqpdf/Pl_Buffer.cc @@ -79,10 +79,16 @@ Pl_Buffer::getBuffer() unsigned char* p = b->getBuffer(); memcpy(p, this->m->data->getBuffer(), this->m->total_size); } - this->m = new Members(); + this->m = PointerHolder(new Members()); return b; } +PointerHolder +Pl_Buffer::getBufferSharedPointer() +{ + return PointerHolder(getBuffer()); +} + void Pl_Buffer::getMallocBuffer(unsigned char **buf, size_t* len) { diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 5f7b39a5..7cde1426 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -157,6 +157,12 @@ QPDFWriter::getBuffer() return result; } +PointerHolder +QPDFWriter::getBufferSharedPointer() +{ + return PointerHolder(getBuffer()); +} + void QPDFWriter::setOutputPipeline(Pipeline* p) {