Commit c6179da9615057a14e74180f640e2e77fdcbf234

Authored by m-holger
1 parent a1a8f35b

Add new method QPDFValue::checkOwnership

libqpdf/QPDF_Array.cc
@@ -7,6 +7,26 @@ @@ -7,6 +7,26 @@
7 7
8 static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); 8 static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
9 9
  10 +inline void
  11 +QPDF_Array::checkOwnership(QPDFObjectHandle const& item) const
  12 +{
  13 + if (auto obj = item.getObjectPtr()) {
  14 + if (qpdf) {
  15 + if (auto item_qpdf = obj->getQPDF()) {
  16 + if (qpdf != item_qpdf) {
  17 + throw std::logic_error(
  18 + "Attempting to add an object from a different QPDF. "
  19 + "Use QPDF::copyForeignObject to add objects from "
  20 + "another file.");
  21 + }
  22 + }
  23 + }
  24 + } else {
  25 + throw std::logic_error(
  26 + "Attempting to add an uninitialized object to a QPDF_Array.");
  27 + }
  28 +}
  29 +
10 QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) : 30 QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
11 QPDFValue(::ot_array, "array") 31 QPDFValue(::ot_array, "array")
12 { 32 {
libqpdf/qpdf/QPDF_Array.hh
@@ -42,6 +42,9 @@ class QPDF_Array: public QPDFValue @@ -42,6 +42,9 @@ class QPDF_Array: public QPDFValue
42 QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse); 42 QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse);
43 QPDF_Array(SparseOHArray const& items); 43 QPDF_Array(SparseOHArray const& items);
44 QPDF_Array(std::vector<std::shared_ptr<QPDFObject>> const& items); 44 QPDF_Array(std::vector<std::shared_ptr<QPDFObject>> const& items);
  45 +
  46 + void checkOwnership(QPDFObjectHandle const& item) const;
  47 +
45 bool sparse{false}; 48 bool sparse{false};
46 SparseOHArray sp_elements; 49 SparseOHArray sp_elements;
47 std::vector<std::shared_ptr<QPDFObject>> elements; 50 std::vector<std::shared_ptr<QPDFObject>> elements;