Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/comphelper/source/container/container.cxx
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <com/sun/star/uno/XInterface.hpp>
21
#include <com/sun/star/container/XIndexAccess.hpp>
22
#include <com/sun/star/container/XChild.hpp>
23
#include <comphelper/container.hxx>
24
#include <o3tl/any.hxx>
25
#include <utility>
26
#include <osl/diagnose.h>
27
28
29
namespace comphelper
30
{
31
32
33
IndexAccessIterator::IndexAccessIterator(css::uno::Reference< css::uno::XInterface> xStartingPoint)
34
0
    :m_xStartingPoint(std::move(xStartingPoint))
35
0
{
36
0
    OSL_ENSURE(m_xStartingPoint.is(), "IndexAccessIterator::IndexAccessIterator : no starting point !");
37
0
}
38
39
0
IndexAccessIterator::~IndexAccessIterator() {}
40
41
42
css::uno::Reference< css::uno::XInterface> const & IndexAccessIterator::Next()
43
0
{
44
0
    bool bCheckingStartingPoint = !m_xCurrentObject.is();
45
        // Is the current node the starting point?
46
0
    bool bAlreadyCheckedCurrent = m_xCurrentObject.is();
47
        // Have I already tested the current node through ShouldHandleElement?
48
0
    if (!m_xCurrentObject.is())
49
0
        m_xCurrentObject = m_xStartingPoint;
50
51
0
    css::uno::Reference< css::uno::XInterface> xSearchLoop( m_xCurrentObject);
52
0
    bool bHasMoreToSearch = true;
53
0
    bool bFoundSomething = false;
54
0
    while (!bFoundSomething && bHasMoreToSearch)
55
0
    {
56
        // Priming loop
57
0
        if (!bAlreadyCheckedCurrent && ShouldHandleElement(xSearchLoop))
58
0
        {
59
0
            m_xCurrentObject = xSearchLoop;
60
0
            bFoundSomething = true;
61
0
        }
62
0
        else
63
0
        {
64
            // First, check to see if there's a match below
65
0
            css::uno::Reference< css::container::XIndexAccess> xContainerAccess(xSearchLoop, css::uno::UNO_QUERY);
66
0
            if (xContainerAccess.is() && xContainerAccess->getCount() && ShouldStepInto(xContainerAccess))
67
0
            {
68
0
                css::uno::Any aElement(xContainerAccess->getByIndex(0));
69
0
                xSearchLoop = *o3tl::doAccess<css::uno::Reference<css::uno::XInterface>>(aElement);
70
0
                bCheckingStartingPoint = false;
71
72
0
                m_arrChildIndizies.push_back(sal_Int32(0));
73
0
            }
74
0
            else
75
0
            {   // otherwise, look above and to the right, if possible
76
0
                while (!m_arrChildIndizies.empty())
77
0
                {   // If the list isn't empty and there's nothing above
78
0
                    css::uno::Reference< css::container::XChild> xChild(xSearchLoop, css::uno::UNO_QUERY);
79
0
                    OSL_ENSURE(xChild.is(), "IndexAccessIterator::Next : a content has no appropriate interface !");
80
81
0
                    css::uno::Reference< css::uno::XInterface> xParent( xChild->getParent());
82
0
                    xContainerAccess.set(xParent, css::uno::UNO_QUERY);
83
0
                    OSL_ENSURE(xContainerAccess.is(), "IndexAccessIterator::Next : a content has an invalid parent !");
84
85
                    // Remove the index that SearchLoop had within this parent from my stack
86
0
                    sal_Int32 nOldSearchChildIndex = m_arrChildIndizies[m_arrChildIndizies.size() - 1];
87
0
                    m_arrChildIndizies.pop_back();
88
89
0
                    if (nOldSearchChildIndex < xContainerAccess->getCount() - 1)
90
0
                    {   // Move to the right in this row
91
0
                        ++nOldSearchChildIndex;
92
                        // and check the next child
93
0
                        css::uno::Any aElement(xContainerAccess->getByIndex(nOldSearchChildIndex));
94
0
                        xSearchLoop = *o3tl::doAccess<css::uno::Reference<css::uno::XInterface>>(aElement);
95
0
                        bCheckingStartingPoint = false;
96
                        // and update its position in the list.
97
0
                        m_arrChildIndizies.push_back(nOldSearchChildIndex);
98
99
0
                        break;
100
0
                    }
101
                    // Finally, if there's nothing more to do in this row (to the right), we'll move on to the next row.
102
0
                    xSearchLoop = std::move(xParent);
103
0
                    bCheckingStartingPoint = false;
104
0
                }
105
106
0
                if (m_arrChildIndizies.empty() && !bCheckingStartingPoint)
107
0
                {   //This is the case if there is nothing to the right in the original search loop
108
0
                    bHasMoreToSearch = false;
109
0
                }
110
0
            }
111
112
0
            if (bHasMoreToSearch)
113
0
            {   // If there is still a node in the tree which can be tested
114
0
                if (ShouldHandleElement(xSearchLoop))
115
0
                {
116
0
                    m_xCurrentObject = xSearchLoop;
117
0
                    bFoundSomething = true;
118
0
                }
119
0
                else
120
0
                    if (bCheckingStartingPoint)
121
0
                        bHasMoreToSearch = false;
122
0
                bAlreadyCheckedCurrent = true;
123
0
            }
124
0
        }
125
0
    }
126
127
0
    if (!bFoundSomething)
128
0
    {
129
0
        OSL_ENSURE(m_arrChildIndizies.empty(), "IndexAccessIterator::Next : items left on stack ! how this ?");
130
0
        Invalidate();
131
0
    }
132
133
0
    return m_xCurrentObject;
134
0
}
135
136
137
}   // namespace comphelper
138
139
140
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */