From 557975a8de71bd6c209c5a29fcb6627c56cec869 Mon Sep 17 00:00:00 2001 From: Guillaume Munch Date: Thu, 2 Jun 2016 21:58:52 +0100 Subject: [PATCH] Replace auto_ptr with unique_ptr This is a mechanical replacement. For now it seems that unique_ptrs are essentially used for exception-safety. More could certainly be done to clarify pointer ownership in general. --- src/Buffer.cpp | 12 ++++++------ src/Buffer.h | 5 +++-- src/BufferList.cpp | 4 ++-- src/Text.cpp | 9 ++++----- src/factory.cpp | 11 ++++++----- src/frontends/qt4/GuiViewSource.cpp | 12 ++++++------ src/frontends/qt4/GuiViewSource.h | 2 +- src/insets/ExternalSupport.cpp | 4 ++-- src/insets/ExternalTransforms.h | 6 ++++-- src/mathed/MathExtern.cpp | 11 ++++++----- src/mathed/MathParser.cpp | 5 +++-- 11 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 3cb7aac094..b75ddd0d70 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -3654,11 +3654,11 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to) } // returns NULL if id-to-row conversion is unsupported -auto_ptr Buffer::getSourceCode(odocstream & os, string const & format, - pit_type par_begin, pit_type par_end, - OutputWhat output, bool master) const +unique_ptr Buffer::getSourceCode(odocstream & os, string const & format, + pit_type par_begin, pit_type par_end, + OutputWhat output, bool master) const { - auto_ptr texrow(NULL); + unique_ptr texrow; OutputParams runparams(¶ms().encoding()); runparams.nice = true; runparams.flavor = params().getOutputFlavor(format); @@ -3712,7 +3712,7 @@ auto_ptr Buffer::getSourceCode(odocstream & os, string const & format, LaTeXFeatures features(*this, params(), runparams); params().validate(features); runparams.use_polyglossia = features.usePolyglossia(); - texrow.reset(new TexRow()); + texrow = make_unique(); texrow->newline(); texrow->newline(); // latex or literate @@ -3755,7 +3755,7 @@ auto_ptr Buffer::getSourceCode(odocstream & os, string const & format, writeDocBookSource(os, absFileName(), runparams, output); } else { // latex or literate - texrow.reset(new TexRow()); + texrow = make_unique(); texrow->newline(); texrow->newline(); otexstream ots(os, *texrow); diff --git a/src/Buffer.h b/src/Buffer.h index d4074b0fdb..f488c4a4e0 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -15,6 +15,7 @@ #include "OutputEnums.h" #include "OutputParams.h" +#include "support/unique_ptr.h" #include "support/strfwd.h" #include "support/types.h" @@ -623,8 +624,8 @@ public: /// get source code (latex/docbook) for some paragraphs, or all paragraphs /// including preamble - /// returns NULL if Id to Row conversion is unsupported - std::auto_ptr getSourceCode(odocstream & os, + /// returns nullptr if Id to Row conversion is unsupported + unique_ptr getSourceCode(odocstream & os, std::string const & format, pit_type par_begin, pit_type par_end, OutputWhat output, bool master) const; diff --git a/src/BufferList.cpp b/src/BufferList.cpp index ff99a090f3..9a5c4e1c27 100644 --- a/src/BufferList.cpp +++ b/src/BufferList.cpp @@ -130,9 +130,9 @@ Buffer * BufferList::newBuffer(string const & s) Buffer * BufferList::createNewBuffer(string const & s) { - auto_ptr tmpbuf; + unique_ptr tmpbuf; try { - tmpbuf.reset(new Buffer(s)); + tmpbuf = make_unique(s); } catch (ExceptionMessage const & message) { if (message.type_ == ErrorException) { Alert::error(message.title_, message.details_); diff --git a/src/Text.cpp b/src/Text.cpp index 794605405a..12ab04f043 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -69,6 +69,7 @@ #include "support/lyxalgo.h" #include "support/lyxtime.h" #include "support/textutils.h" +#include "support/unique_ptr.h" #include @@ -463,8 +464,7 @@ void Text::readParToken(Paragraph & par, Lexer & lex, } else if (token == "\\SpecialChar" || (token == "\\SpecialCharNoPassThru" && !par.layout().pass_thru && !inset().isPassThru())) { - auto_ptr inset; - inset.reset(new InsetSpecialChar); + auto inset = make_unique(); inset->read(lex); inset->setBuffer(*buf); par.insertInset(par.size(), inset.release(), font, change); @@ -473,8 +473,7 @@ void Text::readParToken(Paragraph & par, Lexer & lex, docstring const s = ltrim(lex.getDocString(), "\\"); par.insert(par.size(), s, font, change); } else if (token == "\\IPAChar") { - auto_ptr inset; - inset.reset(new InsetIPAChar); + auto inset = make_unique(); inset->read(lex); inset->setBuffer(*buf); par.insertInset(par.size(), inset.release(), font, change); @@ -499,7 +498,7 @@ void Text::readParToken(Paragraph & par, Lexer & lex, } else if (token == "\\backslash") { par.appendChar('\\', font, change); } else if (token == "\\LyXTable") { - auto_ptr inset(new InsetTabular(buf)); + auto inset = make_unique(buf); inset->read(lex); par.insertInset(par.size(), inset.release(), font, change); } else if (token == "\\change_unchanged") { diff --git a/src/factory.cpp b/src/factory.cpp index b8d94336b9..a03f88fa28 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -65,13 +65,14 @@ #include "frontends/alert.h" #include "support/debug.h" -#include "support/lstrings.h" #include "support/ExceptionMessage.h" - #include "support/lassert.h" +#include "support/lstrings.h" +#include "support/unique_ptr.h" #include + using namespace std; using namespace lyx::support; @@ -309,7 +310,7 @@ Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd) case EXTERNAL_CODE: { InsetExternalParams iep; InsetExternal::string2params(to_utf8(cmd.argument()), *buf, iep); - auto_ptr inset(new InsetExternal(buf)); + auto inset = make_unique(buf); inset->setBuffer(*buf); inset->setParams(iep); return inset.release(); @@ -318,7 +319,7 @@ Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd) case GRAPHICS_CODE: { InsetGraphicsParams igp; InsetGraphics::string2params(to_utf8(cmd.argument()), *buf, igp); - auto_ptr inset(new InsetGraphics(buf)); + auto inset = make_unique(buf); inset->setParams(igp); return inset.release(); } @@ -514,7 +515,7 @@ Inset * readInset(Lexer & lex, Buffer * buf) if (lex.getString() != "\\begin_inset") LYXERR0("Buffer::readInset: Consistency check failed."); - auto_ptr inset; + unique_ptr inset; string tmptok; lex >> tmptok; diff --git a/src/frontends/qt4/GuiViewSource.cpp b/src/frontends/qt4/GuiViewSource.cpp index d5838976a9..46dd35884e 100644 --- a/src/frontends/qt4/GuiViewSource.cpp +++ b/src/frontends/qt4/GuiViewSource.cpp @@ -111,8 +111,8 @@ void ViewSourceWidget::getContent(BufferView const * view, if (par_begin > par_end) swap(par_begin, par_end); odocstringstream ostr; - texrow_ = view->buffer().getSourceCode(ostr, format, - par_begin, par_end + 1, output, master); + texrow_ = view->buffer() + .getSourceCode(ostr, format, par_begin, par_end + 1, output, master); //ensure that the last line can always be selected in its full width str = ostr.str() + "\n"; } @@ -201,7 +201,7 @@ void ViewSourceWidget::realUpdateView() #ifdef DEVEL_VERSION // output tex<->row correspondences in the source panel if the "-dbg latex" // option is given. - if (texrow_.get() && lyx::lyxerr.debugging(Debug::LATEX)) { + if (texrow_ && lyx::lyxerr.debugging(Debug::LATEX)) { QStringList list = qcontent.split(QChar('\n')); docstring_list dlist; for (QStringList::const_iterator it = list.begin(); it != list.end(); ++it) @@ -216,7 +216,7 @@ void ViewSourceWidget::realUpdateView() viewSourceTV->blockSignals(true); bool const changed = setText(qcontent); - if (changed && !texrow_.get()) { + if (changed && !texrow_) { // position-to-row is unavailable // we jump to the first modification const QChar * oc = old.constData(); @@ -246,7 +246,7 @@ void ViewSourceWidget::realUpdateView() //c.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,1); viewSourceTV->setTextCursor(c); - } else if (texrow_.get()) { + } else if (texrow_) { // Use the available position-to-row conversion to highlight // the current selection in the source std::pair rows = texrow_->rowFromCursor(bv_->cursor()); @@ -309,7 +309,7 @@ void ViewSourceWidget::realUpdateView() // need a proper LFUN if we want to implement it in release mode void ViewSourceWidget::gotoCursor() { - if (!bv_ || !texrow_.get()) + if (!bv_ || !texrow_) return; int row = viewSourceTV->textCursor().blockNumber() + 1; const_cast(bv_)->setCursorFromRow(row, *texrow_); diff --git a/src/frontends/qt4/GuiViewSource.h b/src/frontends/qt4/GuiViewSource.h index e089305be5..428da05011 100644 --- a/src/frontends/qt4/GuiViewSource.h +++ b/src/frontends/qt4/GuiViewSource.h @@ -85,7 +85,7 @@ private: QTimer * update_timer_; /// TexRow information from the last source view. If TexRow is unavailable /// for the last format then texrow_ is null. - std::auto_ptr texrow_; + unique_ptr texrow_; }; diff --git a/src/insets/ExternalSupport.cpp b/src/insets/ExternalSupport.cpp index cbbb88bc2b..7ea6d566fd 100644 --- a/src/insets/ExternalSupport.cpp +++ b/src/insets/ExternalSupport.cpp @@ -434,7 +434,7 @@ string const substituteIt(string const & input, else if (id == Resize) ptr = store.getCommandTransformer(params.resizedata); - if (!ptr.get()) + if (!ptr) return input; string result = @@ -473,7 +473,7 @@ string const substituteIt(string const & input, break; } - if (!ptr.get()) + if (!ptr) return input; return subst(input, ptr->placeholder(), ptr->option()); diff --git a/src/insets/ExternalTransforms.h b/src/insets/ExternalTransforms.h index c4213bd7de..8e531f31d8 100644 --- a/src/insets/ExternalTransforms.h +++ b/src/insets/ExternalTransforms.h @@ -16,6 +16,8 @@ #include "graphics/GraphicsParams.h" +#include "support/unique_ptr.h" + #include #include @@ -121,7 +123,7 @@ public: */ class TransformCommand { public: - typedef std::auto_ptr ptr_type; + typedef unique_ptr ptr_type; virtual ~TransformCommand() {} /// The string from the External Template that we seek to replace. @@ -200,7 +202,7 @@ private: */ class TransformOption { public: - typedef std::auto_ptr ptr_type; + typedef unique_ptr ptr_type; virtual ~TransformOption() {} /// The string from the External Template that we seek to replace. diff --git a/src/mathed/MathExtern.cpp b/src/mathed/MathExtern.cpp index 7fbb4af732..f3df8c884e 100644 --- a/src/mathed/MathExtern.cpp +++ b/src/mathed/MathExtern.cpp @@ -45,6 +45,7 @@ #include "support/lstrings.h" #include "support/TempFile.h" #include "support/textutils.h" +#include "support/unique_ptr.h" #include #include @@ -370,7 +371,7 @@ void splitScripts(MathData & ar) // create extra script inset and move superscript over InsetMathScript * p = ar[i].nucleus()->asScriptInset(); - auto_ptr q(new InsetMathScript(buf, true)); + auto q = make_unique(buf, true); swap(q->up(), p->up()); p->removeScript(true); @@ -598,7 +599,7 @@ void extractFunctions(MathData & ar, ExternalMath kind) extractScript(exp, jt, ar.end(), true); // create a proper inset as replacement - auto_ptr p(new InsetMathExFunc(buf, name)); + auto p = make_unique(buf, name); // jt points to the "argument". Get hold of this. MathData::iterator st = @@ -683,7 +684,7 @@ void extractIntegrals(MathData & ar, ExternalMath kind) continue; // core ist part from behind the scripts to the 'd' - auto_ptr p(new InsetMathExInt(buf, from_ascii("int"))); + auto p = make_unique(buf, from_ascii("int")); // handle scripts if available if (!testIntSymbol(*it)) { @@ -768,7 +769,7 @@ void extractSums(MathData & ar) continue; // create a proper inset as replacement - auto_ptr p(new InsetMathExInt(buf, from_ascii("sum"))); + auto p = make_unique(buf, from_ascii("sum")); // collect lower bound and summation index InsetMathScript const * sub = ar[i]->asScriptInset(); @@ -856,7 +857,7 @@ void extractDiff(MathData & ar) } // create a proper diff inset - auto_ptr diff(new InsetMathDiff(buf)); + auto diff = make_unique(buf); // collect function, let jt point behind last used item MathData::iterator jt = it + 1; diff --git a/src/mathed/MathParser.cpp b/src/mathed/MathParser.cpp index 2849b0f80b..d326317520 100644 --- a/src/mathed/MathParser.cpp +++ b/src/mathed/MathParser.cpp @@ -74,9 +74,10 @@ following hack as starting point to write some macros: #include "Encoding.h" #include "Lexer.h" -#include "support/debug.h" #include "support/convert.h" +#include "support/debug.h" #include "support/docstream.h" +#include "support/unique_ptr.h" #include @@ -1945,7 +1946,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags, // Disabled else if (1 && t.cs() == "ar") { - auto_ptr p(new InsetMathXYArrow); + auto p = make_unique(); // try to read target parse(p->cell(0), FLAG_OTPTION, mode); // try to read label