Commit 32907fc14c663e95df0f7c62905b82389c0024a2

Authored by m-holger
1 parent d58ec903

Change type of QPDFValue::object_description to std::shared_ptr<std::variant>

Also, name the type QPDFValue::Description.
libqpdf/QPDFObjectHandle.cc
@@ -2176,7 +2176,8 @@ QPDFObjectHandle::setObjectDescription( @@ -2176,7 +2176,8 @@ QPDFObjectHandle::setObjectDescription(
2176 // This is called during parsing on newly created direct objects, 2176 // This is called during parsing on newly created direct objects,
2177 // so we can't call dereference() here. 2177 // so we can't call dereference() here.
2178 if (isInitialized() && obj.get()) { 2178 if (isInitialized() && obj.get()) {
2179 - auto descr = std::make_shared<std::string>(object_description); 2179 + auto descr =
  2180 + std::make_shared<QPDFValue::Description>(object_description);
2180 obj->setDescription(owning_qpdf, descr); 2181 obj->setDescription(owning_qpdf, descr);
2181 } 2182 }
2182 } 2183 }
libqpdf/QPDFValue.cc
@@ -13,16 +13,28 @@ QPDFValue::do_create(QPDFValue* object) @@ -13,16 +13,28 @@ QPDFValue::do_create(QPDFValue* object)
13 std::string 13 std::string
14 QPDFValue::getDescription() 14 QPDFValue::getDescription()
15 { 15 {
16 - auto description = object_description ? *object_description : "";  
17 - if (auto pos = description.find("$OG"); pos != std::string::npos) {  
18 - description.replace(pos, 3, og.unparse(' '));  
19 - }  
20 - if (auto pos = description.find("$PO"); pos != std::string::npos) {  
21 - qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2  
22 - : (type_code == ::ot_array) ? 1  
23 - : 0; 16 + if (object_description) {
  17 + switch (object_description->index()) {
  18 + case 0:
  19 + {
  20 + auto description = std::get<0>(*object_description);
  21 +
  22 + if (auto pos = description.find("$OG");
  23 + pos != std::string::npos) {
  24 + description.replace(pos, 3, og.unparse(' '));
  25 + }
  26 + if (auto pos = description.find("$PO");
  27 + pos != std::string::npos) {
  28 + qpdf_offset_t shift = (type_code == ::ot_dictionary) ? 2
  29 + : (type_code == ::ot_array) ? 1
  30 + : 0;
24 31
25 - description.replace(pos, 3, std::to_string(parsed_offset + shift)); 32 + description.replace(
  33 + pos, 3, std::to_string(parsed_offset + shift));
  34 + }
  35 + return description;
  36 + }
  37 + }
26 } 38 }
27 - return description; 39 + return {};
28 } 40 }
libqpdf/QPDF_Stream.cc
@@ -123,7 +123,7 @@ QPDF_Stream::QPDF_Stream( @@ -123,7 +123,7 @@ QPDF_Stream::QPDF_Stream(
123 throw std::logic_error("stream object instantiated with non-dictionary " 123 throw std::logic_error("stream object instantiated with non-dictionary "
124 "object for dictionary"); 124 "object for dictionary");
125 } 125 }
126 - auto descr = std::make_shared<std::string>( 126 + auto descr = std::make_shared<QPDFValue::Description>(
127 qpdf->getFilename() + ", stream object " + og.unparse(' ')); 127 qpdf->getFilename() + ", stream object " + og.unparse(' '));
128 setDescription(qpdf, descr, offset); 128 setDescription(qpdf, descr, offset);
129 } 129 }
@@ -283,7 +283,9 @@ QPDF_Stream::getStreamJSON( @@ -283,7 +283,9 @@ QPDF_Stream::getStreamJSON(
283 283
284 void 284 void
285 QPDF_Stream::setDescription( 285 QPDF_Stream::setDescription(
286 - QPDF* qpdf, std::shared_ptr<std::string>& description, qpdf_offset_t offset) 286 + QPDF* qpdf,
  287 + std::shared_ptr<QPDFValue::Description>& description,
  288 + qpdf_offset_t offset)
287 { 289 {
288 this->QPDFValue::setDescription(qpdf, description, offset); 290 this->QPDFValue::setDescription(qpdf, description, offset);
289 setDictDescription(); 291 setDictDescription();
libqpdf/qpdf/QPDFObject_private.hh
@@ -71,7 +71,7 @@ class QPDFObject @@ -71,7 +71,7 @@ class QPDFObject
71 void 71 void
72 setDescription( 72 setDescription(
73 QPDF* qpdf, 73 QPDF* qpdf,
74 - std::shared_ptr<std::string>& description, 74 + std::shared_ptr<QPDFValue::Description>& description,
75 qpdf_offset_t offset = -1) 75 qpdf_offset_t offset = -1)
76 { 76 {
77 return value->setDescription(qpdf, description, offset); 77 return value->setDescription(qpdf, description, offset);
libqpdf/qpdf/QPDFParser.hh
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 #define QPDFPARSER_HH 2 #define QPDFPARSER_HH
3 3
4 #include <qpdf/QPDFObjectHandle.hh> 4 #include <qpdf/QPDFObjectHandle.hh>
  5 +#include <qpdf/QPDFValue.hh>
5 6
6 #include <memory> 7 #include <memory>
7 #include <string> 8 #include <string>
@@ -21,8 +22,8 @@ class QPDFParser @@ -21,8 +22,8 @@ class QPDFParser
21 tokenizer(tokenizer), 22 tokenizer(tokenizer),
22 decrypter(decrypter), 23 decrypter(decrypter),
23 context(context), 24 context(context),
24 - description(std::make_shared<std::string>(  
25 - input->getName() + ", " + object_description + " at offset $PO")) 25 + description(std::make_shared<QPDFValue::Description>(std::string(
  26 + input->getName() + ", " + object_description + " at offset $PO")))
26 { 27 {
27 } 28 }
28 virtual ~QPDFParser() = default; 29 virtual ~QPDFParser() = default;
@@ -49,7 +50,7 @@ class QPDFParser @@ -49,7 +50,7 @@ class QPDFParser
49 QPDFTokenizer& tokenizer; 50 QPDFTokenizer& tokenizer;
50 QPDFObjectHandle::StringDecrypter* decrypter; 51 QPDFObjectHandle::StringDecrypter* decrypter;
51 QPDF* context; 52 QPDF* context;
52 - std::shared_ptr<std::string> description; 53 + std::shared_ptr<QPDFValue::Description> description;
53 }; 54 };
54 55
55 #endif // QPDFPARSER_HH 56 #endif // QPDFPARSER_HH
libqpdf/qpdf/QPDFValue.hh
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 #include <qpdf/Types.h> 8 #include <qpdf/Types.h>
9 9
10 #include <string> 10 #include <string>
  11 +#include <variant>
11 12
12 class QPDF; 13 class QPDF;
13 class QPDFObjectHandle; 14 class QPDFObjectHandle;
@@ -23,10 +24,13 @@ class QPDFValue @@ -23,10 +24,13 @@ class QPDFValue
23 virtual std::shared_ptr<QPDFObject> copy(bool shallow = false) = 0; 24 virtual std::shared_ptr<QPDFObject> copy(bool shallow = false) = 0;
24 virtual std::string unparse() = 0; 25 virtual std::string unparse() = 0;
25 virtual JSON getJSON(int json_version) = 0; 26 virtual JSON getJSON(int json_version) = 0;
  27 +
  28 + using Description = std::variant<std::string>;
  29 +
26 virtual void 30 virtual void
27 setDescription( 31 setDescription(
28 QPDF* qpdf_p, 32 QPDF* qpdf_p,
29 - std::shared_ptr<std::string>& description, 33 + std::shared_ptr<Description>& description,
30 qpdf_offset_t offset) 34 qpdf_offset_t offset)
31 { 35 {
32 qpdf = qpdf_p; 36 qpdf = qpdf_p;
@@ -37,7 +41,7 @@ class QPDFValue @@ -37,7 +41,7 @@ class QPDFValue
37 setDefaultDescription(QPDF* a_qpdf, QPDFObjGen const& a_og) 41 setDefaultDescription(QPDF* a_qpdf, QPDFObjGen const& a_og)
38 { 42 {
39 static auto default_description{ 43 static auto default_description{
40 - std::make_shared<std::string>("object $OG")}; 44 + std::make_shared<Description>("object $OG")};
41 if (!object_description) { 45 if (!object_description) {
42 object_description = default_description; 46 object_description = default_description;
43 } 47 }
@@ -49,7 +53,7 @@ class QPDFValue @@ -49,7 +53,7 @@ class QPDFValue
49 hasDescription() 53 hasDescription()
50 { 54 {
51 return qpdf != nullptr && object_description && 55 return qpdf != nullptr && object_description &&
52 - !object_description->empty(); 56 + !getDescription().empty();
53 } 57 }
54 void 58 void
55 setParsedOffset(qpdf_offset_t offset) 59 setParsedOffset(qpdf_offset_t offset)
@@ -108,7 +112,7 @@ class QPDFValue @@ -108,7 +112,7 @@ class QPDFValue
108 private: 112 private:
109 QPDFValue(QPDFValue const&) = delete; 113 QPDFValue(QPDFValue const&) = delete;
110 QPDFValue& operator=(QPDFValue const&) = delete; 114 QPDFValue& operator=(QPDFValue const&) = delete;
111 - std::shared_ptr<std::string> object_description; 115 + std::shared_ptr<Description> object_description;
112 116
113 const qpdf_object_type_e type_code{::ot_uninitialized}; 117 const qpdf_object_type_e type_code{::ot_uninitialized};
114 char const* type_name{"uninitialized"}; 118 char const* type_name{"uninitialized"};
libqpdf/qpdf/QPDF_Stream.hh
@@ -27,7 +27,9 @@ class QPDF_Stream: public QPDFValue @@ -27,7 +27,9 @@ class QPDF_Stream: public QPDFValue
27 virtual std::string unparse(); 27 virtual std::string unparse();
28 virtual JSON getJSON(int json_version); 28 virtual JSON getJSON(int json_version);
29 virtual void setDescription( 29 virtual void setDescription(
30 - QPDF*, std::shared_ptr<std::string>& description, qpdf_offset_t offset); 30 + QPDF*,
  31 + std::shared_ptr<QPDFValue::Description>& description,
  32 + qpdf_offset_t offset);
31 virtual void disconnect(); 33 virtual void disconnect();
32 QPDFObjectHandle getDict() const; 34 QPDFObjectHandle getDict() const;
33 bool isDataModified() const; 35 bool isDataModified() const;