From 07924ac300f68eaf4825951af6a6ac3c14d6edd6 Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Tue, 26 Oct 2010 16:45:21 +0000 Subject: [PATCH] Remove some unnecessary static_casts and add two FIXMEs about casts that don't look safe. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35856 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Buffer.cpp | 26 +++++++++++--------------- src/BufferView.cpp | 6 ++++++ src/Cursor.cpp | 2 +- src/CutAndPaste.cpp | 27 ++++++++++++--------------- src/DocIterator.cpp | 4 ++-- src/Paragraph.cpp | 9 ++++----- src/insets/InsetLabel.cpp | 8 +++----- src/mathed/MathAtom.cpp | 4 ++-- src/mathed/MathAtom.h | 2 +- 9 files changed, 42 insertions(+), 46 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 40e24b16d7..6115a40762 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -1707,15 +1707,13 @@ void Buffer::updateBibfilesCache(UpdateScope scope) const d->bibfiles_cache_.clear(); for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { if (it->lyxCode() == BIBTEX_CODE) { - InsetBibtex const & inset = - static_cast(*it); + InsetBibtex const & inset = static_cast(*it); support::FileNameList const bibfiles = inset.getBibFiles(); d->bibfiles_cache_.insert(d->bibfiles_cache_.end(), bibfiles.begin(), bibfiles.end()); } else if (it->lyxCode() == INCLUDE_CODE) { - InsetInclude & inset = - static_cast(*it); + InsetInclude & inset = static_cast(*it); Buffer const * const incbuf = inset.getChildBuffer(); if (!incbuf) continue; @@ -2762,20 +2760,16 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope) continue; } - if (doing_export && iit->inset->asInsetMath()) { - InsetMath * im = static_cast(iit->inset); - if (im->asHullInset()) { - InsetMathHull * hull = static_cast(im); - hull->recordLocation(it); - } - } + InsetMath * im = iit->inset->asInsetMath(); + if (doing_export && im) + im->asHullInset()->recordLocation(it); if (iit->inset->lyxCode() != MATHMACRO_CODE) continue; // get macro data MathMacroTemplate & macroTemplate = - static_cast(*iit->inset); + *iit->inset->asInsetMath()->asMacroTemplate(); MacroContext mc(owner_, it); macroTemplate.updateToContext(mc); @@ -2999,10 +2993,12 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to, for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) { if (it->lyxCode() == code) { - InsetCommand & inset = static_cast(*it); - docstring const oldValue = inset.getParam(paramName); + InsetCommand * inset = it->asInsetCommand(); + if (!inset) + continue; + docstring const oldValue = inset->getParam(paramName); if (oldValue == from) - inset.setParam(paramName, to); + inset->setParam(paramName, to); } } } diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 338a55aa5c..4c57dfd479 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -121,6 +121,9 @@ bool findNextInset(DocIterator & dit, vector const & codes, if (inset && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() && (contents.empty() || + //FIXME: This static_cast seems very dangerous. Does this + // mean that if contents is not empty, we must only be + // looking for InsetCommand's ?? static_cast(inset)->getFirstNonOptParam() == contents)) { dit = tmpdit; return true; @@ -146,6 +149,9 @@ bool findInset(DocIterator & dit, vector const & codes, Inset const * inset = tmpdit.nextInset(); if (inset && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) { + //FIXME: This static_cast seems very dangerous. Does this + // mean that if contents is not empty, we must only be + // looking for InsetCommand's ?? contents = static_cast(inset)->getFirstNonOptParam(); } } diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 45745798da..f6d10cbc97 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1315,7 +1315,7 @@ void Cursor::insert(Inset * inset0) { LASSERT(inset0, /**/); if (inMathed()) - insert(MathAtom(inset0)); + insert(MathAtom(inset0->asInsetMath())); else { text()->insertInset(*this, inset0); inset0->setBuffer(bv_->buffer()); diff --git a/src/CutAndPaste.cpp b/src/CutAndPaste.cpp index 3e8661c5fe..364e1e9d03 100644 --- a/src/CutAndPaste.cpp +++ b/src/CutAndPaste.cpp @@ -234,8 +234,8 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist, case MATH_HULL_CODE: { // check for equation labels and resolve duplicates - InsetMathHull & ins = static_cast(*it); - std::vector labels = ins.getLabels(); + InsetMathHull * ins = it->asInsetMath()->asHullInset(); + std::vector labels = ins->getLabels(); for (size_t i = 0; i != labels.size(); ++i) { if (!labels[i]) continue; @@ -251,19 +251,17 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist, for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) { if (itt->lyxCode() == REF_CODE) { - InsetCommand & ref = - static_cast(*itt); - if (ref.getParam("reference") == oldname) - ref.setParam("reference", newname); + InsetCommand * ref = itt->asInsetCommand(); + if (ref->getParam("reference") == oldname) + ref->setParam("reference", newname); } else if (itt->lyxCode() == MATH_REF_CODE) { - InsetMathHull & mi = - static_cast(*itt); + InsetMathHull * mi = itt->asInsetMath()->asHullInset(); // this is necessary to prevent an uninitialized // buffer when the RefInset is in a MathBox. // FIXME audit setBuffer calls - mi.setBuffer(const_cast(buffer)); - if (mi.asRefInset()->getTarget() == oldname) - mi.asRefInset()->changeTarget(newname); + mi->setBuffer(const_cast(buffer)); + if (mi->asRefInset()->getTarget() == oldname) + mi->asRefInset()->changeTarget(newname); } } } @@ -322,10 +320,9 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist, for (InsetIterator itt = inset_iterator_begin(in); itt != i_end; ++itt) { if (itt->lyxCode() == CITE_CODE) { - InsetCommand & ref = - static_cast(*itt); - if (ref.getParam("key") == oldkey) - ref.setParam("key", newkey); + InsetCommand * ref = itt->asInsetCommand(); + if (ref->getParam("key") == oldkey) + ref->setParam("key", newkey); } } break; diff --git a/src/DocIterator.cpp b/src/DocIterator.cpp index c0df50b446..2c244ae2e1 100644 --- a/src/DocIterator.cpp +++ b/src/DocIterator.cpp @@ -142,8 +142,8 @@ Inset * DocIterator::realInset() const LASSERT(inTexted(), /**/); // if we are in a tabular, we need the cell if (inset().lyxCode() == TABULAR_CODE) { - InsetTabular & tabular = static_cast(inset()); - return tabular.cell(idx()).get(); + InsetTabular * tabular = inset().asInsetTabular(); + return tabular->cell(idx()).get(); } return &inset(); } diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index d96ddaeefe..1e90a95cb1 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -3096,8 +3096,8 @@ int Paragraph::checkBiblio(Buffer const & buffer) InsetList::iterator end = d->insetlist_.end(); for (; it != end; ++it) if (it->inset->lyxCode() == BIBITEM_CODE - && it->pos > 0) { - InsetBibitem * olditem = static_cast(it->inset); + && it->pos > 0) { + InsetCommand * olditem = it->inset->asInsetCommand(); oldkey = olditem->getParam("key"); oldlabel = olditem->getParam("label"); erasedInsetPosition = it->pos; @@ -3113,8 +3113,7 @@ int Paragraph::checkBiblio(Buffer const & buffer) // There was an InsetBibitem at the beginning and we did have to // erase one. So we give its properties to the beginning inset. if (hasbibitem) { - InsetBibitem * inset = - static_cast(d->insetlist_.begin()->inset); + InsetCommand * inset = d->insetlist_.begin()->inset->asInsetCommand(); if (!oldkey.empty()) inset->setParam("key", oldkey); inset->setParam("label", oldlabel); @@ -3129,7 +3128,7 @@ int Paragraph::checkBiblio(Buffer const & buffer) if (!oldkey.empty()) inset->setParam("key", oldkey); inset->setParam("label", oldlabel); - insertInset(0, static_cast(inset), + insertInset(0, inset, Change(track_changes ? Change::INSERTED : Change::UNCHANGED)); return 1; diff --git a/src/insets/InsetLabel.cpp b/src/insets/InsetLabel.cpp index 5217dffd1a..ce97f3a84c 100644 --- a/src/insets/InsetLabel.cpp +++ b/src/insets/InsetLabel.cpp @@ -86,12 +86,10 @@ void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs) for (; it != end; ++it) { buffer().undo().recordUndo(it->second); if (it->first->lyxCode() == MATH_REF_CODE) { - InsetMathHull * mi = - static_cast(it->first); + InsetMathHull * mi = it->first->asInsetMath()->asHullInset(); mi->asRefInset()->changeTarget(label); } else { - InsetCommand * ref = - static_cast(it->first); + InsetCommand * ref = it->first->asInsetCommand(); ref->setParam("reference", label); } } @@ -160,7 +158,7 @@ void InsetLabel::addToToc(DocIterator const & cpit) DocIterator const ref_pit(it->second); if (it->first->lyxCode() == MATH_REF_CODE) toc.push_back(TocItem(ref_pit, 1, - static_cast(it->first)->asRefInset() + it->first->asInsetMath()->asHullInset()->asRefInset() ->screenLabel())); else toc.push_back(TocItem(ref_pit, 1, diff --git a/src/mathed/MathAtom.cpp b/src/mathed/MathAtom.cpp index e8b799db53..a8483ea212 100644 --- a/src/mathed/MathAtom.cpp +++ b/src/mathed/MathAtom.cpp @@ -23,8 +23,8 @@ MathAtom::MathAtom() {} -MathAtom::MathAtom(Inset * p) - : nucleus_(static_cast(p)) +MathAtom::MathAtom(InsetMath * p) + : nucleus_(p) {} diff --git a/src/mathed/MathAtom.h b/src/mathed/MathAtom.h index 3b3201db0c..b11d75da18 100644 --- a/src/mathed/MathAtom.h +++ b/src/mathed/MathAtom.h @@ -51,7 +51,7 @@ public: /// std::containers MathAtom(); /// the "real constructor" - explicit MathAtom(Inset * p); + explicit MathAtom(InsetMath * p); /// copy constructor, invokes nucleus_->clone() MathAtom(MathAtom const &); /// we really need to clean up