2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

Merge pull request #979 from m-holger/const

In FUTURE make various QPDFObjectHandle methods const
This commit is contained in:
m-holger 2024-07-19 10:50:08 +01:00 committed by GitHub
commit 9ce18e41f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 2252 additions and 172 deletions

View File

@ -796,12 +796,13 @@ class QPDF
class Resolver
{
friend class QPDFObject;
friend class QPDF_Unresolved;
private:
static void
resolve(QPDF* qpdf, QPDFObjGen const& og)
static QPDFObject*
resolved(QPDF* qpdf, QPDFObjGen og)
{
qpdf->resolve(og);
return qpdf->resolve(og);
}
};
@ -1061,7 +1062,7 @@ class QPDF
QPDFObjGen exp_og,
QPDFObjGen& og,
bool skip_cache_if_in_xref);
void resolve(QPDFObjGen og);
QPDFObject* resolve(QPDFObjGen og);
void resolveObjectsInStream(int obj_stream_number);
void stopOnError(std::string const& message);
QPDFObjectHandle reserveObjectIfNotExists(QPDFObjGen const& og);

View File

@ -22,23 +22,27 @@
#ifndef QPDFOBJECTHANDLE_HH
#define QPDFOBJECTHANDLE_HH
#include <qpdf/Constants.h>
#include <qpdf/DLL.h>
#include <qpdf/Types.h>
#ifdef QPDF_FUTURE
# include <qpdf/QPDFObjectHandle_future.hh>
#else
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
# include <qpdf/Constants.h>
# include <qpdf/DLL.h>
# include <qpdf/Types.h>
#include <qpdf/Buffer.hh>
#include <qpdf/InputSource.hh>
#include <qpdf/JSON.hh>
#include <qpdf/PointerHolder.hh> // unused -- remove in qpdf 12 (see #785)
#include <qpdf/QPDFObjGen.hh>
#include <qpdf/QPDFTokenizer.hh>
# include <functional>
# include <map>
# include <memory>
# include <set>
# include <string>
# include <vector>
# include <qpdf/Buffer.hh>
# include <qpdf/InputSource.hh>
# include <qpdf/JSON.hh>
# include <qpdf/PointerHolder.hh> // unused -- remove in qpdf 12 (see #785)
# include <qpdf/QPDFObjGen.hh>
# include <qpdf/QPDFTokenizer.hh>
class Pipeline;
class QPDF;
@ -291,13 +295,6 @@ class QPDFObjectHandle
QPDF_DLL
QPDFObjectHandle& operator=(QPDFObjectHandle const&) = default;
#ifdef QPDF_FUTURE
QPDF_DLL
QPDFObjectHandle(QPDFObjectHandle&&) = default;
QPDF_DLL
QPDFObjectHandle& operator=(QPDFObjectHandle&&) = default;
#endif
QPDF_DLL
inline bool isInitialized() const;
@ -1363,24 +1360,23 @@ class QPDFObjectHandle
void writeJSON(int json_version, JSON::Writer& p, bool dereference_indirect = false);
private:
QPDF_Array* asArray();
QPDF_Bool* asBool();
QPDF_Dictionary* asDictionary();
QPDF_InlineImage* asInlineImage();
QPDF_Integer* asInteger();
QPDF_Name* asName();
QPDF_Null* asNull();
QPDF_Operator* asOperator();
QPDF_Real* asReal();
QPDF_Reserved* asReserved();
QPDF_Stream* asStream();
QPDF_Stream* asStreamWithAssert();
QPDF_String* asString();
QPDF_Array* asArray() const;
QPDF_Bool* asBool() const;
QPDF_Dictionary* asDictionary() const;
QPDF_InlineImage* asInlineImage() const;
QPDF_Integer* asInteger() const;
QPDF_Name* asName() const;
QPDF_Null* asNull() const;
QPDF_Operator* asOperator() const;
QPDF_Real* asReal() const;
QPDF_Reserved* asReserved() const;
QPDF_Stream* asStream() const;
QPDF_Stream* asStreamWithAssert() const;
QPDF_String* asString() const;
void typeWarning(char const* expected_type, std::string const& warning);
void objectWarning(std::string const& warning);
void assertType(char const* type_name, bool istype);
inline bool dereference();
void typeWarning(char const* expected_type, std::string const& warning) const;
void objectWarning(std::string const& warning) const;
void assertType(char const* type_name, bool istype) const;
void makeDirect(QPDFObjGen::set& visited, bool stop_at_streams);
void disconnect();
void setParsedOffset(qpdf_offset_t offset);
@ -1400,7 +1396,7 @@ class QPDFObjectHandle
std::shared_ptr<QPDFObject> obj;
};
#ifndef QPDF_NO_QPDF_STRING
# ifndef QPDF_NO_QPDF_STRING
// This is short for QPDFObjectHandle::parse, so you can do
// auto oh = "<< /Key (value) >>"_qpdf;
@ -1416,7 +1412,7 @@ QPDF_DLL
QPDFObjectHandle operator ""_qpdf(char const* v, size_t len);
/* clang-format on */
#endif // QPDF_NO_QPDF_STRING
# endif // QPDF_NO_QPDF_STRING
class QPDFObjectHandle::QPDFDictItems
{
@ -1638,4 +1634,5 @@ QPDFObjectHandle::isInitialized() const
return obj != nullptr;
}
#endif // QPDF_FUTURE
#endif // QPDFOBJECTHANDLE_HH

