From 4816432671eef6467354aa252f22bb80acc315b7 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 20 Oct 2015 13:19:19 -0400 Subject: Add type cast definitions for CPDF_Dictionary. This CL adds ToCPDFDictionary type definitions and updates one file to use instead of straight casts. I had to fix two places where we'd casted off the constness of the original pointer. BUG=pdfium:201 R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1399233003 . --- .../fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp') diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index e1e821c4c5..317f96d1dc 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -28,7 +28,7 @@ void CPDF_Object::Destroy() { delete (CPDF_Array*)this; break; case PDFOBJ_DICTIONARY: - delete (CPDF_Dictionary*)this; + delete this->AsDictionary(); break; case PDFOBJ_STREAM: delete (CPDF_Stream*)this; @@ -138,7 +138,9 @@ int CPDF_Object::GetInteger() const { CPDF_Dictionary* CPDF_Object::GetDict() const { switch (m_Type) { case PDFOBJ_DICTIONARY: - return (CPDF_Dictionary*)this; + // The method should be made non-const if we want to not be const. + // See bug #234. + return const_cast(this->AsDictionary()); case PDFOBJ_STREAM: return ((CPDF_Stream*)this)->GetDict(); case PDFOBJ_REFERENCE: { @@ -215,7 +217,7 @@ FX_BOOL CPDF_Object::IsIdentical(CPDF_Object* pOther) const { case PDFOBJ_ARRAY: return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther)); case PDFOBJ_DICTIONARY: - return (((CPDF_Dictionary*)this)->Identical((CPDF_Dictionary*)pOther)); + return this->AsDictionary()->Identical(pOther->AsDictionary()); case PDFOBJ_NULL: return TRUE; case PDFOBJ_STREAM: @@ -264,7 +266,7 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, } case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pCopy = new CPDF_Dictionary(); - CPDF_Dictionary* pThis = (CPDF_Dictionary*)this; + const CPDF_Dictionary* pThis = this->AsDictionary(); FX_POSITION pos = pThis->m_Map.GetStartPosition(); while (pos) { CFX_ByteString key; @@ -283,9 +285,9 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, acc.LoadAllData(pThis, TRUE); FX_DWORD streamSize = acc.GetSize(); CPDF_Dictionary* pDict = pThis->GetDict(); - if (pDict) - pDict = (CPDF_Dictionary*)((CPDF_Object*)pDict) - ->CloneInternal(bDirect, visited); + if (pDict) { + pDict = pDict->CloneInternal(bDirect, visited)->AsDictionary(); + } return new CPDF_Stream(acc.DetachData(), streamSize, pDict); } case PDFOBJ_REFERENCE: { @@ -335,6 +337,14 @@ void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) { } } +CPDF_Dictionary* CPDF_Object::AsDictionary() { + return IsDictionary() ? static_cast(this) : nullptr; +} + +const CPDF_Dictionary* CPDF_Object::AsDictionary() const { + return IsDictionary() ? static_cast(this) : nullptr; +} + CPDF_Number::CPDF_Number(int value) : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} @@ -442,8 +452,8 @@ CPDF_Dictionary* CPDF_Array::GetDict(FX_DWORD i) const { if (!p) { return NULL; } - if (p->GetType() == PDFOBJ_DICTIONARY) { - return (CPDF_Dictionary*)p; + if (CPDF_Dictionary* pDict = p->AsDictionary()) { + return pDict; } if (p->GetType() == PDFOBJ_STREAM) { return ((CPDF_Stream*)p)->GetDict(); @@ -663,8 +673,8 @@ CPDF_Dictionary* CPDF_Dictionary::GetDict(const CFX_ByteStringC& key) const { if (!p) { return nullptr; } - if (p->GetType() == PDFOBJ_DICTIONARY) { - return (CPDF_Dictionary*)p; + if (CPDF_Dictionary* pDict = p->AsDictionary()) { + return pDict; } if (p->GetType() == PDFOBJ_STREAM) { return ((CPDF_Stream*)p)->GetDict(); -- cgit v1.2.3