/src/qpdf/include/qpdf/ObjectHandle.hh
Line | Count | Source |
1 | | // Copyright (c) 2005-2021 Jay Berkenbilt |
2 | | // Copyright (c) 2022-2026 Jay Berkenbilt and Manfred Holger |
3 | | // |
4 | | // This file is part of qpdf. |
5 | | // |
6 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
7 | | // in compliance with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
12 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
13 | | // or implied. See the License for the specific language governing permissions and limitations under |
14 | | // the License. |
15 | | // |
16 | | // Versions of qpdf prior to version 7 were released under the terms of version 2.0 of the Artistic |
17 | | // License. At your option, you may continue to consider qpdf to be licensed under those terms. |
18 | | // Please see the manual for additional information. |
19 | | |
20 | | #ifndef OBJECTHANDLE_HH |
21 | | #define OBJECTHANDLE_HH |
22 | | |
23 | | #include <qpdf/Constants.h> |
24 | | #include <qpdf/DLL.h> |
25 | | #include <qpdf/Types.h> |
26 | | |
27 | | #include <qpdf/JSON.hh> |
28 | | #include <qpdf/QPDFExc.hh> |
29 | | #include <qpdf/QPDFObjGen.hh> |
30 | | |
31 | | #include <cstdint> |
32 | | #include <memory> |
33 | | |
34 | | class QPDF; |
35 | | class QPDF_Dictionary; |
36 | | class QPDFObject; |
37 | | class QPDFObjectHandle; |
38 | | |
39 | | namespace qpdf |
40 | | { |
41 | | class Array; |
42 | | class BaseDictionary; |
43 | | class Dictionary; |
44 | | class Integer; |
45 | | class Stream; |
46 | | |
47 | | enum typed : std::uint8_t { strict = 0, any_flag = 1, optional = 2, any = 3, error = 4 }; |
48 | | |
49 | | // Basehandle is only used as a base-class for QPDFObjectHandle like classes. Currently the only |
50 | | // methods exposed in public API are operators to convert derived objects to QPDFObjectHandle, |
51 | | // QPDFObjGen and bool. |
52 | | class BaseHandle |
53 | | { |
54 | | friend class ::QPDF; |
55 | | |
56 | | public: |
57 | | explicit inline operator bool() const; |
58 | | inline operator QPDFObjectHandle() const; |
59 | | QPDF_DLL |
60 | | operator QPDFObjGen() const; |
61 | | |
62 | | // The rest of the header file is for qpdf internal use only. |
63 | | |
64 | | // Return true if both object handles refer to the same underlying object. |
65 | | bool |
66 | | operator==(BaseHandle const& other) const |
67 | 79.3k | { |
68 | 79.3k | return obj == other.obj; |
69 | 79.3k | } |
70 | | |
71 | | // For arrays, return the number of items in the array. |
72 | | // For null-like objects, return 0. |
73 | | // For all other objects, return 1. |
74 | | size_t size() const; |
75 | | |
76 | | // Return 'true' if size() == 0. |
77 | | bool |
78 | | empty() const |
79 | 4.41k | { |
80 | 4.41k | return size() == 0; |
81 | 4.41k | } |
82 | | |
83 | | QPDFObjectHandle operator[](size_t n) const; |
84 | | QPDFObjectHandle operator[](int n) const; |
85 | | |
86 | | QPDFObjectHandle& at(std::string const& key) const; |
87 | | bool contains(std::string const& key) const; |
88 | | size_t erase(std::string const& key); |
89 | | QPDFObjectHandle& find(std::string const& key) const; |
90 | | bool replace(std::string const& key, QPDFObjectHandle value); |
91 | | QPDFObjectHandle const& operator[](std::string const& key) const; |
92 | | |
93 | | std::shared_ptr<QPDFObject> copy(bool shallow = false) const; |
94 | | // Recursively remove association with any QPDF object. This method may only be called |
95 | | // during final destruction. |
96 | | void disconnect(bool only_direct = true); |
97 | | inline QPDFObjGen id_gen() const; |
98 | | inline bool indirect() const; |
99 | | inline bool null() const; |
100 | | inline qpdf_offset_t offset() const; |
101 | | inline QPDF* qpdf() const; |
102 | | inline qpdf_object_type_e raw_type_code() const; |
103 | | inline qpdf_object_type_e resolved_type_code() const; |
104 | | inline qpdf_object_type_e type_code() const; |
105 | | std::string unparse() const; |
106 | | void write_json(int json_version, JSON::Writer& p) const; |
107 | | static void warn(QPDF*, QPDFExc&&); |
108 | | void warn(QPDFExc&&) const; |
109 | | void warn(std::string const& warning) const; |
110 | | |
111 | | inline std::shared_ptr<QPDFObject> const& obj_sp() const; |
112 | | inline QPDFObjectHandle oh() const; |
113 | | |
114 | | protected: |
115 | 1.33M | BaseHandle() = default; |
116 | | BaseHandle(std::shared_ptr<QPDFObject> const& obj) : |
117 | 3.64M | obj(obj) {}; |
118 | | BaseHandle(std::shared_ptr<QPDFObject>&& obj) : |
119 | 6.76M | obj(std::move(obj)) {}; |
120 | 5.78M | BaseHandle(BaseHandle const&) = default; |
121 | 558k | BaseHandle& operator=(BaseHandle const&) = default; |
122 | 8.37M | BaseHandle(BaseHandle&&) = default; |
123 | 531k | BaseHandle& operator=(BaseHandle&&) = default; |
124 | | |
125 | | inline BaseHandle(QPDFObjectHandle const& oh); |
126 | | inline BaseHandle(QPDFObjectHandle&& oh); |
127 | | |
128 | 27.9M | ~BaseHandle() = default; |
129 | | |
130 | | template <typename T> |
131 | | T* as() const; |
132 | | |
133 | | inline void assign(qpdf_object_type_e required, BaseHandle const& other); |
134 | | inline void assign(qpdf_object_type_e required, BaseHandle&& other); |
135 | | |
136 | | inline void nullify(); |
137 | | |
138 | | std::string description() const; |
139 | | |
140 | | inline QPDFObjectHandle const& get(std::string const& key) const; |
141 | | |
142 | | void no_ci_warn_if(bool condition, std::string const& warning) const; |
143 | | void no_ci_stop_if(bool condition, std::string const& warning) const; |
144 | | void no_ci_stop_damaged_if(bool condition, std::string const& warning) const; |
145 | | std::invalid_argument invalid_error(std::string const& method) const; |
146 | | std::runtime_error type_error(char const* expected_type) const; |
147 | | QPDFExc type_error(char const* expected_type, std::string const& message) const; |
148 | | char const* type_name() const; |
149 | | |
150 | | std::shared_ptr<QPDFObject> obj; |
151 | | }; |
152 | | |
153 | | } // namespace qpdf |
154 | | |
155 | | #endif // QPDFOBJECTHANDLE_HH |