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

loplugin:useuniqueptr in xmlsecurity::BufferNode

Change-Id: Ifaea9f2f0ef5b84372b2ba51deacdb4149bb66e1
Reviewed-on: https://gerrit.libreoffice.org/60616
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 18828273
...@@ -278,9 +278,14 @@ bool BufferNode::hasChildren() const ...@@ -278,9 +278,14 @@ bool BufferNode::hasChildren() const
return (!m_vChildren.empty()); return (!m_vChildren.empty());
} }
std::vector< const BufferNode* >* BufferNode::getChildren() const std::vector< std::unique_ptr<BufferNode> > const & BufferNode::getChildren() const
{ {
return new std::vector< const BufferNode* >( m_vChildren ); return m_vChildren;
}
std::vector< std::unique_ptr<BufferNode> > BufferNode::releaseChildren()
{
return std::move(m_vChildren);
} }
const BufferNode* BufferNode::getFirstChild() const const BufferNode* BufferNode::getFirstChild() const
...@@ -307,13 +312,13 @@ const BufferNode* BufferNode::getFirstChild() const ...@@ -307,13 +312,13 @@ const BufferNode* BufferNode::getFirstChild() const
if (!m_vChildren.empty()) if (!m_vChildren.empty())
{ {
rc = const_cast<BufferNode*>(m_vChildren.front()); rc = m_vChildren.front().get();
} }
return rc; return rc;
} }
void BufferNode::addChild(const BufferNode* pChild, sal_Int32 nPosition) void BufferNode::addChild(std::unique_ptr<BufferNode> pChild, sal_Int32 nPosition)
/****** BufferNode/addChild(pChild,nPosition) ******************************** /****** BufferNode/addChild(pChild,nPosition) ********************************
* *
* NAME * NAME
...@@ -339,17 +344,15 @@ void BufferNode::addChild(const BufferNode* pChild, sal_Int32 nPosition) ...@@ -339,17 +344,15 @@ void BufferNode::addChild(const BufferNode* pChild, sal_Int32 nPosition)
{ {
if (nPosition == -1) if (nPosition == -1)
{ {
m_vChildren.push_back( pChild ); m_vChildren.push_back( std::move(pChild) );
} }
else else
{ {
std::vector< const BufferNode* >::iterator ii = m_vChildren.begin(); m_vChildren.insert(m_vChildren.begin() + nPosition, std::move(pChild));
ii += nPosition;
m_vChildren.insert(ii, pChild);
} }
} }
void BufferNode::addChild(const BufferNode* pChild) void BufferNode::addChild(std::unique_ptr<BufferNode> pChild)
/****** BufferNode/addChild() ************************************************ /****** BufferNode/addChild() ************************************************
* *
* NAME * NAME
...@@ -371,14 +374,14 @@ void BufferNode::addChild(const BufferNode* pChild) ...@@ -371,14 +374,14 @@ void BufferNode::addChild(const BufferNode* pChild)
* The new child BufferNode is appended at the end. * The new child BufferNode is appended at the end.
******************************************************************************/ ******************************************************************************/
{ {
addChild(pChild, -1); addChild(std::move(pChild), -1);
} }
void BufferNode::removeChild(const BufferNode* pChild) void BufferNode::removeChild(const BufferNode* pChild)
/****** BufferNode/removeChild *********************************************** /****** BufferNode/removeChild ***********************************************
* *
* NAME * NAME
* removeChild -- removes a child BufferNode from the children list. * removeChild -- removes and deletes a child BufferNode from the children list.
* *
* SYNOPSIS * SYNOPSIS
* removeChild(pChild); * removeChild(pChild);
...@@ -393,7 +396,9 @@ void BufferNode::removeChild(const BufferNode* pChild) ...@@ -393,7 +396,9 @@ void BufferNode::removeChild(const BufferNode* pChild)
* empty * empty
******************************************************************************/ ******************************************************************************/
{ {
auto ii = std::find(m_vChildren.begin(), m_vChildren.end(), pChild); auto ii = std::find_if(m_vChildren.begin(), m_vChildren.end(),
[pChild] (const std::unique_ptr<BufferNode>& i)
{ return i.get() == pChild; });
if (ii != m_vChildren.end()) if (ii != m_vChildren.end())
m_vChildren.erase( ii ); m_vChildren.erase( ii );
} }
...@@ -418,7 +423,9 @@ sal_Int32 BufferNode::indexOfChild(const BufferNode* pChild) const ...@@ -418,7 +423,9 @@ sal_Int32 BufferNode::indexOfChild(const BufferNode* pChild) const
* is not found, -1 is returned. * is not found, -1 is returned.
******************************************************************************/ ******************************************************************************/
{ {
auto ii = std::find(m_vChildren.begin(), m_vChildren.end(), pChild); auto ii = std::find_if(m_vChildren.begin(), m_vChildren.end(),
[pChild] (const std::unique_ptr<BufferNode>& i)
{ return i.get() == pChild; });
if (ii == m_vChildren.end()) if (ii == m_vChildren.end())
return -1; return -1;
...@@ -486,12 +493,12 @@ const BufferNode* BufferNode::isAncestor(const BufferNode* pDescendant) const ...@@ -486,12 +493,12 @@ const BufferNode* BufferNode::isAncestor(const BufferNode* pDescendant) const
if (pDescendant != nullptr) if (pDescendant != nullptr)
{ {
auto ii = std::find_if(m_vChildren.cbegin(), m_vChildren.cend(), auto ii = std::find_if(m_vChildren.cbegin(), m_vChildren.cend(),
[&pDescendant](const BufferNode* pChild) { [&pDescendant](const std::unique_ptr<BufferNode>& pChild) {
return (pChild == pDescendant) || (pChild->isAncestor(pDescendant) != nullptr); return (pChild.get() == pDescendant) || (pChild->isAncestor(pDescendant) != nullptr);
}); });
if (ii != m_vChildren.end()) if (ii != m_vChildren.end())
rc = const_cast<BufferNode*>(*ii); rc = ii->get();
} }
return rc; return rc;
...@@ -636,9 +643,8 @@ void BufferNode::notifyBranch() ...@@ -636,9 +643,8 @@ void BufferNode::notifyBranch()
* empty * empty
******************************************************************************/ ******************************************************************************/
{ {
for( const BufferNode* ii : m_vChildren ) for( std::unique_ptr<BufferNode>& pBufferNode : m_vChildren )
{ {
BufferNode* pBufferNode = const_cast<BufferNode*>(ii);
pBufferNode->elementCollectorNotify(); pBufferNode->elementCollectorNotify();
pBufferNode->notifyBranch(); pBufferNode->notifyBranch();
} }
...@@ -756,7 +762,7 @@ bool BufferNode::isECInSubTreeIncluded(sal_Int32 nIgnoredSecurityId) const ...@@ -756,7 +762,7 @@ bool BufferNode::isECInSubTreeIncluded(sal_Int32 nIgnoredSecurityId) const
if ( !rc ) if ( !rc )
{ {
rc = std::any_of(m_vChildren.begin(), m_vChildren.end(), rc = std::any_of(m_vChildren.begin(), m_vChildren.end(),
[nIgnoredSecurityId](const BufferNode* pBufferNode) { [nIgnoredSecurityId](const std::unique_ptr<BufferNode>& pBufferNode) {
return pBufferNode->isECInSubTreeIncluded(nIgnoredSecurityId); return pBufferNode->isECInSubTreeIncluded(nIgnoredSecurityId);
}); });
} }
...@@ -832,7 +838,7 @@ bool BufferNode::isBlockerInSubTreeIncluded(sal_Int32 nIgnoredSecurityId) const ...@@ -832,7 +838,7 @@ bool BufferNode::isBlockerInSubTreeIncluded(sal_Int32 nIgnoredSecurityId) const
******************************************************************************/ ******************************************************************************/
{ {
return std::any_of(m_vChildren.begin(), m_vChildren.end(), return std::any_of(m_vChildren.begin(), m_vChildren.end(),
[nIgnoredSecurityId](const BufferNode* pBufferNode) { [nIgnoredSecurityId](const std::unique_ptr<BufferNode>& pBufferNode) {
ElementMark* pBlocker = pBufferNode->getBlocker(); ElementMark* pBlocker = pBufferNode->getBlocker();
return (pBlocker != nullptr && return (pBlocker != nullptr &&
(nIgnoredSecurityId == cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID || (nIgnoredSecurityId == cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID ||
...@@ -864,15 +870,15 @@ const BufferNode* BufferNode::getNextChild(const BufferNode* pChild) const ...@@ -864,15 +870,15 @@ const BufferNode* BufferNode::getNextChild(const BufferNode* pChild) const
BufferNode* rc = nullptr; BufferNode* rc = nullptr;
bool bChildFound = false; bool bChildFound = false;
for( const BufferNode* i : m_vChildren ) for( std::unique_ptr<BufferNode> const & i : m_vChildren )
{ {
if (bChildFound) if (bChildFound)
{ {
rc = const_cast<BufferNode*>(i); rc = i.get();
break; break;
} }
if( i == pChild ) if( i.get() == pChild )
{ {
bChildFound = true; bChildFound = true;
} }
...@@ -881,34 +887,4 @@ const BufferNode* BufferNode::getNextChild(const BufferNode* pChild) const ...@@ -881,34 +887,4 @@ const BufferNode* BufferNode::getNextChild(const BufferNode* pChild) const
return rc; return rc;
} }
void BufferNode::freeAllChildren()
/****** BufferNode/freeAllChildren *******************************************
*
* NAME
* freeAllChildren -- free all his child BufferNode.
*
* SYNOPSIS
* freeAllChildren();
*
* FUNCTION
* see NAME
*
* INPUTS
* empty
*
* RESULT
* empty
******************************************************************************/
{
for( const BufferNode* i : m_vChildren )
{
BufferNode *pChild = const_cast<BufferNode *>(i);
pChild->freeAllChildren();
delete pChild;
}
m_vChildren.clear();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp> #include <com/sun/star/xml/wrapper/XXMLElementWrapper.hpp>
#include <memory>
#include <vector> #include <vector>
class ElementMark; class ElementMark;
...@@ -49,7 +50,7 @@ private: ...@@ -49,7 +50,7 @@ private:
BufferNode* m_pParent; BufferNode* m_pParent;
/* all child BufferNodes */ /* all child BufferNodes */
std::vector< const BufferNode* > m_vChildren; std::vector< std::unique_ptr<BufferNode> > m_vChildren;
/* all ElementCollector holding this BufferNode */ /* all ElementCollector holding this BufferNode */
std::vector< const ElementCollector* > m_vElementCollectors; std::vector< const ElementCollector* > m_vElementCollectors;
...@@ -89,10 +90,11 @@ public: ...@@ -89,10 +90,11 @@ public:
OUString printChildren() const; OUString printChildren() const;
bool hasAnything() const; bool hasAnything() const;
bool hasChildren() const; bool hasChildren() const;
std::vector< const BufferNode* >* getChildren() const; std::vector< std::unique_ptr< BufferNode> > const & getChildren() const;
std::vector< std::unique_ptr< BufferNode> > releaseChildren();
const BufferNode* getFirstChild() const; const BufferNode* getFirstChild() const;
void addChild(const BufferNode* pChild, sal_Int32 nPosition); void addChild(std::unique_ptr<BufferNode> pChild, sal_Int32 nPosition);
void addChild(const BufferNode* pChild); void addChild(std::unique_ptr<BufferNode> pChild);
void removeChild(const BufferNode* pChild); void removeChild(const BufferNode* pChild);
sal_Int32 indexOfChild(const BufferNode* pChild) const; sal_Int32 indexOfChild(const BufferNode* pChild) const;
const BufferNode* getParent() const { return m_pParent;} const BufferNode* getParent() const { return m_pParent;}
...@@ -106,7 +108,6 @@ public: ...@@ -106,7 +108,6 @@ public:
css::xml::wrapper::XXMLElementWrapper >& xXMLElement); css::xml::wrapper::XXMLElementWrapper >& xXMLElement);
void notifyBranch(); void notifyBranch();
void elementCollectorNotify(); void elementCollectorNotify();
void freeAllChildren();
}; };
#endif #endif
......
...@@ -56,11 +56,7 @@ SAXEventKeeperImpl::~SAXEventKeeperImpl() ...@@ -56,11 +56,7 @@ SAXEventKeeperImpl::~SAXEventKeeperImpl()
/* /*
* delete the BufferNode tree * delete the BufferNode tree
*/ */
if (m_pRootBufferNode != nullptr) m_pRootBufferNode.reset();
{
m_pRootBufferNode->freeAllChildren();
m_pRootBufferNode.reset();
}
m_pCurrentBufferNode = m_pCurrentBlockingBufferNode = nullptr; m_pCurrentBufferNode = m_pCurrentBlockingBufferNode = nullptr;
...@@ -103,7 +99,7 @@ void SAXEventKeeperImpl::setCurrentBufferNode(BufferNode* pBufferNode) ...@@ -103,7 +99,7 @@ void SAXEventKeeperImpl::setCurrentBufferNode(BufferNode* pBufferNode)
if (pBufferNode->getParent() == nullptr) if (pBufferNode->getParent() == nullptr)
{ {
m_pCurrentBufferNode->addChild(pBufferNode); m_pCurrentBufferNode->addChild(std::unique_ptr<BufferNode>(pBufferNode));
pBufferNode->setParent(m_pCurrentBufferNode); pBufferNode->setParent(m_pCurrentBufferNode);
} }
...@@ -329,14 +325,12 @@ OUString SAXEventKeeperImpl::printBufferNode( ...@@ -329,14 +325,12 @@ OUString SAXEventKeeperImpl::printBufferNode(
} }
rc.append("\n"); rc.append("\n");
std::vector< const BufferNode* >* vChildren = pBufferNode->getChildren(); std::vector< std::unique_ptr<BufferNode> > const & vChildren = pBufferNode->getChildren();
for( const auto& jj : *vChildren ) for( const auto& jj : vChildren )
{ {
rc.append(printBufferNode(jj, nIndent+4)); rc.append(printBufferNode(jj.get(), nIndent+4));
} }
delete vChildren;
return rc.makeStringAndClear(); return rc.makeStringAndClear();
} }
...@@ -358,20 +352,18 @@ cssu::Sequence< cssu::Reference< cssxw::XXMLElementWrapper > > ...@@ -358,20 +352,18 @@ cssu::Sequence< cssu::Reference< cssxw::XXMLElementWrapper > >
* list - the child Elements list. * list - the child Elements list.
******************************************************************************/ ******************************************************************************/
{ {
std::vector< const BufferNode* >* vChildren = pBufferNode->getChildren(); std::vector< std::unique_ptr<BufferNode> > const & vChildren = pBufferNode->getChildren();
cssu::Sequence < cssu::Reference< cssu::Sequence < cssu::Reference<
cssxw::XXMLElementWrapper > > aChildrenCollection ( vChildren->size()); cssxw::XXMLElementWrapper > > aChildrenCollection ( vChildren.size());
sal_Int32 nIndex = 0; sal_Int32 nIndex = 0;
for( const auto& i : *vChildren ) for( const auto& i : vChildren )
{ {
aChildrenCollection[nIndex] = i->getXMLElement(); aChildrenCollection[nIndex] = i->getXMLElement();
nIndex++; nIndex++;
} }
delete vChildren;
return aChildrenCollection; return aChildrenCollection;
} }
...@@ -501,23 +493,16 @@ void SAXEventKeeperImpl::smashBufferNode( ...@@ -501,23 +493,16 @@ void SAXEventKeeperImpl::smashBufferNode(
sal_Int32 nIndex = pParent->indexOfChild(pBufferNode); sal_Int32 nIndex = pParent->indexOfChild(pBufferNode);
std::vector< const BufferNode* >* vChildren = pBufferNode->getChildren(); std::vector< std::unique_ptr<BufferNode> > vChildren = pBufferNode->releaseChildren();
pParent->removeChild(pBufferNode); pParent->removeChild(pBufferNode); // delete buffernode
pBufferNode->setParent(nullptr);
for( const auto& i : *vChildren ) for( auto& i : vChildren )
{ {
const_cast<BufferNode *>(i)->setParent(pParent); i->setParent(pParent);
pParent->addChild(i, nIndex); pParent->addChild(std::move(i), nIndex);
nIndex++; nIndex++;
} }
delete vChildren;
/*
* delete the BufferNode
*/
delete pBufferNode;
} }
} }
......
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