From 9504421ecd6f8f57e42e44e29667578e303f1901 Mon Sep 17 00:00:00 2001 From: m-holger Date: Tue, 4 Mar 2025 14:38:07 +0000 Subject: [PATCH] Move QPDFObject::unparse to BaseHandle --- include/qpdf/ObjectHandle.hh | 1 + libqpdf/QPDFObjectHandle.cc | 36 ++++++++++++++++-------------------- libqpdf/qpdf/QPDFObject_private.hh | 1 - libtests/sparse_array.cc | 4 +++- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/include/qpdf/ObjectHandle.hh b/include/qpdf/ObjectHandle.hh index 50649ff..e6f1a5c 100644 --- a/include/qpdf/ObjectHandle.hh +++ b/include/qpdf/ObjectHandle.hh @@ -61,6 +61,7 @@ namespace qpdf inline QPDF* qpdf() const; inline qpdf_object_type_e raw_type_code() const; inline qpdf_object_type_e type_code() const; + std::string unparse() const; protected: BaseHandle() = default; diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index d7ceec5..4e09b24 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -369,9 +369,9 @@ BaseHandle::copy(bool shallow) const } std::string -QPDFObject::unparse() +BaseHandle::unparse() const { - switch (getResolvedTypeCode()) { + switch (type_code()) { case ::ot_uninitialized: throw std::logic_error("QPDFObjectHandle: attempting to unparse an uninitialized object"); return ""; // does not return @@ -381,18 +381,18 @@ QPDFObject::unparse() case ::ot_null: return "null"; case ::ot_boolean: - return std::get(value).val ? "true" : "false"; + return std::get(obj->value).val ? "true" : "false"; case ::ot_integer: - return std::to_string(std::get(value).val); + return std::to_string(std::get(obj->value).val); case ::ot_real: - return std::get(value).val; + return std::get(obj->value).val; case ::ot_string: - return std::get(value).unparse(false); + return std::get(obj->value).unparse(false); case ::ot_name: - return Name::normalize(std::get(value).name); + return Name::normalize(std::get(obj->value).name); case ::ot_array: { - auto const& a = std::get(value); + auto const& a = std::get(obj->value); std::string result = "[ "; if (a.sp) { int next = 0; @@ -401,9 +401,7 @@ QPDFObject::unparse() for (int j = next; j < key; ++j) { result += "null "; } - auto item_og = item.second.id_gen(); - result += item_og.isIndirect() ? item_og.unparse(' ') + " R " - : item.second.getObj()->unparse() + " "; + result += item.second.unparse() + " "; next = ++key; } for (int j = next; j < a.sp->size; ++j) { @@ -411,9 +409,7 @@ QPDFObject::unparse() } } else { for (auto const& item: a.elements) { - auto item_og = item.id_gen(); - result += item_og.isIndirect() ? item_og.unparse(' ') + " R " - : item.getObj()->unparse() + " "; + result += item.unparse() + " "; } } result += "]"; @@ -421,7 +417,7 @@ QPDFObject::unparse() } case ::ot_dictionary: { - auto const& items = std::get(value).items; + auto const& items = std::get(obj->value).items; std::string result = "<< "; for (auto& iter: items) { if (!iter.second.null()) { @@ -432,11 +428,11 @@ QPDFObject::unparse() return result; } case ::ot_stream: - return og.unparse(' ') + " R"; + return obj->og.unparse(' ') + " R"; case ::ot_operator: - return std::get(value).val; + return std::get(obj->value).val; case ::ot_inlineimage: - return std::get(value).val; + return std::get(obj->value).val; case ::ot_unresolved: throw std::logic_error("QPDFObjectHandle: attempting to unparse a unresolved object"); return ""; // does not return @@ -444,7 +440,7 @@ QPDFObject::unparse() throw std::logic_error("attempted to unparse a QPDFObjectHandle from a destroyed QPDF"); return ""; // does not return case ::ot_reference: - return og.unparse(' ') + " R"; + return obj->og.unparse(' ') + " R"; } return {}; // unreachable } @@ -1438,7 +1434,7 @@ QPDFObjectHandle::unparseResolved() const if (!obj) { throw std::logic_error("attempted to dereference an uninitialized QPDFObjectHandle"); } - return obj->unparse(); + return BaseHandle::unparse(); } std::string diff --git a/libqpdf/qpdf/QPDFObject_private.hh b/libqpdf/qpdf/QPDFObject_private.hh index 91c5e36..18cf771 100644 --- a/libqpdf/qpdf/QPDFObject_private.hh +++ b/libqpdf/qpdf/QPDFObject_private.hh @@ -295,7 +295,6 @@ class QPDFObject qpdf, og, std::forward(T(std::forward(args)...))); } - std::string unparse(); void write_json(int json_version, JSON::Writer& p); void disconnect(); std::string getStringValue() const; diff --git a/libtests/sparse_array.cc b/libtests/sparse_array.cc index 1412ff0..7bbdc23 100644 --- a/libtests/sparse_array.cc +++ b/libtests/sparse_array.cc @@ -95,7 +95,9 @@ main() assert(b.at(3).second.isNull()); assert(b.at(8).second.isNull()); assert(b.at(5).second.isIndirect()); - assert(obj->unparse() == "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]"); + assert( + QPDFObjectHandle(obj).unparse() == + "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]"); auto c = QPDFObjectHandle(obj).unsafeShallowCopy(); auto d = QPDFObjectHandle(obj).shallowCopy(); b.at(7).second.setArrayItem(2, "42"_qpdf); -- libgit2 0.21.4