Move C-based ProgressReporter helper into QPDFWriter

This commit is contained in:
Jay Berkenbilt 2022-06-18 14:56:58 -04:00
parent 8e361d98f0
commit 3a7ee7e938
4 changed files with 43 additions and 29 deletions

View File

@ -29,6 +29,7 @@
#include <qpdf/DLL.h>
#include <qpdf/Types.h>
#include <functional>
#include <list>
#include <map>
#include <memory>
@ -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<void(int)>);
virtual ~FunctionProgressReporter();
virtual void reportProgress(int) override;
private:
std::function<void(int)> 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.

View File

@ -25,6 +25,30 @@
#include <algorithm>
#include <stdlib.h>
QPDFWriter::ProgressReporter::~ProgressReporter()
{
// Must be explicit and not inline -- see QPDF_DLL_CLASS in
// README-maintainer
}
QPDFWriter::FunctionProgressReporter::FunctionProgressReporter(
std::function<void(int)> 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"),

View File

@ -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<QPDFWriter::ProgressReporter>(
new ProgressReporter(report_progress, data)));
new QPDFWriter::FunctionProgressReporter(
std::bind(report_progress, std::placeholders::_1, data))));
}
QPDF_ERROR_CODE

View File

@ -125,6 +125,7 @@ main()
print_size(QPDFTokenizer::Token);
print_size(QPDFUsage);
print_size(QPDFWriter);
print_size(QPDFWriter::FunctionProgressReporter);
print_size(QPDFXRefEntry);
return 0;
}