]> git.lyx.org Git - features.git/commitdiff
Simplify Changers
authorYuriy Skalko <yuriy.skalko@gmail.com>
Thu, 12 Nov 2020 12:09:36 +0000 (14:09 +0200)
committerYuriy Skalko <yuriy.skalko@gmail.com>
Thu, 12 Nov 2020 12:22:00 +0000 (14:22 +0200)
16 files changed:
src/FontInfo.cpp
src/MetricsInfo.cpp
src/TextMetrics.cpp
src/insets/InsetCollapsible.cpp
src/insets/InsetText.cpp
src/mathed/InsetMathChar.cpp
src/mathed/InsetMathDiagram.cpp
src/mathed/InsetMathFontOld.cpp
src/mathed/InsetMathFrac.cpp
src/mathed/InsetMathHull.cpp
src/mathed/InsetMathMacro.cpp
src/mathed/MathStream.cpp
src/mathed/MathSupport.cpp
src/support/Changer.h
src/support/Makefile.am
src/support/RefChanger.h [deleted file]

index 967771373a08417dd6ecb93b868c21ec3aac4200..9f21bcc3d38e9c070c7433cd730d6d3b9fab93da 100644 (file)
@@ -25,7 +25,6 @@
 #include "support/docstring.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
 
 #include <algorithm>
 #include <ostream>
@@ -327,19 +326,19 @@ FontInfo & FontInfo::realize(FontInfo const & tmplt)
 
 Changer FontInfo::changeColor(ColorCode const color)
 {
-       return make_change(color_, color);
+       return changeVar(color_, color);
 }
 
 
 Changer FontInfo::changeShape(FontShape const shape)
 {
-       return make_change(shape_, shape);
+       return changeVar(shape_, shape);
 }
 
 
 Changer FontInfo::changeStyle(MathStyle const new_style)
 {
-       return make_change(style_, new_style);
+       return changeVar(style_, new_style);
 }
 
 
@@ -347,7 +346,7 @@ Changer FontInfo::change(FontInfo font, bool realize)
 {
        if (realize)
                font.realize(*this);
-       return make_change(*this, font);
+       return changeVar(*this, font);
 }
 
 
index 5aeb9a29801be0e5e12bd60d48de10fca494bf0a..809c0cec68997f7f7b81533c7715ba4ee6a5f6dd 100644 (file)
@@ -21,8 +21,6 @@
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
-#include "support/RefChanger.h"
-
 using namespace std;
 
 
@@ -83,16 +81,16 @@ Changer MetricsBase::changeEnsureMath(Inset::mode_type mode)
 {
        switch (mode) {
        case Inset::UNDECIDED_MODE:
-               return Changer();
+               return noChange();
        case Inset::TEXT_MODE:
-               return isMathFont(fontname) ? changeFontSet("textnormal") : Changer();
+               return isMathFont(fontname) ? changeFontSet("textnormal") : noChange();
        case Inset::MATH_MODE:
                // FIXME:
                //   \textit{\ensuremath{\text{a}}}
                // should appear in italics
-               return isTextFont(fontname) ? changeFontSet("mathnormal"): Changer();
+               return isTextFont(fontname) ? changeFontSet("mathnormal"): noChange();
        }
-       return Changer();
+       return noChange();
 }
 
 
@@ -196,10 +194,10 @@ Changer MetricsBase::changeScript()
                return font.changeStyle(SCRIPTSCRIPT_STYLE);
        case INHERIT_STYLE:
        case IGNORE_STYLE:
-               return Changer();
+               return noChange();
        }
        //remove Warning
-       return Changer();
+       return noChange();
 }
 
 
@@ -215,10 +213,10 @@ Changer MetricsBase::changeFrac()
                return font.changeStyle(SCRIPTSCRIPT_STYLE);
        case INHERIT_STYLE:
        case IGNORE_STYLE:
-               return Changer();
+               return noChange();
        }
        //remove Warning
-       return Changer();
+       return noChange();
 }
 
 
