LibreOffice Module comphelper (master)  1
container.cxx
Go to the documentation of this file.
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 <osl/diagnose.h>
26 
27 
28 namespace comphelper
29 {
30 
31 
32 IndexAccessIterator::IndexAccessIterator(css::uno::Reference< css::uno::XInterface> const & xStartingPoint)
33  :m_xStartingPoint(xStartingPoint)
34 {
35  OSL_ENSURE(m_xStartingPoint.is(), "IndexAccessIterator::IndexAccessIterator : no starting point !");
36 }
37 
38 IndexAccessIterator::~IndexAccessIterator() {}
39 
40 
41 css::uno::Reference< css::uno::XInterface> const & IndexAccessIterator::Next()
42 {
43  bool bCheckingStartingPoint = !m_xCurrentObject.is();
44  // Is the current node the starting point?
45  bool bAlreadyCheckedCurrent = m_xCurrentObject.is();
46  // Have I already tested the current node through ShouldHandleElement?
47  if (!m_xCurrentObject.is())
48  m_xCurrentObject = m_xStartingPoint;
49 
50  css::uno::Reference< css::uno::XInterface> xSearchLoop( m_xCurrentObject);
51  bool bHasMoreToSearch = true;
52  bool bFoundSomething = false;
53  while (!bFoundSomething && bHasMoreToSearch)
54  {
55  // Priming loop
56  if (!bAlreadyCheckedCurrent && ShouldHandleElement(xSearchLoop))
57  {
58  m_xCurrentObject = xSearchLoop;
59  bFoundSomething = true;
60  }
61  else
62  {
63  // First, check to see if there's a match below
64  css::uno::Reference< css::container::XIndexAccess> xContainerAccess(xSearchLoop, css::uno::UNO_QUERY);
65  if (xContainerAccess.is() && xContainerAccess->getCount() && ShouldStepInto(xContainerAccess))
66  {
67  css::uno::Any aElement(xContainerAccess->getByIndex(0));
68  xSearchLoop = *o3tl::doAccess<css::uno::Reference<css::uno::XInterface>>(aElement);
69  bCheckingStartingPoint = false;
70 
71  m_arrChildIndizies.push_back(sal_Int32(0));
72  }
73  else
74  { // otherwise, look above and to the right, if possible
75  while (!m_arrChildIndizies.empty())
76  { // If the list isn't empty and there's nothing above
77  css::uno::Reference< css::container::XChild> xChild(xSearchLoop, css::uno::UNO_QUERY);
78  OSL_ENSURE(xChild.is(), "IndexAccessIterator::Next : a content has no appropriate interface !");
79 
80  css::uno::Reference< css::uno::XInterface> xParent( xChild->getParent());
81  xContainerAccess.set(xParent, css::uno::UNO_QUERY);
82  OSL_ENSURE(xContainerAccess.is(), "IndexAccessIterator::Next : a content has an invalid parent !");
83 
84  // Remove the index that SearchLoop had within this parent from my stack
85  sal_Int32 nOldSearchChildIndex = m_arrChildIndizies[m_arrChildIndizies.size() - 1];
86  m_arrChildIndizies.pop_back();
87 
88  if (nOldSearchChildIndex < xContainerAccess->getCount() - 1)
89  { // Move to the right in this row
90  ++nOldSearchChildIndex;
91  // and check the next child
92  css::uno::Any aElement(xContainerAccess->getByIndex(nOldSearchChildIndex));
93  xSearchLoop = *o3tl::doAccess<css::uno::Reference<css::uno::XInterface>>(aElement);
94  bCheckingStartingPoint = false;
95  // and update its position in the list.
96  m_arrChildIndizies.push_back(nOldSearchChildIndex);
97 
98  break;
99  }
100  // Finally, if there's nothing more to do in this row (to the right), we'll move on to the next row.
101  xSearchLoop = xParent;
102  bCheckingStartingPoint = false;
103  }
104 
105  if (m_arrChildIndizies.empty() && !bCheckingStartingPoint)
106  { //This is the case if there is nothing to the right in the original search loop
107  bHasMoreToSearch = false;
108  }
109  }
110 
111  if (bHasMoreToSearch)
112  { // If there is still a node in the tree which can be tested
113  if (ShouldHandleElement(xSearchLoop))
114  {
115  m_xCurrentObject = xSearchLoop;
116  bFoundSomething = true;
117  }
118  else
119  if (bCheckingStartingPoint)
120  bHasMoreToSearch = false;
121  bAlreadyCheckedCurrent = true;
122  }
123  }
124  }
125 
126  if (!bFoundSomething)
127  {
128  OSL_ENSURE(m_arrChildIndizies.empty(), "IndexAccessIterator::Next : items left on stack ! how this ?");
129  Invalidate();
130  }
131 
132  return m_xCurrentObject;
133 }
134 
135 
136 } // namespace comphelper
137 
138 
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */