diff --git a/include/qpdf/QPDFWriter.hh b/include/qpdf/QPDFWriter.hh index e44839d4..245e11f3 100644 --- a/include/qpdf/QPDFWriter.hh +++ b/include/qpdf/QPDFWriter.hh @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -80,7 +81,7 @@ class QPDFWriter class QPDF_DLL_CLASS ProgressReporter { public: - virtual ~ProgressReporter() = default; + virtual ~ProgressReporter(); // This method is called with a value from 0 to 100 to // indicate approximate progress through the write process. @@ -88,6 +89,20 @@ class QPDFWriter virtual void reportProgress(int) = 0; }; + // This is a progress reporter that takes a function. It is used + // by the C APIs, but it is available if you want to just register + // a C function as a handler. + class QPDF_DLL_CLASS FunctionProgressReporter: public ProgressReporter + { + public: + FunctionProgressReporter(std::function); + virtual ~FunctionProgressReporter(); + virtual void reportProgress(int) override; + + private: + std::function handler; + }; + // Setting Output. Output may be set only one time. If you don't // use the filename version of the QPDFWriter constructor, you // must call exactly one of these methods. diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index b1bb4ad7..d160ff99 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -25,6 +25,30 @@ #include #include +QPDFWriter::ProgressReporter::~ProgressReporter() +{ + // Must be explicit and not inline -- see QPDF_DLL_CLASS in + // README-maintainer +} + +QPDFWriter::FunctionProgressReporter::FunctionProgressReporter( + std::function handler) : + handler(handler) +{ +} + +QPDFWriter::FunctionProgressReporter::~FunctionProgressReporter() +{ + // Must be explicit and not inline -- see QPDF_DLL_CLASS in + // README-maintainer +} + +void +QPDFWriter::FunctionProgressReporter::reportProgress(int progress) +{ + this->handler(progress); +} + QPDFWriter::Members::Members(QPDF& pdf) : pdf(pdf), filename("unspecified"), diff --git a/libqpdf/qpdf-c.cc b/libqpdf/qpdf-c.cc index 468811b3..bde0ac72 100644 --- a/libqpdf/qpdf-c.cc +++ b/libqpdf/qpdf-c.cc @@ -61,33 +61,6 @@ _qpdf_data::_qpdf_data() : { } -namespace -{ - class ProgressReporter: public QPDFWriter::ProgressReporter - { - public: - ProgressReporter(void (*handler)(int, void*), void* data); - virtual ~ProgressReporter() = default; - virtual void reportProgress(int); - - private: - void (*handler)(int, void*); - void* data; - }; -} // namespace - -ProgressReporter::ProgressReporter(void (*handler)(int, void*), void* data) : - handler(handler), - data(data) -{ -} - -void -ProgressReporter::reportProgress(int progress) -{ - this->handler(progress, this->data); -} - // must set qpdf->filename and qpdf->password static void call_read(qpdf_data qpdf) @@ -851,7 +824,8 @@ qpdf_register_progress_reporter( QTC::TC("qpdf", "qpdf-c registered progress reporter"); qpdf->qpdf_writer->registerProgressReporter( std::shared_ptr( - new ProgressReporter(report_progress, data))); + new QPDFWriter::FunctionProgressReporter( + std::bind(report_progress, std::placeholders::_1, data)))); } QPDF_ERROR_CODE diff --git a/qpdf/sizes.cc b/qpdf/sizes.cc index 62eef6a8..6d4635fd 100644 --- a/qpdf/sizes.cc +++ b/qpdf/sizes.cc @@ -125,6 +125,7 @@ main() print_size(QPDFTokenizer::Token); print_size(QPDFUsage); print_size(QPDFWriter); + print_size(QPDFWriter::FunctionProgressReporter); print_size(QPDFXRefEntry); return 0; }