@@ -227,7 +225,7 @@ Changer MetricsBase::changeArray(bool small)
        if (small)
                return font.changeStyle(SCRIPT_STYLE);
        return (font.style() == DISPLAY_STYLE) ? font.changeStyle(TEXT_STYLE)
-               : Changer();
+               : noChange();
 }
 
 
index 5cd7a603a05762228cb123d2e49013e8126dc378..ddc84cfd442d5157889dd57081eecb403f56be2c 100644 (file)
@@ -43,7 +43,7 @@
 
 #include "support/debug.h"
 #include "support/lassert.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 
 #include <stdlib.h>
 #include <cmath>
@@ -1812,8 +1812,8 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
                return;
        size_t const nrows = pm.rows().size();
        // Remember left and right margin for drawing math numbers
-       Changer changeleft = make_change(pi.leftx, x + leftMargin(pit));
-       Changer changeright = make_change(pi.rightx, x + width() - rightMargin(pit));
+       Changer changeleft = changeVar(pi.leftx, x + leftMargin(pit));
+       Changer changeright = changeVar(pi.rightx, x + width() - rightMargin(pit));
 
        // Use fast lane in nodraw stage.
        if (pi.pain.isNull()) {
index c118545002cc11875d85df1f0c22812486d54151..62a830b06493531c57d9da05ab242f30a0d1c6ef 100644 (file)
@@ -39,7 +39,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 #include "support/TempFile.h"
 
 using namespace std;
@@ -314,8 +314,8 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                // Do not draw the cue for INSERTED -- it is already in the button and
                // that's enough.
                Changer cdummy = (pi.change.type == Change::INSERTED)
-                       ? make_change(pi.change, Change())
-                       : Changer();
+                       ? changeVar(pi.change, Change())
+                       : noChange();
                InsetText::draw(pi, textx, texty);
                break;
        }
@@ -333,7 +333,7 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                // We will take care of the frame and the change tracking cue
                // ourselves, below.
                {
-                       Changer cdummy = make_change(pi.change, Change());
+                       Changer cdummy = changeVar(pi.change, Change());
                        const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
                        InsetText::draw(pi, textx, texty);
                        const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
index ec223fa83a13c5350979be155ef4d1dc28ec5d29..3305eddf96c591f39b02ba5a8e78f3d0480979ba 100644 (file)
@@ -61,7 +61,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 
 #include <algorithm>
 #include <stack>
@@ -229,10 +229,10 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
                                pi.backgroundColor(this));
 
        {
-               Changer dummy = make_change(pi.background_color,
+               Changer dummy = changeVar(pi.background_color,
                                            pi.backgroundColor(this, false));
                // The change tracking cue must not be inherited
-               Changer dummy2 = make_change(pi.change, Change());
+               Changer dummy2 = changeVar(pi.change, Change());
                tm.draw(pi, x + leftOffset(pi.base.bv), y);
        }
 
index aa1ddf8c5b7657c02393f4c1906e14a1ad29c730..decfac2d59ec8c073c0c16846c7be5dc877240ba 100644 (file)
@@ -121,7 +121,7 @@ void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
        } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
                Changer dummy1 = mi.base.changeFontSet("mathnormal");
                Changer dummy2 = Encodings::isMathAlpha(char_)
-                               ? Changer()
+                               ? noChange()
                                : mi.base.font.changeShape(UP_SHAPE);
                dim = theFontMetrics(mi.base.font).dimension(char_);
                kerning_ = -mathed_char_kerning(mi.base.font, char_);
@@ -166,7 +166,7 @@ void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
                } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
                        Changer dummy1 = pi.base.changeFontSet("mathnormal");
                        Changer dummy2 = Encodings::isMathAlpha(char_)
-                                       ? Changer()
+                                       ? noChange()
                                        : pi.base.font.changeShape(UP_SHAPE);
                        pi.draw(x, y, char_);
                        return;
index 4a7ea3c3b822d9221b6f0629bcdfff6229d2dcec..baa20a5fa7cfef8ab5c2dba9d37ff48788bc450c 100644 (file)
@@ -51,7 +51,7 @@ void InsetMathDiagram::metrics(MetricsInfo & mi, Dimension & dim) const
        Changer dummy2 = mi.base.changeEnsureMath();
        FontInfo & f = mi.base.font;
        Changer dummy = (f.style() == DISPLAY_STYLE) ? f.changeStyle(TEXT_STYLE)
