Commit 277779a363bf452557aa9b8b1a2d1aed8c1c1bee

Authored by m-holger
Committed by GitHub
2 parents 6a0d6fe1 2d11e4dd

Merge pull request #1582 from m-holger/null

Add private-API Null class
libqpdf/QPDFAcroFormDocumentHelper.cc
@@ -252,16 +252,15 @@ QPDFFormFieldObjectHelper @@ -252,16 +252,15 @@ QPDFFormFieldObjectHelper
252 QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h) 252 QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h)
253 { 253 {
254 QPDFObjectHandle oh = h.getObjectHandle(); 254 QPDFObjectHandle oh = h.getObjectHandle();
255 - QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull());  
256 if (!oh.isDictionaryOfType("", "/Widget")) { 255 if (!oh.isDictionaryOfType("", "/Widget")) {
257 - return result; 256 + return Null::temp();
258 } 257 }
259 analyze(); 258 analyze();
260 QPDFObjGen og(oh.getObjGen()); 259 QPDFObjGen og(oh.getObjGen());
261 if (m->annotation_to_field.contains(og)) { 260 if (m->annotation_to_field.contains(og)) {
262 - result = m->annotation_to_field[og]; 261 + return m->annotation_to_field[og];
263 } 262 }
264 - return result; 263 + return Null::temp();
265 } 264 }
266 265
267 void 266 void
libqpdf/QPDFAnnotationObjectHelper.cc
@@ -56,20 +56,16 @@ QPDFAnnotationObjectHelper::getAppearanceStream(std::string const& which, std::s @@ -56,20 +56,16 @@ QPDFAnnotationObjectHelper::getAppearanceStream(std::string const& which, std::s
56 // appearance stream when /AP is a dictionary, but files have been seen in the wild 56 // appearance stream when /AP is a dictionary, but files have been seen in the wild
57 // where Appearance State is `/N` and `/AP` is a stream. Therefore, if `which` points to 57 // where Appearance State is `/N` and `/AP` is a stream. Therefore, if `which` points to
58 // a stream, disregard state and just use the stream. See qpdf issue #949 for details. 58 // a stream, disregard state and just use the stream. See qpdf issue #949 for details.
59 - QTC::TC("qpdf", "QPDFAnnotationObjectHelper AP stream");  
60 return ap_sub; 59 return ap_sub;
61 } 60 }
62 - if (ap_sub.isDictionary() && (!desired_state.empty())) {  
63 - QTC::TC("qpdf", "QPDFAnnotationObjectHelper AP dictionary"); 61 + if (ap_sub.isDictionary() && !desired_state.empty()) {
64 QPDFObjectHandle ap_sub_val = ap_sub.getKey(desired_state); 62 QPDFObjectHandle ap_sub_val = ap_sub.getKey(desired_state);
65 if (ap_sub_val.isStream()) { 63 if (ap_sub_val.isStream()) {
66 - QTC::TC("qpdf", "QPDFAnnotationObjectHelper AP sub stream");  
67 return ap_sub_val; 64 return ap_sub_val;
68 } 65 }
69 } 66 }
70 } 67 }
71 - QTC::TC("qpdf", "QPDFAnnotationObjectHelper AP null");  
72 - return QPDFObjectHandle::newNull(); 68 + return Null::temp();
73 } 69 }
74 70
75 std::string 71 std::string
libqpdf/QPDFEmbeddedFileDocumentHelper.cc
1 #include <qpdf/QPDFEmbeddedFileDocumentHelper.hh> 1 #include <qpdf/QPDFEmbeddedFileDocumentHelper.hh>
2 2
3 #include <qpdf/QPDFNameTreeObjectHelper.hh> 3 #include <qpdf/QPDFNameTreeObjectHelper.hh>
  4 +#include <qpdf/QPDFObjectHandle_private.hh>
