Kaydet (Commit) b1f9aa5f authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:useuniqueptr in ScChartPositionMap

Change-Id: I7c3209dff1c09c40d7a3bb57db0f0b26254318f2
Reviewed-on: https://gerrit.libreoffice.org/60996
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 4ffb8946
...@@ -26,17 +26,17 @@ ...@@ -26,17 +26,17 @@
#include <map> #include <map>
// map of row number to ScAddress* // map of row number to ScAddress*
typedef std::map<sal_uLong, ScAddress*> RowMap; typedef std::map<sal_uLong, std::unique_ptr<ScAddress>> RowMap;
// map of column number to RowMap* // map of column number to RowMap
typedef std::map<sal_uLong, RowMap*> ColumnMap; typedef std::map<sal_uLong, RowMap> ColumnMap;
class ScChartPositionMap class ScChartPositionMap
{ {
friend class ScChartPositioner; friend class ScChartPositioner;
std::unique_ptr<ScAddress*[]> ppData; std::unique_ptr<std::unique_ptr<ScAddress>[]> ppData;
std::unique_ptr<ScAddress*[]> ppColHeader; std::unique_ptr<std::unique_ptr<ScAddress>[]> ppColHeader;
std::unique_ptr<ScAddress*[]> ppRowHeader; std::unique_ptr<std::unique_ptr<ScAddress>[]> ppRowHeader;
sal_uLong nCount; sal_uLong nCount;
SCCOL nColCount; SCCOL nColCount;
SCROW nRowCount; SCROW nRowCount;
...@@ -65,7 +65,7 @@ public: ...@@ -65,7 +65,7 @@ public:
const ScAddress* GetPosition( sal_uLong nIndex ) const const ScAddress* GetPosition( sal_uLong nIndex ) const
{ {
if ( nIndex < nCount ) if ( nIndex < nCount )
return ppData[ nIndex ]; return ppData[ nIndex ].get();
return nullptr; return nullptr;
} }
...@@ -73,19 +73,19 @@ public: ...@@ -73,19 +73,19 @@ public:
const ScAddress* GetPosition( SCCOL nChartCol, SCROW nChartRow ) const const ScAddress* GetPosition( SCCOL nChartCol, SCROW nChartRow ) const
{ {
if ( IsValid( nChartCol, nChartRow ) ) if ( IsValid( nChartCol, nChartRow ) )
return ppData[ GetIndex( nChartCol, nChartRow ) ]; return ppData[ GetIndex( nChartCol, nChartRow ) ].get();
return nullptr; return nullptr;
} }
const ScAddress* GetColHeaderPosition( SCCOL nChartCol ) const const ScAddress* GetColHeaderPosition( SCCOL nChartCol ) const
{ {
if ( nChartCol < nColCount ) if ( nChartCol < nColCount )
return ppColHeader[ nChartCol ]; return ppColHeader[ nChartCol ].get();
return nullptr; return nullptr;
} }
const ScAddress* GetRowHeaderPosition( SCROW nChartRow ) const const ScAddress* GetRowHeaderPosition( SCROW nChartRow ) const
{ {
if ( nChartRow < nRowCount ) if ( nChartRow < nRowCount )
return ppRowHeader[ nChartRow ]; return ppRowHeader[ nChartRow ].get();
return nullptr; return nullptr;
} }
}; };
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include <document.hxx> #include <document.hxx>
#include <rechead.hxx> #include <rechead.hxx>
#include <osl/diagnose.h> #include <osl/diagnose.h>
#include <o3tl/make_unique.hxx>
#include <memory> #include <memory>
...@@ -354,7 +355,7 @@ void ScChartPositioner::CreatePositionMap() ...@@ -354,7 +355,7 @@ void ScChartPositioner::CreatePositionMap()
GlueState(); GlueState();
const bool bNoGlue = (eGlue == ScChartGlue::NONE); const bool bNoGlue = (eGlue == ScChartGlue::NONE);
std::unique_ptr<ColumnMap> pCols( new ColumnMap ); ColumnMap aColMap;
SCROW nNoGlueRow = 0; SCROW nNoGlueRow = 0;
for ( size_t i = 0, nRanges = aRangeListRef->size(); i < nRanges; ++i ) for ( size_t i = 0, nRanges = aRangeListRef->size(); i < nRanges; ++i )
{ {
...@@ -367,15 +368,7 @@ void ScChartPositioner::CreatePositionMap() ...@@ -367,15 +368,7 @@ void ScChartPositioner::CreatePositionMap()
static_cast<sal_uLong>(nCol1)); static_cast<sal_uLong>(nCol1));
for ( nCol = nCol1; nCol <= nCol2; ++nCol, ++nInsCol ) for ( nCol = nCol1; nCol <= nCol2; ++nCol, ++nInsCol )
{ {
RowMap* pCol = nullptr; RowMap* pCol = &aColMap[nInsCol];
ColumnMap::const_iterator it = pCols->find( nInsCol );
if ( it == pCols->end() )
{
pCol = new RowMap;
pCols->emplace(nInsCol, pCol);
}
else
pCol = it->second;
// in other table a new ColKey already was created, // in other table a new ColKey already was created,
// the rows must be equal to be filled with Dummy // the rows must be equal to be filled with Dummy
...@@ -384,7 +377,7 @@ void ScChartPositioner::CreatePositionMap() ...@@ -384,7 +377,7 @@ void ScChartPositioner::CreatePositionMap()
{ {
if ( pCol->find( nInsRow ) == pCol->end() ) if ( pCol->find( nInsRow ) == pCol->end() )
{ {
pCol->emplace( nInsRow, new ScAddress( nCol, nRow, nTab ) ); pCol->emplace( nInsRow, o3tl::make_unique<ScAddress>( nCol, nRow, nTab ) );
} }
} }
} }
...@@ -394,13 +387,13 @@ void ScChartPositioner::CreatePositionMap() ...@@ -394,13 +387,13 @@ void ScChartPositioner::CreatePositionMap()
} }
// count of data // count of data
nColCount = static_cast< SCSIZE >( pCols->size()); nColCount = static_cast< SCSIZE >( aColMap.size());
if ( !pCols->empty() ) if ( !aColMap.empty() )
{ {
RowMap* pCol = pCols->begin()->second; RowMap& rCol = aColMap.begin()->second;
if ( bDummyUpperLeft ) if ( bDummyUpperLeft )
(*pCol)[ 0 ] = nullptr; // Dummy for labeling rCol[ 0 ] = nullptr; // Dummy for labeling
nRowCount = static_cast< SCSIZE >( pCol->size()); nRowCount = static_cast< SCSIZE >( rCol.size());
} }
else else
nRowCount = 0; nRowCount = 0;
...@@ -411,27 +404,10 @@ void ScChartPositioner::CreatePositionMap() ...@@ -411,27 +404,10 @@ void ScChartPositioner::CreatePositionMap()
if ( nColCount==0 || nRowCount==0 ) if ( nColCount==0 || nRowCount==0 )
{ // create an entry without data { // create an entry without data
RowMap* pCol; RowMap& rCol = aColMap[0];
if ( !pCols->empty() )
pCol = pCols->begin()->second;
else
{
pCol = new RowMap;
(*pCols)[ 0 ] = pCol;
}
nColCount = 1; nColCount = 1;
if ( !pCol->empty() ) assert ( rCol.empty() );
{ // cannot be if nColCount==0 || nRowCount==0 rCol[ 0 ] = nullptr;
ScAddress* pPos = pCol->begin()->second;
if ( pPos )
{
sal_uLong nCurrentKey = pCol->begin()->first;
delete pPos;
(*pCol)[ nCurrentKey ] = nullptr;
}
}
else
(*pCol)[ 0 ] = nullptr;
nRowCount = 1; nRowCount = 1;
nColAdd = 0; nColAdd = 0;
nRowAdd = 0; nRowAdd = 0;
...@@ -440,26 +416,20 @@ void ScChartPositioner::CreatePositionMap() ...@@ -440,26 +416,20 @@ void ScChartPositioner::CreatePositionMap()
{ {
if ( bNoGlue ) if ( bNoGlue )
{ // fill gaps with Dummies, first column is master { // fill gaps with Dummies, first column is master
RowMap* pFirstCol = pCols->begin()->second; RowMap& rFirstCol = aColMap.begin()->second;
sal_uLong nCount = pFirstCol->size(); sal_uLong nCount = rFirstCol.size();
RowMap::const_iterator it1 = pFirstCol->begin(); RowMap::const_iterator it1 = rFirstCol.begin();
for ( sal_uLong n = 0; n < nCount; n++, ++it1 ) for ( sal_uLong n = 0; n < nCount; n++, ++it1 )
{ {
sal_uLong nKey = it1->first; sal_uLong nKey = it1->first;
for (ColumnMap::const_iterator it2 = ++pCols->begin(); it2 != pCols->end(); ++it2 ) for (ColumnMap::iterator it2 = ++aColMap.begin(); it2 != aColMap.end(); ++it2 )
it2->second->emplace( nKey, nullptr ); // no data it2->second.emplace( nKey, nullptr ); // no data
} }
} }
} }
pPositionMap.reset( new ScChartPositionMap( static_cast<SCCOL>(nColCount), static_cast<SCROW>(nRowCount), pPositionMap.reset( new ScChartPositionMap( static_cast<SCCOL>(nColCount), static_cast<SCROW>(nRowCount),
static_cast<SCCOL>(nColAdd), static_cast<SCROW>(nRowAdd), *pCols ) ); static_cast<SCCOL>(nColAdd), static_cast<SCROW>(nRowAdd), aColMap ) );
// cleanup
for (ColumnMap::const_iterator it = pCols->begin(); it != pCols->end(); ++it )
{ // Only delete tables, not the ScAddress*!
delete it->second;
}
} }
void ScChartPositioner::InvalidateGlue() void ScChartPositioner::InvalidateGlue()
...@@ -470,45 +440,41 @@ void ScChartPositioner::InvalidateGlue() ...@@ -470,45 +440,41 @@ void ScChartPositioner::InvalidateGlue()
ScChartPositionMap::ScChartPositionMap( SCCOL nChartCols, SCROW nChartRows, ScChartPositionMap::ScChartPositionMap( SCCOL nChartCols, SCROW nChartRows,
SCCOL nColAdd, SCROW nRowAdd, ColumnMap& rCols ) : SCCOL nColAdd, SCROW nRowAdd, ColumnMap& rCols ) :
ppData( new ScAddress* [ nChartCols * nChartRows ] ), ppData( new std::unique_ptr<ScAddress> [ nChartCols * nChartRows ] ),
ppColHeader( new ScAddress* [ nChartCols ] ), ppColHeader( new std::unique_ptr<ScAddress> [ nChartCols ] ),
ppRowHeader( new ScAddress* [ nChartRows ] ), ppRowHeader( new std::unique_ptr<ScAddress> [ nChartRows ] ),
nCount( static_cast<sal_uLong>(nChartCols) * nChartRows ), nCount( static_cast<sal_uLong>(nChartCols) * nChartRows ),
nColCount( nChartCols ), nColCount( nChartCols ),
nRowCount( nChartRows ) nRowCount( nChartRows )
{ {
OSL_ENSURE( nColCount && nRowCount, "ScChartPositionMap without dimension" ); OSL_ENSURE( nColCount && nRowCount, "ScChartPositionMap without dimension" );
ColumnMap::const_iterator pColIter = rCols.begin(); ColumnMap::iterator pColIter = rCols.begin();
RowMap* pCol1 = pColIter->second; RowMap& rCol1 = pColIter->second;
RowMap::const_iterator pPos1Iter; RowMap::iterator pPos1Iter;
// row header // row header
pPos1Iter = pCol1->begin(); pPos1Iter = rCol1.begin();
if ( nRowAdd ) if ( nRowAdd )
++pPos1Iter; ++pPos1Iter;
if ( nColAdd ) if ( nColAdd )
{ // independent { // independent
SCROW nRow = 0; SCROW nRow = 0;
for ( ; nRow < nRowCount && pPos1Iter != pCol1->end(); nRow++ ) for ( ; nRow < nRowCount && pPos1Iter != rCol1.end(); nRow++ )
{ {
ppRowHeader[ nRow ] = pPos1Iter->second; ppRowHeader[ nRow ] = std::move(pPos1Iter->second);
++pPos1Iter; ++pPos1Iter;
} }
for ( ; nRow < nRowCount; nRow++ )
ppRowHeader[ nRow ] = nullptr;
} }
else else
{ // copy { // copy
SCROW nRow = 0; SCROW nRow = 0;
for ( ; nRow < nRowCount && pPos1Iter != pCol1->end(); nRow++ ) for ( ; nRow < nRowCount && pPos1Iter != rCol1.end(); nRow++ )
{ {
ppRowHeader[ nRow ] = pPos1Iter->second ? if (pPos1Iter->second)
new ScAddress( *pPos1Iter->second ) : nullptr; ppRowHeader[ nRow ].reset(new ScAddress( *pPos1Iter->second ));
++pPos1Iter; ++pPos1Iter;
} }
for ( ; nRow < nRowCount; nRow++ )
ppRowHeader[ nRow ] = nullptr;
} }
if ( nColAdd ) if ( nColAdd )
{ {
...@@ -521,56 +487,33 @@ ScChartPositionMap::ScChartPositionMap( SCCOL nChartCols, SCROW nChartRows, ...@@ -521,56 +487,33 @@ ScChartPositionMap::ScChartPositionMap( SCCOL nChartCols, SCROW nChartRows,
{ {
if ( pColIter != rCols.end() ) if ( pColIter != rCols.end() )
{ {
RowMap* pCol2 = pColIter->second; RowMap& rCol2 = pColIter->second;
RowMap::const_iterator pPosIter = pCol2->begin(); RowMap::iterator pPosIter = rCol2.begin();
if ( pPosIter != pCol2->end() ) if ( pPosIter != rCol2.end() )
{ {
if ( nRowAdd ) if ( nRowAdd )
{ {
ppColHeader[ nCol ] = pPosIter->second; // independent ppColHeader[ nCol ] = std::move(pPosIter->second); // independent
++pPosIter; ++pPosIter;
} }
else else if ( pPosIter->second )
ppColHeader[ nCol ] = pPosIter->second ? ppColHeader[ nCol ].reset( new ScAddress( *pPosIter->second ) );
new ScAddress( *pPosIter->second ) : nullptr;
} }
SCROW nRow = 0; SCROW nRow = 0;
for ( ; nRow < nRowCount && pPosIter != pCol2->end(); nRow++, nIndex++ ) for ( ; nRow < nRowCount && pPosIter != rCol2.end(); nRow++, nIndex++ )
{ {
ppData[ nIndex ] = pPosIter->second; ppData[ nIndex ] = std::move(pPosIter->second);
++pPosIter; ++pPosIter;
} }
for ( ; nRow < nRowCount; nRow++, nIndex++ )
ppData[ nIndex ] = nullptr;
++pColIter; ++pColIter;
} }
else
{
ppColHeader[ nCol ] = nullptr;
for ( SCROW nRow = 0; nRow < nRowCount; nRow++, nIndex++ )
{
ppData[ nIndex ] = nullptr;
}
}
} }
} }
ScChartPositionMap::~ScChartPositionMap() ScChartPositionMap::~ScChartPositionMap()
{ {
for ( sal_uLong nIndex=0; nIndex < nCount; nIndex++ )
{
delete ppData[nIndex];
}
for ( SCCOL j=0; j < nColCount; j++ )
{
delete ppColHeader[j];
}
for ( SCROW i=0; i < nRowCount; i++ )
{
delete ppRowHeader[i];
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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