-               : Changer();
+               : noChange();
        InsetMathGrid::metrics(mi, dim);
 }
 
@@ -61,7 +61,7 @@ void InsetMathDiagram::draw(PainterInfo & pi, int x, int y) const
        Changer dummy2 = pi.base.changeEnsureMath();
        FontInfo & f = pi.base.font;
        Changer dummy = (f.style() == DISPLAY_STYLE) ? f.changeStyle(TEXT_STYLE)
-               : Changer();
+               : noChange();
        InsetMathGrid::draw(pi, x, y);
 }
 
index cde388840cd9b755df53199b5575579e4dbfc708..383efaf30fe736bd47eab043c6a4b6079d33093d 100644 (file)
@@ -60,7 +60,7 @@ void InsetMathFontOld::metrics(MetricsInfo & mi, Dimension & dim) const
        bool really_change_font = fontname != "textcal";
 
        Changer dummy = really_change_font ? mi.base.changeFontSet(fontname)
-               : Changer();
+               : noChange();
        cell(0).metrics(mi, dim);
 }
 
@@ -77,7 +77,7 @@ void InsetMathFontOld::draw(PainterInfo & pi, int x, int y) const
        bool really_change_font = fontname != "textcal";
 
        Changer dummy = really_change_font ? pi.base.changeFontSet(fontname)
-               : Changer();
+               : noChange();
        cell(0).draw(pi, x, y);
 }
 
index 03125e99ca34dc85222f32060e2060c2dcfc7664..4870743b48dd819bc52ebf62d28139050118c474 100644 (file)
@@ -215,7 +215,7 @@ void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
                        dim.des = dim2.des;
                }
                Changer dummy = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE)
-                       : Changer();
+                       : noChange();
                Changer dummy2 = mi.base.changeScript();
                if (latexkeys const * slash = slash_symbol()) {
                        Dimension dimslash;
@@ -297,7 +297,7 @@ void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
                        xx += cell(2).dimension(*pi.base.bv).wid + 4;
                }
                Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE)
-                       : Changer();
+                       : noChange();
                // nice fraction
                Changer dummy2 = pi.base.changeScript();
                cell(0).draw(pi, xx + 1, y - dy);
index 43f49584afcc236866c5540356343576420b3085..6c0d2cffb6e2b0b78ffaf22ee90c0ea2c02395b4 100644 (file)
@@ -65,7 +65,7 @@
 #include "support/filetools.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 
 #include <sstream>
 
