include/o3tl/sorted_vector.hxx | 48 +++++++++++++++++++++++++-- sw/source/filter/html/htmltabw.cxx | 6 +-- sw/source/filter/inc/wrtswtbl.hxx | 6 +-- sw/source/filter/writer/wrtswtbl.cxx | 31 +++++++++-------- sw/source/filter/ww8/docxattributeoutput.cxx | 4 +- sw/source/filter/ww8/rtfattributeoutput.cxx | 8 ++-- 6 files changed, 74 insertions(+), 29 deletions(-)
New commits: commit fad919eb0d30b2303193e1c00ba765514957652c Author: Noel Grandin <noel.gran...@collabora.co.uk> AuthorDate: Mon Sep 3 11:32:58 2018 +0200 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Thu Sep 6 09:41:48 2018 +0200 make SwWriteTableRows be a vector of std::unique_ptr and update o3tl::sorted_vector to handle that Change-Id: I11a9ec3ec09f835cbd7e49ccda133b9f210d761e Reviewed-on: https://gerrit.libreoffice.org/59931 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/include/o3tl/sorted_vector.hxx b/include/o3tl/sorted_vector.hxx index 9141c592ffd8..254d627604df 100644 --- a/include/o3tl/sorted_vector.hxx +++ b/include/o3tl/sorted_vector.hxx @@ -13,6 +13,8 @@ #include <vector> #include <algorithm> #include <functional> +#include <memory> +#include <type_traits> namespace o3tl { @@ -27,8 +29,11 @@ struct find_unique; @tpl Compare comparison method @tpl Find look up index of a Value in the array */ -template<typename Value, typename Compare = std::less<Value>, - template<typename, typename> class Find = find_unique > +template< + typename Value, + typename Compare = std::less<Value>, + template<typename, typename> class Find = find_unique, + bool = std::is_copy_constructible<Value>::value > class sorted_vector { private: @@ -42,6 +47,17 @@ public: // MODIFIERS + std::pair<const_iterator,bool> insert( Value&& x ) + { + std::pair<const_iterator, bool> const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); + if (!ret.second) + { + const_iterator const it = m_vector.insert(m_vector.begin() + (ret.first - m_vector.begin()), std::move(x)); + return std::make_pair(it, true); + } + return std::make_pair(ret.first, false); + } + std::pair<const_iterator,bool> insert( const Value& x ) { std::pair<const_iterator, bool> const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); @@ -197,6 +213,26 @@ private: vector_t m_vector; }; +/* Specialise the template for cases like Value = std::unique_ptr<T>, where + MSVC2017 needs some help +*/ +template< + typename Value, + typename Compare, + template<typename, typename> class Find > +class sorted_vector<Value,Compare,Find,false> : public sorted_vector<Value, Compare, Find, true> +{ +public: + using sorted_vector<Value, Compare, Find, true>::sorted_vector; + + sorted_vector(sorted_vector const&) = delete; + sorted_vector& operator=(sorted_vector const&) = delete; + + sorted_vector() = default; + sorted_vector(sorted_vector&&) = default; + sorted_vector& operator=(sorted_vector&&) = default; +}; + /** Implements an ordering function over a pointer, where the comparison uses the < operator on the pointed-to types. Very useful for the cases where we put pointers to objects inside a sorted_vector. @@ -209,6 +245,14 @@ template <class T> struct less_ptr_to } }; +template <class T> struct less_uniqueptr_to +{ + bool operator() ( std::unique_ptr<T> const& lhs, std::unique_ptr<T> const& rhs ) const + { + return (*lhs) < (*rhs); + } +}; + /** the elements are totally ordered by Compare, for no 2 elements !Compare(a,b) && !Compare(b,a) is true */ diff --git a/sw/source/filter/html/htmltabw.cxx b/sw/source/filter/html/htmltabw.cxx index 0d400245efb9..d3f76c41adb1 100644 --- a/sw/source/filter/html/htmltabw.cxx +++ b/sw/source/filter/html/htmltabw.cxx @@ -564,10 +564,10 @@ void SwHTMLWrtTable::Write( SwHTMLWriter& rWrt, sal_Int16 eAlign, // determine value of RULES bool bRowsHaveBorder = false; bool bRowsHaveBorderOnly = true; - SwWriteTableRow *pRow = m_aRows[0]; + SwWriteTableRow *pRow = m_aRows[0].get(); for( SwWriteTableRows::size_type nRow=1; nRow < m_aRows.size(); ++nRow ) { - SwWriteTableRow *pNextRow = m_aRows[nRow]; + SwWriteTableRow *pNextRow = m_aRows[nRow].get(); bool bBorder = ( pRow->bBottomBorder || pNextRow->bTopBorder ); bRowsHaveBorder |= bBorder; bRowsHaveBorderOnly &= bBorder; @@ -808,7 +808,7 @@ void SwHTMLWrtTable::Write( SwHTMLWriter& rWrt, sal_Int16 eAlign, for( SwWriteTableRows::size_type nRow = 0; nRow < m_aRows.size(); ++nRow ) { - const SwWriteTableRow *pRow2 = m_aRows[nRow]; + const SwWriteTableRow *pRow2 = m_aRows[nRow].get(); OutTableCells( rWrt, pRow2->GetCells(), pRow2->GetBackground() ); if( !m_nCellSpacing && nRow < m_aRows.size()-1 && pRow2->bBottomBorder && diff --git a/sw/source/filter/inc/wrtswtbl.hxx b/sw/source/filter/inc/wrtswtbl.hxx index 2537dbb5dd29..7c61ba07de8c 100644 --- a/sw/source/filter/inc/wrtswtbl.hxx +++ b/sw/source/filter/inc/wrtswtbl.hxx @@ -151,10 +151,8 @@ inline bool SwWriteTableRow::operator<( const SwWriteTableRow& rRow ) const return nPos < rRow.nPos - (mbUseLayoutHeights ? 0 : ROWFUZZY); } -class SwWriteTableRows : public o3tl::sorted_vector<SwWriteTableRow*, o3tl::less_ptr_to<SwWriteTableRow> > { -public: - ~SwWriteTableRows() { DeleteAndDestroyAll(); } -}; +using SwWriteTableRows + = o3tl::sorted_vector< std::unique_ptr<SwWriteTableRow>, o3tl::less_uniqueptr_to<SwWriteTableRow> >; class SW_DLLPUBLIC SwWriteTableCol { diff --git a/sw/source/filter/writer/wrtswtbl.cxx b/sw/source/filter/writer/wrtswtbl.cxx index e0a7273e8c76..0fc83c31b38a 100644 --- a/sw/source/filter/writer/wrtswtbl.cxx +++ b/sw/source/filter/writer/wrtswtbl.cxx @@ -367,7 +367,7 @@ long SwWriteTable::GetAbsHeight(long nRawHeight, size_t const nRow, if( nRow==0 ) { nRawHeight -= m_nCellSpacing; - pRow = m_aRows[nRow]; + pRow = m_aRows[nRow].get(); if( pRow->HasTopBorder() ) nRawHeight -= m_nBorder; } @@ -376,7 +376,7 @@ long SwWriteTable::GetAbsHeight(long nRawHeight, size_t const nRow, if( nRow+nRowSpan==m_aRows.size() ) { if( !pRow || nRowSpan > 1 ) - pRow = m_aRows[nRow+nRowSpan-1]; + pRow = m_aRows[nRow+nRowSpan-1].get(); if( pRow->HasBottomBorder() ) nRawHeight -= m_nBorder; } @@ -432,9 +432,8 @@ void SwWriteTable::CollectTableRowsCols( long nStartRPos, nLineHeight /= nLines - nLine; // divided through the number of remaining sub rows nRPos += nLineHeight; } - SwWriteTableRow *pRow = new SwWriteTableRow( nRPos, m_bUseLayoutHeights); - if( !m_aRows.insert( pRow ).second ) - delete pRow; + std::unique_ptr<SwWriteTableRow> pRow(new SwWriteTableRow( nRPos, m_bUseLayoutHeights)); + m_aRows.insert( std::move(pRow) ); } else { @@ -444,7 +443,9 @@ void SwWriteTable::CollectTableRowsCols( long nStartRPos, nRPos = nStartRPos + nParentLineHeight; #if OSL_DEBUG_LEVEL > 0 SwWriteTableRow aSrchRow( nRPos, m_bUseLayoutHeights ); - OSL_ENSURE( m_aRows.find( &aSrchRow ) != m_aRows.end(), "Parent-Row not found" ); + OSL_ENSURE( std::find_if(m_aRows.begin(), m_aRows.end(), + [&](std::unique_ptr<SwWriteTableRow> const & p) + { return *p == aSrchRow; }) != m_aRows.end(), "Parent-Row not found" ); SwWriteTableRow aRowCheckPos(nCheckPos,m_bUseLayoutHeights); SwWriteTableRow aRowRPos(nRPos,m_bUseLayoutHeights); OSL_ENSURE( !m_bUseLayoutHeights || @@ -561,7 +562,9 @@ void SwWriteTable::FillTableRowsCols( long nStartRPos, sal_uInt16 nStartRow, // And their index sal_uInt16 nOldRow = nRow; SwWriteTableRow aSrchRow( nRPos,m_bUseLayoutHeights ); - SwWriteTableRows::const_iterator it2 = m_aRows.find( &aSrchRow ); + SwWriteTableRows::const_iterator it2 = std::find_if(m_aRows.begin(), m_aRows.end(), + [&](std::unique_ptr<SwWriteTableRow> const &p) + { return *p == aSrchRow; }); // coupled methods out of sync ... assert( it2 != m_aRows.end() ); @@ -575,8 +578,8 @@ void SwWriteTable::FillTableRowsCols( long nStartRPos, sal_uInt16 nStartRow, --nOldRow; } - SwWriteTableRow *pRow = m_aRows[nOldRow]; - SwWriteTableRow *pEndRow = m_aRows[nRow]; + SwWriteTableRow *pRow = m_aRows[nOldRow].get(); + SwWriteTableRow *pEndRow = m_aRows[nRow].get(); if( nLine+1==nNumOfHeaderRows && nParentLineHeight==0 ) m_nHeadEndRow = nRow; @@ -798,17 +801,17 @@ SwWriteTable::SwWriteTable(const SwTable* pTable, const SwHTMLTableLayout *pLayo for( sal_uInt16 nRow=0; nRow<nRows; ++nRow ) { - SwWriteTableRow *pRow = - new SwWriteTableRow( (nRow+1)*ROW_DFLT_HEIGHT, m_bUseLayoutHeights ); + std::unique_ptr<SwWriteTableRow> pRow( + new SwWriteTableRow( (nRow+1)*ROW_DFLT_HEIGHT, m_bUseLayoutHeights )); pRow->nTopBorder = 0; pRow->nBottomBorder = 0; - m_aRows.insert( pRow ); + m_aRows.insert( std::move(pRow) ); } // And now fill with life for( sal_uInt16 nRow=0; nRow<nRows; ++nRow ) { - SwWriteTableRow *pRow = m_aRows[nRow]; + SwWriteTableRow *pRow = m_aRows[nRow].get(); bool bHeightExported = false; for( sal_uInt16 nCol=0; nCol<nCols; nCol++ ) @@ -859,7 +862,7 @@ SwWriteTable::SwWriteTable(const SwTable* pTable, const SwHTMLTableLayout *pLayo if( !(nBorderMask & 1) ) pRow->bTopBorder = false; - SwWriteTableRow *pEndRow = m_aRows[nRow+nRowSpan-1]; + SwWriteTableRow *pEndRow = m_aRows[nRow+nRowSpan-1].get(); if( !(nBorderMask & 2) ) pEndRow->bBottomBorder = false; diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx index adb5e65d913b..af29b9fcd4e0 100644 --- a/sw/source/filter/ww8/docxattributeoutput.cxx +++ b/sw/source/filter/ww8/docxattributeoutput.cxx @@ -3254,7 +3254,7 @@ void DocxAttributeOutput::TableCellProperties( ww8::WW8TableNodeInfoInner::Point // Horizontal spans const SwWriteTableRows& rRows = m_xTableWrt->GetRows( ); - SwWriteTableRow *pRow = rRows[ nRow ]; + SwWriteTableRow *pRow = rRows[ nRow ].get(); const SwWriteTableCells& rTableCells = pRow->GetCells(); if (nCell < rTableCells.size() ) { @@ -4081,7 +4081,7 @@ void DocxAttributeOutput::TableVerticalCell( ww8::WW8TableNodeInfoInner::Pointer } const SwWriteTableRows& rRows = m_xTableWrt->GetRows( ); - SwWriteTableRow *pRow = rRows[ pTableTextNodeInfoInner->getRow( ) ]; + SwWriteTableRow *pRow = rRows[ pTableTextNodeInfoInner->getRow( ) ].get(); sal_uInt32 nCell = pTableTextNodeInfoInner->getCell(); const SwWriteTableCells& rTableCells = pRow->GetCells(); if (nCell < rTableCells.size() ) diff --git a/sw/source/filter/ww8/rtfattributeoutput.cxx b/sw/source/filter/ww8/rtfattributeoutput.cxx index 86bbb4201a2d..8fba8b326d63 100644 --- a/sw/source/filter/ww8/rtfattributeoutput.cxx +++ b/sw/source/filter/ww8/rtfattributeoutput.cxx @@ -653,7 +653,7 @@ void RtfAttributeOutput::TableDefinition( // The cell-dependent properties const double fWidthRatio = m_pTableWrt->GetAbsWidthRatio(); const SwWriteTableRows& aRows = m_pTableWrt->GetRows(); - SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()]; + SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()].get(); SwTwips nSz = 0; // Not using m_nTableDepth, which is not yet incremented here. @@ -687,7 +687,7 @@ void RtfAttributeOutput::TableDefaultBorders( */ const SwWriteTableRows& aRows = m_pTableWrt->GetRows(); - SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()]; + SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()].get(); const SwWriteTableCell* const pCell = pRow->GetCells()[pTableTextNodeInfoInner->getCell()].get(); const SwFrameFormat* pCellFormat = pCell->GetBox()->GetFrameFormat(); @@ -742,7 +742,7 @@ void RtfAttributeOutput::TableBackgrounds( aColor = pRowColorProp->GetColor(); const SwWriteTableRows& aRows = m_pTableWrt->GetRows(); - SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()]; + SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()].get(); const SwWriteTableCell* const pCell = pRow->GetCells()[pTableTextNodeInfoInner->getCell()].get(); const SwFrameFormat* pCellFormat = pCell->GetBox()->GetFrameFormat(); @@ -830,7 +830,7 @@ void RtfAttributeOutput::TableVerticalCell( ww8::WW8TableNodeInfoInner::Pointer_t pTableTextNodeInfoInner) { const SwWriteTableRows& aRows = m_pTableWrt->GetRows(); - SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()]; + SwWriteTableRow* pRow = aRows[pTableTextNodeInfoInner->getRow()].get(); const SwWriteTableCell* const pCell = pRow->GetCells()[pTableTextNodeInfoInner->getCell()].get(); const SwFrameFormat* pCellFormat = pCell->GetBox()->GetFrameFormat(); _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits