Commit 1607a34a authored by Ivan Donchevskii's avatar Ivan Donchevskii

Utils: use unique_ptr for QFile object

Do not use raw pointer.

Change-Id: I6810c3ec3a60a43829130cf5e5a00e52db95b965
Reviewed-by: Joerg Bornemann's avatarJoerg Bornemann <joerg.bornemann@qt.io>
parent dae4477c
......@@ -395,18 +395,11 @@ FileSaverBase::FileSaverBase()
{
}
FileSaverBase::~FileSaverBase()
{
delete m_file;
}
bool FileSaverBase::finalize()
{
m_file->close();
setResult(m_file->error() == QFile::NoError);
// We delete the object, so it is really closed even if it is a QTemporaryFile.
delete m_file;
m_file = 0;
m_file.reset();
return !m_hasError;
}
......@@ -488,10 +481,10 @@ FileSaver::FileSaver(const QString &filename, QIODevice::OpenMode mode)
}
}
if (mode & (QIODevice::ReadOnly | QIODevice::Append)) {
m_file = new QFile(filename);
m_file.reset(new QFile{filename});
m_isSafe = false;
} else {
m_file = new SaveFile(filename);
m_file.reset(new SaveFile{filename});
m_isSafe = true;
}
if (!m_file->open(QIODevice::WriteOnly | mode)) {
......@@ -507,22 +500,22 @@ bool FileSaver::finalize()
if (!m_isSafe)
return FileSaverBase::finalize();
SaveFile *sf = static_cast<SaveFile *>(m_file);
SaveFile *sf = static_cast<SaveFile *>(m_file.get());
if (m_hasError) {
if (sf->isOpen())
sf->rollback();
} else {
setResult(sf->commit());
}
delete sf;
m_file = 0;
m_file.reset();
return !m_hasError;
}
TempFileSaver::TempFileSaver(const QString &templ)
: m_autoRemove(true)
{
QTemporaryFile *tempFile = new QTemporaryFile();
m_file.reset(new QTemporaryFile{});
QTemporaryFile *tempFile = static_cast<QTemporaryFile *>(m_file.get());
if (!templ.isEmpty())
tempFile->setFileTemplate(templ);
tempFile->setAutoRemove(false);
......@@ -532,14 +525,12 @@ TempFileSaver::TempFileSaver(const QString &templ)
tempFile->errorString());
m_hasError = true;
}
m_file = tempFile;
m_fileName = tempFile->fileName();
}
TempFileSaver::~TempFileSaver()
{
delete m_file;
m_file = 0;
m_file.reset();
if (m_autoRemove)
QFile::remove(m_fileName);
}
......
......@@ -35,6 +35,7 @@
#include <QStringList>
#include <functional>
#include <memory>
namespace Utils {class FileName; }
......@@ -174,7 +175,7 @@ class QTCREATOR_UTILS_EXPORT FileSaverBase
Q_DECLARE_TR_FUNCTIONS(Utils::FileUtils) // sic!
public:
FileSaverBase();
virtual ~FileSaverBase();
virtual ~FileSaverBase() = default;
QString fileName() const { return m_fileName; }
bool hasError() const { return m_hasError; }
......@@ -190,8 +191,10 @@ public:
bool setResult(QXmlStreamWriter *stream);
bool setResult(bool ok);
QFile *file() { return m_file.get(); }
protected:
QFile *m_file;
std::unique_ptr<QFile> m_file;
QString m_fileName;
QString m_errorString;
bool m_hasError;
......@@ -208,7 +211,6 @@ public:
virtual bool finalize();
using FileSaverBase::finalize;
QFile *file() { return m_file; }
private:
bool m_isSafe;
......@@ -221,8 +223,6 @@ public:
explicit TempFileSaver(const QString &templ = QString());
~TempFileSaver();
QTemporaryFile *file() { return reinterpret_cast<QTemporaryFile *>(m_file); }
void setAutoRemove(bool on) { m_autoRemove = on; }
private:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment