Coverage Report

Created: 2026-12-01 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
0
        {
68
0
            return obj == other.obj;
69
0
        }
70
71
        // Structural equivalence check per PDF Annex J rules.
72
        bool equivalent_to(BaseHandle const& other, int depth = 10) const;
73
74
        // For arrays, return the number of items in the array.
75
        // For null-like objects, return 0.
76
        // For all other objects, return 1.
77
        size_t size() const;
78
79
        // Return 'true' if size() == 0.
80
        bool
81
        empty() const
82
28.3k
        {
83
28.3k
            return size() == 0;
84
28.3k
        }
85
86
        QPDFObjectHandle operator[](size_t n) const;
87
        QPDFObjectHandle operator[](int n) const;
88
89
        QPDFObjectHandle& at(std::string const& key) const;
90
        bool contains(std::string const& key) const;
91
        size_t erase(std::string const& key);
92
        QPDFObjectHandle& find(std::string const& key) const;
93
        bool replace(std::string const& key, QPDFObjectHandle value);
94
        QPDFObjectHandle const& operator[](std::string const& key) const;
95
96
        std::shared_ptr<QPDFObject> copy(bool shallow = false) const;
97
        // Recursively remove association with any QPDF object. This method may only be called
98
        // during final destruction.
99
        void disconnect(bool only_direct = true);
100
        inline QPDFObjGen id_gen() const;
101
        inline bool indirect() const;
102
        inline bool null() const;
103
        inline qpdf_offset_t offset() const;
104
        inline QPDF* qpdf() const;
105
        inline qpdf_object_type_e raw_type_code() const;
106
        inline qpdf_object_type_e resolved_type_code() const;
107
        inline qpdf_object_type_e type_code() const;
108
        std::string unparse() const;
109
        void write_json(int json_version, JSON::Writer& p) const;
110
        static void warn(QPDF*, QPDFExc&&);
111
        void warn(QPDFExc&&) const;
112
        void warn(std::string const& warning) const;
113
114
        inline std::shared_ptr<QPDFObject> const& obj_sp() const;
115
        inline QPDFObjectHandle oh() const;
116
117
      protected:
118
349M
        BaseHandle() = default;
119
        BaseHandle(std::shared_ptr<QPDFObject> const& obj) :
120
21.5M
            obj(obj) {};
121
        BaseHandle(std::shared_ptr<QPDFObject>&& obj) :
122
27.7M
            obj(std::move(obj)) {};
123
194M
        BaseHandle(BaseHandle const&) = default;
124
96.3k
        BaseHandle& operator=(BaseHandle const&) = default;
125
48.7M
        BaseHandle(BaseHandle&&) = default;
126
13.6M
        BaseHandle& operator=(BaseHandle&&) = default;
127
128
        inline BaseHandle(QPDFObjectHandle const& oh);
129
        inline BaseHandle(QPDFObjectHandle&& oh);
130
131
980M
        ~BaseHandle() = default;
132
133
        template <typename T>
134
        T* as() const;
135
136
        inline void assign(qpdf_object_type_e required, BaseHandle const& other);
137
        inline void assign(qpdf_object_type_e required, BaseHandle&& other);
138
        inline void nullify();
139
140
        std::string description() const;
141
        inline QPDFObjectHandle const& get(std::string const& key) const;
142
143
        void no_ci_warn_if(bool condition, std::string const& warning) const;
144
        void no_ci_stop_if(bool condition, std::string const& warning) const;
145
        void no_ci_stop_damaged_if(bool condition, std::string const& warning) const;
146
        std::invalid_argument invalid_error(std::string const& method) const;
147
        std::runtime_error type_error(char const* expected_type) const;
148
        QPDFExc type_error(char const* expected_type, std::string const& message) const;
149
        char const* type_name() const;
150
151
        std::shared_ptr<QPDFObject> obj;
152
153
      private:
154
        inline QPDFObjectHandle referenced_object() const;
155
    };
156
157
} // namespace qpdf
158
159
#endif // QPDFOBJECTHANDLE_HH