2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-02-02 11:58:25 +00:00

Deprecate QPDFObjectHandle::isInitialized and remove from library

This commit is contained in:
m-holger 2024-06-19 17:18:43 +01:00 committed by m-holger
parent 5a33c75b98
commit 0afaaea22a
14 changed files with 46 additions and 53 deletions

View File

@ -131,7 +131,7 @@ main(int argc, char* argv[])
QPDFObjectHandle fileinfo; QPDFObjectHandle fileinfo;
for (auto const& it: Keys) { for (auto const& it: Keys) {
if (!fileinfo.isInitialized()) { if (!fileinfo) {
if (filetrailer.hasKey("/Info")) { if (filetrailer.hasKey("/Info")) {
QTC::TC("examples", "pdf-mod-info has info"); QTC::TC("examples", "pdf-mod-info has info");
fileinfo = filetrailer.getKey("/Info"); fileinfo = filetrailer.getKey("/Info");

View File

@ -300,8 +300,7 @@ class QPDFObjectHandle
QPDF_DLL QPDF_DLL
explicit inline operator bool() const noexcept; explicit inline operator bool() const noexcept;
QPDF_DLL [[deprecated("use operator bool()")]] QPDF_DLL inline bool isInitialized() const;
inline bool isInitialized() const;
// This method returns true if the QPDFObjectHandle objects point to exactly the same underlying // This method returns true if the QPDFObjectHandle objects point to exactly the same underlying
// object, meaning that changes to one are reflected in the other, or "if you paint one, the // object, meaning that changes to one are reflected in the other, or "if you paint one, the

View File

@ -300,8 +300,7 @@ class QPDFObjectHandle
QPDF_DLL QPDF_DLL
explicit inline operator bool() const noexcept; explicit inline operator bool() const noexcept;
QPDF_DLL [[deprecated("use operator bool()")]] QPDF_DLL inline bool isInitialized() const noexcept;
inline bool isInitialized() const noexcept;
// This method returns true if the QPDFObjectHandle objects point to exactly the same underlying // This method returns true if the QPDFObjectHandle objects point to exactly the same underlying
// object, meaning that changes to one are reflected in the other, or "if you paint one, the // object, meaning that changes to one are reflected in the other, or "if you paint one, the

View File

@ -188,7 +188,7 @@ NNTreeIterator::resetLimits(QPDFObjectHandle node, std::list<PathElement>::itera
} }
} }
} }
if (first.isInitialized() && last.isInitialized()) { if (first && last) {
auto limits = QPDFObjectHandle::newArray(); auto limits = QPDFObjectHandle::newArray();
limits.appendItem(first); limits.appendItem(first);
limits.appendItem(last); limits.appendItem(last);

View File

@ -526,7 +526,7 @@ QPDF::warn(
void void
QPDF::setTrailer(QPDFObjectHandle obj) QPDF::setTrailer(QPDFObjectHandle obj)
{ {
if (m->trailer.isInitialized()) { if (m->trailer) {
return; return;
} }
m->trailer = obj; m->trailer = obj;
@ -591,7 +591,7 @@ QPDF::reconstruct_xref(QPDFExc& e)
} }
} }
m->file->seek(pos, SEEK_SET); m->file->seek(pos, SEEK_SET);
} else if (!m->trailer.isInitialized() && t1.isWord("trailer")) { } else if (!m->trailer && t1.isWord("trailer")) {
auto pos = m->file->tell(); auto pos = m->file->tell();
QPDFObjectHandle t = readTrailer(); QPDFObjectHandle t = readTrailer();
if (!t.isDictionary()) { if (!t.isDictionary()) {
@ -606,7 +606,7 @@ QPDF::reconstruct_xref(QPDFExc& e)
} }
m->deleted_objects.clear(); m->deleted_objects.clear();
if (!m->trailer.isInitialized()) { if (!m->trailer) {
qpdf_offset_t max_offset{0}; qpdf_offset_t max_offset{0};
// If there are any xref streams, take the last one to appear. // If there are any xref streams, take the last one to appear.
for (auto const& iter: m->xref_table) { for (auto const& iter: m->xref_table) {
@ -640,7 +640,7 @@ QPDF::reconstruct_xref(QPDFExc& e)
} }
} }
if (!m->trailer.isInitialized()) { if (!m->trailer) {
// We could check the last encountered object to see if it was an xref stream. If so, we // We could check the last encountered object to see if it was an xref stream. If so, we
// could try to get the trailer from there. This may make it possible to recover files with // could try to get the trailer from there. This may make it possible to recover files with
// bad startxref pointers even when they have object streams. // bad startxref pointers even when they have object streams.
@ -730,7 +730,7 @@ QPDF::read_xref(qpdf_offset_t xref_offset)
} }
} }
if (!m->trailer.isInitialized()) { if (!m->trailer) {
throw damagedPDF("", 0, "unable to find trailer while reading xref"); throw damagedPDF("", 0, "unable to find trailer while reading xref");
} }
int size = m->trailer.getKey("/Size").getIntValueAsInt(); int size = m->trailer.getKey("/Size").getIntValueAsInt();
@ -1000,7 +1000,7 @@ QPDF::read_xrefTable(qpdf_offset_t xref_offset)
throw damagedPDF("", "expected trailer dictionary"); throw damagedPDF("", "expected trailer dictionary");
} }
if (!m->trailer.isInitialized()) { if (!m->trailer) {
setTrailer(cur_trailer); setTrailer(cur_trailer);
if (!m->trailer.hasKey("/Size")) { if (!m->trailer.hasKey("/Size")) {
@ -1258,7 +1258,7 @@ QPDF::processXRefStream(qpdf_offset_t xref_offset, QPDFObjectHandle& xref_obj)
} }
} }
if (!m->trailer.isInitialized()) { if (!m->trailer) {
setTrailer(dict); setTrailer(dict);
} }
@ -2052,7 +2052,7 @@ QPDF::makeIndirectFromQPDFObject(std::shared_ptr<QPDFObject> const& obj)
QPDFObjectHandle QPDFObjectHandle
QPDF::makeIndirectObject(QPDFObjectHandle oh) QPDF::makeIndirectObject(QPDFObjectHandle oh)
{ {
if (!oh.isInitialized()) { if (!oh) {
throw std::logic_error("attempted to make an uninitialized QPDFObjectHandle indirect"); throw std::logic_error("attempted to make an uninitialized QPDFObjectHandle indirect");
} }
return makeIndirectFromQPDFObject(oh.getObj()); return makeIndirectFromQPDFObject(oh.getObj());
@ -2163,7 +2163,7 @@ QPDF::replaceObject(int objid, int generation, QPDFObjectHandle oh)
void void
QPDF::replaceObject(QPDFObjGen const& og, QPDFObjectHandle oh) QPDF::replaceObject(QPDFObjGen const& og, QPDFObjectHandle oh)
{ {
if (!oh.isInitialized() || (oh.isIndirect() && !(oh.isStream() && oh.getObjGen() == og))) { if (!oh || (oh.isIndirect() && !(oh.isStream() && oh.getObjGen() == og))) {
QTC::TC("qpdf", "QPDF replaceObject called with indirect object"); QTC::TC("qpdf", "QPDF replaceObject called with indirect object");
throw std::logic_error("QPDF::replaceObject called with indirect object handle"); throw std::logic_error("QPDF::replaceObject called with indirect object handle");
} }

View File

@ -396,7 +396,7 @@ QPDFFormFieldObjectHelper::setRadioButtonValue(QPDFObjectHandle name)
} else { } else {
annot = kid; annot = kid;
} }
if (!annot.isInitialized()) { if (!annot) {
QTC::TC("qpdf", "QPDFObjectHandle broken radio button"); QTC::TC("qpdf", "QPDFObjectHandle broken radio button");
this->oh.warnIfPossible("unable to set the value of this radio button"); this->oh.warnIfPossible("unable to set the value of this radio button");
continue; continue;
@ -459,7 +459,7 @@ QPDFFormFieldObjectHelper::setCheckBoxValue(bool value)
// Set /AS to the on value or /Off in addition to setting /V. // Set /AS to the on value or /Off in addition to setting /V.
QPDFObjectHandle name = QPDFObjectHandle::newName(value ? on_value : "/Off"); QPDFObjectHandle name = QPDFObjectHandle::newName(value ? on_value : "/Off");
setFieldAttribute("/V", name); setFieldAttribute("/V", name);
if (!annot.isInitialized()) { if (!annot) {
QTC::TC("qpdf", "QPDFObjectHandle broken checkbox"); QTC::TC("qpdf", "QPDFObjectHandle broken checkbox");
this->oh.warnIfPossible("unable to set the value of this checkbox"); this->oh.warnIfPossible("unable to set the value of this checkbox");
return; return;
@ -815,7 +815,7 @@ QPDFFormFieldObjectHelper::generateTextAppearance(QPDFAnnotationObjectHelper& ao
QPDFObjectHandle resources = AS.getDict().getKey("/Resources"); QPDFObjectHandle resources = AS.getDict().getKey("/Resources");
QPDFObjectHandle font = getFontFromResource(resources, font_name); QPDFObjectHandle font = getFontFromResource(resources, font_name);
bool found_font_in_dr = false; bool found_font_in_dr = false;
if (!font.isInitialized()) { if (!font) {
QPDFObjectHandle dr = getDefaultResources(); QPDFObjectHandle dr = getDefaultResources();
font = getFontFromResource(dr, font_name); font = getFontFromResource(dr, font_name);
found_font_in_dr = font.isDictionary(); found_font_in_dr = font.isDictionary();

View File

@ -2165,7 +2165,7 @@ QPDFObjectHandle::parseContentStream_data(
input->seek(offset, SEEK_SET); input->seek(offset, SEEK_SET);
auto obj = auto obj =
QPDFParser(*input, "content", tokenizer, nullptr, context, false).parse(empty, true); QPDFParser(*input, "content", tokenizer, nullptr, context, false).parse(empty, true);
if (!obj.isInitialized()) { if (!obj) {
// EOF // EOF
break; break;
} }
@ -2520,7 +2520,7 @@ QPDFObjectHandle::makeDirect(bool allow_streams)
void void
QPDFObjectHandle::assertInitialized() const QPDFObjectHandle::assertInitialized() const
{ {
if (!isInitialized()) { if (!obj) {
throw std::logic_error("operation attempted on uninitialized QPDFObjectHandle"); throw std::logic_error("operation attempted on uninitialized QPDFObjectHandle");
} }
} }
@ -3018,34 +3018,30 @@ QPDFObjectHandle::QPDFArrayItems::end()
QPDFObjGen QPDFObjGen
QPDFObjectHandle::getObjGen() const QPDFObjectHandle::getObjGen() const
{ {
return isInitialized() ? obj->getObjGen() : QPDFObjGen(); return obj ? obj->getObjGen() : QPDFObjGen();
} }
// Indirect object accessors // Indirect object accessors
QPDF* QPDF*
QPDFObjectHandle::getOwningQPDF() const QPDFObjectHandle::getOwningQPDF() const
{ {
return isInitialized() ? this->obj->getQPDF() : nullptr; return obj ? obj->getQPDF() : nullptr;
} }
QPDF& QPDF&
QPDFObjectHandle::getQPDF(std::string const& error_msg) const QPDFObjectHandle::getQPDF(std::string const& error_msg) const
{ {
auto result = isInitialized() ? this->obj->getQPDF() : nullptr; if (auto result = obj ? obj->getQPDF() : nullptr) {
if (result == nullptr) {
throw std::runtime_error(
error_msg.empty() ? "attempt to use a null qpdf object" : error_msg);
}
return *result; return *result;
} }
throw std::runtime_error(error_msg.empty() ? "attempt to use a null qpdf object" : error_msg);
}
void void
QPDFObjectHandle::setParsedOffset(qpdf_offset_t offset) QPDFObjectHandle::setParsedOffset(qpdf_offset_t offset)
{ {
// This is called during parsing on newly created direct objects, if (obj) {
// so we can't call dereference() here. obj->setParsedOffset(offset);
if (isInitialized()) {
this->obj->setParsedOffset(offset);
} }
} }

View File

@ -67,7 +67,7 @@ QPDFOutlineDocumentHelper::resolveNamedDest(QPDFObjectHandle name)
{ {
QPDFObjectHandle result; QPDFObjectHandle result;
if (name.isName()) { if (name.isName()) {
if (!m->dest_dict.isInitialized()) { if (!m->dest_dict) {
m->dest_dict = qpdf.getRoot().getKey("/Dests"); m->dest_dict = qpdf.getRoot().getKey("/Dests");
} }
QTC::TC("qpdf", "QPDFOutlineDocumentHelper name named dest"); QTC::TC("qpdf", "QPDFOutlineDocumentHelper name named dest");
@ -85,7 +85,7 @@ QPDFOutlineDocumentHelper::resolveNamedDest(QPDFObjectHandle name)
} }
} }
} }
if (!result.isInitialized()) { if (!result) {
return QPDFObjectHandle::newNull(); return QPDFObjectHandle::newNull();
} }
if (result.isDictionary()) { if (result.isDictionary()) {

View File

@ -59,8 +59,8 @@ QPDFOutlineObjectHelper::getDest()
QTC::TC("qpdf", "QPDFOutlineObjectHelper action dest"); QTC::TC("qpdf", "QPDFOutlineObjectHelper action dest");
dest = A.getKey("/D"); dest = A.getKey("/D");
} }
if (!dest.isInitialized()) { if (!dest) {
dest = QPDFObjectHandle::newNull(); return QPDFObjectHandle::newNull();
} }
if (dest.isName() || dest.isString()) { if (dest.isName() || dest.isString()) {

View File

@ -1381,7 +1381,7 @@ QPDFWriter::unparseObject(
} }
} }
if (extensions.isInitialized()) { if (extensions) {
std::set<std::string> keys = extensions.getKeys(); std::set<std::string> keys = extensions.getKeys();
if (keys.count("/ADBE") > 0) { if (keys.count("/ADBE") > 0) {
have_extensions_adbe = true; have_extensions_adbe = true;
@ -1412,7 +1412,7 @@ QPDFWriter::unparseObject(
} }
} }
if (extensions.isInitialized()) { if (extensions) {
QTC::TC("qpdf", "QPDFWriter preserve Extensions"); QTC::TC("qpdf", "QPDFWriter preserve Extensions");
QPDFObjectHandle adbe = extensions.getKey("/ADBE"); QPDFObjectHandle adbe = extensions.getKey("/ADBE");
if (adbe.isDictionary() && if (adbe.isDictionary() &&

View File

@ -661,18 +661,17 @@ void
QPDF_Stream::replaceFilterData( QPDF_Stream::replaceFilterData(
QPDFObjectHandle const& filter, QPDFObjectHandle const& decode_parms, size_t length) QPDFObjectHandle const& filter, QPDFObjectHandle const& decode_parms, size_t length)
{ {
if (filter.isInitialized()) { if (filter) {
this->stream_dict.replaceKey("/Filter", filter); stream_dict.replaceKey("/Filter", filter);
} }
if (decode_parms.isInitialized()) { if (decode_parms) {
this->stream_dict.replaceKey("/DecodeParms", decode_parms); stream_dict.replaceKey("/DecodeParms", decode_parms);
} }
if (length == 0) { if (length == 0) {
QTC::TC("qpdf", "QPDF_Stream unknown stream length"); QTC::TC("qpdf", "QPDF_Stream unknown stream length");
this->stream_dict.removeKey("/Length"); stream_dict.removeKey("/Length");
} else { } else {
this->stream_dict.replaceKey( stream_dict.replaceKey("/Length", QPDFObjectHandle::newInteger(QIntC::to_longlong(length)));
"/Length", QPDFObjectHandle::newInteger(QIntC::to_longlong(length)));
} }
} }

View File

@ -325,7 +325,7 @@ QPDF::JSONReactor::anyErrors() const
void void
QPDF::JSONReactor::containerStart() QPDF::JSONReactor::containerStart()
{ {
if (next_obj.isInitialized()) { if (next_obj) {
stack.emplace_back(next_state, std::move(next_obj)); stack.emplace_back(next_state, std::move(next_obj));
next_obj = QPDFObjectHandle(); next_obj = QPDFObjectHandle();
} else { } else {
@ -558,7 +558,7 @@ QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value)
throw std::logic_error("stack empty in st_object_top"); throw std::logic_error("stack empty in st_object_top");
} }
auto& tos = stack.back(); auto& tos = stack.back();
if (!tos.object.isInitialized()) { if (!tos.object) {
throw std::logic_error("current object uninitialized in st_object_top"); throw std::logic_error("current object uninitialized in st_object_top");
} }
if (key == "value") { if (key == "value") {
@ -767,7 +767,7 @@ QPDF::JSONReactor::makeObject(JSON const& value)
result = QPDFObjectHandle::newNull(); result = QPDFObjectHandle::newNull();
} }
} }
if (!result.isInitialized()) { if (!result) {
throw std::logic_error("JSONReactor::makeObject didn't initialize the object"); throw std::logic_error("JSONReactor::makeObject didn't initialize the object");
} }

View File

@ -955,7 +955,7 @@ qpdf_oh_is_initialized(qpdf_data qpdf, qpdf_oh oh)
{ {
QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_initialized"); QTC::TC("qpdf", "qpdf-c called qpdf_oh_is_initialized");
return do_with_oh<QPDF_BOOL>( return do_with_oh<QPDF_BOOL>(
qpdf, oh, return_false, [](QPDFObjectHandle& o) { return o.isInitialized(); }); qpdf, oh, return_false, [](QPDFObjectHandle& o) { return static_cast<bool>(o); });
} }
QPDF_BOOL QPDF_BOOL

View File

@ -1428,7 +1428,7 @@ test_42(QPDF& pdf, char const* arg2)
assert(i == ai.end()); assert(i == ai.end());
++i; ++i;
assert(i == ai.end()); assert(i == ai.end());
assert(!i_value.isInitialized()); assert(!i_value);
--i; --i;
assert(i_value.getName() == "/Item2"); assert(i_value.getName() == "/Item2");
assert(i->getName() == "/Item2"); assert(i->getName() == "/Item2");
@ -1444,7 +1444,7 @@ test_42(QPDF& pdf, char const* arg2)
++i; ++i;
++i; ++i;
assert(i == di.end()); assert(i == di.end());
assert(!i_value.second.isInitialized()); assert(!i_value.second);
} }
assert("" == qtest.getStringValue()); assert("" == qtest.getStringValue());
array.getArrayItem(-1).assertNull(); array.getArrayItem(-1).assertNull();
@ -1542,7 +1542,7 @@ test_42(QPDF& pdf, char const* arg2)
assert(m1.a == 0 && m1.b == 0 && m1.c == 0 && m1.d == 0 && m1.e == 0 && m1.f == 0); assert(m1.a == 0 && m1.b == 0 && m1.c == 0 && m1.d == 0 && m1.e == 0 && m1.f == 0);
// Uninitialized // Uninitialized
QPDFObjectHandle uninitialized; QPDFObjectHandle uninitialized;
assert(!uninitialized.isInitialized()); assert(!uninitialized);
assert(!uninitialized.isInteger()); assert(!uninitialized.isInteger());
assert(!uninitialized.isDictionary()); assert(!uninitialized.isDictionary());
assert(!uninitialized.isScalar()); assert(!uninitialized.isScalar());
@ -1692,7 +1692,7 @@ test_46(QPDF& pdf, char const* arg2)
assert(iter1_val.first == 2); assert(iter1_val.first == 2);
++iter1; ++iter1;
assert(iter1 == new1.end()); assert(iter1 == new1.end());
assert(!iter1_val.second.isInitialized()); assert(!iter1_val.second);
++iter1; ++iter1;
assert(iter1->first == 1); assert(iter1->first == 1);
--iter1; --iter1;
@ -1842,7 +1842,7 @@ test_48(QPDF& pdf, char const* arg2)
assert(iter1_val.first == "2"); assert(iter1_val.first == "2");
++iter1; ++iter1;
assert(iter1 == new1.end()); assert(iter1 == new1.end());
assert(!iter1_val.second.isInitialized()); assert(!iter1_val.second);
++iter1; ++iter1;
assert(iter1->first == "1"); assert(iter1->first == "1");
--iter1; --iter1;