From 1edbed57016f8aa1b98ebef679f42f3aa091174a Mon Sep 17 00:00:00 2001 From: m-holger Date: Fri, 10 Oct 2025 11:37:39 +0100 Subject: [PATCH] Refactor `QPDF::getAllPages` and `Pages` methods: move implementation to new `Pages::all` method, add `Pages::size` and `empty` methods, move `getAllPagesInternal` to private section, streamline encapsulation, and enhance code organization. --- libqpdf/QPDF_pages.cc | 12 +++++++++--- libqpdf/qpdf/QPDF_private.hh | 29 +++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/libqpdf/QPDF_pages.cc b/libqpdf/QPDF_pages.cc index e5a4e97..98c9936 100644 --- a/libqpdf/QPDF_pages.cc +++ b/libqpdf/QPDF_pages.cc @@ -42,10 +42,16 @@ using Pages = QPDF::Doc::Pages; std::vector const& QPDF::getAllPages() { + return m->pages.all(); +} + +std::vector const& +Pages::all() +{ // Note that pushInheritedAttributesToPage may also be used to initialize m->all_pages. if (m->all_pages.empty() && !m->invalid_page_found) { m->ever_called_get_all_pages = true; - auto root = getRoot(); + auto root = qpdf.getRoot(); QPDFObjGen::set visited; QPDFObjGen::set seen; QPDFObjectHandle pages = root.getKey("/Pages"); @@ -77,14 +83,14 @@ QPDF::getAllPages() qpdf_e_pages, m->file->getName(), "", 0, "root of pages tree has no /Kids array"); } try { - m->pages.getAllPagesInternal(pages, visited, seen, false, false); + getAllPagesInternal(pages, visited, seen, false, false); } catch (...) { m->all_pages.clear(); m->invalid_page_found = false; throw; } if (m->invalid_page_found) { - m->pages.flattenPagesTree(); + flattenPagesTree(); m->invalid_page_found = false; } } diff --git a/libqpdf/qpdf/QPDF_private.hh b/libqpdf/qpdf/QPDF_private.hh index 6c0ec2b..d2f5fc1 100644 --- a/libqpdf/qpdf/QPDF_private.hh +++ b/libqpdf/qpdf/QPDF_private.hh @@ -867,12 +867,20 @@ class QPDF::Doc::Pages: Common { } - void getAllPagesInternal( - QPDFObjectHandle cur_pages, - QPDFObjGen::set& visited, - QPDFObjGen::set& seen, - bool media_box, - bool resources); + std::vector const& all(); + + bool + empty() + { + return all().empty(); + } + + size_t + size() + { + return all().size(); + } + void insertPage(QPDFObjectHandle newpage, int pos); void flattenPagesTree(); void insertPageobjToPage(QPDFObjectHandle const& obj, int pos, bool check_duplicate); @@ -882,6 +890,15 @@ class QPDF::Doc::Pages: Common std::map>&, bool allow_changes, bool warn_skipped_keys); + + private: + void getAllPagesInternal( + QPDFObjectHandle cur_pages, + QPDFObjGen::set& visited, + QPDFObjGen::set& seen, + bool media_box, + bool resources); + }; // class QPDF::Doc::Pages class QPDF::Members: Doc -- libgit2 0.21.4