From 6cbc55a5b5ac4a1c44906ed9dd9ad5c50cd93ea1 Mon Sep 17 00:00:00 2001 From: m-holger Date: Wed, 21 Dec 2022 13:14:05 +0000 Subject: [PATCH] Add new virtual method QPDFObject::getStringValue Avoid dynamic casting. --- libqpdf/QPDFObjectHandle.cc | 50 ++++++++++++------------------ libqpdf/QPDF_InlineImage.cc | 6 ---- libqpdf/QPDF_Name.cc | 6 ---- libqpdf/QPDF_Operator.cc | 6 ---- libqpdf/QPDF_Real.cc | 6 ---- libqpdf/QPDF_String.cc | 6 ---- libqpdf/qpdf/QPDFObject_private.hh | 6 +++- libqpdf/qpdf/QPDFValue.hh | 5 +++ libqpdf/qpdf/QPDF_InlineImage.hh | 6 +++- libqpdf/qpdf/QPDF_Name.hh | 6 +++- libqpdf/qpdf/QPDF_Operator.hh | 6 +++- libqpdf/qpdf/QPDF_Real.hh | 6 +++- libqpdf/qpdf/QPDF_String.hh | 6 +++- 13 files changed, 55 insertions(+), 66 deletions(-) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index f86167b6..169a930d 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -639,9 +639,8 @@ QPDFObjectHandle::getValueAsUInt(unsigned int& value) std::string QPDFObjectHandle::getRealValue() { - auto real = asReal(); - if (real) { - return real->getVal(); + if (isReal()) { + return obj->getStringValue(); } else { typeWarning("real", "returning 0.0"); QTC::TC("qpdf", "QPDFObjectHandle real returning 0.0"); @@ -652,11 +651,10 @@ QPDFObjectHandle::getRealValue() bool QPDFObjectHandle::getValueAsReal(std::string& value) { - auto real = asReal(); - if (real == nullptr) { + if (!isReal()) { return false; } - value = real->getVal(); + value = obj->getStringValue(); return true; } @@ -665,9 +663,8 @@ QPDFObjectHandle::getValueAsReal(std::string& value) std::string QPDFObjectHandle::getName() { - auto name = asName(); - if (name) { - return name->getName(); + if (isName()) { + return obj->getStringValue(); } else { typeWarning("name", "returning dummy name"); QTC::TC("qpdf", "QPDFObjectHandle name returning dummy name"); @@ -678,11 +675,10 @@ QPDFObjectHandle::getName() bool QPDFObjectHandle::getValueAsName(std::string& value) { - auto name = asName(); - if (name == nullptr) { + if (!isName()) { return false; } - value = name->getName(); + value = obj->getStringValue(); return true; } @@ -691,9 +687,8 @@ QPDFObjectHandle::getValueAsName(std::string& value) std::string QPDFObjectHandle::getStringValue() { - auto str = asString(); - if (str) { - return str->getVal(); + if (isString()) { + return obj->getStringValue(); } else { typeWarning("string", "returning empty string"); QTC::TC("qpdf", "QPDFObjectHandle string returning empty string"); @@ -704,11 +699,10 @@ QPDFObjectHandle::getStringValue() bool QPDFObjectHandle::getValueAsString(std::string& value) { - auto str = asString(); - if (str == nullptr) { + if (!isString()) { return false; } - value = str->getVal(); + value = obj->getStringValue(); return true; } @@ -741,9 +735,8 @@ QPDFObjectHandle::getValueAsUTF8(std::string& value) std::string QPDFObjectHandle::getOperatorValue() { - auto op = asOperator(); - if (op) { - return op->getVal(); + if (isOperator()) { + return obj->getStringValue(); } else { typeWarning("operator", "returning fake value"); QTC::TC("qpdf", "QPDFObjectHandle operator returning fake value"); @@ -754,20 +747,18 @@ QPDFObjectHandle::getOperatorValue() bool QPDFObjectHandle::getValueAsOperator(std::string& value) { - auto op = asOperator(); - if (op == nullptr) { + if (!isOperator()) { return false; } - value = op->getVal(); + value = obj->getStringValue(); return true; } std::string QPDFObjectHandle::getInlineImageValue() { - auto image = asInlineImage(); - if (image) { - return image->getVal(); + if (isInlineImage()) { + return obj->getStringValue(); } else { typeWarning("inlineimage", "returning empty data"); QTC::TC("qpdf", "QPDFObjectHandle inlineimage returning empty data"); @@ -778,11 +769,10 @@ QPDFObjectHandle::getInlineImageValue() bool QPDFObjectHandle::getValueAsInlineImage(std::string& value) { - auto image = asInlineImage(); - if (image == nullptr) { + if (!isInlineImage()) { return false; } - value = image->getVal(); + value = obj->getStringValue(); return true; } diff --git a/libqpdf/QPDF_InlineImage.cc b/libqpdf/QPDF_InlineImage.cc index 3ba9719f..18f2fed6 100644 --- a/libqpdf/QPDF_InlineImage.cc +++ b/libqpdf/QPDF_InlineImage.cc @@ -29,9 +29,3 @@ QPDF_InlineImage::getJSON(int json_version) { return JSON::makeNull(); } - -std::string -QPDF_InlineImage::getVal() const -{ - return this->val; -} diff --git a/libqpdf/QPDF_Name.cc b/libqpdf/QPDF_Name.cc index 47e917a1..353cf311 100644 --- a/libqpdf/QPDF_Name.cc +++ b/libqpdf/QPDF_Name.cc @@ -61,9 +61,3 @@ QPDF_Name::getJSON(int json_version) return JSON::makeString(this->name); } } - -std::string -QPDF_Name::getName() const -{ - return this->name; -} diff --git a/libqpdf/QPDF_Operator.cc b/libqpdf/QPDF_Operator.cc index e0d64474..d1b2969d 100644 --- a/libqpdf/QPDF_Operator.cc +++ b/libqpdf/QPDF_Operator.cc @@ -29,9 +29,3 @@ QPDF_Operator::getJSON(int json_version) { return JSON::makeNull(); } - -std::string -QPDF_Operator::getVal() const -{ - return this->val; -} diff --git a/libqpdf/QPDF_Real.cc b/libqpdf/QPDF_Real.cc index c092cfe3..b5e78595 100644 --- a/libqpdf/QPDF_Real.cc +++ b/libqpdf/QPDF_Real.cc @@ -61,9 +61,3 @@ QPDF_Real::getJSON(int json_version) } return JSON::makeNumber(result); } - -std::string -QPDF_Real::getVal() -{ - return this->val; -} diff --git a/libqpdf/QPDF_String.cc b/libqpdf/QPDF_String.cc index 6e72a96f..c3148fa0 100644 --- a/libqpdf/QPDF_String.cc +++ b/libqpdf/QPDF_String.cc @@ -173,12 +173,6 @@ QPDF_String::unparse(bool force_binary) return result; } -std::string -QPDF_String::getVal() const -{ - return this->val; -} - std::string QPDF_String::getUTF8Val() const { diff --git a/libqpdf/qpdf/QPDFObject_private.hh b/libqpdf/qpdf/QPDFObject_private.hh index 6ca81286..447743fc 100644 --- a/libqpdf/qpdf/QPDFObject_private.hh +++ b/libqpdf/qpdf/QPDFObject_private.hh @@ -38,7 +38,11 @@ class QPDFObject { return value->getJSON(json_version); } - + std::string + getStringValue() const + { + return value->getStringValue(); + } // Return a unique type code for the object qpdf_object_type_e getTypeCode() const diff --git a/libqpdf/qpdf/QPDFValue.hh b/libqpdf/qpdf/QPDFValue.hh index 690b3b39..da80e37f 100644 --- a/libqpdf/qpdf/QPDFValue.hh +++ b/libqpdf/qpdf/QPDFValue.hh @@ -67,6 +67,11 @@ class QPDFValue disconnect() { } + virtual std::string + getStringValue() const + { + return ""; + } protected: QPDFValue() = default; diff --git a/libqpdf/qpdf/QPDF_InlineImage.hh b/libqpdf/qpdf/QPDF_InlineImage.hh index d6d62efd..553d1d71 100644 --- a/libqpdf/qpdf/QPDF_InlineImage.hh +++ b/libqpdf/qpdf/QPDF_InlineImage.hh @@ -11,7 +11,11 @@ class QPDF_InlineImage: public QPDFValue virtual std::shared_ptr copy(bool shallow = false); virtual std::string unparse(); virtual JSON getJSON(int json_version); - std::string getVal() const; + virtual std::string + getStringValue() const + { + return val; + } private: QPDF_InlineImage(std::string const& val); diff --git a/libqpdf/qpdf/QPDF_Name.hh b/libqpdf/qpdf/QPDF_Name.hh index 833ac822..136ad5cb 100644 --- a/libqpdf/qpdf/QPDF_Name.hh +++ b/libqpdf/qpdf/QPDF_Name.hh @@ -11,10 +11,14 @@ class QPDF_Name: public QPDFValue virtual std::shared_ptr copy(bool shallow = false); virtual std::string unparse(); virtual JSON getJSON(int json_version); - std::string getName() const; // Put # into strings with characters unsuitable for name token static std::string normalizeName(std::string const& name); + virtual std::string + getStringValue() const + { + return name; + } private: QPDF_Name(std::string const& name); diff --git a/libqpdf/qpdf/QPDF_Operator.hh b/libqpdf/qpdf/QPDF_Operator.hh index f420b793..4fedb808 100644 --- a/libqpdf/qpdf/QPDF_Operator.hh +++ b/libqpdf/qpdf/QPDF_Operator.hh @@ -11,7 +11,11 @@ class QPDF_Operator: public QPDFValue virtual std::shared_ptr copy(bool shallow = false); virtual std::string unparse(); virtual JSON getJSON(int json_version); - std::string getVal() const; + virtual std::string + getStringValue() const + { + return val; + } private: QPDF_Operator(std::string const& val); diff --git a/libqpdf/qpdf/QPDF_Real.hh b/libqpdf/qpdf/QPDF_Real.hh index cb3c1846..f470e1a6 100644 --- a/libqpdf/qpdf/QPDF_Real.hh +++ b/libqpdf/qpdf/QPDF_Real.hh @@ -13,7 +13,11 @@ class QPDF_Real: public QPDFValue virtual std::shared_ptr copy(bool shallow = false); virtual std::string unparse(); virtual JSON getJSON(int json_version); - std::string getVal(); + virtual std::string + getStringValue() const + { + return val; + } private: QPDF_Real(std::string const& val); diff --git a/libqpdf/qpdf/QPDF_String.hh b/libqpdf/qpdf/QPDF_String.hh index d349439a..32a05a6c 100644 --- a/libqpdf/qpdf/QPDF_String.hh +++ b/libqpdf/qpdf/QPDF_String.hh @@ -18,8 +18,12 @@ class QPDF_String: public QPDFValue virtual std::string unparse(); std::string unparse(bool force_binary); virtual JSON getJSON(int json_version); - std::string getVal() const; std::string getUTF8Val() const; + virtual std::string + getStringValue() const + { + return val; + } private: QPDF_String(std::string const& val);