@@ -616,8 +616,8 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
        if (previewState(bv)) {
                // Do not draw change tracking cue if taken care of by RowPainter
                // already.
-               Changer dummy = !canPaintChange(*bv) ? make_change(pi.change, Change())
-                       : Changer();
+               Changer dummy = !canPaintChange(*bv) ? changeVar(pi.change, Change())
+                       : noChange();
                if (previewTooSmall(dim)) {
                        // we have an extra frame
                        preview_->draw(pi, x + ERROR_FRAME_WIDTH, y);
@@ -635,7 +635,7 @@ void InsetMathHull::draw(PainterInfo & pi, int x, int y) const
                                ? Color_selectiontext : standardColor();
        bool const really_change_color = pi.base.font.color() == Color_none;
        Changer dummy0 = really_change_color ? pi.base.font.changeColor(color)
-               : Changer();
+               : noChange();
        if (numberedType()) {
                BufferParams::MathNumber const math_number = buffer().params().getMathNumber();
                for (row_type row = 0; row < nrows(); ++row) {
index 57294c075354d889ad4513b406b4fd9ebe22d717..2ecef18a52154cd8456306200a00eedd98c1dca6 100644 (file)
@@ -39,7 +39,7 @@
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 #include "support/textutils.h"
 
 #include <ostream>
@@ -91,7 +91,7 @@ public:
                // macro arguments are in macros
                LATTEST(mathMacro_->nesting() > 0);
                /// The macro nesting can change display of insets. Change it locally.
-               Changer chg = make_change(mi.base.macro_nesting,
+               Changer chg = changeVar(mi.base.macro_nesting,
                                          mathMacro_->nesting() == 1 ? 0 : mathMacro_->nesting());
 
                MathRow::Element e_beg(mi, MathRow::BEGIN);
@@ -344,7 +344,7 @@ bool InsetMathMacro::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
                return InsetMath::addToMathRow(mrow, mi);
 
        /// The macro nesting can change display of insets. Change it locally.
-       Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
+       Changer chg = changeVar(mi.base.macro_nesting, d->nesting_);
 
        MathRow::Element e_beg(mi, MathRow::BEGIN);
        e_beg.inset = this;
@@ -569,7 +569,7 @@ InsetMath::marker_type InsetMathMacro::marker(BufferView const * bv) const
 void InsetMathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        /// The macro nesting can change display of insets. Change it locally.
-       Changer chg = make_change(mi.base.macro_nesting, d->nesting_);
+       Changer chg = changeVar(mi.base.macro_nesting, d->nesting_);
 
        // set edit mode for which we will have calculated metrics. But only
        d->editing_[mi.base.bv] = editMode(mi.base.bv);
index 3d759bc2611c3bcd7259718b8326f585d25f903b..2f25ce6c95d0a28e556121cfeae84f05c710a6ba 100644 (file)
@@ -19,7 +19,6 @@
 #include "TexRow.h"
 
 #include "support/docstring.h"
-#include "support/RefChanger.h"
 #include "support/textutils.h"
 
 #include <algorithm>
@@ -182,7 +181,7 @@ void WriteStream::asciiOnly(bool ascii)
 
 Changer WriteStream::changeRowEntry(TexRow::RowEntry entry)
 {
-       return make_change(row_entry_, entry);
+       return changeVar(row_entry_, entry);
 }
 
 
index 4f276856e58c8b008c9bb734962ecb62753d166b..732c5ac2475e584f05bf0f81580a297fa946de14 100644 (file)
@@ -27,6 +27,7 @@
 #include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
+#include "support/Changer.h"
 #include "support/debug.h"
 #include "support/docstream.h"
 #include "support/lassert.h"
@@ -696,7 +697,7 @@ int mathedSymbolDim(MetricsBase & mb, Dimension & dim, latexkeys const * sym)
                                 mb.fontname != "mathfrak" &&
                                 mb.fontname != "mathcal" &&
                                 mb.fontname != "mathscr");
-       Changer dummy = change_font ? mb.changeFontSet(font) : Changer();
+       Changer dummy = change_font ? mb.changeFontSet(font) : noChange();
        mathed_string_dim(mb.font, mathedSymbol(mb, sym), dim);
        return mathed_char_kerning(mb.font, mathedSymbol(mb, sym).back());
 }
@@ -720,7 +721,7 @@ void mathedSymbolDraw(PainterInfo & pi, int x, int y, latexkeys const * sym)
                                 pi.base.fontname != "mathfrak" &&
                                 pi.base.fontname != "mathcal" &&
                                 pi.base.fontname != "mathscr");
-       Changer dummy = change_font ? pi.base.changeFontSet(font) : Changer();
+       Changer dummy = change_font ? pi.base.changeFontSet(font) : noChange();
        pi.draw(x, y, mathedSymbol(pi.base, sym));
 }
 
index 212bcf1d85f8430ae1a5bd41ad7a125c0c18180c..19a6b9a0cb28ea4c2027b88a8ac1b9ef21b00d35 100644 (file)
 
 namespace lyx {
 
-// Forward declaration for support/RefChanger.h
 struct Revertible {
-       virtual ~Revertible() {}
-       virtual void revert() {}
-       virtual void keep() {}
+       virtual ~Revertible() = default;
 };
 
 using Changer = unique_ptr<Revertible>;
 
 
+/// A RefChanger records the current value of \param ref, allowing to
+/// temporarily assign new values to it.  The original value is restored
+/// automatically when the object is destroyed, unless it is disabled.
+///
+/// RefChanger is movable, and doing so prolongs the duration of the temporary
+/// assignment. This allows classes to supply their own changer methods.
+///
+/// Naturally, be careful not to extend the life of a RefChanger beyond that of
+/// the reference it modifies. The RefChanger can be disabled by calling
+/// ->keep() or ->revert(). Once disabled, the reference is never accessed
+/// again.
+template<typename X>
+class RevertibleRef : public Revertible {
+public:
+       RevertibleRef(X & ref) : ref(ref), old(ref), enabled(true) {}
+       //
+       ~RevertibleRef() override { revert(); }
+       //
+       void revert() { if (enabled) { enabled = false; ref = old; } }
+       //
+       void keep() { enabled = false; }
+       //
+       X & ref;
+       X const old;
+private:
+       bool enabled;
+};
+
+
+template <typename X>
+using RefChanger = unique_ptr<RevertibleRef<X>>;
+
+
+/// Saves the value of \param ref in a movable object
+template <typename X>
+inline RefChanger<X> make_save(X & ref)
+{
+       return make_unique<RevertibleRef<X>>(ref);
+}
+
+inline Changer noChange()
+{
+       return Changer();
+}
+
+/// Temporarily assign value val to reference ref.
+/// To apply the change conditionally, one can write:
+///     Changer dummy = (cond) ? changeVar(ref, val) : noChange();
+template <typename X>
+inline Changer changeVar(X & ref, X const val)
+{
+       auto rc = make_save(ref);
+       ref = val;
+       return rc;
+}
+
 } // namespace lyx
 
+
 #endif //LYX_CHANGER_H
index 785b97f395bff73e9a6f7f05066fd665f7fa070b..addeab86b8a198237f977ded1925b0d02c205caa 100644 (file)
@@ -98,7 +98,6 @@ liblyxsupport_a_SOURCES = \
        qstring_helpers.cpp \
        qstring_helpers.h \
        regex.h \
-       RefChanger.h \
        signals.h \
        socktools.cpp \
        socktools.h \
diff --git a/src/support/RefChanger.h b/src/support/RefChanger.h
deleted file mode 100644 (file)
index ae35cc3..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-// -*- C++ -*-
-/**
- * \file RefChanger.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author Guillaume Munch
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#ifndef LYX_REFCHANGER_H
-#define LYX_REFCHANGER_H
-
-#include "support/Changer.h"
-
-
-namespace lyx {
-
-/// A RefChanger records the current value of \param ref, allowing to
-/// temporarily assign new values to it.  The original value is restored
-/// automatically when the object is destroyed, unless it is disabled.
-///
-/// RefChanger is movable, and doing so prolongs the duration of the temporary
-/// assignment. This allows classes to supply their own changer methods.
-///
-/// Naturally, be careful not to extend the life of a RefChanger beyond that of
-/// the reference it modifies. The RefChanger can be disabled by calling
-/// ->keep() or ->revert(). Once disabled, the reference is never accessed
-/// again.
-template<typename X>
-class RevertibleRef : public Revertible {
-public:
-       RevertibleRef(X & ref) : ref(ref), old(ref), enabled(true) {}
-       //
-       ~RevertibleRef() { revert(); }
-       //
-       void revert() override { if (enabled) { enabled = false; ref = old; } }
-       //
-       void keep() override { enabled = false; }
-       //
-       X & ref;
-       X const old;
-private:
-       bool enabled;
-};
-
-
-template <typename X> using RefChanger = unique_ptr<RevertibleRef<X>>;
-
-
-/// Saves the value of \param ref in a movable object
-template <typename X> RefChanger<X> make_save(X & ref)
-{
-       return make_unique<RevertibleRef<X>>(ref);
-}
-
-/// Temporarily assign value val to reference ref.
-/// To apply the change conditionnally, one can write:
-///     Changer dummy = (cond) ? make_change(a, b) : Changer();
-template <typename X>
-RefChanger<X> make_change(X & ref, X const val)
-{
-       auto rc = make_save(ref);
-       ref = val;
-       return rc;
-}
-
-
-} // namespace lyx
-
-
-#endif //LYX_REFCHANGER_H