4 #include <qpdf/QPDF_private.hh> 5 #include <qpdf/QPDF_private.hh>
5 6
6 // File attachments are stored in the /EmbeddedFiles (name tree) key of the /Names dictionary from 7 // File attachments are stored in the /EmbeddedFiles (name tree) key of the /Names dictionary from
@@ -143,11 +144,10 @@ QPDFEmbeddedFileDocumentHelper::removeEmbeddedFile(std::string const&amp; name) @@ -143,11 +144,10 @@ QPDFEmbeddedFileDocumentHelper::removeEmbeddedFile(std::string const&amp; name)
143 if (iter == m->embedded_files->end()) { 144 if (iter == m->embedded_files->end()) {
144 return false; 145 return false;
145 } 146 }
146 - auto oh = iter->second;  
147 - iter.remove();  
148 - if (oh.isIndirect()) {  
149 - qpdf.replaceObject(oh.getObjGen(), QPDFObjectHandle::newNull()); 147 + if (iter->second.indirect()) {
  148 + qpdf.replaceObject(iter->second, Null());
150 } 149 }
  150 + iter.remove();
151 151
152 return true; 152 return true;
153 } 153 }
libqpdf/QPDFFileSpecObjectHelper.cc
@@ -76,7 +76,7 @@ QPDFFileSpecObjectHelper::getEmbeddedFileStream(std::string const&amp; key) @@ -76,7 +76,7 @@ QPDFFileSpecObjectHelper::getEmbeddedFileStream(std::string const&amp; key)
76 } 76 }
77 } 77 }
78 } 78 }
79 - return QPDFObjectHandle::newNull(); 79 + return Null::temp();
80 } 80 }
81 81
82 QPDFObjectHandle 82 QPDFObjectHandle
libqpdf/QPDFObjectHandle.cc
@@ -25,6 +25,8 @@ @@ -25,6 +25,8 @@
25 using namespace std::literals; 25 using namespace std::literals;
26 using namespace qpdf; 26 using namespace qpdf;
27 27
  28 +const Null Null::temp_;
  29 +
28 BaseHandle:: 30 BaseHandle::
29 operator QPDFObjGen() const 31 operator QPDFObjGen() const
30 { 32 {
libqpdf/qpdf/QPDFObjectHandle_private.hh
@@ -468,6 +468,39 @@ namespace qpdf @@ -468,6 +468,39 @@ namespace qpdf
468 } 468 }
469 }; 469 };
470 470
  471 + class Null final: public BaseHandle
  472 + {
  473 + public:
  474 + // Unlike other types, the Null default constructor creates a valid null object.
  475 + Null() :
  476 + BaseHandle(QPDFObject::create<QPDF_Null>())
  477 + {
  478 + }
  479 +
  480 + Null(Null const&) = default;
  481 + Null(Null&&) = default;
  482 + Null& operator=(Null const&) = default;
  483 + Null& operator=(Null&&) = default;
  484 + ~Null() = default;
  485 +
  486 + // For legacy support, return a Null object to be used as a temporary return value.
  487 + static QPDFObjectHandle
  488 + temp()
  489 + {
  490 + return temp_.oh();
  491 + }
  492 +
  493 + // For legacy support, return an explicit temporary Null object if oh is null.
  494 + static QPDFObjectHandle
  495 + if_null(QPDFObjectHandle oh)
  496 + {
  497 + return oh ? std::move(oh) : Null::temp();
  498 + }
  499 +
  500 + private:
  501 + static const Null temp_;
  502 + }; // class Null
  503 +
471 class Stream final: public BaseHandle 504 class Stream final: public BaseHandle
472 { 505 {
473 public: 506 public:
qpdf/qpdf.testcov
@@ -181,10 +181,6 @@ QPDFFormFieldObjectHelper TU absent 0 @@ -181,10 +181,6 @@ QPDFFormFieldObjectHelper TU absent 0
181 QPDFFormFieldObjectHelper TM absent 0 181 QPDFFormFieldObjectHelper TM absent 0
182 QPDFFormFieldObjectHelper Q present 1 182 QPDFFormFieldObjectHelper Q present 1
183 QPDFFormFieldObjectHelper DA present 1 183 QPDFFormFieldObjectHelper DA present 1
184 -QPDFAnnotationObjectHelper AP stream 0  
185 -QPDFAnnotationObjectHelper AP dictionary 0  
186 -QPDFAnnotationObjectHelper AP sub stream 0  
187 -QPDFAnnotationObjectHelper AP null 0  
188 QPDFAcroFormDocumentHelper field found 1 184 QPDFAcroFormDocumentHelper field found 1
189 QPDFAcroFormDocumentHelper annotation found 1 185 QPDFAcroFormDocumentHelper annotation found 1
190 QPDFJob automatically set keep files open 1 186 QPDFJob automatically set keep files open 1