Commit f0a8178091dfc87bbf9a6751f8fedf007e8eb144

Authored by m-holger
Committed by Jay Berkenbilt
1 parent 5aa8225f

Refactor QPDFObject creation and cloning

Move responsibility for creating shared pointers to objects and cloning from QPDFObjectHandle to QPDFObject.
include/qpdf/QPDFObject.hh
... ... @@ -63,6 +63,7 @@ class QPDFObject
63 63 static constexpr object_type_e ot_inlineimage = ::ot_inlineimage;
64 64  
65 65 virtual ~QPDFObject() = default;
  66 + virtual std::shared_ptr<QPDFObject> shallowCopy() = 0;
66 67 virtual std::string unparse() = 0;
67 68 virtual JSON getJSON(int json_version) = 0;
68 69  
... ... @@ -102,6 +103,7 @@ class QPDFObject
102 103 releaseResolved()
103 104 {
104 105 }
  106 + static std::shared_ptr<QPDFObject> do_create(QPDFObject*);
105 107  
106 108 private:
107 109 QPDFObject(QPDFObject const&) = delete;
... ...
include/qpdf/QPDFObjectHandle.hh
... ... @@ -1551,7 +1551,7 @@ class QPDFObjectHandle
1551 1551  
1552 1552 private:
1553 1553 QPDFObjectHandle(QPDF*, int objid, int generation);
1554   - QPDFObjectHandle(QPDFObject*);
  1554 + QPDFObjectHandle(std::shared_ptr<QPDFObject> const&);
1555 1555  
1556 1556 enum parser_state_e {
1557 1557 st_top,
... ...
libqpdf/QPDF.cc
... ... @@ -1989,7 +1989,7 @@ QPDF::resolve(int objid, int generation)
1989 1989 this->m->file->getLastOffset(),
1990 1990 ("loop detected resolving object " + QUtil::int_to_string(objid) +
1991 1991 " " + QUtil::int_to_string(generation)));
1992   - return std::shared_ptr<QPDFObject>(new QPDF_Null);
  1992 + return QPDF_Null::create();
1993 1993 }
1994 1994 ResolveRecorder rr(this, og);
1995 1995  
... ...
libqpdf/QPDFObject.cc
... ... @@ -6,6 +6,13 @@ QPDFObject::QPDFObject() :
6 6 {
7 7 }
8 8  
  9 +std::shared_ptr<QPDFObject>
  10 +QPDFObject::do_create(QPDFObject* object)
  11 +{
  12 + std::shared_ptr<QPDFObject> obj(object);
  13 + return obj;
  14 +}
  15 +
