Commit 9504421ecd6f8f57e42e44e29667578e303f1901

Authored by m-holger
1 parent dab919e5

Move QPDFObject::unparse to BaseHandle

include/qpdf/ObjectHandle.hh
@@ -61,6 +61,7 @@ namespace qpdf @@ -61,6 +61,7 @@ namespace qpdf
61 inline QPDF* qpdf() const; 61 inline QPDF* qpdf() const;
62 inline qpdf_object_type_e raw_type_code() const; 62 inline qpdf_object_type_e raw_type_code() const;
63 inline qpdf_object_type_e type_code() const; 63 inline qpdf_object_type_e type_code() const;
  64 + std::string unparse() const;
64 65
65 protected: 66 protected:
66 BaseHandle() = default; 67 BaseHandle() = default;
libqpdf/QPDFObjectHandle.cc
@@ -369,9 +369,9 @@ BaseHandle::copy(bool shallow) const @@ -369,9 +369,9 @@ BaseHandle::copy(bool shallow) const
369 } 369 }
370 370
371 std::string 371 std::string
372 -QPDFObject::unparse() 372 +BaseHandle::unparse() const
373 { 373 {
374 - switch (getResolvedTypeCode()) { 374 + switch (type_code()) {
375 case ::ot_uninitialized: 375 case ::ot_uninitialized:
376 throw std::logic_error("QPDFObjectHandle: attempting to unparse an uninitialized object"); 376 throw std::logic_error("QPDFObjectHandle: attempting to unparse an uninitialized object");
377 return ""; // does not return 377 return ""; // does not return
@@ -381,18 +381,18 @@ QPDFObject::unparse() @@ -381,18 +381,18 @@ QPDFObject::unparse()
381 case ::ot_null: 381 case ::ot_null:
382 return "null"; 382 return "null";
383 case ::ot_boolean: 383 case ::ot_boolean:
384 - return std::get<QPDF_Bool>(value).val ? "true" : "false"; 384 + return std::get<QPDF_Bool>(obj->value).val ? "true" : "false";
385 case ::ot_integer: 385 case ::ot_integer:
386 - return std::to_string(std::get<QPDF_Integer>(value).val); 386 + return std::to_string(std::get<QPDF_Integer>(obj->value).val);
387 case ::ot_real: 387 case ::ot_real:
388 - return std::get<QPDF_Real>(value).val; 388 + return std::get<QPDF_Real>(obj->value).val;
389 case ::ot_string: 389 case ::ot_string:
390 - return std::get<QPDF_String>(value).unparse(false); 390 + return std::get<QPDF_String>(obj->value).unparse(false);
391 case ::ot_name: 391 case ::ot_name:
392 - return Name::normalize(std::get<QPDF_Name>(value).name); 392 + return Name::normalize(std::get<QPDF_Name>(obj->value).name);
393 case ::ot_array: 393 case ::ot_array:
394 { 394 {
395 - auto const& a = std::get<QPDF_Array>(value); 395 + auto const& a = std::get<QPDF_Array>(obj->value);
396 std::string result = "[ "; 396 std::string result = "[ ";
397 if (a.sp) { 397 if (a.sp) {
398 int next = 0; 398 int next = 0;
@@ -401,9 +401,7 @@ QPDFObject::unparse() @@ -401,9 +401,7 @@ QPDFObject::unparse()
401 for (int j = next; j < key; ++j) { 401 for (int j = next; j < key; ++j) {
402 result += "null "; 402 result += "null ";
403 } 403 }
404 - auto item_og = item.second.id_gen();  
405 - result += item_og.isIndirect() ? item_og.unparse(' ') + " R "  
406 - : item.second.getObj()->unparse() + " "; 404 + result += item.second.unparse() + " ";
407 next = ++key; 405 next = ++key;
408 } 406 }
409 for (int j = next; j < a.sp->size; ++j) { 407 for (int j = next; j < a.sp->size; ++j) {
@@ -411,9 +409,7 @@ QPDFObject::unparse() @@ -411,9 +409,7 @@ QPDFObject::unparse()
411 } 409 }
412 } else { 410 } else {
413 for (auto const& item: a.elements) { 411 for (auto const& item: a.elements) {
414 - auto item_og = item.id_gen();  
415 - result += item_og.isIndirect() ? item_og.unparse(' ') + " R "  
416 - : item.getObj()->unparse() + " "; 412 + result += item.unparse() + " ";
417 } 413 }
418 } 414 }
419 result += "]"; 415 result += "]";
@@ -421,7 +417,7 @@ QPDFObject::unparse() @@ -421,7 +417,7 @@ QPDFObject::unparse()
421 } 417 }
422 case ::ot_dictionary: 418 case ::ot_dictionary:
423 { 419 {
424 - auto const& items = std::get<QPDF_Dictionary>(value).items; 420 + auto const& items = std::get<QPDF_Dictionary>(obj->value).items;
425 std::string result = "<< "; 421 std::string result = "<< ";
426 for (auto& iter: items) { 422 for (auto& iter: items) {
427 if (!iter.second.null()) { 423 if (!iter.second.null()) {
@@ -432,11 +428,11 @@ QPDFObject::unparse() @@ -432,11 +428,11 @@ QPDFObject::unparse()
432 return result; 428 return result;
433 } 429 }
434 case ::ot_stream: 430 case ::ot_stream:
435 - return og.unparse(' ') + " R"; 431 + return obj->og.unparse(' ') + " R";
436 case ::ot_operator: 432 case ::ot_operator:
437 - return std::get<QPDF_Operator>(value).val; 433 + return std::get<QPDF_Operator>(obj->value).val;
438 case ::ot_inlineimage: 434 case ::ot_inlineimage:
439 - return std::get<QPDF_InlineImage>(value).val; 435 + return std::get<QPDF_InlineImage>(obj->value).val;
440 case ::ot_unresolved: 436 case ::ot_unresolved:
441 throw std::logic_error("QPDFObjectHandle: attempting to unparse a unresolved object"); 437 throw std::logic_error("QPDFObjectHandle: attempting to unparse a unresolved object");
442 return ""; // does not return 438 return ""; // does not return
@@ -444,7 +440,7 @@ QPDFObject::unparse() @@ -444,7 +440,7 @@ QPDFObject::unparse()
444 throw std::logic_error("attempted to unparse a QPDFObjectHandle from a destroyed QPDF"); 440 throw std::logic_error("attempted to unparse a QPDFObjectHandle from a destroyed QPDF");
445 return ""; // does not return 441 return ""; // does not return
446 case ::ot_reference: 442 case ::ot_reference:
447 - return og.unparse(' ') + " R"; 443 + return obj->og.unparse(' ') + " R";
448 } 444 }
449 return {}; // unreachable 445 return {}; // unreachable
450 } 446 }
@@ -1438,7 +1434,7 @@ QPDFObjectHandle::unparseResolved() const @@ -1438,7 +1434,7 @@ QPDFObjectHandle::unparseResolved() const
1438 if (!obj) { 1434 if (!obj) {
1439 throw std::logic_error("attempted to dereference an uninitialized QPDFObjectHandle"); 1435 throw std::logic_error("attempted to dereference an uninitialized QPDFObjectHandle");
1440 } 1436 }
1441 - return obj->unparse(); 1437 + return BaseHandle::unparse();
1442 } 1438 }
1443 1439
1444 std::string 1440 std::string
libqpdf/qpdf/QPDFObject_private.hh
@@ -295,7 +295,6 @@ class QPDFObject @@ -295,7 +295,6 @@ class QPDFObject
295 qpdf, og, std::forward<T>(T(std::forward<Args>(args)...))); 295 qpdf, og, std::forward<T>(T(std::forward<Args>(args)...)));
296 } 296 }
297 297
298 - std::string unparse();  
299 void write_json(int json_version, JSON::Writer& p); 298 void write_json(int json_version, JSON::Writer& p);
300 void disconnect(); 299 void disconnect();
301 std::string getStringValue() const; 300 std::string getStringValue() const;
libtests/sparse_array.cc
@@ -95,7 +95,9 @@ main() @@ -95,7 +95,9 @@ main()
95 assert(b.at(3).second.isNull()); 95 assert(b.at(3).second.isNull());
96 assert(b.at(8).second.isNull()); 96 assert(b.at(8).second.isNull());
97 assert(b.at(5).second.isIndirect()); 97 assert(b.at(5).second.isIndirect());
98 - assert(obj->unparse() == "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]"); 98 + assert(
  99 + QPDFObjectHandle(obj).unparse() ==
  100 + "[ null null null null null 3 0 R null [ 0 1 2 3 ] null null ]");
99 auto c = QPDFObjectHandle(obj).unsafeShallowCopy(); 101 auto c = QPDFObjectHandle(obj).unsafeShallowCopy();
100 auto d = QPDFObjectHandle(obj).shallowCopy(); 102 auto d = QPDFObjectHandle(obj).shallowCopy();
101 b.at(7).second.setArrayItem(2, "42"_qpdf); 103 b.at(7).second.setArrayItem(2, "42"_qpdf);