From 0e1c858f90471745ebd51c4664c33bde08ed4257 Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Thu, 5 Jan 2023 23:55:25 +0800 Subject: [PATCH] LibPDF: Move casting code to its own cast_to function This functionality was previously part of the resolve_to() Document method, and thus only available only when resolving objects through the Document class. There are many use cases where this casting can be used, but no resolution is needed. This commit moves this functionality into a new cast_to function, and makes the resolve_to function call it internally. With this new function in place we can now offer new versions of DictObject::get_* and ArrayObject::get_*_at that don't perform Document resolution unnecessarily when not required. --- Userland/Libraries/LibPDF/Document.h | 15 +-------------- Userland/Libraries/LibPDF/ObjectDerivatives.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibPDF/Document.h b/Userland/Libraries/LibPDF/Document.h index 15305bb0c1..7557f677ea 100644 --- a/Userland/Libraries/LibPDF/Document.h +++ b/Userland/Libraries/LibPDF/Document.h @@ -116,20 +116,7 @@ public: template PDFErrorOr> resolve_to(Value const& value) { - auto resolved = TRY(resolve(value)); - - if constexpr (IsSame) - return resolved.get(); - else if constexpr (IsSame) - return resolved.get(); - else if constexpr (IsSame) - return resolved.get(); - else if constexpr (IsSame) - return resolved.get>(); - else if constexpr (IsObject) - return resolved.get>()->cast(); - - VERIFY_NOT_REACHED(); + return cast_to(TRY(resolve(value))); } private: diff --git a/Userland/Libraries/LibPDF/ObjectDerivatives.h b/Userland/Libraries/LibPDF/ObjectDerivatives.h index 23cbd94b2b..bb5b31a394 100644 --- a/Userland/Libraries/LibPDF/ObjectDerivatives.h +++ b/Userland/Libraries/LibPDF/ObjectDerivatives.h @@ -195,4 +195,20 @@ private: Value m_value; }; +template +UnwrappedValueType cast_to(Value const& value) +{ + if constexpr (IsSame) + return value.get(); + else if constexpr (IsSame) + return value.get(); + else if constexpr (IsSame) + return value.get(); + else if constexpr (IsSame) + return value.get>(); + else if constexpr (IsObject) + return value.get>()->cast(); + VERIFY_NOT_REACHED(); +} + }