9 16 void
10 17 QPDFObject::setDescription(QPDF* qpdf, std::string const& description)
11 18 {
... ...
libqpdf/QPDFObjectHandle.cc
... ... @@ -235,7 +235,7 @@ QPDFObjectHandle::QPDFObjectHandle(QPDF* qpdf, int objid, int generation) :
235 235 {
236 236 }
237 237  
238   -QPDFObjectHandle::QPDFObjectHandle(QPDFObject* data) :
  238 +QPDFObjectHandle::QPDFObjectHandle(std::shared_ptr<QPDFObject> const& data) :
239 239 initialized(true),
240 240 qpdf(0),
241 241 objid(0),
... ... @@ -2335,7 +2335,7 @@ QPDFObjectHandle::parseInternal(
2335 2335 if (old_state == st_array) {
2336 2336 // There's no newArray(SparseOHArray) since
2337 2337 // SparseOHArray is not part of the public API.
2338   - object = QPDFObjectHandle(new QPDF_Array(olist));
  2338 + object = QPDFObjectHandle(QPDF_Array::create(olist));
2339 2339 setObjectDescriptionFromInput(
2340 2340 object, context, object_description, input, offset);
2341 2341 // The `offset` points to the next of "[". Set the
... ... @@ -2499,25 +2499,25 @@ QPDFObjectHandle::newIndirect(QPDF* qpdf, int objid, int generation)
2499 2499 QPDFObjectHandle
2500 2500 QPDFObjectHandle::newBool(bool value)
2501 2501 {
2502   - return QPDFObjectHandle(new QPDF_Bool(value));
  2502 + return QPDFObjectHandle(QPDF_Bool::create(value));
2503 2503 }
2504 2504  
2505 2505 QPDFObjectHandle
2506 2506 QPDFObjectHandle::newNull()
2507 2507 {
2508   - return QPDFObjectHandle(new QPDF_Null());
  2508 + return QPDFObjectHandle(QPDF_Null::create());
2509 2509 }
2510 2510  
2511 2511 QPDFObjectHandle
2512 2512 QPDFObjectHandle::newInteger(long long value)
2513 2513 {
2514   - return QPDFObjectHandle(new QPDF_Integer(value));
  2514 + return QPDFObjectHandle(QPDF_Integer::create(value));
2515 2515 }
2516 2516  
2517 2517 QPDFObjectHandle
2518 2518 QPDFObjectHandle::newReal(std::string const& value)
2519 2519 {
2520   - return QPDFObjectHandle(new QPDF_Real(value));
  2520 + return QPDFObjectHandle(QPDF_Real::create(value));
2521 2521 }
2522 2522  
2523 2523 QPDFObjectHandle
... ... @@ -2525,37 +2525,37 @@ QPDFObjectHandle::newReal(
2525 2525 double value, int decimal_places, bool trim_trailing_zeroes)
2526 2526 {
2527 2527 return QPDFObjectHandle(
2528   - new QPDF_Real(value, decimal_places, trim_trailing_zeroes));
  2528 + QPDF_Real::create(value, decimal_places, trim_trailing_zeroes));
2529 2529 }
2530 2530  
2531 2531 QPDFObjectHandle
2532 2532 QPDFObjectHandle::newName(std::string const& name)
2533 2533 {
2534   - return QPDFObjectHandle(new QPDF_Name(name));
  2534 + return QPDFObjectHandle(QPDF_Name::create(name));
2535 2535 }
2536 2536  
2537 2537 QPDFObjectHandle
2538 2538 QPDFObjectHandle::newString(std::string const& str)
2539 2539 {
2540   - return QPDFObjectHandle(new QPDF_String(str));
  2540 + return QPDFObjectHandle(QPDF_String::create(str));
2541 2541 }
2542 2542  
2543 2543 QPDFObjectHandle
2544 2544 QPDFObjectHandle::newUnicodeString(std::string const& utf8_str)
2545 2545 {
2546   - return QPDFObjectHandle(QPDF_String::new_utf16(utf8_str));
  2546 + return QPDFObjectHandle(QPDF_String::create_utf16(utf8_str));
2547 2547 }
2548 2548  
2549 2549 QPDFObjectHandle
2550 2550 QPDFObjectHandle::newOperator(std::string const& value)
2551 2551 {
2552   - return QPDFObjectHandle(new QPDF_Operator(value));
  2552 + return QPDFObjectHandle(QPDF_Operator::create(value));
2553 2553 }
2554 2554  
2555 2555 QPDFObjectHandle
2556 2556 QPDFObjectHandle::newInlineImage(std::string const& value)
2557 2557 {
2558   - return QPDFObjectHandle(new QPDF_InlineImage(value));
  2558 + return QPDFObjectHandle(QPDF_InlineImage::create(value));
2559 2559 }
2560 2560  
2561 2561 QPDFObjectHandle
... ... @@ -2567,7 +2567,7 @@ QPDFObjectHandle::newArray()
2567 2567 QPDFObjectHandle
2568 2568 QPDFObjectHandle::newArray(std::vector<QPDFObjectHandle> const& items)
2569 2569 {
2570   - return QPDFObjectHandle(new QPDF_Array(items));
  2570 + return QPDFObjectHandle(QPDF_Array::create(items));
2571 2571 }
2572 2572  
2573 2573 QPDFObjectHandle
... ... @@ -2635,7 +2635,7 @@ QPDFObjectHandle
2635 2635 QPDFObjectHandle::newDictionary(
2636 2636 std::map<std::string, QPDFObjectHandle> const& items)
2637 2637 {
2638   - return QPDFObjectHandle(new QPDF_Dictionary(items));
  2638 + return QPDFObjectHandle(QPDF_Dictionary::create(items));
2639 2639 }
2640 2640  
2641 2641 QPDFObjectHandle
... ... @@ -2647,8 +2647,8 @@ QPDFObjectHandle::newStream(
2647 2647 qpdf_offset_t offset,
2648 2648 size_t length)
2649 2649 {
2650   - QPDFObjectHandle result = QPDFObjectHandle(
2651   - new QPDF_Stream(qpdf, objid, generation, stream_dict, offset, length));
  2650 + QPDFObjectHandle result = QPDFObjectHandle(QPDF_Stream::create(
  2651 + qpdf, objid, generation, stream_dict, offset, length));
2652 2652 if (offset) {
2653 2653 result.setParsedOffset(offset);
2654 2654 }
... ... @@ -2665,8 +2665,7 @@ QPDFObjectHandle::newStream(QPDF* qpdf)
2665 2665 QTC::TC("qpdf", "QPDFObjectHandle newStream");
2666 2666 QPDFObjectHandle stream_dict = newDictionary();
2667 2667 QPDFObjectHandle result = qpdf->makeIndirectObject(
2668   - QPDFObjectHandle(new QPDF_Stream(qpdf, 0, 0, stream_dict, 0, 0)));
2669   - // Indirect objects are guaranteed to be initialized
  2668 + QPDFObjectHandle(QPDF_Stream::create(qpdf, 0, 0, stream_dict, 0, 0)));
2670 2669 result.dereference();
2671 2670 QPDF_Stream* stream = dynamic_cast<QPDF_Stream*>(result.obj.get());
2672 2671 stream->setObjGen(result.getObjectID(), result.getGeneration());
... ... @@ -2706,7 +2705,7 @@ QPDFObjectHandle::newReserved(QPDF* qpdf)
2706 2705 QPDFObjectHandle
2707 2706 QPDFObjectHandle::makeReserved()
2708 2707 {
2709   - return QPDFObjectHandle(new QPDF_Reserved());
  2708 + return QPDFObjectHandle(QPDF_Reserved::create());
2710 2709 }
2711 2710  
2712 2711 void
... ... @@ -2753,17 +2752,9 @@ QPDFObjectHandle::shallowCopyInternal(
2753 2752 throw std::runtime_error("attempt to make a shallow copy of a stream");
2754 2753 }
2755 2754  
2756   - if (isArray()) {
2757   - QTC::TC("qpdf", "QPDFObjectHandle shallow copy array");
2758   - // No newArray for shallow copying the sparse array
2759   - QPDF_Array* arr = dynamic_cast<QPDF_Array*>(obj.get());
2760   - new_obj =
2761   - QPDFObjectHandle(new QPDF_Array(arr->getElementsForShallowCopy()));
2762   - } else if (isDictionary()) {
2763   - QTC::TC("qpdf", "QPDFObjectHandle shallow copy dictionary");
2764   - new_obj = newDictionary(getDictAsMap());
  2755 + if (isArray() || isDictionary()) {
  2756 + new_obj = QPDFObjectHandle(obj->shallowCopy());
2765 2757 } else {
2766   - QTC::TC("qpdf", "QPDFObjectHandle shallow copy scalar");
2767 2758 new_obj = *this;
2768 2759 }
2769 2760  
... ... @@ -2812,27 +2803,14 @@ QPDFObjectHandle::copyObject(
2812 2803  
2813 2804 std::shared_ptr<QPDFObject> new_obj;
2814 2805  
2815   - if (isBool()) {
2816   - QTC::TC("qpdf", "QPDFObjectHandle clone bool");
2817   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Bool(getBoolValue()));
2818   - } else if (isNull()) {
2819   - QTC::TC("qpdf", "QPDFObjectHandle clone null");
2820   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Null());
2821   - } else if (isInteger()) {
2822   - QTC::TC("qpdf", "QPDFObjectHandle clone integer");
2823   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Integer(getIntValue()));
2824   - } else if (isReal()) {
2825   - QTC::TC("qpdf", "QPDFObjectHandle clone real");
2826   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Real(getRealValue()));
2827   - } else if (isName()) {
2828   - QTC::TC("qpdf", "QPDFObjectHandle clone name");
2829   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Name(getName()));
2830   - } else if (isString()) {
2831   - QTC::TC("qpdf", "QPDFObjectHandle clone string");
2832   - new_obj =
2833   - std::shared_ptr<QPDFObject>(new QPDF_String(getStringValue()));
  2806 + if (isBool() ||
  2807 + isInteger() ||
  2808 + isName() ||
  2809 + isNull() ||
  2810 + isReal() ||
  2811 + isString()) {
  2812 + new_obj = obj->shallowCopy();
2834 2813 } else if (isArray()) {
2835   - QTC::TC("qpdf", "QPDFObjectHandle clone array");
2836 2814 std::vector<QPDFObjectHandle> items;
2837 2815 int n = getArrayNItems();
2838 2816 for (int i = 0; i < n; ++i) {
... ... @@ -2843,9 +2821,8 @@ QPDFObjectHandle::copyObject(
2843 2821 visited, cross_indirect, first_level_only, stop_at_streams);
2844 2822 }
2845 2823 }
2846   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Array(items));
  2824 + new_obj = QPDF_Array::create(items);
