use std::unique_ptr in PPDCache

Change-Id: Ib47ffaa0fe754d8aafdf338d8ec8c2109beb21a5
Reviewed-on: https://gerrit.libreoffice.org/43484
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin 2017-10-18 09:43:21 +02:00
parent e355fb41db
commit afe4d2527e
2 changed files with 15 additions and 20 deletions

View file

@ -177,7 +177,6 @@ private:
PPDParser( const OUString& rFile );
PPDParser( const OUString& rFile, std::vector<PPDKey*> keys );
~PPDParser();
void parseOrderDependency(const OString& rLine);
void parseOpenUI(const OString& rLine, const OString& rPPDGroup);
@ -190,6 +189,7 @@ private:
static void initPPDFiles(PPDCache &rPPDCache);
static OUString getPPDFile( const OUString& rFile );
public:
~PPDParser();
static const PPDParser* getParser( const OUString& rFile );
const PPDKey* getKey( int n ) const;

View file

@ -239,21 +239,11 @@ namespace psp
class PPDCache
{
public:
std::list< PPDParser* > aAllParsers;
std::unordered_map< OUString, OUString, OUStringHash >* pAllPPDFiles;
std::list< std::unique_ptr<PPDParser> > aAllParsers;
std::unique_ptr<std::unordered_map< OUString, OUString, OUStringHash >> pAllPPDFiles;
PPDCache()
: pAllPPDFiles(nullptr)
{}
~PPDCache()
{
while( aAllParsers.begin() != aAllParsers.end() )
{
delete aAllParsers.front();
aAllParsers.pop_front();
}
delete pAllPPDFiles;
pAllPPDFiles = nullptr;
}
};
}
@ -450,7 +440,7 @@ void PPDParser::initPPDFiles(PPDCache &rPPDCache)
if( rPPDCache.pAllPPDFiles )
return;
rPPDCache.pAllPPDFiles = new std::unordered_map< OUString, OUString, OUStringHash >;
rPPDCache.pAllPPDFiles.reset(new std::unordered_map< OUString, OUString, OUStringHash >);
// check installation directories
std::vector< OUString > aPathList;
@ -509,7 +499,7 @@ OUString PPDParser::getPPDFile( const OUString& rFile )
if( it == rPPDCache.pAllPPDFiles->end() && bRetry )
{
// a new file ? rehash
delete rPPDCache.pAllPPDFiles; rPPDCache.pAllPPDFiles = nullptr;
rPPDCache.pAllPPDFiles.reset();
bRetry = false;
// note this is optimized for office start where
// no new files occur and initPPDFiles is called only once
@ -561,9 +551,9 @@ const PPDParser* PPDParser::getParser( const OUString& rFile )
PPDCache &rPPDCache = thePPDCache::get();
for( ::std::list< PPDParser* >::const_iterator it = rPPDCache.aAllParsers.begin(); it != rPPDCache.aAllParsers.end(); ++it )
if( (*it)->m_aFile == aFile )
return *it;
for( auto const & i : rPPDCache.aAllParsers )
if( i->m_aFile == aFile )
return i.get();
PPDParser* pNewParser = nullptr;
if( !aFile.startsWith( "CUPS:" ) && !aFile.startsWith( "CPD:" ) )
@ -587,9 +577,14 @@ const PPDParser* PPDParser::getParser( const OUString& rFile )
{
// this may actually be the SGENPRT parser,
// so ensure uniqueness here
rPPDCache.aAllParsers.remove( pNewParser );
rPPDCache.aAllParsers.erase(
std::remove_if(
rPPDCache.aAllParsers.begin(),
rPPDCache.aAllParsers.end(),
[pNewParser] (std::unique_ptr<PPDParser> const & x) { return x.get() == pNewParser; } ),
rPPDCache.aAllParsers.end());
// insert new parser to list
rPPDCache.aAllParsers.push_front( pNewParser );
rPPDCache.aAllParsers.push_front( std::unique_ptr<PPDParser>(pNewParser) );
}
return pNewParser;
}