From 92d076ed7a6ec209fdfc3b92a86a510622d1330f Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 10 Oct 2025 20:38:00 +0100 Subject: [PATCH] Refactor `Pages::all`: inline common case, rename existing implementation to `cache`, update calls, streamline initialization, and enhance assertion usage. --- libqpdf/QPDF_pages.cc | 16 ++++++++-------- libqpdf/qpdf/QPDF_private.hh | 8 ++++++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/libqpdf/QPDF_pages.cc b/libqpdf/QPDF_pages.cc index 9043ca8..fa0ea63 100644 --- a/libqpdf/QPDF_pages.cc +++ b/libqpdf/QPDF_pages.cc @@ -4,6 +4,7 @@ #include #include #include +#include // In support of page manipulation APIs, these methods internally maintain state about pages in a // pair of data structures: all_pages, which is a vector of page objects, and pageobj_to_pages_pos, @@ -46,7 +47,7 @@ QPDF::getAllPages() } std::vector const& -Pages::all() +Pages::cache() { // Note that pushInheritedAttributesToPage may also be used to initialize m->all_pages. if (all_pages.empty() && !invalid_page_found) { @@ -253,7 +254,7 @@ Pages::update_cache() all_pages.clear(); pageobj_to_pages_pos.clear(); pushed_inherited_attributes_to_pages = false; - all(); + cache(); } void @@ -309,9 +310,9 @@ Pages::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) return; } - // Calling getAllPages() resolves any duplicated page objects, repairs broken nodes, and detects + // Calling cache() resolves any duplicated page objects, repairs broken nodes, and detects // loops, so we don't have to do those activities here. - (void)all(); + (void)cache(); // key_ancestors is a mapping of page attribute keys to a stack of Pages nodes that contain // values for them. @@ -321,10 +322,9 @@ Pages::pushInheritedAttributesToPage(bool allow_changes, bool warn_skipped_keys) key_ancestors, allow_changes, warn_skipped_keys); - if (!key_ancestors.empty()) { - throw std::logic_error( - "key_ancestors not empty after pushing inherited attributes to pages"); - } + util::assertion( + key_ancestors.empty(), + "key_ancestors not empty after pushing inherited attributes to pages"); pushed_inherited_attributes_to_pages = true; ever_pushed_inherited_attributes_to_pages_ = true; } diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index aeeb697..c56c6b3 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -865,7 +865,11 @@ class QPDF::Doc::Pages: Common { } - std::vector const& all(); + std::vector const& + all() + { + return !all_pages.empty() ? all_pages : cache(); + } bool empty() @@ -906,7 +910,7 @@ class QPDF::Doc::Pages: Common std::map>&, bool allow_changes, bool warn_skipped_keys); - + std::vector const& cache(); void getAllPagesInternal( QPDFObjectHandle cur_pages, QPDFObjGen::set& visited, -- libgit2 0.21.4