Files
libreoffice/comphelper/source/container/container.cxx

141 lines
5.7 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-29 14:02:24 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-29 10:28:15 +00:00
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/container/XIndexAccess.hpp>
#include <com/sun/star/container/XChild.hpp>
#include <comphelper/container.hxx>
#include <o3tl/any.hxx>
2000-09-29 10:28:15 +00:00
#include <osl/diagnose.h>
2000-09-29 10:28:15 +00:00
namespace comphelper
{
IndexAccessIterator::IndexAccessIterator(css::uno::Reference< css::uno::XInterface> const & xStartingPoint)
2000-09-29 10:28:15 +00:00
:m_xStartingPoint(xStartingPoint)
,m_xCurrentObject(nullptr)
2000-09-29 10:28:15 +00:00
{
2001-03-22 12:33:25 +00:00
OSL_ENSURE(m_xStartingPoint.is(), "IndexAccessIterator::IndexAccessIterator : no starting point !");
2000-09-29 10:28:15 +00:00
}
IndexAccessIterator::~IndexAccessIterator() {}
css::uno::Reference< css::uno::XInterface> const & IndexAccessIterator::Next()
2000-09-29 10:28:15 +00:00
{
bool bCheckingStartingPoint = !m_xCurrentObject.is();
// Is the current node the starting point?
bool bAlreadyCheckedCurrent = m_xCurrentObject.is();
// Have I already tested the current node through ShouldHandleElement?
2000-09-29 10:28:15 +00:00
if (!m_xCurrentObject.is())
m_xCurrentObject = m_xStartingPoint;
css::uno::Reference< css::uno::XInterface> xSearchLoop( m_xCurrentObject);
bool bHasMoreToSearch = true;
bool bFoundSomething = false;
2000-09-29 10:28:15 +00:00
while (!bFoundSomething && bHasMoreToSearch)
{
// Priming loop
2000-09-29 10:28:15 +00:00
if (!bAlreadyCheckedCurrent && ShouldHandleElement(xSearchLoop))
{
m_xCurrentObject = xSearchLoop;
bFoundSomething = true;
2000-09-29 10:28:15 +00:00
}
else
{
// First, check to see if there's a match below
css::uno::Reference< css::container::XIndexAccess> xContainerAccess(xSearchLoop, css::uno::UNO_QUERY);
2000-09-29 10:28:15 +00:00
if (xContainerAccess.is() && xContainerAccess->getCount() && ShouldStepInto(xContainerAccess))
{
css::uno::Any aElement(xContainerAccess->getByIndex(0));
xSearchLoop = *o3tl::doAccess<css::uno::Reference<css::uno::XInterface>>(aElement);
bCheckingStartingPoint = false;
2000-09-29 10:28:15 +00:00
m_arrChildIndizies.push_back((sal_Int32)0);
}
else
{ // otherwise, look above and to the right, if possible
2000-09-29 10:28:15 +00:00
while (m_arrChildIndizies.size() > 0)
{ // If the list isn't empty and there's nothing above
css::uno::Reference< css::container::XChild> xChild(xSearchLoop, css::uno::UNO_QUERY);
2014-03-29 18:09:41 +01:00
OSL_ENSURE(xChild.is(), "IndexAccessIterator::Next : a content has no appropriate interface !");
2000-09-29 10:28:15 +00:00
css::uno::Reference< css::uno::XInterface> xParent( xChild->getParent());
xContainerAccess.set(xParent, css::uno::UNO_QUERY);
2001-03-22 12:33:25 +00:00
OSL_ENSURE(xContainerAccess.is(), "IndexAccessIterator::Next : a content has an invalid parent !");
2000-09-29 10:28:15 +00:00
2012-03-10 21:58:25 +01:00
// Remove the index that SearchLoop had within this parent from my stack
2000-09-29 10:28:15 +00:00
sal_Int32 nOldSearchChildIndex = m_arrChildIndizies[m_arrChildIndizies.size() - 1];
m_arrChildIndizies.pop_back();
if (nOldSearchChildIndex < xContainerAccess->getCount() - 1)
{ // Move to the right in this row
2000-09-29 10:28:15 +00:00
++nOldSearchChildIndex;
2012-03-10 21:58:25 +01:00
// and check the next child
css::uno::Any aElement(xContainerAccess->getByIndex(nOldSearchChildIndex));
xSearchLoop = *o3tl::doAccess<css::uno::Reference<css::uno::XInterface>>(aElement);
bCheckingStartingPoint = false;
2012-03-10 21:58:25 +01:00
// and update its position in the list.
m_arrChildIndizies.push_back(nOldSearchChildIndex);
2000-09-29 10:28:15 +00:00
break;
}
// Finally, if there's nothing more to do in this row (to the right), we'll move on to the next row.
2000-09-29 10:28:15 +00:00
xSearchLoop = xParent;
bCheckingStartingPoint = false;
2000-09-29 10:28:15 +00:00
}
2012-02-19 16:59:40 +04:00
if (m_arrChildIndizies.empty() && !bCheckingStartingPoint)
{ //This is the case if there is nothing to the right in the original search loop
bHasMoreToSearch = false;
2000-09-29 10:28:15 +00:00
}
}
if (bHasMoreToSearch)
{ // If there is still a node in the tree which can be tested
2000-09-29 10:28:15 +00:00
if (ShouldHandleElement(xSearchLoop))
{
m_xCurrentObject = xSearchLoop;
bFoundSomething = true;
2000-09-29 10:28:15 +00:00
}
else
if (bCheckingStartingPoint)
bHasMoreToSearch = false;
bAlreadyCheckedCurrent = true;
2000-09-29 10:28:15 +00:00
}
}
}
if (!bFoundSomething)
{
2012-02-19 16:59:40 +04:00
OSL_ENSURE(m_arrChildIndizies.empty(), "IndexAccessIterator::Next : items left on stack ! how this ?");
2000-09-29 10:28:15 +00:00
Invalidate();
}
return m_xCurrentObject;
}
2000-09-29 10:28:15 +00:00
} // namespace comphelper
2000-09-29 10:28:15 +00:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */