aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-06-18 20:56:58 +0200
committerJay Berkenbilt <ejb@ql.org>2022-06-19 14:46:58 +0200
commit3a7ee7e93847d41cae414ff77cc75fd6394751ef (patch)
tree4d28778c0b1fe2c9f8feaad5adaab4b1ff29693a
parent8e361d98f0bb23d58cbc773367ba76dffced7bdb (diff)
downloadqpdf-3a7ee7e93847d41cae414ff77cc75fd6394751ef.tar.zst
Move C-based ProgressReporter helper into QPDFWriter
-rw-r--r--include/qpdf/QPDFWriter.hh17
-rw-r--r--libqpdf/QPDFWriter.cc24
-rw-r--r--libqpdf/qpdf-c.cc30
-rw-r--r--qpdf/sizes.cc1
4 files changed, 43 insertions, 29 deletions
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 <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.
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 <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"),
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<QPDFWriter::ProgressReporter>(
- 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;
}