From 5a6c1398d0e559fb6a048cb0dca46ba9f9309a77 Mon Sep 17 00:00:00 2001 From: weili Date: Mon, 11 Jul 2016 14:43:40 -0700 Subject: Use smart pointers for class owned member variables Replace raw member variables to smart pointer type to better maintain the ownership and to ease the management. BUG=pdfium:518 Review-Url: https://codereview.chromium.org/2136683002 --- core/fpdfdoc/cpdf_variabletext.cpp | 14 +++++----- core/fpdfdoc/cpvt_sectioninfo.cpp | 31 ++++++++++++++++++++++ core/fpdfdoc/cpvt_sectioninfo.h | 43 +++++++----------------------- core/fpdfdoc/cpvt_wordinfo.cpp | 54 ++++++++++++++++++++++++++++++++++++++ core/fpdfdoc/cpvt_wordinfo.h | 53 ++++++------------------------------- core/fpdfdoc/doc_annot.cpp | 10 +++---- core/fpdfdoc/include/fpdf_doc.h | 8 +++--- 7 files changed, 118 insertions(+), 95 deletions(-) create mode 100644 core/fpdfdoc/cpvt_sectioninfo.cpp create mode 100644 core/fpdfdoc/cpvt_wordinfo.cpp (limited to 'core/fpdfdoc') diff --git a/core/fpdfdoc/cpdf_variabletext.cpp b/core/fpdfdoc/cpdf_variabletext.cpp index 010d426cf2..1ac36ade86 100644 --- a/core/fpdfdoc/cpdf_variabletext.cpp +++ b/core/fpdfdoc/cpdf_variabletext.cpp @@ -264,9 +264,9 @@ void CPDF_VariableText::Initialize() { if (!m_bInitial) { CPVT_SectionInfo secinfo; if (m_bRichText) { - secinfo.pSecProps = new CPVT_SecProps(0.0f, 0.0f, 0); - secinfo.pWordProps = new CPVT_WordProps( - GetDefaultFontIndex(), kDefaultFontSize, 0, ScriptType::Normal, 0); + secinfo.pSecProps.reset(new CPVT_SecProps(0.0f, 0.0f, 0)); + secinfo.pWordProps.reset(new CPVT_WordProps( + GetDefaultFontIndex(), kDefaultFontSize, 0, ScriptType::Normal, 0)); } CPVT_WordPlace place; place.nSecIndex = 0; @@ -333,9 +333,9 @@ CPVT_WordPlace CPDF_VariableText::InsertSection( CPVT_SectionInfo secinfo; if (m_bRichText) { if (pSecProps) - secinfo.pSecProps = new CPVT_SecProps(*pSecProps); + secinfo.pSecProps.reset(new CPVT_SecProps(*pSecProps)); if (pWordProps) - secinfo.pWordProps = new CPVT_WordProps(*pWordProps); + secinfo.pWordProps.reset(new CPVT_WordProps(*pWordProps)); } AddSection(NewPlace, secinfo); newplace = NewPlace; @@ -426,9 +426,9 @@ void CPDF_VariableText::SetText(const FX_WCHAR* text, CPVT_SectionInfo secinfo; if (m_bRichText) { if (pSecProps) - secinfo.pSecProps = new CPVT_SecProps(*pSecProps); + secinfo.pSecProps.reset(new CPVT_SecProps(*pSecProps)); if (pWordProps) - secinfo.pWordProps = new CPVT_WordProps(*pWordProps); + secinfo.pWordProps.reset(new CPVT_WordProps(*pWordProps)); } if (CSection* pSection = m_SectionArray.GetAt(0)) pSection->m_SecInfo = secinfo; diff --git a/core/fpdfdoc/cpvt_sectioninfo.cpp b/core/fpdfdoc/cpvt_sectioninfo.cpp new file mode 100644 index 0000000000..eb5c1bb809 --- /dev/null +++ b/core/fpdfdoc/cpvt_sectioninfo.cpp @@ -0,0 +1,31 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fpdfdoc/cpvt_sectioninfo.h" + +CPVT_SectionInfo::CPVT_SectionInfo() : rcSection(), nTotalLine(0) {} + +CPVT_SectionInfo::~CPVT_SectionInfo() {} + +CPVT_SectionInfo::CPVT_SectionInfo(const CPVT_SectionInfo& other) { + operator=(other); +} + +void CPVT_SectionInfo::operator=(const CPVT_SectionInfo& other) { + if (this == &other) + return; + + rcSection = other.rcSection; + nTotalLine = other.nTotalLine; + if (other.pSecProps) + pSecProps.reset(new CPVT_SecProps(*other.pSecProps)); + else + pSecProps.reset(); + if (other.pWordProps) + pWordProps.reset(new CPVT_WordProps(*other.pWordProps)); + else + pWordProps.reset(); +} diff --git a/core/fpdfdoc/cpvt_sectioninfo.h b/core/fpdfdoc/cpvt_sectioninfo.h index e466ae5006..9d9b99fc27 100644 --- a/core/fpdfdoc/cpvt_sectioninfo.h +++ b/core/fpdfdoc/cpvt_sectioninfo.h @@ -7,48 +7,23 @@ #ifndef CORE_FPDFDOC_CPVT_SECTIONINFO_H_ #define CORE_FPDFDOC_CPVT_SECTIONINFO_H_ +#include + #include "core/fpdfdoc/cpvt_floatrect.h" #include "core/fpdfdoc/include/cpvt_secprops.h" #include "core/fpdfdoc/include/cpvt_wordprops.h" struct CPVT_SectionInfo { - CPVT_SectionInfo() - : rcSection(), nTotalLine(0), pSecProps(nullptr), pWordProps(nullptr) {} - - ~CPVT_SectionInfo() { - delete pSecProps; - delete pWordProps; - } - - CPVT_SectionInfo(const CPVT_SectionInfo& other) - : rcSection(), nTotalLine(0), pSecProps(nullptr), pWordProps(nullptr) { - operator=(other); - } - - void operator=(const CPVT_SectionInfo& other) { - if (this == &other) - return; - - rcSection = other.rcSection; - nTotalLine = other.nTotalLine; - if (other.pSecProps) { - if (pSecProps) - *pSecProps = *other.pSecProps; - else - pSecProps = new CPVT_SecProps(*other.pSecProps); - } - if (other.pWordProps) { - if (pWordProps) - *pWordProps = *other.pWordProps; - else - pWordProps = new CPVT_WordProps(*other.pWordProps); - } - } + CPVT_SectionInfo(); + CPVT_SectionInfo(const CPVT_SectionInfo& other); + ~CPVT_SectionInfo(); + + void operator=(const CPVT_SectionInfo& other); CPVT_FloatRect rcSection; int32_t nTotalLine; - CPVT_SecProps* pSecProps; - CPVT_WordProps* pWordProps; + std::unique_ptr pSecProps; + std::unique_ptr pWordProps; }; #endif // CORE_FPDFDOC_CPVT_SECTIONINFO_H_ diff --git a/core/fpdfdoc/cpvt_wordinfo.cpp b/core/fpdfdoc/cpvt_wordinfo.cpp new file mode 100644 index 0000000000..2303f30032 --- /dev/null +++ b/core/fpdfdoc/cpvt_wordinfo.cpp @@ -0,0 +1,54 @@ +// Copyright 2016 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fpdfdoc/cpvt_wordinfo.h" + +CPVT_WordInfo::CPVT_WordInfo() + : Word(0), + nCharset(FXFONT_ANSI_CHARSET), + fWordX(0.0f), + fWordY(0.0f), + fWordTail(0.0f), + nFontIndex(-1) {} + +CPVT_WordInfo::CPVT_WordInfo(uint16_t word, + int32_t charset, + int32_t fontIndex, + CPVT_WordProps* pProps) + : Word(word), + nCharset(charset), + fWordX(0.0f), + fWordY(0.0f), + fWordTail(0.0f), + nFontIndex(fontIndex) {} + +CPVT_WordInfo::CPVT_WordInfo(const CPVT_WordInfo& word) + : Word(0), + nCharset(FXFONT_ANSI_CHARSET), + fWordX(0.0f), + fWordY(0.0f), + fWordTail(0.0f), + nFontIndex(-1) { + operator=(word); +} + +CPVT_WordInfo::~CPVT_WordInfo() {} + +void CPVT_WordInfo::operator=(const CPVT_WordInfo& word) { + if (this == &word) + return; + + Word = word.Word; + nCharset = word.nCharset; + nFontIndex = word.nFontIndex; + fWordX = word.fWordX; + fWordY = word.fWordY; + fWordTail = word.fWordTail; + if (word.pWordProps) + pWordProps.reset(new CPVT_WordProps(*word.pWordProps)); + else + pWordProps.reset(); +} diff --git a/core/fpdfdoc/cpvt_wordinfo.h b/core/fpdfdoc/cpvt_wordinfo.h index 1ab0df21af..c690e2aa7b 100644 --- a/core/fpdfdoc/cpvt_wordinfo.h +++ b/core/fpdfdoc/cpvt_wordinfo.h @@ -7,58 +7,21 @@ #ifndef CORE_FPDFDOC_CPVT_WORDINFO_H_ #define CORE_FPDFDOC_CPVT_WORDINFO_H_ +#include + #include "core/fpdfdoc/include/cpvt_wordprops.h" #include "core/fxcrt/include/fx_system.h" struct CPVT_WordInfo { - CPVT_WordInfo() - : Word(0), - nCharset(FXFONT_ANSI_CHARSET), - fWordX(0.0f), - fWordY(0.0f), - fWordTail(0.0f), - nFontIndex(-1), - pWordProps(nullptr) {} - + CPVT_WordInfo(); CPVT_WordInfo(uint16_t word, int32_t charset, int32_t fontIndex, - CPVT_WordProps* pProps) - : Word(word), - nCharset(charset), - fWordX(0.0f), - fWordY(0.0f), - fWordTail(0.0f), - nFontIndex(fontIndex), - pWordProps(pProps) {} - - CPVT_WordInfo(const CPVT_WordInfo& word) - : Word(0), - nCharset(FXFONT_ANSI_CHARSET), - fWordX(0.0f), - fWordY(0.0f), - fWordTail(0.0f), - nFontIndex(-1), - pWordProps(nullptr) { - operator=(word); - } - - ~CPVT_WordInfo() { delete pWordProps; } - - void operator=(const CPVT_WordInfo& word) { - if (this == &word) - return; + CPVT_WordProps* pProps); + CPVT_WordInfo(const CPVT_WordInfo& word); + ~CPVT_WordInfo(); - Word = word.Word; - nCharset = word.nCharset; - nFontIndex = word.nFontIndex; - if (word.pWordProps) { - if (pWordProps) - *pWordProps = *word.pWordProps; - else - pWordProps = new CPVT_WordProps(*word.pWordProps); - } - } + void operator=(const CPVT_WordInfo& word); uint16_t Word; int32_t nCharset; @@ -66,7 +29,7 @@ struct CPVT_WordInfo { FX_FLOAT fWordY; FX_FLOAT fWordTail; int32_t nFontIndex; - CPVT_WordProps* pWordProps; + std::unique_ptr pWordProps; }; #endif // CORE_FPDFDOC_CPVT_WORDINFO_H_ diff --git a/core/fpdfdoc/doc_annot.cpp b/core/fpdfdoc/doc_annot.cpp index 9b2f9e505f..fe17e9c811 100644 --- a/core/fpdfdoc/doc_annot.cpp +++ b/core/fpdfdoc/doc_annot.cpp @@ -41,7 +41,8 @@ CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage) pAnnots->RemoveAt(i + 1); pDict = pAnnots->GetDictAt(i); } - m_AnnotList.push_back(new CPDF_Annot(pDict, this)); + m_AnnotList.push_back( + std::unique_ptr(new CPDF_Annot(pDict, this))); if (bRegenerateAP && pDict->GetStringBy("Subtype") == "Widget" && CPDF_InterForm::UpdatingAPEnabled()) { FPDF_GenerateAP(m_pDocument, pDict); @@ -49,10 +50,7 @@ CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage) } } -CPDF_AnnotList::~CPDF_AnnotList() { - for (CPDF_Annot* annot : m_AnnotList) - delete annot; -} +CPDF_AnnotList::~CPDF_AnnotList() {} void CPDF_AnnotList::DisplayPass(CPDF_Page* pPage, CFX_RenderDevice* pDevice, @@ -62,7 +60,7 @@ void CPDF_AnnotList::DisplayPass(CPDF_Page* pPage, FX_BOOL bWidgetPass, CPDF_RenderOptions* pOptions, FX_RECT* clip_rect) { - for (CPDF_Annot* pAnnot : m_AnnotList) { + for (const auto& pAnnot : m_AnnotList) { bool bWidget = pAnnot->GetSubType() == "Widget"; if ((bWidgetPass && !bWidget) || (!bWidgetPass && bWidget)) continue; diff --git a/core/fpdfdoc/include/fpdf_doc.h b/core/fpdfdoc/include/fpdf_doc.h index afa5a30b18..be52a1b829 100644 --- a/core/fpdfdoc/include/fpdf_doc.h +++ b/core/fpdfdoc/include/fpdf_doc.h @@ -374,8 +374,10 @@ class CPDF_AnnotList { CPDF_RenderOptions* pOptions, FX_RECT* pClipRect); size_t Count() const { return m_AnnotList.size(); } - CPDF_Annot* GetAt(size_t index) const { return m_AnnotList[index]; } - const std::vector& All() const { return m_AnnotList; } + CPDF_Annot* GetAt(size_t index) const { return m_AnnotList[index].get(); } + const std::vector>& All() const { + return m_AnnotList; + } CPDF_Document* GetDocument() const { return m_pDocument; } protected: @@ -389,7 +391,7 @@ class CPDF_AnnotList { FX_RECT* clip_rect); CPDF_Document* const m_pDocument; - std::vector m_AnnotList; + std::vector> m_AnnotList; }; #define COLORTYPE_TRANSPARENT 0 -- cgit v1.2.3