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