File diff suppressed because it is too large Load Diff

View File

@ -1847,11 +1847,11 @@ QPDF::readObjectAtOffset(
return oh;
}
void
QPDFObject*
QPDF::resolve(QPDFObjGen og)
{
if (!isUnresolved(og)) {
return;
return m->obj_cache[og].object.get();
}
if (m->resolving.count(og)) {
@ -1860,7 +1860,7 @@ QPDF::resolve(QPDFObjGen og)
QTC::TC("qpdf", "QPDF recursion loop in resolve");
warn(damagedPDF("", "loop detected resolving object " + og.unparse(' ')));
updateCache(og, QPDF_Null::create(), -1, -1);
return;
return m->obj_cache[og].object.get();
}
ResolveRecorder rr(this, og);
@ -1901,6 +1901,7 @@ QPDF::resolve(QPDFObjGen og)
auto result(m->obj_cache[og].object);
result->setDefaultDescription(this, og);
return result.get();
}
void

View File

@ -3,13 +3,6 @@
#include <qpdf/QPDF.hh>
#include <qpdf/QPDF_Destroyed.hh>
void
QPDFObject::doResolve()
{
auto og = value->og;
QPDF::Resolver::resolve(value->qpdf, og);
}
void
QPDFObject::destroy()
{

File diff suppressed because it is too large Load Diff

View File

@ -130,8 +130,7 @@ QPDF_Array::unparse()
for (int j = next; j < key; ++j) {
result += "null ";
}
item.second->resolve();
auto og = item.second->getObjGen();
auto og = item.second->resolved_object()->getObjGen();
result += og.isIndirect() ? og.unparse(' ') + " R " : item.second->unparse() + " ";
next = ++key;
}
@ -140,8 +139,7 @@ QPDF_Array::unparse()
}
} else {
for (auto const& item: elements) {
item->resolve();
auto og = item->getObjGen();
auto og = item->resolved_object()->getObjGen();
result += og.isIndirect() ? og.unparse(' ') + " R " : item->unparse() + " ";
}
}

View File

@ -1,6 +1,7 @@
#include <qpdf/QPDF_Unresolved.hh>
#include <stdexcept>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFObject_private.hh>
QPDF_Unresolved::QPDF_Unresolved(QPDF* qpdf, QPDFObjGen const& og) :
QPDFValue(::ot_unresolved, "unresolved", qpdf, og)
@ -16,19 +17,23 @@ QPDF_Unresolved::create(QPDF* qpdf, QPDFObjGen const& og)
std::shared_ptr<QPDFObject>
QPDF_Unresolved::copy(bool shallow)
{
throw std::logic_error("attempted to shallow copy an unresolved QPDFObjectHandle");
return nullptr;
return QPDF::Resolver::resolved(qpdf, og)->copy(shallow);
}
std::string
QPDF_Unresolved::unparse()
{
throw std::logic_error("attempted to unparse an unresolved QPDFObjectHandle");
return "";
return QPDF::Resolver::resolved(qpdf, og)->unparse();
}
void
QPDF_Unresolved::writeJSON(int json_version, JSON::Writer& p)
{
throw std::logic_error("attempted to get JSON from an unresolved QPDFObjectHandle");
QPDF::Resolver::resolved(qpdf, og)->writeJSON(json_version, p);
}
std::string
QPDF_Unresolved::getStringValue() const
{
return QPDF::Resolver::resolved(qpdf, og)->getStringValue();
}

View File

@ -5,8 +5,8 @@
// include/qpdf/QPDFObject.hh. See comments there for an explanation.
#include <qpdf/Constants.h>
#include <qpdf/DLL.h>
#include <qpdf/JSON.hh>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFValue.hh>
#include <qpdf/Types.h>
@ -43,18 +43,26 @@ class QPDFObject
{
return value->getStringValue();
}
// Return a unique type code for the resolved object
qpdf_object_type_e
getResolvedTypeCode() const
{
auto tc = value->type_code;
return tc == ::ot_unresolved
? QPDF::Resolver::resolved(value->qpdf, value->og)->value->type_code
: tc;
}
// Return a unique type code for the object
qpdf_object_type_e
getTypeCode() const
getTypeCode() const noexcept
{
return value->type_code;
}
// Return a string literal that describes the type, useful for debugging and testing
char const*
getTypeName() const
{
return value->type_name;
return resolved_object()->value->type_name;
}
QPDF*
@ -157,20 +165,23 @@ class QPDFObject
{
return value->type_code == ::ot_unresolved;
}
void
resolve()
const QPDFObject*
resolved_object() const
{
if (isUnresolved()) {
doResolve();
}
return isUnresolved() ? QPDF::Resolver::resolved(value->qpdf, value->og) : this;
}
void doResolve();
template <typename T>
T*
as()
as() const
{
return dynamic_cast<T*>(value.get());
if (auto result = dynamic_cast<T*>(value.get())) {
return result;
} else {
return isUnresolved()
? dynamic_cast<T*>(QPDF::Resolver::resolved(value->qpdf, value->og)->value.get())
: nullptr;
}
}
private:

View File

@ -11,6 +11,7 @@ class QPDF_Unresolved: public QPDFValue
std::shared_ptr<QPDFObject> copy(bool shallow = false) override;
std::string unparse() override;
void writeJSON(int json_version, JSON::Writer& p) override;
std::string getStringValue() const override;
private:
QPDF_Unresolved(QPDF* qpdf, QPDFObjGen const& og);