Commit eef51d0c5ac88f96ac79831c0a32d9eca88a651b
Committed by
GitHub
Merge pull request #1673 from m-holger/referenced
Add referenced_object method to BaseHandle for retrieving referenced …
Showing
3 changed files
with
32 additions
and
3 deletions
include/qpdf/ObjectHandle.hh
| ... | ... | @@ -132,11 +132,9 @@ namespace qpdf |
| 132 | 132 | |
| 133 | 133 | inline void assign(qpdf_object_type_e required, BaseHandle const& other); |
| 134 | 134 | inline void assign(qpdf_object_type_e required, BaseHandle&& other); |
| 135 | - | |
| 136 | 135 | inline void nullify(); |
| 137 | 136 | |
| 138 | 137 | std::string description() const; |
| 139 | - | |
| 140 | 138 | inline QPDFObjectHandle const& get(std::string const& key) const; |
| 141 | 139 | |
| 142 | 140 | void no_ci_warn_if(bool condition, std::string const& warning) const; |
| ... | ... | @@ -148,6 +146,9 @@ namespace qpdf |
| 148 | 146 | char const* type_name() const; |
| 149 | 147 | |
| 150 | 148 | std::shared_ptr<QPDFObject> obj; |
| 149 | + | |
| 150 | + private: | |
| 151 | + inline QPDFObjectHandle referenced_object() const; | |
| 151 | 152 | }; |
| 152 | 153 | |
| 153 | 154 | } // namespace qpdf | ... | ... |
libqpdf/QPDFObjectHandle.cc
| ... | ... | @@ -322,7 +322,7 @@ BaseHandle::copy(bool shallow) const |
| 322 | 322 | throw std::logic_error("attempted to shallow copy QPDFObjectHandle from destroyed QPDF"); |
| 323 | 323 | return {}; // does not return |
| 324 | 324 | case ::ot_reference: |
| 325 | - return obj->qpdf->getObject(obj->og).obj_sp(); | |
| 325 | + return referenced_object().obj_sp(); | |
| 326 | 326 | } |
| 327 | 327 | return {}; // unreachable |
| 328 | 328 | } | ... | ... |
libqpdf/qpdf/QPDFObjectHandle_private.hh
| ... | ... | @@ -6,6 +6,7 @@ |
| 6 | 6 | #include <qpdf/QPDFObject_private.hh> |
| 7 | 7 | #include <qpdf/QPDF_private.hh> |
| 8 | 8 | #include <qpdf/QUtil.hh> |
| 9 | +#include <qpdf/Util.hh> | |
| 9 | 10 | |
| 10 | 11 | #include <concepts> |
| 11 | 12 | #include <utility> |
| ... | ... | @@ -757,6 +758,33 @@ namespace qpdf |
| 757 | 758 | return {obj}; |
| 758 | 759 | } |
| 759 | 760 | |
| 761 | + /// @brief Retrieve the QPDFObjectHandle for the object referenced by a reference object. | |
| 762 | + /// | |
| 763 | + /// Look up and return the object from the document's object table using QPDF::getObject(). The | |
| 764 | + /// returned value is a QPDFObjectHandle that wraps the shared pointer to the underlying | |
| 765 | + /// QPDFObject held in the document's cache. | |
| 766 | + /// | |
| 767 | + /// @note We must perform the lookup because qpdf represents certain replacements using | |
| 768 | + /// QPDF_Reference. In particular, `QPDF::makeIndirectObject` used to make the input | |
| 769 | + /// object indirect and then return the original input object. To replicate that | |
| 770 | + /// existing behavior the implementation now makes the input object indirect and | |
| 771 | + /// returns it, while modifying the input object to become a reference to the | |
| 772 | + /// newly-created indirect object. Looking up the resulting indirect object in the | |
| 773 | + /// document's object table via `QPDF::getObject()` ensures callers receive the | |
| 774 | + /// cached object and avoids surprises if the indirect object is subsequently | |
| 775 | + /// replaced. | |
| 776 | + /// | |
| 777 | + /// @return The QPDFObjectHandle for the object referenced by a reference object. | |
| 778 | + /// | |
| 779 | + /// @since 12.3.3 | |
| 780 | + inline QPDFObjectHandle | |
| 781 | + BaseHandle::referenced_object() const | |
| 782 | + { | |
| 783 | + qpdf_expect(resolved_type_code() == ::ot_reserved); | |
| 784 | + qpdf_expect(obj->qpdf); | |
| 785 | + return obj->qpdf->getObject(obj->og); | |
| 786 | + } | |
| 787 | + | |
| 760 | 788 | inline void |
| 761 | 789 | BaseHandle::assign(qpdf_object_type_e required, BaseHandle const& other) |
| 762 | 790 | { | ... | ... |