Commit cfaae47dc6704a54e3e84decbfbe8840c33f2fc4

Authored by Jay Berkenbilt
1 parent 3e98fe46

Add getBufferSharedPointer() to Pl_Buffer and QPDFWriter

ChangeLog
1 1 2022-02-06 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * Pl_Buffer and QPDFWriter: add getBufferSharedPointer(), which
  4 + turns a PointerHolder<Buffer> but will return a
  5 + std::shared_ptr<Buffer> in qpdf 11.
  6 +
3 7 * From m-holger: add getKeyIfDict(), which calls getKey for
4 8 dictionaries and returns null if called on null. This is for
5 9 easier access to optional, lower-level dictionaries.
... ...
... ... @@ -420,6 +420,8 @@ auto x = std::make_unique&lt;T[]&gt;(5)
420 420  
421 421 PointerHolder in public API:
422 422  
  423 + PointerHolder<Buffer> Pl_Buffer::getBufferSharedPointer();
  424 + PointerHolder<Buffer> QPDFWriter::getBufferSharedPointer();
423 425 QUtil::read_file_into_memory(
424 426 char const*, PointerHolder<char>&, unsigned long&)
425 427 QPDFObjectHandle::addContentTokenFilter(
... ...
include/qpdf/Pl_Buffer.hh
... ... @@ -51,10 +51,14 @@ class Pl_Buffer: public Pipeline
51 51  
52 52 // Each call to getBuffer() resets this object -- see notes above.
53 53 // The caller is responsible for deleting the returned Buffer
54   - // object.
  54 + // object. See also getBufferSharedPointer() and getMallocBuffer().
55 55 QPDF_DLL
56 56 Buffer* getBuffer();
57 57  
  58 + // Same as getBuffer but wraps the result in a shared pointer.
  59 + QPDF_DLL
  60 + PointerHolder<Buffer> getBufferSharedPointer();
  61 +
58 62 // getMallocBuffer behaves in the same was as getBuffer except the
59 63 // buffer is allocated with malloc(), making it suitable for use
60 64 // when calling from other languages. If there is no data, *buf is
... ...
include/qpdf/QPDFWriter.hh
... ... @@ -118,13 +118,18 @@ class QPDFWriter
118 118 QPDF_DLL
119 119 void setOutputMemory();
120 120  
121   - // Return the buffer object containing the PDF file. If
  121 + // Return the buffer object containing the PDF file. If
122 122 // setOutputMemory() has been called, this method may be called
123   - // exactly one time after write() has returned. The caller is
124   - // responsible for deleting the buffer when done.
  123 + // exactly one time after write() has returned. The caller is
  124 + // responsible for deleting the buffer when done. See also
  125 + // getBufferSharedPointer().
125 126 QPDF_DLL
126 127 Buffer* getBuffer();
127 128  
  129 + // Return getBuffer() in a shared pointer.
  130 + QPDF_DLL
  131 + PointerHolder<Buffer> getBufferSharedPointer();
  132 +
128 133 // Supply your own pipeline object. Output will be written to
129 134 // this pipeline, and QPDFWriter will call finish() on the
130 135 // pipeline. It is the caller's responsibility to manage the
... ...
libqpdf/Pl_Buffer.cc
... ... @@ -79,10 +79,16 @@ Pl_Buffer::getBuffer()
79 79 unsigned char* p = b->getBuffer();
80 80 memcpy(p, this->m->data->getBuffer(), this->m->total_size);
81 81 }
82   - this->m = new Members();
  82 + this->m = PointerHolder<Members>(new Members());
83 83 return b;
84 84 }
85 85  
  86 +PointerHolder<Buffer>
  87 +Pl_Buffer::getBufferSharedPointer()
  88 +{
  89 + return PointerHolder<Buffer>(getBuffer());
  90 +}
  91 +
86 92 void
87 93 Pl_Buffer::getMallocBuffer(unsigned char **buf, size_t* len)
88 94 {
... ...
libqpdf/QPDFWriter.cc
... ... @@ -157,6 +157,12 @@ QPDFWriter::getBuffer()
157 157 return result;
158 158 }
159 159  
  160 +PointerHolder<Buffer>
  161 +QPDFWriter::getBufferSharedPointer()
  162 +{
  163 + return PointerHolder<Buffer>(getBuffer());
  164 +}
  165 +
160 166 void
161 167 QPDFWriter::setOutputPipeline(Pipeline* p)
162 168 {
... ...