From 64059014c9288e740214be8242cac4a29e29284c Mon Sep 17 00:00:00 2001 From: m-holger Date: Mon, 14 Nov 2022 22:06:04 +0000 Subject: Refactor QPDFObjectHandle::shallowCopy --- include/qpdf/QPDFObjectHandle.hh | 2 -- libqpdf/QPDFObjectHandle.cc | 65 +++------------------------------------- 2 files changed, 4 insertions(+), 63 deletions(-) diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh index df6c0742..27b9f9e0 100644 --- a/include/qpdf/QPDFObjectHandle.hh +++ b/include/qpdf/QPDFObjectHandle.hh @@ -1628,8 +1628,6 @@ class QPDFObjectHandle void objectWarning(std::string const& warning); void assertType(char const* type_name, bool istype); bool dereference(); - void copyObject1(std::set& visited); - void shallowCopyInternal1(QPDFObjectHandle& oh); void copyObject( std::set& visited, bool cross_indirect, diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 2990b019..6b4d2cd8 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -2200,68 +2200,11 @@ QPDFObjectHandle::hasObjectDescription() QPDFObjectHandle QPDFObjectHandle::shallowCopy() { - QPDFObjectHandle result; - shallowCopyInternal1(result); - return result; -} - -void -QPDFObjectHandle::shallowCopyInternal1(QPDFObjectHandle& new_obj) -{ - assertInitialized(); - - if (isStream()) { - // Handled bt QPDF_Stream::copy() - } - new_obj = QPDFObjectHandle(obj->copy(true)); - - std::set visited; - new_obj.copyObject1(visited); -} - -void -QPDFObjectHandle::copyObject1(std::set& visited) -{ - assertInitialized(); - - std::shared_ptr new_obj; - - if (isStream()) { - new_obj = obj->copy(); - } - - auto cur_og = getObjGen(); - if (cur_og.getObj() != 0) { - if (visited.count(cur_og)) { - throw std::runtime_error( - "loop detected while converting object from " - "indirect to direct"); - } - visited.insert(cur_og); - } - - if (isReserved()) { - new_obj = obj->copy(); - } - - if (isBool() || isInteger() || isName() || isNull() || isReal() || - isString()) { - // copy(true) and copy(false) are the same - new_obj = obj->copy(); - } else if (isArray()) { - new_obj = obj->copy(); - } else if (isDictionary()) { - new_obj = obj->copy(); - } else { - throw std::logic_error("QPDFObjectHandle::makeDirectInternal: " - "unknown object type"); - } - - this->obj = new_obj; - - if (cur_og.getObj()) { - visited.erase(cur_og); + if (!dereference()) { + throw std::logic_error("operation attempted on uninitialized " + "QPDFObjectHandle"); } + return QPDFObjectHandle(obj->copy()); } QPDFObjectHandle -- cgit v1.2.3-54-g00ecf