Commit c1377176f8a65f62c4a2f582062554dc69dd5e81

Authored by m-holger
Committed by GitHub
2 parents 0a6ab106 64f9b7b2

Merge pull request #1276 from m-holger/obj

Refactor QPDFObjectHandle::getTypeName
libqpdf/QPDFObjectHandle.cc
... ... @@ -30,6 +30,7 @@
30 30 #include <qpdf/QUtil.hh>
31 31  
32 32 #include <algorithm>
  33 +#include <array>
33 34 #include <cctype>
34 35 #include <climits>
35 36 #include <cstdlib>
... ... @@ -263,7 +264,23 @@ char const*
263 264 QPDFObjectHandle::getTypeName() const
264 265 #endif
265 266 {
266   - return obj ? obj->getTypeName() : "uninitialized";
  267 + static constexpr std::array<char const*, 15> tn{
  268 + "uninitialized",
  269 + "reserved",
  270 + "null",
  271 + "boolean",
  272 + "integer",
  273 + "real",
  274 + "string",
  275 + "name",
  276 + "array",
  277 + "dictionary",
  278 + "stream",
  279 + "operator",
  280 + "inline-image",
  281 + "unresolved",
  282 + "destroyed"};
  283 + return obj ? tn[getTypeCode()] : "uninitialized";
267 284 }
268 285  
269 286 QPDF_Array*
... ... @@ -2528,7 +2545,7 @@ QPDFObjectHandle::typeWarning(char const* expected_type, std::string const&amp; warn
2528 2545 description,
2529 2546 0,
2530 2547 std::string("operation for ") + expected_type + " attempted on object of type " +
2531   - obj->getTypeName() + ": " + warning));
  2548 + QPDFObjectHandle(*this).getTypeName() + ": " + warning));
2532 2549 }
2533 2550  
2534 2551 #ifndef QPDF_FUTURE
... ... @@ -2565,7 +2582,7 @@ QPDFObjectHandle::assertType(char const* type_name, bool istype) const
2565 2582 if (!istype) {
2566 2583 throw std::runtime_error(
2567 2584 std::string("operation for ") + type_name + " attempted on object of type " +
2568   - (obj ? obj->getTypeName() : "uninitialized"));
  2585 + QPDFObjectHandle(*this).getTypeName());
2569 2586 }
2570 2587 }
2571 2588  
... ...
libqpdf/QPDF_Array.cc
... ... @@ -26,24 +26,24 @@ QPDF_Array::checkOwnership(QPDFObjectHandle const&amp; item) const
26 26 }
27 27  
28 28 QPDF_Array::QPDF_Array() :
29   - QPDFValue(::ot_array, "array")
  29 + QPDFValue(::ot_array)
30 30 {
31 31 }
32 32  
33 33 QPDF_Array::QPDF_Array(QPDF_Array const& other) :
34   - QPDFValue(::ot_array, "array"),
  34 + QPDFValue(::ot_array),
35 35 sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr)
36 36 {
37 37 }
38 38  
39 39 QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
40   - QPDFValue(::ot_array, "array")
  40 + QPDFValue(::ot_array)
41 41 {
42 42 setFromVector(v);
43 43 }
44 44  
45 45 QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& v, bool sparse) :
46   - QPDFValue(::ot_array, "array")
  46 + QPDFValue(::ot_array)
47 47 {
48 48 if (sparse) {
49 49 sp = std::make_unique<Sparse>();
... ...
libqpdf/QPDF_Bool.cc
... ... @@ -3,7 +3,7 @@
3 3 #include <qpdf/JSON_writer.hh>
4 4  
5 5 QPDF_Bool::QPDF_Bool(bool val) :
6   - QPDFValue(::ot_boolean, "boolean"),
  6 + QPDFValue(::ot_boolean),
7 7 val(val)
8 8 {
9 9 }
... ...
libqpdf/QPDF_Destroyed.cc
... ... @@ -3,7 +3,7 @@
3 3 #include <stdexcept>
4 4  
5 5 QPDF_Destroyed::QPDF_Destroyed() :
6   - QPDFValue(::ot_destroyed, "destroyed")
  6 + QPDFValue(::ot_destroyed)
7 7 {
8 8 }
9 9  
... ...
libqpdf/QPDF_Dictionary.cc
... ... @@ -9,13 +9,13 @@
9 9 using namespace std::literals;
10 10  
11 11 QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) :
12   - QPDFValue(::ot_dictionary, "dictionary"),
  12 + QPDFValue(::ot_dictionary),
13 13 items(items)
14 14 {
15 15 }
16 16  
17 17 QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) :
18   - QPDFValue(::ot_dictionary, "dictionary"),
  18 + QPDFValue(::ot_dictionary),
19 19 items(items)
20 20 {
21 21 }
... ...
libqpdf/QPDF_InlineImage.cc
... ... @@ -3,7 +3,7 @@
3 3 #include <qpdf/JSON_writer.hh>
4 4  
5 5 QPDF_InlineImage::QPDF_InlineImage(std::string const& val) :
6   - QPDFValue(::ot_inlineimage, "inline-image"),
  6 + QPDFValue(::ot_inlineimage),
7 7 val(val)
8 8 {
9 9 }
... ...
libqpdf/QPDF_Integer.cc
... ... @@ -4,7 +4,7 @@
4 4 #include <qpdf/QUtil.hh>
5 5  
6 6 QPDF_Integer::QPDF_Integer(long long val) :
7   - QPDFValue(::ot_integer, "integer"),
  7 + QPDFValue(::ot_integer),
