From e8cb751f3bf816dc721115a5dc27da9eefaeb886 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Mon, 9 Apr 2018 14:35:12 +0200 Subject: loplugin:useuniqueptr in DataStream Change-Id: I05a22f05a7d41aa06539e05a1969f9520cc3ab31 Reviewed-on: https://gerrit.libreoffice.org/52649 Tested-by: Jenkins Reviewed-by: Noel Grandin --- sc/source/ui/docshell/datastream.cxx | 50 ++++++++++++------------------------ sc/source/ui/inc/datastream.hxx | 2 +- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/sc/source/ui/docshell/datastream.cxx b/sc/source/ui/docshell/datastream.cxx index eea81a1f41ae..08f882812d9e 100644 --- a/sc/source/ui/docshell/datastream.cxx +++ b/sc/source/ui/docshell/datastream.cxx @@ -93,24 +93,15 @@ public: namespace datastreams { -void emptyLineQueue( std::queue& rQueue ) -{ - while (!rQueue.empty()) - { - delete rQueue.front(); - rQueue.pop(); - } -} - class ReaderThread : public salhelper::Thread { - SvStream *mpStream; + std::unique_ptr mpStream; size_t mnColCount; bool mbTerminate; osl::Mutex maMtxTerminate; - std::queue maPendingLines; - std::queue maUsedLines; + std::queue> maPendingLines; + std::queue> maUsedLines; osl::Mutex maMtxLines; osl::Condition maCondReadStream; @@ -120,9 +111,9 @@ class ReaderThread : public salhelper::Thread public: - ReaderThread(SvStream *pData, size_t nColCount): + ReaderThread(std::unique_ptr pData, size_t nColCount): Thread("ReaderThread"), - mpStream(pData), + mpStream(std::move(pData)), mnColCount(nColCount), mbTerminate(false) { @@ -130,13 +121,6 @@ public: maConfig.text_qualifier = '"'; } - virtual ~ReaderThread() override - { - delete mpStream; - emptyLineQueue(maPendingLines); - emptyLineQueue(maUsedLines); - } - bool isTerminateRequested() { osl::MutexGuard aGuard(maMtxTerminate); @@ -161,9 +145,9 @@ public: maCondConsume.reset(); } - DataStream::LinesType* popNewLines() + std::unique_ptr popNewLines() { - DataStream::LinesType* pLines = maPendingLines.front(); + auto pLines = std::move(maPendingLines.front()); maPendingLines.pop(); return pLines; } @@ -179,9 +163,9 @@ public: return !maPendingLines.empty(); } - void pushUsedLines( DataStream::LinesType* pLines ) + void pushUsedLines( std::unique_ptr pLines ) { - maUsedLines.push(pLines); + maUsedLines.push(std::move(pLines)); } osl::Mutex& getLinesMutex() @@ -194,20 +178,20 @@ private: { while (!isTerminateRequested()) { - DataStream::LinesType* pLines = nullptr; + std::unique_ptr pLines; osl::ResettableMutexGuard aGuard(maMtxLines); if (!maUsedLines.empty()) { // Re-use lines from previous runs. - pLines = maUsedLines.front(); + pLines = std::move(maUsedLines.front()); maUsedLines.pop(); aGuard.clear(); // unlock } else { aGuard.clear(); // unlock - pLines = new DataStream::LinesType(10); + pLines.reset(new DataStream::LinesType(10)); } // Read & store new lines from stream. @@ -229,7 +213,7 @@ private: maCondReadStream.reset(); aGuard.reset(); // lock } - maPendingLines.push(pLines); + maPendingLines.push(std::move(pLines)); maCondConsume.set(); if (!mpStream->good()) requestTerminate(); @@ -320,7 +304,7 @@ DataStream::~DataStream() mxReaderThread->endThread(); mxReaderThread->join(); } - delete mpLines; + mpLines.reset(); } DataStream::Line DataStream::ConsumeLine() @@ -333,7 +317,7 @@ DataStream::Line DataStream::ConsumeLine() osl::ResettableMutexGuard aGuard(mxReaderThread->getLinesMutex()); if (mpLines) - mxReaderThread->pushUsedLines(mpLines); + mxReaderThread->pushUsedLines(std::move(mpLines)); while (!mxReaderThread->hasNewLines()) { @@ -397,8 +381,8 @@ void DataStream::StartImport() if (!mxReaderThread.is()) { - SvStream *pStream = new SvFileStream(msURL, StreamMode::READ); - mxReaderThread = new datastreams::ReaderThread(pStream, maStartRange.aEnd.Col() - maStartRange.aStart.Col() + 1); + std::unique_ptr pStream(new SvFileStream(msURL, StreamMode::READ)); + mxReaderThread = new datastreams::ReaderThread(std::move(pStream), maStartRange.aEnd.Col() - maStartRange.aStart.Col() + 1); mxReaderThread->launch(); } mbRunning = true; diff --git a/sc/source/ui/inc/datastream.hxx b/sc/source/ui/inc/datastream.hxx index b60de3fb852e..d71ddce79855 100644 --- a/sc/source/ui/inc/datastream.hxx +++ b/sc/source/ui/inc/datastream.hxx @@ -111,7 +111,7 @@ private: bool mbRunning; bool mbValuesInLine; bool mbRefreshOnEmptyLine; - LinesType* mpLines; + std::unique_ptr mpLines; size_t mnLinesCount; size_t mnLinesSinceRefresh; double mfLastRefreshTime; -- cgit