2847 2825 } else if (isDictionary()) {
2848   - QTC::TC("qpdf", "QPDFObjectHandle clone dictionary");
2849 2826 std::map<std::string, QPDFObjectHandle> items;
2850 2827 for (auto const& key: getKeys()) {
2851 2828 items[key] = getKey(key);
... ... @@ -2855,7 +2832,7 @@ QPDFObjectHandle::copyObject(
2855 2832 visited, cross_indirect, first_level_only, stop_at_streams);
2856 2833 }
2857 2834 }
2858   - new_obj = std::shared_ptr<QPDFObject>(new QPDF_Dictionary(items));
  2835 + new_obj = QPDF_Dictionary::create(items);
2859 2836 } else {
2860 2837 throw std::logic_error("QPDFObjectHandle::makeDirectInternal: "
2861 2838 "unknown object type");
... ... @@ -3144,7 +3121,7 @@ QPDFObjectHandle::dereference()
3144 3121 if (obj.get() == 0) {
3145 3122 // QPDF::resolve never returns an uninitialized object, but
3146 3123 // check just in case.
3147   - this->obj = std::shared_ptr<QPDFObject>(new QPDF_Null());
  3124 + this->obj = QPDF_Null::create();
3148 3125 } else if (dynamic_cast<QPDF_Reserved*>(obj.get())) {
3149 3126 // Do not resolve
3150 3127 this->reserved = true;
... ...
libqpdf/QPDF_Array.cc
... ... @@ -14,6 +14,24 @@ QPDF_Array::QPDF_Array(SparseOHArray const&amp; items) :
14 14 {
15 15 }
16 16  
  17 +std::shared_ptr<QPDFObject>
  18 +QPDF_Array::create(std::vector<QPDFObjectHandle> const& items)
  19 +{
  20 + return do_create(new QPDF_Array(items));
  21 +}
  22 +
  23 +std::shared_ptr<QPDFObject>
  24 +QPDF_Array::create(SparseOHArray const& items)
  25 +{
  26 + return do_create(new QPDF_Array(items));
  27 +}
  28 +
  29 +std::shared_ptr<QPDFObject>
  30 +QPDF_Array::shallowCopy()
  31 +{
  32 + return create(elements);
  33 +}
  34 +
17 35 void
18 36 QPDF_Array::releaseResolved()
19 37 {
... ... @@ -121,12 +139,6 @@ QPDF_Array::eraseItem(int at)
121 139 this->elements.erase(QIntC::to_size(at));
122 140 }
123 141  
124   -SparseOHArray const&
125   -QPDF_Array::getElementsForShallowCopy() const
126   -{
127   - return this->elements;
128   -}
129   -
130 142 void
131 143 QPDF_Array::addExplicitElementsToList(std::list<QPDFObjectHandle>& l) const
132 144 {
... ...
libqpdf/QPDF_Bool.cc
... ... @@ -5,6 +5,18 @@ QPDF_Bool::QPDF_Bool(bool val) :
5 5 {
6 6 }
7 7  
  8 +std::shared_ptr<QPDFObject>
  9 +QPDF_Bool::create(bool value)
  10 +{
  11 + return do_create(new QPDF_Bool(value));
  12 +}
  13 +
  14 +std::shared_ptr<QPDFObject>
  15 +QPDF_Bool::shallowCopy()
  16 +{
  17 + return create(val);
  18 +}
  19 +
8 20 std::string
9 21 QPDF_Bool::unparse()
10 22 {
... ...
libqpdf/QPDF_Dictionary.cc
... ... @@ -9,6 +9,18 @@ QPDF_Dictionary::QPDF_Dictionary(
9 9 {
10 10 }
11 11  
  12 +std::shared_ptr<QPDFObject>
  13 +QPDF_Dictionary::create(std::map<std::string, QPDFObjectHandle> const& items)
  14 +{
  15 + return do_create(new QPDF_Dictionary(items));
  16 +}
  17 +
  18 +std::shared_ptr<QPDFObject>
  19 +QPDF_Dictionary::shallowCopy()
  20 +{
  21 + return create(items);
  22 +}
  23 +
12 24 void
13 25 QPDF_Dictionary::releaseResolved()
14 26 {
... ...
libqpdf/QPDF_InlineImage.cc
1 1 #include <qpdf/QPDF_InlineImage.hh>
2 2  
3   -#include <qpdf/QUtil.hh>
4   -
5 3 QPDF_InlineImage::QPDF_InlineImage(std::string const& val) :
6 4 val(val)
7 5 {
8 6 }
9 7  
  8 +std::shared_ptr<QPDFObject>
  9 +QPDF_InlineImage::create(std::string const& val)
  10 +{
  11 + return do_create(new QPDF_InlineImage(val));
  12 +}
  13 +
  14 +std::shared_ptr<QPDFObject>
  15 +QPDF_InlineImage::shallowCopy()
  16 +{
  17 + return create(val);
  18 +}
  19 +
10 20 std::string
11 21 QPDF_InlineImage::unparse()
12 22 {
... ...
libqpdf/QPDF_Integer.cc
... ... @@ -7,6 +7,18 @@ QPDF_Integer::QPDF_Integer(long long val) :
7 7 {
8 8 }
9 9  
  10 +std::shared_ptr<QPDFObject>
  11 +QPDF_Integer::create(long long value)
  12 +{
  13 + return do_create(new QPDF_Integer(value));
  14 +}
  15 +
  16 +std::shared_ptr<QPDFObject>
  17 +QPDF_Integer::shallowCopy()
  18 +{
  19 + return create(val);
  20 +}
  21 +
10 22 std::string
11 23 QPDF_Integer::unparse()
12 24 {
... ...
libqpdf/QPDF_Name.cc
... ... @@ -9,6 +9,18 @@ QPDF_Name::QPDF_Name(std::string const&amp; name) :
9 9 {
10 10 }
11 11  
  12 +std::shared_ptr<QPDFObject>
  13 +QPDF_Name::create(std::string const& name)
  14 +{
  15 + return do_create(new QPDF_Name(name));
  16 +}
  17 +
  18 +std::shared_ptr<QPDFObject>
  19 +QPDF_Name::shallowCopy()
  20 +{
  21 + return create(name);
  22 +}
  23 +
12 24 std::string
13 25 QPDF_Name::normalizeName(std::string const& name)
14 26 {
... ...
libqpdf/QPDF_Null.cc
1 1 #include <qpdf/QPDF_Null.hh>
2 2  
  3 +std::shared_ptr<QPDFObject>
  4 +QPDF_Null::create()
  5 +{
  6 + return do_create(new QPDF_Null());
  7 +}
  8 +
  9 +std::shared_ptr<QPDFObject>
  10 +QPDF_Null::shallowCopy()
  11 +{
  12 + return create();
  13 +}
  14 +
3 15 std::string
4 16 QPDF_Null::unparse()
5 17 {
... ...
libqpdf/QPDF_Operator.cc
1 1 #include <qpdf/QPDF_Operator.hh>
2 2  
3   -#include <qpdf/QUtil.hh>
4   -
5 3 QPDF_Operator::QPDF_Operator(std::string const& val) :
6 4 val(val)
7 5 {
8 6 }
9 7  
  8 +std::shared_ptr<QPDFObject>
  9 +QPDF_Operator::create(std::string const& val)
  10 +{
  11 + return do_create(new QPDF_Operator(val));
  12 +}
  13 +
  14 +std::shared_ptr<QPDFObject>
  15 +QPDF_Operator::shallowCopy()
  16 +{
  17 + return create(val);
  18 +}
  19 +
10 20 std::string
11 21 QPDF_Operator::unparse()
12 22 {
... ...
libqpdf/QPDF_Real.cc
... ... @@ -13,6 +13,25 @@ QPDF_Real::QPDF_Real(
13 13 {
14 14 }
15 15  
  16 +std::shared_ptr<QPDFObject>
  17 +QPDF_Real::create(std::string const& val)
  18 +{
  19 + return do_create(new QPDF_Real(val));
  20 +}
  21 +
  22 +std::shared_ptr<QPDFObject>
  23 +QPDF_Real::create(double value, int decimal_places, bool trim_trailing_zeroes)
  24 +{
  25 + return do_create(
  26 + new QPDF_Real(value, decimal_places, trim_trailing_zeroes));
  27 +}
  28 +
  29 +std::shared_ptr<QPDFObject>
  30 +QPDF_Real::shallowCopy()
  31 +{
  32 + return create(val);
  33 +}
  34 +
16 35 std::string
17 36 QPDF_Real::unparse()
18 37 {
... ...
libqpdf/QPDF_Reserved.cc
... ... @@ -2,6 +2,18 @@
2 2  
3 3 #include <stdexcept>
4 4  
  5 +std::shared_ptr<QPDFObject>
  6 +QPDF_Reserved::create()
  7 +{
  8 + return do_create(new QPDF_Reserved());
  9 +}
  10 +
  11 +std::shared_ptr<QPDFObject>
  12 +QPDF_Reserved::shallowCopy()
  13 +{
  14 + return create();
  15 +}
  16 +
5 17 std::string
6 18 QPDF_Reserved::unparse()
7 19 {
... ...
libqpdf/QPDF_Stream.cc
... ... @@ -134,6 +134,25 @@ QPDF_Stream::QPDF_Stream(
134 134 QUtil::int_to_string(this->generation));
135 135 }
136 136  
  137 +std::shared_ptr<QPDFObject>
  138 +QPDF_Stream::create(
  139 + QPDF* qpdf,
  140 + int objid,
  141 + int generation,
  142 + QPDFObjectHandle stream_dict,
  143 + qpdf_offset_t offset,
  144 + size_t length)
  145 +{
  146 + return do_create(
  147 + new QPDF_Stream(qpdf, objid, generation, stream_dict, offset,length));
  148 +}
  149 +
  150 +std::shared_ptr<QPDFObject>
  151 +QPDF_Stream::shallowCopy()
  152 +{
  153 + throw std::logic_error("stream objects cannot be cloned");
  154 +}
  155 +
137 156 void
138 157 QPDF_Stream::registerStreamFilter(
139 158 std::string const& filter_name,
... ...
libqpdf/QPDF_String.cc
1 1 #include <qpdf/QPDF_String.hh>
2 2  
3   -#include <qpdf/QTC.hh>
4 3 #include <qpdf/QUtil.hh>
5 4  
6 5 // DO NOT USE ctype -- it is locale dependent for some things, and
... ... @@ -26,14 +25,26 @@ QPDF_String::QPDF_String(std::string const&amp; val) :
26 25 {
27 26 }
28 27  
29   -QPDF_String*
30   -QPDF_String::new_utf16(std::string const& utf8_val)
  28 +std::shared_ptr<QPDFObject>
  29 +QPDF_String::create(std::string const& val)
  30 +{
  31 + return do_create(new QPDF_String(val));
  32 +}
  33 +
  34 +std::shared_ptr<QPDFObject>
  35 +QPDF_String::create_utf16(std::string const& utf8_val)
31 36 {
32 37 std::string result;
33 38 if (!QUtil::utf8_to_pdf_doc(utf8_val, result, '?')) {
34 39 result = QUtil::utf8_to_utf16(utf8_val);
35 40 }
36   - return new QPDF_String(result);
  41 + return do_create(new QPDF_String(result));
  42 +}
  43 +
  44 +std::shared_ptr<QPDFObject>
  45 +QPDF_String::shallowCopy()
  46 +{
  47 + return create(val);
37 48 }
38 49  
39 50 std::string
... ...
libqpdf/qpdf/QPDF_Array.hh
... ... @@ -10,9 +10,10 @@
10 10 class QPDF_Array: public QPDFObject
11 11 {
12 12 public:
13   - QPDF_Array(std::vector<QPDFObjectHandle> const& items);
14   - QPDF_Array(SparseOHArray const& items);
15 13 virtual ~QPDF_Array() = default;
  14 + static std::shared_ptr<QPDFObject> create(std::vector<QPDFObjectHandle> const& items);
  15 + static std::shared_ptr<QPDFObject> create(SparseOHArray const& items);
  16 + virtual std::shared_ptr<QPDFObject> shallowCopy();
16 17 virtual std::string unparse();
17 18 virtual JSON getJSON(int json_version);
18 19 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -31,13 +32,14 @@ class QPDF_Array: public QPDFObject
31 32 // Helper methods for QPDF and QPDFObjectHandle -- these are
32 33 // public methods since the whole class is not part of the public
33 34 // API. Otherwise, these would be wrapped in accessor classes.
34   - SparseOHArray const& getElementsForShallowCopy() const;
35 35 void addExplicitElementsToList(std::list<QPDFObjectHandle>&) const;
36 36  
37 37 protected:
38 38 virtual void releaseResolved();
39 39  
40 40 private:
  41 + QPDF_Array(std::vector<QPDFObjectHandle> const& items);
  42 + QPDF_Array(SparseOHArray const& items);
41 43 SparseOHArray elements;
42 44 };
43 45  
... ...
libqpdf/qpdf/QPDF_Bool.hh
... ... @@ -6,8 +6,9 @@
6 6 class QPDF_Bool: public QPDFObject
7 7 {
8 8 public:
9   - QPDF_Bool(bool val);
10 9 virtual ~QPDF_Bool() = default;
  10 + static std::shared_ptr<QPDFObject> create(bool val);
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
11 12 virtual std::string unparse();
12 13 virtual JSON getJSON(int json_version);
13 14 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -15,6 +16,7 @@ class QPDF_Bool: public QPDFObject
15 16 bool getVal() const;
16 17  
17 18 private:
  19 + QPDF_Bool(bool val);
18 20 bool val;
19 21 };
20 22  
... ...
libqpdf/qpdf/QPDF_Dictionary.hh
... ... @@ -11,8 +11,9 @@
11 11 class QPDF_Dictionary: public QPDFObject
12 12 {
13 13 public:
14   - QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items);
15 14 virtual ~QPDF_Dictionary() = default;
  15 + static std::shared_ptr<QPDFObject> create(std::map<std::string, QPDFObjectHandle> const& items);
  16 + virtual std::shared_ptr<QPDFObject> shallowCopy();
16 17 virtual std::string unparse();
17 18 virtual JSON getJSON(int json_version);
18 19 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -36,6 +37,7 @@ class QPDF_Dictionary: public QPDFObject
36 37 virtual void releaseResolved();
37 38  
38 39 private:
  40 + QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items);
39 41 std::map<std::string, QPDFObjectHandle> items;
40 42 };
41 43  
... ...
libqpdf/qpdf/QPDF_InlineImage.hh
... ... @@ -6,8 +6,9 @@
6 6 class QPDF_InlineImage: public QPDFObject
7 7 {
8 8 public:
9   - QPDF_InlineImage(std::string const& val);
10 9 virtual ~QPDF_InlineImage() = default;
  10 + static std::shared_ptr<QPDFObject> create(std::string const& val);
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
11 12 virtual std::string unparse();
12 13 virtual JSON getJSON(int json_version);
13 14 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -15,6 +16,7 @@ class QPDF_InlineImage: public QPDFObject
15 16 std::string getVal() const;
16 17  
17 18 private:
  19 + QPDF_InlineImage(std::string const& val);
18 20 std::string val;
19 21 };
20 22  
... ...
libqpdf/qpdf/QPDF_Integer.hh
... ... @@ -6,8 +6,9 @@
6 6 class QPDF_Integer: public QPDFObject
7 7 {
8 8 public:
9   - QPDF_Integer(long long val);
10 9 virtual ~QPDF_Integer() = default;
  10 + static std::shared_ptr<QPDFObject> create(long long value);
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
11 12 virtual std::string unparse();
12 13 virtual JSON getJSON(int json_version);
13 14 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -15,6 +16,7 @@ class QPDF_Integer: public QPDFObject
15 16 long long getVal() const;
16 17  
17 18 private:
  19 + QPDF_Integer(long long val);
18 20 long long val;
19 21 };
20 22  
... ...
libqpdf/qpdf/QPDF_Name.hh
... ... @@ -6,8 +6,9 @@
6 6 class QPDF_Name: public QPDFObject
7 7 {
8 8 public:
9   - QPDF_Name(std::string const& name);
10 9 virtual ~QPDF_Name() = default;
  10 + static std::shared_ptr<QPDFObject> create(std::string const& name);
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
11 12 virtual std::string unparse();
12 13 virtual JSON getJSON(int json_version);
13 14 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -18,6 +19,7 @@ class QPDF_Name: public QPDFObject
18 19 static std::string normalizeName(std::string const& name);
19 20  
20 21 private:
  22 + QPDF_Name(std::string const& name);
21 23 std::string name;
22 24 };
23 25  
... ...
libqpdf/qpdf/QPDF_Null.hh
... ... @@ -7,10 +7,14 @@ class QPDF_Null: public QPDFObject
7 7 {
8 8 public:
9 9 virtual ~QPDF_Null() = default;
  10 + static std::shared_ptr<QPDFObject> create();
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
10 12 virtual std::string unparse();
11 13 virtual JSON getJSON(int json_version);
12 14 virtual QPDFObject::object_type_e getTypeCode() const;
13 15 virtual char const* getTypeName() const;
  16 + private:
  17 + QPDF_Null() = default;
14 18 };
15 19  
16 20 #endif // QPDF_NULL_HH
... ...
libqpdf/qpdf/QPDF_Operator.hh
... ... @@ -6,8 +6,9 @@
6 6 class QPDF_Operator: public QPDFObject
7 7 {
8 8 public:
9   - QPDF_Operator(std::string const& val);
10 9 virtual ~QPDF_Operator() = default;
  10 + static std::shared_ptr<QPDFObject> create(std::string const& val);
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
11 12 virtual std::string unparse();
12 13 virtual JSON getJSON(int json_version);
13 14 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -15,6 +16,7 @@ class QPDF_Operator: public QPDFObject
15 16 std::string getVal() const;
16 17  
17 18 private:
  19 + QPDF_Operator(std::string const& val);
18 20 std::string val;
19 21 };
20 22  
... ...
libqpdf/qpdf/QPDF_Real.hh
... ... @@ -6,9 +6,11 @@
6 6 class QPDF_Real: public QPDFObject
7 7 {
8 8 public:
9   - QPDF_Real(std::string const& val);
10   - QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes);
11 9 virtual ~QPDF_Real() = default;
  10 + static std::shared_ptr<QPDFObject> create(std::string const& val);
  11 + static std::shared_ptr<QPDFObject> create(
  12 + double value, int decimal_places, bool trim_trailing_zeroes);
  13 + virtual std::shared_ptr<QPDFObject> shallowCopy();
12 14 virtual std::string unparse();
13 15 virtual JSON getJSON(int json_version);
14 16 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -16,6 +18,8 @@ class QPDF_Real: public QPDFObject
16 18 std::string getVal();
17 19  
18 20 private:
  21 + QPDF_Real(std::string const& val);
  22 + QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes);
19 23 // Store reals as strings to avoid roundoff errors.
20 24 std::string val;
21 25 };
... ...
libqpdf/qpdf/QPDF_Reserved.hh
... ... @@ -7,10 +7,14 @@ class QPDF_Reserved: public QPDFObject
7 7 {
8 8 public:
9 9 virtual ~QPDF_Reserved() = default;
  10 + static std::shared_ptr<QPDFObject> create();
  11 + virtual std::shared_ptr<QPDFObject> shallowCopy();
10 12 virtual std::string unparse();
11 13 virtual JSON getJSON(int json_version);
12 14 virtual QPDFObject::object_type_e getTypeCode() const;
13 15 virtual char const* getTypeName() const;
  16 + private:
  17 + QPDF_Reserved() = default;
14 18 };
15 19  
16 20 #endif // QPDF_RESERVED_HH
... ...
libqpdf/qpdf/QPDF_Stream.hh
... ... @@ -16,14 +16,15 @@ class QPDF;
16 16 class QPDF_Stream: public QPDFObject
17 17 {
18 18 public:
19   - QPDF_Stream(
  19 + virtual ~QPDF_Stream() = default;
  20 + static std::shared_ptr<QPDFObject> create(
20 21 QPDF*,
21 22 int objid,
22 23 int generation,
23 24 QPDFObjectHandle stream_dict,
24 25 qpdf_offset_t offset,
25 26 size_t length);
26   - virtual ~QPDF_Stream() = default;
  27 + virtual std::shared_ptr<QPDFObject> shallowCopy();
27 28 virtual std::string unparse();
28 29 virtual JSON getJSON(int json_version);
29 30 virtual QPDFObject::object_type_e getTypeCode() const;
... ... @@ -83,6 +84,13 @@ class QPDF_Stream: public QPDFObject
83 84 virtual void releaseResolved();
84 85  
85 86 private:
  87 + QPDF_Stream(
  88 + QPDF*,
  89 + int objid,
  90 + int generation,
  91 + QPDFObjectHandle stream_dict,
  92 + qpdf_offset_t offset,
  93 + size_t length);
86 94 static std::map<std::string, std::string> filter_abbreviations;
87 95 static std::
88 96 map<std::string, std::function<std::shared_ptr<QPDFStreamFilter>()>>
... ...
libqpdf/qpdf/QPDF_String.hh
... ... @@ -7,10 +7,13 @@
7 7  
8 8 class QPDF_String: public QPDFObject
9 9 {
  10 + friend class QPDFWriter;
  11 +
10 12 public:
11   - QPDF_String(std::string const& val);
12   - static QPDF_String* new_utf16(std::string const& utf8_val);
13 13 virtual ~QPDF_String() = default;
  14 + static std::shared_ptr<QPDFObject> create(std::string const& val);
  15 + static std::shared_ptr<QPDFObject> create_utf16(std::string const& utf8_val);
  16 + virtual std::shared_ptr<QPDFObject> shallowCopy();
14 17 virtual std::string unparse();
15 18 virtual QPDFObject::object_type_e getTypeCode() const;
16 19 virtual char const* getTypeName() const;
... ... @@ -20,6 +23,7 @@ class QPDF_String: public QPDFObject
20 23 std::string getUTF8Val() const;
21 24  
22 25 private:
  26 + QPDF_String(std::string const& val);
23 27 bool useHexString() const;
24 28 std::string val;
25 29 };
... ...
qpdf/qpdf.testcov
... ... @@ -70,14 +70,6 @@ QPDFTokenizer null in name 0
70 70 QPDFTokenizer bad name 0
71 71 QPDF_Stream invalid filter 0
72 72 QPDF UseOutlines but no Outlines 0
73   -QPDFObjectHandle clone bool 0
74   -QPDFObjectHandle clone null 0
75   -QPDFObjectHandle clone integer 0
76   -QPDFObjectHandle clone real 0
77   -QPDFObjectHandle clone name 0
78   -QPDFObjectHandle clone string 0
79   -QPDFObjectHandle clone array 0
80   -QPDFObjectHandle clone dictionary 0
81 73 QPDFObjectHandle makeDirect loop 0
82 74 QPDFObjectHandle copy stream 1
83 75 QPDF default for xref stream field 0 0
... ... @@ -199,9 +191,6 @@ QPDF updateAllPagesCache 0
199 191 QPDF insert non-indirect page 0
200 192 QPDF insert indirect page 0
201 193 QPDFObjectHandle ERR shallow copy stream 0
202   -QPDFObjectHandle shallow copy array 0
203   -QPDFObjectHandle shallow copy dictionary 0
204   -QPDFObjectHandle shallow copy scalar 0
205 194 QPDFObjectHandle newStream with string 0
206 195 QPDF unknown key not inherited 0
207 196 QPDF_Stream provider length not provided 0
... ...