From 8c79f0e0cfdc2be6a4f81ae02840f0d5f8d78f53 Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Fri, 6 Jan 2023 00:19:12 +0800 Subject: LibPDF: Add more utility methods to {Dict,Array}Object Being both of them containers, these classes already offered a set of methods to retrieve an inner element by key or index, respectively, with different methods for the different subtypes of the PDF::Object type returning the element cast to the correct type pointer. On top of that, DictObject offered an additional method to obtain an element as an Object pointer. While these methods were useful, they have some shortcomings: * They always take a Document pointer to first perform an object resolution, in case the element is a Reference. This is not always necessary though, as there are values that are always meant to be immediate, and hence the resolution lookup adds overhead. * There was no easy way to get an individual Object element from an ArrayObject like there is in DictObject. This makes it difficult to obtain such values, as one first needs to call dict.get() to get a Value, then cast it manually to a NonnullRefPtr. This commit fixes these two issues by: * Adding a new method that returns an Object for a given index. * Adding overloads for this new method, and all the existing methods described above, that do *not* take a Document, and therefore do *not* perform an object resolution lookup. --- Userland/Libraries/LibPDF/ObjectDerivatives.cpp | 19 ++++++++++++++++++- Userland/Libraries/LibPDF/ObjectDerivatives.h | 13 +++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibPDF/ObjectDerivatives.cpp b/Userland/Libraries/LibPDF/ObjectDerivatives.cpp index 888e7d668b..c86f59c2e4 100644 --- a/Userland/Libraries/LibPDF/ObjectDerivatives.cpp +++ b/Userland/Libraries/LibPDF/ObjectDerivatives.cpp @@ -10,6 +10,11 @@ namespace PDF { +PDFErrorOr> ArrayObject::get_object_at(Document* document, size_t index) const +{ + return document->resolve_to(at(index)); +} + PDFErrorOr> DictObject::get_object(Document* document, FlyString const& key) const { return document->resolve_to(get_value(key)); @@ -23,10 +28,22 @@ PDFErrorOr> DictObject::get_object(Document* document, Fly return document->resolve_to(m_elements[index]); \ } \ \ + NonnullRefPtr ArrayObject::get_##snake_name##_at(size_t index) const \ + { \ + VERIFY(index < m_elements.size()); \ + return cast_to(m_elements[index]); \ + } \ + \ PDFErrorOr> DictObject::get_##snake_name(Document* document, FlyString const& key) const \ { \ - return document->resolve_to(get(key).value()); \ + return document->resolve_to(get_value(key)); \ + } \ + \ + NonnullRefPtr DictObject::get_##snake_name(FlyString const& key) const \ + { \ + return cast_to(get_value(key)); \ } + ENUMERATE_OBJECT_TYPES(DEFINE_ACCESSORS) #undef DEFINE_INDEXER diff --git a/Userland/Libraries/LibPDF/ObjectDerivatives.h b/Userland/Libraries/LibPDF/ObjectDerivatives.h index bb5b31a394..1078c7c917 100644 --- a/Userland/Libraries/LibPDF/ObjectDerivatives.h +++ b/Userland/Libraries/LibPDF/ObjectDerivatives.h @@ -82,8 +82,12 @@ public: ALWAYS_INLINE Value const& operator[](size_t index) const { return at(index); } ALWAYS_INLINE Value const& at(size_t index) const { return m_elements[index]; } -#define DEFINE_INDEXER(class_name, snake_name) \ - PDFErrorOr> get_##snake_name##_at(Document*, size_t index) const; + PDFErrorOr> get_object_at(Document* document, size_t index) const; + NonnullRefPtr get_object_at(size_t index) const { return at(index).get>(); }; + +#define DEFINE_INDEXER(class_name, snake_name) \ + PDFErrorOr> get_##snake_name##_at(Document*, size_t index) const; \ + NonnullRefPtr get_##snake_name##_at(size_t index) const; ENUMERATE_OBJECT_TYPES(DEFINE_INDEXER) #undef DEFINE_INDEXER @@ -128,8 +132,9 @@ public: PDFErrorOr> get_object(Document*, FlyString const& key) const; -#define DEFINE_GETTER(class_name, snake_name) \ - PDFErrorOr> get_##snake_name(Document*, FlyString const& key) const; +#define DEFINE_GETTER(class_name, snake_name) \ + PDFErrorOr> get_##snake_name(Document*, FlyString const& key) const; \ + NonnullRefPtr get_##snake_name(FlyString const& key) const; ENUMERATE_OBJECT_TYPES(DEFINE_GETTER) #undef DEFINE_GETTER -- cgit v1.2.3