2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-08 06:15:23 +00:00

Refactor QPDFObjectHandle::getTypeName

This commit is contained in:
m-holger 2024-08-12 17:52:42 +01:00
parent 0a6ab1060f
commit 64f9b7b242
17 changed files with 42 additions and 35 deletions

View File

@ -30,6 +30,7 @@
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
#include <algorithm> #include <algorithm>
#include <array>
#include <cctype> #include <cctype>
#include <climits> #include <climits>
#include <cstdlib> #include <cstdlib>
@ -263,7 +264,23 @@ char const*
QPDFObjectHandle::getTypeName() const QPDFObjectHandle::getTypeName() const
#endif #endif
{ {
return obj ? obj->getTypeName() : "uninitialized"; static constexpr std::array<char const*, 15> tn{
"uninitialized",
"reserved",
"null",
"boolean",
"integer",
"real",
"string",
"name",
"array",
"dictionary",
"stream",
"operator",
"inline-image",
"unresolved",
"destroyed"};
return obj ? tn[getTypeCode()] : "uninitialized";
} }
QPDF_Array* QPDF_Array*
@ -2528,7 +2545,7 @@ QPDFObjectHandle::typeWarning(char const* expected_type, std::string const& warn
description, description,
0, 0,
std::string("operation for ") + expected_type + " attempted on object of type " + std::string("operation for ") + expected_type + " attempted on object of type " +
obj->getTypeName() + ": " + warning)); QPDFObjectHandle(*this).getTypeName() + ": " + warning));
} }
#ifndef QPDF_FUTURE #ifndef QPDF_FUTURE
@ -2565,7 +2582,7 @@ QPDFObjectHandle::assertType(char const* type_name, bool istype) const
if (!istype) { if (!istype) {
throw std::runtime_error( throw std::runtime_error(
std::string("operation for ") + type_name + " attempted on object of type " + std::string("operation for ") + type_name + " attempted on object of type " +
(obj ? obj->getTypeName() : "uninitialized")); QPDFObjectHandle(*this).getTypeName());
} }
} }

View File

@ -26,24 +26,24 @@ QPDF_Array::checkOwnership(QPDFObjectHandle const& item) const
} }
QPDF_Array::QPDF_Array() : QPDF_Array::QPDF_Array() :
QPDFValue(::ot_array, "array") QPDFValue(::ot_array)
{ {
} }
QPDF_Array::QPDF_Array(QPDF_Array const& other) : QPDF_Array::QPDF_Array(QPDF_Array const& other) :
QPDFValue(::ot_array, "array"), QPDFValue(::ot_array),
sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr) sp(other.sp ? std::make_unique<Sparse>(*other.sp) : nullptr)
{ {
} }
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) : QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
QPDFValue(::ot_array, "array") QPDFValue(::ot_array)
{ {
setFromVector(v); setFromVector(v);
} }
QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& v, bool sparse) : QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& v, bool sparse) :
QPDFValue(::ot_array, "array") QPDFValue(::ot_array)
{ {
if (sparse) { if (sparse) {
sp = std::make_unique<Sparse>(); sp = std::make_unique<Sparse>();

View File

@ -3,7 +3,7 @@
#include <qpdf/JSON_writer.hh> #include <qpdf/JSON_writer.hh>
QPDF_Bool::QPDF_Bool(bool val) : QPDF_Bool::QPDF_Bool(bool val) :
QPDFValue(::ot_boolean, "boolean"), QPDFValue(::ot_boolean),
val(val) val(val)
{ {
} }

View File

@ -3,7 +3,7 @@
#include <stdexcept> #include <stdexcept>
QPDF_Destroyed::QPDF_Destroyed() : QPDF_Destroyed::QPDF_Destroyed() :
QPDFValue(::ot_destroyed, "destroyed") QPDFValue(::ot_destroyed)
{ {
} }

View File

@ -9,13 +9,13 @@
using namespace std::literals; using namespace std::literals;
QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) : QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle> const& items) :
QPDFValue(::ot_dictionary, "dictionary"), QPDFValue(::ot_dictionary),
items(items) items(items)
{ {
} }
QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) : QPDF_Dictionary::QPDF_Dictionary(std::map<std::string, QPDFObjectHandle>&& items) :
QPDFValue(::ot_dictionary, "dictionary"), QPDFValue(::ot_dictionary),
items(items) items(items)
{ {
} }

View File

@ -3,7 +3,7 @@
#include <qpdf/JSON_writer.hh> #include <qpdf/JSON_writer.hh>
QPDF_InlineImage::QPDF_InlineImage(std::string const& val) : QPDF_InlineImage::QPDF_InlineImage(std::string const& val) :
QPDFValue(::ot_inlineimage, "inline-image"), QPDFValue(::ot_inlineimage),
val(val) val(val)
{ {
} }

View File

@ -4,7 +4,7 @@
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
QPDF_Integer::QPDF_Integer(long long val) : QPDF_Integer::QPDF_Integer(long long val) :
QPDFValue(::ot_integer, "integer"), QPDFValue(::ot_integer),
val(val) val(val)
{ {
} }

View File

@ -4,7 +4,7 @@
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
QPDF_Name::QPDF_Name(std::string const& name) : QPDF_Name::QPDF_Name(std::string const& name) :
QPDFValue(::ot_name, "name"), QPDFValue(::ot_name),
name(name) name(name)
{ {
} }

View File

@ -4,7 +4,7 @@
#include <qpdf/QPDFObject_private.hh> #include <qpdf/QPDFObject_private.hh>
QPDF_Null::QPDF_Null(QPDF* qpdf, QPDFObjGen og) : QPDF_Null::QPDF_Null(QPDF* qpdf, QPDFObjGen og) :
QPDFValue(::ot_null, "null", qpdf, og) QPDFValue(::ot_null, qpdf, og)
{ {
} }

View File

@ -3,7 +3,7 @@
#include <qpdf/JSON_writer.hh> #include <qpdf/JSON_writer.hh>
QPDF_Operator::QPDF_Operator(std::string const& val) : QPDF_Operator::QPDF_Operator(std::string const& val) :
QPDFValue(::ot_operator, "operator"), QPDFValue(::ot_operator),
val(val) val(val)
{ {
} }

View File

@ -4,13 +4,13 @@
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
QPDF_Real::QPDF_Real(std::string const& val) : QPDF_Real::QPDF_Real(std::string const& val) :
QPDFValue(::ot_real, "real"), QPDFValue(::ot_real),
val(val) val(val)
{ {
} }
QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) : QPDF_Real::QPDF_Real(double value, int decimal_places, bool trim_trailing_zeroes) :
QPDFValue(::ot_real, "real"), QPDFValue(::ot_real),
val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes)) val(QUtil::double_to_string(value, decimal_places, trim_trailing_zeroes))
{ {
} }

