diff options
author | Ivan Komissarov <abbapoh@gmail.com> | 2021-05-13 00:42:17 +0200 |
---|---|---|
committer | Ivan Komissarov <ABBAPOH@gmail.com> | 2021-05-13 13:26:16 +0000 |
commit | 2ca976dc2c9025ce41ed785b9b2c9f1249c81526 (patch) | |
tree | 26bb599b9413647f13421eb437c50a0fb575485e /src/lib/corelib | |
parent | 19913abe592d1fb7c3f2dd721e8268dee087be80 (diff) | |
download | qbs-2ca976dc2c9025ce41ed785b9b2c9f1249c81526.tar.gz |
Use std::unique_ptr in BinaryFile and TextFile
Change-Id: I74fae5409155eda7bbaa762bfc55c4caf3b89790
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Diffstat (limited to 'src/lib/corelib')
-rw-r--r-- | src/lib/corelib/jsextensions/binaryfile.cpp | 16 | ||||
-rw-r--r-- | src/lib/corelib/jsextensions/textfile.cpp | 16 |
2 files changed, 8 insertions, 24 deletions
diff --git a/src/lib/corelib/jsextensions/binaryfile.cpp b/src/lib/corelib/jsextensions/binaryfile.cpp index f02f0bff6..bb35235b1 100644 --- a/src/lib/corelib/jsextensions/binaryfile.cpp +++ b/src/lib/corelib/jsextensions/binaryfile.cpp @@ -67,7 +67,6 @@ public: }; static QScriptValue ctor(QScriptContext *context, QScriptEngine *engine); - ~BinaryFile() override; Q_INVOKABLE void close(); Q_INVOKABLE QString filePath(); @@ -87,7 +86,7 @@ private: // ResourceAcquiringScriptObject implementation void releaseResources() override; - QFile *m_file = nullptr; + std::unique_ptr<QFile> m_file; }; QScriptValue BinaryFile::ctor(QScriptContext *context, QScriptEngine *engine) @@ -120,11 +119,6 @@ QScriptValue BinaryFile::ctor(QScriptContext *context, QScriptEngine *engine) return engine->newQObject(t, QScriptEngine::QtOwnership); } -BinaryFile::~BinaryFile() -{ - delete m_file; -} - BinaryFile::BinaryFile(QScriptContext *context, const QString &filePath, OpenMode mode) { Q_ASSERT(thisObject().engine() == engine()); @@ -146,12 +140,11 @@ BinaryFile::BinaryFile(QScriptContext *context, const QString &filePath, OpenMod return; } - m_file = new QFile(filePath); + m_file = std::make_unique<QFile>(filePath); if (Q_UNLIKELY(!m_file->open(m))) { context->throwError(Tr::tr("Unable to open file '%1': %2") .arg(filePath, m_file->errorString())); - delete m_file; - m_file = nullptr; + m_file.reset(); } } @@ -160,8 +153,7 @@ void BinaryFile::close() if (checkForClosed()) return; m_file->close(); - delete m_file; - m_file = nullptr; + m_file.reset(); } QString BinaryFile::filePath() diff --git a/src/lib/corelib/jsextensions/textfile.cpp b/src/lib/corelib/jsextensions/textfile.cpp index 3c7f2d316..f4171b117 100644 --- a/src/lib/corelib/jsextensions/textfile.cpp +++ b/src/lib/corelib/jsextensions/textfile.cpp @@ -74,7 +74,6 @@ public: }; static QScriptValue ctor(QScriptContext *context, QScriptEngine *engine); - ~TextFile() override; Q_INVOKABLE void close(); Q_INVOKABLE QString filePath(); @@ -95,7 +94,7 @@ private: // ResourceAcquiringScriptObject implementation void releaseResources() override; - QFile *m_file = nullptr; + std::unique_ptr<QFile> m_file; QTextCodec *m_codec = nullptr; }; @@ -136,18 +135,13 @@ QScriptValue TextFile::ctor(QScriptContext *context, QScriptEngine *engine) return engine->newQObject(t, QScriptEngine::QtOwnership); } -TextFile::~TextFile() -{ - delete m_file; -} - TextFile::TextFile(QScriptContext *context, const QString &filePath, OpenMode mode, const QString &codec) { Q_UNUSED(codec) Q_ASSERT(thisObject().engine() == engine()); - m_file = new QFile(filePath); + m_file = std::make_unique<QFile>(filePath); const auto newCodec = QTextCodec::codecForName(qPrintable(codec)); m_codec = newCodec ? newCodec : QTextCodec::codecForName("UTF-8"); QIODevice::OpenMode m = QIODevice::NotOpen; @@ -161,8 +155,7 @@ TextFile::TextFile(QScriptContext *context, const QString &filePath, OpenMode mo if (Q_UNLIKELY(!m_file->open(m))) { context->throwError(Tr::tr("Unable to open file '%1': %2") .arg(filePath, m_file->errorString())); - delete m_file; - m_file = nullptr; + m_file.reset(); } } @@ -171,8 +164,7 @@ void TextFile::close() if (checkForClosed()) return; m_file->close(); - delete m_file; - m_file = nullptr; + m_file.reset(); } QString TextFile::filePath() |