From b382b246b62b66100733449b2bf75a01fd1d263f Mon Sep 17 00:00:00 2001 From: Guillaume MM Date: Sun, 2 Apr 2017 21:04:06 +0200 Subject: [PATCH] MathAtom is a unique_ptr Fix coverity suggestion of defining a move constructor --- src/mathed/MathAtom.cpp | 27 +++++---------------------- src/mathed/MathAtom.h | 26 ++++++++++---------------- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/src/mathed/MathAtom.cpp b/src/mathed/MathAtom.cpp index a8483ea212..cbf26ec9f7 100644 --- a/src/mathed/MathAtom.cpp +++ b/src/mathed/MathAtom.cpp @@ -18,37 +18,20 @@ using namespace std; namespace lyx { -MathAtom::MathAtom() - : nucleus_(0) -{} - - MathAtom::MathAtom(InsetMath * p) - : nucleus_(p) + : unique_ptr(p) {} MathAtom::MathAtom(MathAtom const & at) - : nucleus_(0) -{ - if (at.nucleus_) - nucleus_ = static_cast(at.nucleus_->clone()); -} + : unique_ptr(at ? static_cast(at->clone()) : nullptr) +{} MathAtom & MathAtom::operator=(MathAtom const & at) { - if (&at == this) - return *this; - MathAtom tmp(at); - swap(tmp.nucleus_, nucleus_); - return *this; -} - - -MathAtom::~MathAtom() -{ - delete nucleus_; + // copy then move-assign + return operator=(MathAtom(at)); } diff --git a/src/mathed/MathAtom.h b/src/mathed/MathAtom.h index b11d75da18..7801fe6c5d 100644 --- a/src/mathed/MathAtom.h +++ b/src/mathed/MathAtom.h @@ -40,33 +40,27 @@ Andre' */ +#include + + namespace lyx { class Inset; class InsetMath; -class MathAtom { +class MathAtom : public std::unique_ptr { public: - /// default constructor, object is useless, but we need it to put it into - /// std::containers - MathAtom(); + MathAtom() = default; + MathAtom(MathAtom &&) = default; + MathAtom & operator=(MathAtom &&) = default; /// the "real constructor" explicit MathAtom(InsetMath * p); - /// copy constructor, invokes nucleus_->clone() + /// copy constructor, invokes clone() MathAtom(MathAtom const &); - /// we really need to clean up - ~MathAtom(); - /// assignment invokes nucleus_->clone() MathAtom & operator=(MathAtom const &); - /// access to the inset (checked with gprof) - InsetMath * nucleus() { return nucleus_; } - InsetMath const * nucleus() const { return nucleus_; } /// access to the inset - InsetMath const * operator->() const { return nucleus_; } - -private: - /// - InsetMath * nucleus_; + InsetMath * nucleus() { return get(); } + InsetMath const * nucleus() const { return get(); } }; -- 2.39.2