]> git.lyx.org Git - features.git/commitdiff
Remove unneccessary uses of dynamic_cast from the code.
authorVincent van Ravesteijn <vfr@lyx.org>
Tue, 26 Oct 2010 15:03:51 +0000 (15:03 +0000)
committerVincent van Ravesteijn <vfr@lyx.org>
Tue, 26 Oct 2010 15:03:51 +0000 (15:03 +0000)
A dynamic_cast is necessary when:

- the object to be casted is from an external library because we can't add Qxxx::asXxxx() to Qt e.g.:
    * QAbstractListModel to GuiIdListModel,
    * QValidator to PathValidator,
    * QWidget to TabWorkArea,
    * QWidget to GuiWorkArea;

- the object is to be casted from an interface to the implementing class, because the Interface does not know by whom it is implemented:
    * ProgressInterface to GuiProgress,
    * Application to GuiApplication.

A dynamic_cast can be replaced by:
  - already existing as***Inset() functions, e.g.:
    * asHullInset(),
    * asInsetMath()->asMacro(),
    * asInsetText();

  - a static_cast when we are sure this can't go wrong, e.g.:
    * we are sure that CellData::inset->clone() is an InsetTableCell,
    * in cases where we explicitly check it->lyxCode().

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@35855 a592a061-630c-0410-9148-cb99ea01b6c8

src/Buffer.cpp
src/CutAndPaste.cpp
src/DocIterator.cpp
src/Text3.cpp
src/insets/InsetTabular.cpp
src/mathed/InsetMathNest.cpp
src/mathed/MathMacro.cpp

index ad14807e10f4bc30a11d27b554f94eab00274dd4..40e24b16d794473da2b1761bfdab6eb29d61acba 100644 (file)
@@ -2085,7 +2085,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                bool success = false;
                for (; it != end; ++it) {
                        if (it->lyxCode() == BRANCH_CODE) {
-                               InsetBranch & ins = dynamic_cast<InsetBranch &>(*it);
+                               InsetBranch & ins = static_cast<InsetBranch &>(*it);
                                if (ins.branch() == oldname) {
                                        undo().recordUndo(it);
                                        ins.rename(newname);
@@ -2830,7 +2830,7 @@ void Buffer::getUsedBranches(std::list<docstring> & result, bool const from_mast
        InsetIterator const end = inset_iterator_end(inset());
        for (; it != end; ++it) {
                if (it->lyxCode() == BRANCH_CODE) {
-                       InsetBranch & br = dynamic_cast<InsetBranch &>(*it);
+                       InsetBranch & br = static_cast<InsetBranch &>(*it);
                        docstring const name = br.branch();
                        if (!from_master && !params().branchlist().find(name))
                                result.push_back(name);
index e716cc3a817ca35306ab119f33549b0741904461..3e8661c5fe3637ca8827a4523b825134548178b9 100644 (file)
@@ -272,7 +272,7 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist,
 
                case LABEL_CODE: {
                        // check for duplicates
-                       InsetLabel & lab = dynamic_cast<InsetLabel &>(*it);
+                       InsetLabel & lab = static_cast<InsetLabel &>(*it);
                        docstring const oldname = lab.getParam("name");
                        lab.updateCommand(oldname, false);
                        // We need to update the buffer reference cache.
@@ -310,7 +310,7 @@ pasteSelectionHelper(Cursor & cur, ParagraphList const & parlist,
 
                case BIBITEM_CODE: {
                        // check for duplicates
-                       InsetBibitem & bib = dynamic_cast<InsetBibitem &>(*it);
+                       InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
                        docstring const oldkey = bib.getParam("key");
                        bib.updateCommand(oldkey, false);
                        // We need to update the buffer reference cache.
index f396ef5869f931b2c92454baa49f55a131106402..c0df50b446f8fc61f2b84eb5f37c1fdf22842602 100644 (file)
@@ -91,7 +91,7 @@ DocIterator DocIterator::clone(Buffer * buffer) const
 
 bool DocIterator::inRegexped() const
 {
-       InsetMathHull * i = dynamic_cast<InsetMathHull *>(inset().asInsetMath());
+       InsetMathHull * i = inset().asInsetMath()->asHullInset();
        return i && i->getType() == hullRegexp;
 }
 
index c132531bb7c5a738f54475d90b3ca584364823e7..9b4c6d9aff8f9c9f2aa418de76db4f3ce8aabfee 100644 (file)
@@ -268,10 +268,10 @@ static bool doInsertInset(Cursor & cur, Text * text,
        cur.buffer()->errors("Paste");
        cur.clearSelection(); // bug 393
        cur.finishUndo();
-       InsetText * insetText = dynamic_cast<InsetText *>(inset);
-       if (insetText) {
-               insetText->fixParagraphsFont();
-               if (!insetText->allowMultiPar() || cur.lastpit() == 0) {
+       InsetText * inset_text = inset->asInsetText();
+       if (inset_text) {
+               inset_text->fixParagraphsFont();
+               if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
                        // reset first par to default
                        cur.text()->paragraphs().begin()
                                ->setPlainOrDefaultLayout(bparams.documentClass());
index 0c1b2e280c354b3f6df21aae46e6284c6cf801e6..00a155d91843367525ac2cf49b2b91c41164f9bb 100644 (file)
@@ -580,7 +580,7 @@ Tabular::CellData::CellData(CellData const & cs)
          rotate(cs.rotate),
          align_special(cs.align_special),
          p_width(cs.p_width),
-         inset(dynamic_cast<InsetTableCell *>(cs.inset->clone()))
+         inset(static_cast<InsetTableCell *>(cs.inset->clone()))
 {
 }
 
index 0b11be1308614340f3c0c5aec9f6305c16832f9b..d0c2397a2e47b5fdb1e020b94421fb14cff8ddd9 100644 (file)
@@ -1011,7 +1011,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_REGEXP_MODE: {
-               InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
+               InsetMathHull * i = cur.inset().asInsetMath()->asHullInset();           
                if (i && i->getType() == hullRegexp) {
                        cur.message(_("Already in regular expression mode"));
                        break;
index 6a232716dce29bbd1a8cce64d9e7a257f406c2ee..da398683b029062d5eb66e79c58a5f3803de1c71 100644 (file)
@@ -164,7 +164,7 @@ bool MathMacro::editMode(BufferView const * bv) const {
                        // look if there is no other macro in edit mode above
                        ++i;
                        for (; i != cur.depth(); ++i) {
-                               MathMacro const * macro = dynamic_cast<MathMacro const *>(&cur[i].inset());
+                               MathMacro const * macro = cur[i].asInsetMath()->asMacro();
                                if (macro && macro->displayMode() == DISPLAY_NORMAL)
                                        return false;
                        }