/* * Copyright (c) 2021-2022, Matthew Olsson * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace PDF { class Value : public Variant> { public: using Variant::Variant; template Value(RefPtr const& refptr) : Variant(nullptr) { if (refptr) set>(*refptr); } template Value(NonnullRefPtr const& refptr) requires(!IsSame) : Variant(nullptr) { set>(*refptr); } [[nodiscard]] String to_string(int indent = 0) const; [[nodiscard]] ALWAYS_INLINE bool has_number() const { return has() || has(); } [[nodiscard]] ALWAYS_INLINE bool has_u32() const { return has() && get() >= 0; } [[nodiscard]] ALWAYS_INLINE bool has_u16() const { return has() && get() >= 0 && get() < 65536; } [[nodiscard]] ALWAYS_INLINE u32 get_u32() const { VERIFY(has_u32()); return get(); } [[nodiscard]] ALWAYS_INLINE u16 get_u16() const { VERIFY(has_u16()); return get(); } [[nodiscard]] ALWAYS_INLINE int to_int() const { if (has()) return get(); return static_cast(get()); } [[nodiscard]] ALWAYS_INLINE float to_float() const { if (has()) return get(); return static_cast(get()); } [[nodiscard]] ALWAYS_INLINE u32 as_ref_index() const { return get().as_ref_index(); } [[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const { return get().as_ref_generation_index(); } }; } namespace AK { template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, PDF::Value const& value) { return Formatter::format(builder, value.to_string()); } }; }