/src/serenity/Userland/Libraries/LibPDF/Value.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/ByteString.h> |
10 | | #include <AK/Format.h> |
11 | | #include <AK/RefPtr.h> |
12 | | #include <AK/Variant.h> |
13 | | #include <LibPDF/Forward.h> |
14 | | #include <LibPDF/Object.h> |
15 | | #include <LibPDF/Reference.h> |
16 | | |
17 | | namespace PDF { |
18 | | |
19 | | class Value : public Variant<Empty, nullptr_t, bool, int, float, Reference, NonnullRefPtr<Object>> { |
20 | | public: |
21 | | using Variant::Variant; |
22 | | |
23 | | template<IsObject T> |
24 | | Value(RefPtr<T> const& refptr) |
25 | | : Variant(nullptr) |
26 | | { |
27 | | if (refptr) |
28 | | set<NonnullRefPtr<Object>>(*refptr); |
29 | | } |
30 | | |
31 | | template<IsObject T> |
32 | | Value(NonnullRefPtr<T> const& refptr) |
33 | | requires(!IsSame<Object, T>) |
34 | 25 | : Variant(nullptr) |
35 | 25 | { |
36 | 25 | set<NonnullRefPtr<Object>>(*refptr); |
37 | 25 | } _ZN3PDF5ValueC2ITkNS_8IsObjectENS_10NameObjectEEERKN2AK13NonnullRefPtrIT_EEQnt6IsSameINS_6ObjectES5_E Line | Count | Source | 34 | 6 | : Variant(nullptr) | 35 | 6 | { | 36 | 6 | set<NonnullRefPtr<Object>>(*refptr); | 37 | 6 | } |
_ZN3PDF5ValueC2ITkNS_8IsObjectENS_10DictObjectEEERKN2AK13NonnullRefPtrIT_EEQnt6IsSameINS_6ObjectES5_E Line | Count | Source | 34 | 13 | : Variant(nullptr) | 35 | 13 | { | 36 | 13 | set<NonnullRefPtr<Object>>(*refptr); | 37 | 13 | } |
Unexecuted instantiation: _ZN3PDF5ValueC2ITkNS_8IsObjectENS_12StringObjectEEERKN2AK13NonnullRefPtrIT_EEQnt6IsSameINS_6ObjectES5_E _ZN3PDF5ValueC2ITkNS_8IsObjectENS_11ArrayObjectEEERKN2AK13NonnullRefPtrIT_EEQnt6IsSameINS_6ObjectES5_E Line | Count | Source | 34 | 6 | : Variant(nullptr) | 35 | 6 | { | 36 | 6 | set<NonnullRefPtr<Object>>(*refptr); | 37 | 6 | } |
Unexecuted instantiation: _ZN3PDF5ValueC2ITkNS_8IsObjectENS_13IndirectValueEEERKN2AK13NonnullRefPtrIT_EEQnt6IsSameINS_6ObjectES5_E Unexecuted instantiation: _ZN3PDF5ValueC2ITkNS_8IsObjectENS_12StreamObjectEEERKN2AK13NonnullRefPtrIT_EEQnt6IsSameINS_6ObjectES5_E |
38 | | |
39 | | [[nodiscard]] ByteString to_byte_string(int indent = 0) const; |
40 | | |
41 | 0 | [[nodiscard]] ALWAYS_INLINE bool has_number() const { return has<int>() || has<float>(); } |
42 | | |
43 | | [[nodiscard]] ALWAYS_INLINE bool has_u32() const |
44 | 66 | { |
45 | 66 | return has<int>() && get<int>() >= 0; |
46 | 66 | } |
47 | | |
48 | | [[nodiscard]] ALWAYS_INLINE bool has_u16() const |
49 | 12 | { |
50 | 12 | return has<int>() && get<int>() >= 0 && get<int>() < 65536; |
51 | 12 | } |
52 | | |
53 | | [[nodiscard]] ALWAYS_INLINE u32 get_u32() const |
54 | 36 | { |
55 | 36 | VERIFY(has_u32()); |
56 | 36 | return get<int>(); |
57 | 36 | } |
58 | | |
59 | | [[nodiscard]] ALWAYS_INLINE u16 get_u16() const |
60 | 6 | { |
61 | 6 | VERIFY(has_u16()); |
62 | 6 | return get<int>(); |
63 | 6 | } |
64 | | |
65 | | [[nodiscard]] ALWAYS_INLINE int to_int() const |
66 | 0 | { |
67 | 0 | if (has<int>()) |
68 | 0 | return get<int>(); |
69 | 0 | return static_cast<int>(get<float>()); |
70 | 0 | } |
71 | | |
72 | | [[nodiscard]] ALWAYS_INLINE float to_float() const |
73 | 0 | { |
74 | 0 | if (has<float>()) |
75 | 0 | return get<float>(); |
76 | 0 | return static_cast<float>(get<int>()); |
77 | 0 | } |
78 | | |
79 | | [[nodiscard]] ALWAYS_INLINE u32 as_ref_index() const |
80 | 0 | { |
81 | 0 | return get<Reference>().as_ref_index(); |
82 | 0 | } |
83 | | |
84 | | [[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const |
85 | 0 | { |
86 | 0 | return get<Reference>().as_ref_generation_index(); |
87 | 0 | } |
88 | | }; |
89 | | |
90 | | } |
91 | | |
92 | | namespace AK { |
93 | | |
94 | | template<> |
95 | | struct Formatter<PDF::Value> : Formatter<StringView> { |
96 | | ErrorOr<void> format(FormatBuilder& builder, PDF::Value const& value) |
97 | 0 | { |
98 | 0 | return Formatter<StringView>::format(builder, value.to_byte_string()); |
99 | 0 | } |
100 | | }; |
101 | | |
102 | | } |