1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:17:44 +00:00

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<Object>.

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.
This commit is contained in:
Rodrigo Tobar 2023-01-06 00:19:12 +08:00 committed by Andreas Kling
parent 0e1c858f90
commit 8c79f0e0cf
2 changed files with 27 additions and 5 deletions

View file

@ -10,6 +10,11 @@
namespace PDF {
PDFErrorOr<NonnullRefPtr<Object>> ArrayObject::get_object_at(Document* document, size_t index) const
{
return document->resolve_to<Object>(at(index));
}
PDFErrorOr<NonnullRefPtr<Object>> DictObject::get_object(Document* document, FlyString const& key) const
{
return document->resolve_to<Object>(get_value(key));
@ -23,10 +28,22 @@ PDFErrorOr<NonnullRefPtr<Object>> DictObject::get_object(Document* document, Fly
return document->resolve_to<class_name>(m_elements[index]); \
} \
\
NonnullRefPtr<class_name> ArrayObject::get_##snake_name##_at(size_t index) const \
{ \
VERIFY(index < m_elements.size()); \
return cast_to<class_name>(m_elements[index]); \
} \
\
PDFErrorOr<NonnullRefPtr<class_name>> DictObject::get_##snake_name(Document* document, FlyString const& key) const \
{ \
return document->resolve_to<class_name>(get(key).value()); \
return document->resolve_to<class_name>(get_value(key)); \
} \
\
NonnullRefPtr<class_name> DictObject::get_##snake_name(FlyString const& key) const \
{ \
return cast_to<class_name>(get_value(key)); \
}
ENUMERATE_OBJECT_TYPES(DEFINE_ACCESSORS)
#undef DEFINE_INDEXER