MathAtom is a unique_ptr

Fix coverity suggestion of defining a move constructor
This commit is contained in:
Guillaume MM 2017-04-02 21:04:06 +02:00
parent 723f0e14d9
commit b382b246b6
2 changed files with 15 additions and 38 deletions

View File

@ -18,37 +18,20 @@ using namespace std;
namespace lyx {
MathAtom::MathAtom()
: nucleus_(0)
{}
MathAtom::MathAtom(InsetMath * p)
: nucleus_(p)
: unique_ptr<InsetMath>(p)
{}
MathAtom::MathAtom(MathAtom const & at)
: nucleus_(0)
{
if (at.nucleus_)
nucleus_ = static_cast<InsetMath*>(at.nucleus_->clone());
}
: unique_ptr<InsetMath>(at ? static_cast<InsetMath*>(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));
}

View File

@ -40,33 +40,27 @@ Andre'
*/
#include <memory>
namespace lyx {
class Inset;
class InsetMath;
class MathAtom {
class MathAtom : public std::unique_ptr<InsetMath> {
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(); }
};