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,6 +30,7 @@
30 #include <qpdf/QUtil.hh> 30 #include <qpdf/QUtil.hh>
31 31
32 #include <algorithm> 32 #include <algorithm>
  33 +#include <array>
33 #include <cctype> 34 #include <cctype>
34 #include <climits> 35 #include <climits>
35 #include <cstdlib> 36 #include <cstdlib>
@@ -263,7 +264,23 @@ char const* @@ -263,7 +264,23 @@ char const*
263 QPDFObjectHandle::getTypeName() const 264 QPDFObjectHandle::getTypeName() const
264 #endif 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 QPDF_Array* 286 QPDF_Array*
@@ -2528,7 +2545,7 @@ QPDFObjectHandle::typeWarning(char const* expected_type, std::string const&amp; warn @@ -2528,7 +2545,7 @@ QPDFObjectHandle::typeWarning(char const* expected_type, std::string const&amp; warn
2528 description, 2545 description,
2529 0, 2546 0,
2530 std::string("operation for ") + expected_type + " attempted on object of type " + 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 #ifndef QPDF_FUTURE 2551 #ifndef QPDF_FUTURE
@@ -2565,7 +2582,7 @@ QPDFObjectHandle::assertType(char const* type_name, bool istype) const @@ -2565,7 +2582,7 @@ QPDFObjectHandle::assertType(char const* type_name, bool istype) const
2565 if (!istype) { 2582 if (!istype) {
2566 throw std::runtime_error( 2583 throw std::runtime_error(
2567 std::string("operation for ") + type_name + " attempted on object of type " + 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,24 +26,24 @@ QPDF_Array::checkOwnership(QPDFObjectHandle const&amp; item) const
26 } 26 }
27 27
28 QPDF_Array::QPDF_Array() : 28 QPDF_Array::QPDF_Array() :
29 - QPDFValue(::ot_array, "array") 29 + QPDFValue(::ot_array)
30 { 30 {
31 } 31 }
32 32
33 QPDF_Array::QPDF_Array(QPDF_Array const& other) : 33 QPDF_Array::QPDF_Array(QPDF_Array const& other) :
34 - QPDFValue(::ot_array, "array"), 34 + QPDFValue(::ot_array),
35 sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr) 35 sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr)
36 { 36 {
37 } 37 }
38 38
39 QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) : 39 QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
40 - QPDFValue(::ot_array, "array") 40 + QPDFValue(::ot_array)
41 { 41 {
42 setFromVector(v); 42 setFromVector(v);
43 } 43 }
44 44
45 QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& v, bool sparse) : 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 if (sparse) { 48 if (sparse) {
49 sp = std::make_unique<Sparse>(); 49 sp = std::make_unique<Sparse>();
libqpdf/QPDF_Bool.cc
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 #include <qpdf/JSON_writer.hh> 3 #include <qpdf/JSON_writer.hh>
4 4
5 QPDF_Bool::QPDF_Bool(bool val) : 5 QPDF_Bool::QPDF_Bool(bool val) :
6 - QPDFValue(::ot_boolean, "boolean"), 6 + QPDFValue(::ot_boolean),
7 val(val) 7 val(val)
8 { 8 {
9 } 9 }
libqpdf/QPDF_Destroyed.cc
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 #include <stdexcept> 3 #include <stdexcept>
4 4
5 QPDF_Destroyed::QPDF_Destroyed() : 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,13 +9,13 @@
9 using namespace std::literals; 9 using namespace std::literals;
10 10
11 QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) : 11 QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) :
12 - QPDFValue(::ot_dictionary, "dictionary"), 12 + QPDFValue(::ot_dictionary),
13 items(items) 13 items(items)
14 { 14 {
15 } 15 }
16 16
17 QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) : 17 QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) :
18 - QPDFValue(::ot_dictionary, "dictionary"), 18 + QPDFValue(::ot_dictionary),
19 items(items) 19 items(items)
20 { 20 {
21 } 21 }
libqpdf/QPDF_InlineImage.cc
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 #include <qpdf/JSON_writer.hh> 3 #include <qpdf/JSON_writer.hh>
4 4
5 QPDF_InlineImage::QPDF_InlineImage(std::string const& val) : 5 QPDF_InlineImage::QPDF_InlineImage(std::string const& val) :
6 - QPDFValue(::ot_inlineimage, "inline-image"), 6 + QPDFValue(::ot_inlineimage),
7 val(val) 7 val(val)
8 { 8 {
9 } 9 }
libqpdf/QPDF_Integer.cc
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 #include <qpdf/QUtil.hh> 4 #include <qpdf/QUtil.hh>
5 5
6 QPDF_Integer::QPDF_Integer(long long val) : 6 QPDF_Integer::QPDF_Integer(long long val) :
7 - QPDFValue(::ot_integer, "integer"), 7 + QPDFValue(::ot_integer),
8 val(val) 8 val(val)
9 { 9 {
10 } 10 }
libqpdf/QPDF_Name.cc
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 #include <qpdf/QUtil.hh> 4 #include <qpdf/QUtil.hh>
5 5
6 QPDF_Name::QPDF_Name(std::string const& name) : 6 QPDF_Name::QPDF_Name(std::string const& name) :
7 - QPDFValue(::ot_name, "name"), 7 + QPDFValue(::ot_name),
8 name(name) 8 name(name)
9 { 9 {
10 } 10 }
libqpdf/QPDF_Null.cc
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 #include <qpdf/QPDFObject_private.hh> 4 #include <qpdf/QPDFObject_private.hh>
5 5
6 QPDF_Null::QPDF_Null(QPDF* qpdf, QPDFObjGen og) : 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,7 +3,7 @@
3 #include <qpdf/JSON_writer.hh> 3 #include <qpdf/JSON_writer.hh>
4 4
5 QPDF_Operator::QPDF_Operator(std::string const& val) : 5 QPDF_Operator::QPDF_Operator(std::string const& val) :
6 - QPDFValue(::ot_operator, "operator"), 6 + QPDFValue(::ot_operator),
7 val(val) 7 val(val)
8 { 8 {
9 } 9 }
libqpdf/QPDF_Real.cc
@@ -4,13 +4,13 @@ @@ -4,13 +4,13 @@
4 #include <qpdf/QUtil.hh> 4 #include <qpdf/QUtil.hh>
5 5
6 QPDF_Real::QPDF_Real(std::string const& val) : 6 QPDF_Real::QPDF_Real(std::string const& val) :
7 - QPDFValue(::ot_real, "real"), 7 + QPDFValue(::ot_real),
8 val(val) 8 val(val)
9 { 9 {
10 } 10 }
11 11
12 QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) : 12 QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) :
13 - QPDFValue(::ot_real, "real"), 13 + QPDFValue(::ot_real),
14 val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes)) 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,7 +3,7 @@
3 #include <stdexcept> 3 #include <stdexcept>
4 4
5 QPDF_Reserved::QPDF_Reserved() : 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,7 +112,7 @@ QPDF_Stream::QPDF_Stream(
112 QPDFObjectHandle stream_dict, 112 QPDFObjectHandle stream_dict,
113 qpdf_offset_t offset, 113 qpdf_offset_t offset,
114 size_t length) : 114 size_t length) :
115 - QPDFValue(::ot_stream, "stream"), 115 + QPDFValue(::ot_stream),
116 filter_on_write(true), 116 filter_on_write(true),
117 stream_dict(stream_dict), 117 stream_dict(stream_dict),
118 length(length) 118 length(length)
libqpdf/QPDF_String.cc
@@ -13,7 +13,7 @@ is_iso_latin1_printable(char ch) @@ -13,7 +13,7 @@ is_iso_latin1_printable(char ch)
13 } 13 }
14 14
15 QPDF_String::QPDF_String(std::string const& val) : 15 QPDF_String::QPDF_String(std::string const& val) :
16 - QPDFValue(::ot_string, "string"), 16 + QPDFValue(::ot_string),
17 val(val) 17 val(val)
18 { 18 {
19 } 19 }
libqpdf/QPDF_Unresolved.cc
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 #include <qpdf/QPDFObject_private.hh> 4 #include <qpdf/QPDFObject_private.hh>
5 5
6 QPDF_Unresolved::QPDF_Unresolved(QPDF* qpdf, QPDFObjGen const& og) : 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,12 +58,6 @@ class QPDFObject
58 { 58 {
59 return value->type_code; 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 QPDF* 62 QPDF*
69 getQPDF() const 63 getQPDF() const
libqpdf/qpdf/QPDFValue.hh
@@ -122,15 +122,12 @@ class QPDFValue: public std::enable_shared_from_this&lt;QPDFValue&gt; @@ -122,15 +122,12 @@ class QPDFValue: public std::enable_shared_from_this&lt;QPDFValue&gt;
122 protected: 122 protected:
123 QPDFValue() = default; 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 type_code(type_code), 130 type_code(type_code),
133 - type_name(type_name),  
134 qpdf(qpdf), 131 qpdf(qpdf),
135 og(og) 132 og(og)
136 { 133 {
@@ -144,7 +141,6 @@ class QPDFValue: public std::enable_shared_from_this&lt;QPDFValue&gt; @@ -144,7 +141,6 @@ class QPDFValue: public std::enable_shared_from_this&lt;QPDFValue&gt;
144 std::shared_ptr<Description> object_description; 141 std::shared_ptr<Description> object_description;
145 142
146 const qpdf_object_type_e type_code{::ot_uninitialized}; 143 const qpdf_object_type_e type_code{::ot_uninitialized};
147 - char const* type_name{"uninitialized"};  
148 144
149 protected: 145 protected:
150 QPDF* qpdf{nullptr}; 146 QPDF* qpdf{nullptr};