View File

@ -3,7 +3,7 @@
#include <stdexcept> #include <stdexcept>
QPDF_Reserved::QPDF_Reserved() : QPDF_Reserved::QPDF_Reserved() :
QPDFValue(::ot_reserved, "reserved") QPDFValue(::ot_reserved)
{ {
} }

View File

@ -112,7 +112,7 @@ QPDF_Stream::QPDF_Stream(
QPDFObjectHandle stream_dict, QPDFObjectHandle stream_dict,
qpdf_offset_t offset, qpdf_offset_t offset,
size_t length) : size_t length) :
QPDFValue(::ot_stream, "stream"), QPDFValue(::ot_stream),
filter_on_write(true), filter_on_write(true),
stream_dict(stream_dict), stream_dict(stream_dict),
length(length) length(length)

View File

@ -13,7 +13,7 @@ is_iso_latin1_printable(char ch)
} }
QPDF_String::QPDF_String(std::string const& val) : QPDF_String::QPDF_String(std::string const& val) :
QPDFValue(::ot_string, "string"), QPDFValue(::ot_string),
val(val) val(val)
{ {
} }

View File

@ -4,7 +4,7 @@
#include <qpdf/QPDFObject_private.hh> #include <qpdf/QPDFObject_private.hh>
QPDF_Unresolved::QPDF_Unresolved(QPDF* qpdf, QPDFObjGen const& og) : QPDF_Unresolved::QPDF_Unresolved(QPDF* qpdf, QPDFObjGen const& og) :
QPDFValue(::ot_unresolved, "unresolved", qpdf, og) QPDFValue(::ot_unresolved, qpdf, og)
{ {
} }

View File

@ -58,12 +58,6 @@ class QPDFObject
{ {
return value->type_code; return value->type_code;
} }
// Return a string literal that describes the type, useful for debugging and testing
char const*
getTypeName() const
{
return resolved_object()->value->type_name;
}
QPDF* QPDF*
getQPDF() const getQPDF() const

View File

@ -122,15 +122,12 @@ class QPDFValue: public std::enable_shared_from_this<QPDFValue>
protected: protected:
QPDFValue() = default; QPDFValue() = default;
QPDFValue(qpdf_object_type_e type_code, char const* type_name) : QPDFValue(qpdf_object_type_e type_code) :
type_code(type_code), type_code(type_code)
type_name(type_name)
{ {
} }
QPDFValue( QPDFValue(qpdf_object_type_e type_code, QPDF* qpdf, QPDFObjGen og) :
qpdf_object_type_e type_code, char const* type_name, QPDF* qpdf, QPDFObjGen const& og) :
type_code(type_code), type_code(type_code),
type_name(type_name),
qpdf(qpdf), qpdf(qpdf),
og(og) og(og)
{ {
@ -144,7 +141,6 @@ class QPDFValue: public std::enable_shared_from_this<QPDFValue>
std::shared_ptr<Description> object_description; std::shared_ptr<Description> object_description;
const qpdf_object_type_e type_code{::ot_uninitialized}; const qpdf_object_type_e type_code{::ot_uninitialized};
char const* type_name{"uninitialized"};
protected: protected:
QPDF* qpdf{nullptr}; QPDF* qpdf{nullptr};