Commit 3a7ee7e93847d41cae414ff77cc75fd6394751ef

Authored by Jay Berkenbilt
1 parent 8e361d98

Move C-based ProgressReporter helper into QPDFWriter

include/qpdf/QPDFWriter.hh
@@ -29,6 +29,7 @@ @@ -29,6 +29,7 @@
29 #include <qpdf/DLL.h> 29 #include <qpdf/DLL.h>
30 #include <qpdf/Types.h> 30 #include <qpdf/Types.h>
31 31
  32 +#include <functional>
32 #include <list> 33 #include <list>
33 #include <map> 34 #include <map>
34 #include <memory> 35 #include <memory>
@@ -80,7 +81,7 @@ class QPDFWriter @@ -80,7 +81,7 @@ class QPDFWriter
80 class QPDF_DLL_CLASS ProgressReporter 81 class QPDF_DLL_CLASS ProgressReporter
81 { 82 {
82 public: 83 public:
83 - virtual ~ProgressReporter() = default; 84 + virtual ~ProgressReporter();
84 85
85 // This method is called with a value from 0 to 100 to 86 // This method is called with a value from 0 to 100 to
86 // indicate approximate progress through the write process. 87 // indicate approximate progress through the write process.
@@ -88,6 +89,20 @@ class QPDFWriter @@ -88,6 +89,20 @@ class QPDFWriter
88 virtual void reportProgress(int) = 0; 89 virtual void reportProgress(int) = 0;
89 }; 90 };
90 91
  92 + // This is a progress reporter that takes a function. It is used
  93 + // by the C APIs, but it is available if you want to just register
  94 + // a C function as a handler.
  95 + class QPDF_DLL_CLASS FunctionProgressReporter: public ProgressReporter
  96 + {
  97 + public:
  98 + FunctionProgressReporter(std::function<void(int)>);
  99 + virtual ~FunctionProgressReporter();
  100 + virtual void reportProgress(int) override;
  101 +
  102 + private:
  103 + std::function<void(int)> handler;
  104 + };
  105 +
91 // Setting Output. Output may be set only one time. If you don't 106 // Setting Output. Output may be set only one time. If you don't
92 // use the filename version of the QPDFWriter constructor, you 107 // use the filename version of the QPDFWriter constructor, you
93 // must call exactly one of these methods. 108 // must call exactly one of these methods.
libqpdf/QPDFWriter.cc
@@ -25,6 +25,30 @@ @@ -25,6 +25,30 @@
25 #include <algorithm> 25 #include <algorithm>
26 #include <stdlib.h> 26 #include <stdlib.h>
27 27
  28 +QPDFWriter::ProgressReporter::~ProgressReporter()
  29 +{
  30 + // Must be explicit and not inline -- see QPDF_DLL_CLASS in
  31 + // README-maintainer
  32 +}
  33 +
  34 +QPDFWriter::FunctionProgressReporter::FunctionProgressReporter(
  35 + std::function<void(int)> handler) :
  36 + handler(handler)
  37 +{
  38 +}
  39 +
  40 +QPDFWriter::FunctionProgressReporter::~FunctionProgressReporter()
  41 +{
  42 + // Must be explicit and not inline -- see QPDF_DLL_CLASS in
  43 + // README-maintainer
  44 +}
  45 +
  46 +void
  47 +QPDFWriter::FunctionProgressReporter::reportProgress(int progress)
  48 +{
  49 + this->handler(progress);
  50 +}
  51 +
28 QPDFWriter::Members::Members(QPDF& pdf) : 52 QPDFWriter::Members::Members(QPDF& pdf) :
29 pdf(pdf), 53 pdf(pdf),
30 filename("unspecified"), 54 filename("unspecified"),
libqpdf/qpdf-c.cc
@@ -61,33 +61,6 @@ _qpdf_data::_qpdf_data() : @@ -61,33 +61,6 @@ _qpdf_data::_qpdf_data() :
61 { 61 {
62 } 62 }
63 63
64 -namespace  
65 -{  
66 - class ProgressReporter: public QPDFWriter::ProgressReporter  
67 - {  
68 - public:  
69 - ProgressReporter(void (*handler)(int, void*), void* data);  
70 - virtual ~ProgressReporter() = default;  
71 - virtual void reportProgress(int);  
72 -  
73 - private:  
74 - void (*handler)(int, void*);  
75 - void* data;  
76 - };  
77 -} // namespace  
78 -  
79 -ProgressReporter::ProgressReporter(void (*handler)(int, void*), void* data) :  
80 - handler(handler),  
81 - data(data)  
82 -{  
83 -}  
84 -  
85 -void  
86 -ProgressReporter::reportProgress(int progress)  
87 -{  
88 - this->handler(progress, this->data);  
89 -}  
90 -  
91 // must set qpdf->filename and qpdf->password 64 // must set qpdf->filename and qpdf->password
92 static void 65 static void
93 call_read(qpdf_data qpdf) 66 call_read(qpdf_data qpdf)
@@ -851,7 +824,8 @@ qpdf_register_progress_reporter( @@ -851,7 +824,8 @@ qpdf_register_progress_reporter(
851 QTC::TC("qpdf", "qpdf-c registered progress reporter"); 824 QTC::TC("qpdf", "qpdf-c registered progress reporter");
852 qpdf->qpdf_writer->registerProgressReporter( 825 qpdf->qpdf_writer->registerProgressReporter(
853 std::shared_ptr<QPDFWriter::ProgressReporter>( 826 std::shared_ptr<QPDFWriter::ProgressReporter>(
854 - new ProgressReporter(report_progress, data))); 827 + new QPDFWriter::FunctionProgressReporter(
  828 + std::bind(report_progress, std::placeholders::_1, data))));
855 } 829 }
856 830
857 QPDF_ERROR_CODE 831 QPDF_ERROR_CODE
qpdf/sizes.cc
@@ -125,6 +125,7 @@ main() @@ -125,6 +125,7 @@ main()
125 print_size(QPDFTokenizer::Token); 125 print_size(QPDFTokenizer::Token);
126 print_size(QPDFUsage); 126 print_size(QPDFUsage);
127 print_size(QPDFWriter); 127 print_size(QPDFWriter);
  128 + print_size(QPDFWriter::FunctionProgressReporter);
128 print_size(QPDFXRefEntry); 129 print_size(QPDFXRefEntry);
129 return 0; 130 return 0;
130 } 131 }