1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:17:34 +00:00

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.
This commit is contained in:
Rodrigo Tobar 2023-01-05 23:55:25 +08:00 committed by Andreas Kling
parent f510b2b180
commit 0e1c858f90
2 changed files with 17 additions and 14 deletions

View file

@ -116,20 +116,7 @@ public:
template<IsValueType T>
PDFErrorOr<UnwrappedValueType<T>> resolve_to(Value const& value)
{
auto resolved = TRY(resolve(value));
if constexpr (IsSame<T, bool>)
return resolved.get<bool>();
else if constexpr (IsSame<T, int>)
return resolved.get<int>();
else if constexpr (IsSame<T, float>)
return resolved.get<float>();
else if constexpr (IsSame<T, Object>)
return resolved.get<NonnullRefPtr<Object>>();
else if constexpr (IsObject<T>)
return resolved.get<NonnullRefPtr<Object>>()->cast<T>();
VERIFY_NOT_REACHED();
return cast_to<T>(TRY(resolve(value)));
}
private:

View file

@ -195,4 +195,20 @@ private:
Value m_value;
};
template<IsValueType T>
UnwrappedValueType<T> cast_to(Value const& value)
{
if constexpr (IsSame<T, bool>)
return value.get<bool>();
else if constexpr (IsSame<T, int>)
return value.get<int>();
else if constexpr (IsSame<T, float>)
return value.get<float>();
else if constexpr (IsSame<T, Object>)
return value.get<NonnullRefPtr<Object>>();
else if constexpr (IsObject<T>)
return value.get<NonnullRefPtr<Object>>()->cast<T>();
VERIFY_NOT_REACHED();
}
}