8 8 val(val)
9 9 {
10 10 }
... ...
libqpdf/QPDF_Name.cc
... ... @@ -4,7 +4,7 @@
4 4 #include <qpdf/QUtil.hh>
5 5  
6 6 QPDF_Name::QPDF_Name(std::string const& name) :
7   - QPDFValue(::ot_name, "name"),
  7 + QPDFValue(::ot_name),
8 8 name(name)
9 9 {
10 10 }
... ...
libqpdf/QPDF_Null.cc
... ... @@ -4,7 +4,7 @@
4 4 #include <qpdf/QPDFObject_private.hh>
5 5  
6 6 QPDF_Null::QPDF_Null(QPDF* qpdf, QPDFObjGen og) :
7   - QPDFValue(::ot_null, "null", qpdf, og)
  7 + QPDFValue(::ot_null, qpdf, og)
8 8 {
9 9 }
10 10  
... ...
libqpdf/QPDF_Operator.cc
... ... @@ -3,7 +3,7 @@
3 3 #include <qpdf/JSON_writer.hh>
4 4  
5 5 QPDF_Operator::QPDF_Operator(std::string const& val) :
6   - QPDFValue(::ot_operator, "operator"),
  6 + QPDFValue(::ot_operator),
7 7 val(val)
8 8 {
9 9 }
... ...
libqpdf/QPDF_Real.cc
... ... @@ -4,13 +4,13 @@
4 4 #include <qpdf/QUtil.hh>
5 5  
6 6 QPDF_Real::QPDF_Real(std::string const& val) :
7   - QPDFValue(::ot_real, "real"),
  7 + QPDFValue(::ot_real),
8 8 val(val)
9 9 {
10 10 }
11 11  
12 12 QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) :
13   - QPDFValue(::ot_real, "real"),
  13 + QPDFValue(::ot_real),
14 14 val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes))
15 15 {
16 16 }
... ...
libqpdf/QPDF_Reserved.cc
... ... @@ -3,7 +3,7 @@
3 3 #include <stdexcept>
4 4  
5 5 QPDF_Reserved::QPDF_Reserved() :
6   - QPDFValue(::ot_reserved, "reserved")
  6 + QPDFValue(::ot_reserved)
7 7 {
8 8 }
9 9  
... ...
libqpdf/QPDF_Stream.cc
... ... @@ -112,7 +112,7 @@ QPDF_Stream::QPDF_Stream(
112 112 QPDFObjectHandle stream_dict,
113 113 qpdf_offset_t offset,
114 114 size_t length) :
115   - QPDFValue(::ot_stream, "stream"),
  115 + QPDFValue(::ot_stream),
116 116 filter_on_write(true),
117 117 stream_dict(stream_dict),
118 118 length(length)
... ...
libqpdf/QPDF_String.cc
... ... @@ -13,7 +13,7 @@ is_iso_latin1_printable(char ch)
13 13 }
14 14  
15 15 QPDF_String::QPDF_String(std::string const& val) :
16   - QPDFValue(::ot_string, "string"),
  16 + QPDFValue(::ot_string),
17 17 val(val)
18 18 {
19 19 }
... ...
libqpdf/QPDF_Unresolved.cc
... ... @@ -4,7 +4,7 @@
4 4 #include <qpdf/QPDFObject_private.hh>
5 5  
6 6 QPDF_Unresolved::QPDF_Unresolved(QPDF* qpdf, QPDFObjGen const& og) :
7   - QPDFValue(::ot_unresolved, "unresolved", qpdf, og)
  7 + QPDFValue(::ot_unresolved, qpdf, og)
8 8 {
9 9 }
10 10  
... ...
libqpdf/qpdf/QPDFObject_private.hh
... ... @@ -58,12 +58,6 @@ class QPDFObject
58 58 {
59 59 return value->type_code;
60 60 }
61   - // Return a string literal that describes the type, useful for debugging and testing
62   - char const*
63   - getTypeName() const
64   - {
65   - return resolved_object()->value->type_name;
66   - }
67 61  
68 62 QPDF*
69 63 getQPDF() const
... ...
libqpdf/qpdf/QPDFValue.hh
... ... @@ -122,15 +122,12 @@ class QPDFValue: public std::enable_shared_from_this&lt;QPDFValue&gt;
122 122 protected:
123 123 QPDFValue() = default;
124 124  
125   - QPDFValue(qpdf_object_type_e type_code, char const* type_name) :
126   - type_code(type_code),
127   - type_name(type_name)
  125 + QPDFValue(qpdf_object_type_e type_code) :
  126 + type_code(type_code)
128 127 {
129 128 }
130   - QPDFValue(
131   - qpdf_object_type_e type_code, char const* type_name, QPDF* qpdf, QPDFObjGen const& og) :
  129 + QPDFValue(qpdf_object_type_e type_code, QPDF* qpdf, QPDFObjGen og) :
132 130 type_code(type_code),
133   - type_name(type_name),
134 131 qpdf(qpdf),
135 132 og(og)
136 133 {
... ... @@ -144,7 +141,6 @@ class QPDFValue: public std::enable_shared_from_this&lt;QPDFValue&gt;
144 141 std::shared_ptr<Description> object_description;
145 142  
146 143 const qpdf_object_type_e type_code{::ot_uninitialized};
147   - char const* type_name{"uninitialized"};
148 144  
149 145 protected:
150 146 QPDF* qpdf{nullptr};
... ...