xref: /AOO42X/main/comphelper/source/container/container.cxx (revision e2530cf997e32d49427a30bc8b11ebe297a9a57e)
1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp>
27cdf0e10cSrcweir #include <com/sun/star/container/XIndexAccess.hpp>
28cdf0e10cSrcweir #include <com/sun/star/container/XChild.hpp>
29cdf0e10cSrcweir #include <comphelper/container.hxx>
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //.........................................................................
33cdf0e10cSrcweir namespace comphelper
34cdf0e10cSrcweir {
35cdf0e10cSrcweir //.........................................................................
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //==============================================================================
IndexAccessIterator(::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> xStartingPoint)38cdf0e10cSrcweir IndexAccessIterator::IndexAccessIterator(::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> xStartingPoint)
39cdf0e10cSrcweir     :m_xStartingPoint(xStartingPoint)
40cdf0e10cSrcweir     ,m_xCurrentObject(NULL)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     OSL_ENSURE(m_xStartingPoint.is(), "IndexAccessIterator::IndexAccessIterator : no starting point !");
43cdf0e10cSrcweir }
44cdf0e10cSrcweir 
~IndexAccessIterator()45cdf0e10cSrcweir IndexAccessIterator::~IndexAccessIterator() {}
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //------------------------------------------------------------------------------
Next()48cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> IndexAccessIterator::Next()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     sal_Bool bCheckingStartingPoint = !m_xCurrentObject.is();
51cdf0e10cSrcweir         // ist die aktuelle Node der Anfangspunkt ?
52cdf0e10cSrcweir     sal_Bool bAlreadyCheckedCurrent = m_xCurrentObject.is();
53cdf0e10cSrcweir         // habe ich die aktuelle Node schon mal mittels ShouldHandleElement testen ?
54cdf0e10cSrcweir     if (!m_xCurrentObject.is())
55cdf0e10cSrcweir         m_xCurrentObject = m_xStartingPoint;
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> xSearchLoop( m_xCurrentObject);
58cdf0e10cSrcweir     sal_Bool bHasMoreToSearch = sal_True;
59cdf0e10cSrcweir     sal_Bool bFoundSomething = sal_False;
60cdf0e10cSrcweir     while (!bFoundSomething && bHasMoreToSearch)
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         // pre-order-traversierung
63cdf0e10cSrcweir         if (!bAlreadyCheckedCurrent && ShouldHandleElement(xSearchLoop))
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             m_xCurrentObject = xSearchLoop;
66cdf0e10cSrcweir             bFoundSomething = sal_True;
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir         else
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir             // zuerst absteigen, wenn moeglich
71cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess> xContainerAccess(xSearchLoop, ::com::sun::star::uno::UNO_QUERY);
72cdf0e10cSrcweir             if (xContainerAccess.is() && xContainerAccess->getCount() && ShouldStepInto(xContainerAccess))
73cdf0e10cSrcweir             {   // zum ersten Child
74cdf0e10cSrcweir                 ::com::sun::star::uno::Any aElement(xContainerAccess->getByIndex(0));
75cdf0e10cSrcweir                 xSearchLoop = *(::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>*)aElement.getValue();
76cdf0e10cSrcweir                 bCheckingStartingPoint = sal_False;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir                 m_arrChildIndizies.push_back((sal_Int32)0);
79cdf0e10cSrcweir             }
80cdf0e10cSrcweir             else
81cdf0e10cSrcweir             {
82cdf0e10cSrcweir                 // dann nach oben und nach rechts, wenn moeglich
83cdf0e10cSrcweir                 while (m_arrChildIndizies.size() > 0)
84cdf0e10cSrcweir                 {   // (mein Stack ist nich leer, also kann ich noch nach oben gehen)
85cdf0e10cSrcweir                     ::com::sun::star::uno::Reference< ::com::sun::star::container::XChild> xChild(xSearchLoop, ::com::sun::star::uno::UNO_QUERY);
86cdf0e10cSrcweir                     OSL_ENSURE(xChild.is(), "IndexAccessIterator::Next : a content has no approriate interface !");
87cdf0e10cSrcweir 
88cdf0e10cSrcweir                     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> xParent( xChild->getParent());
89cdf0e10cSrcweir                     xContainerAccess = ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess>(xParent, ::com::sun::star::uno::UNO_QUERY);
90cdf0e10cSrcweir                     OSL_ENSURE(xContainerAccess.is(), "IndexAccessIterator::Next : a content has an invalid parent !");
91cdf0e10cSrcweir 
92cdf0e10cSrcweir                     // den Index, den SearchLoop in diesem Parent hatte, von meinem 'Stack'
93cdf0e10cSrcweir                     sal_Int32 nOldSearchChildIndex = m_arrChildIndizies[m_arrChildIndizies.size() - 1];
94cdf0e10cSrcweir                     m_arrChildIndizies.pop_back();
95cdf0e10cSrcweir 
96cdf0e10cSrcweir                     if (nOldSearchChildIndex < xContainerAccess->getCount() - 1)
97cdf0e10cSrcweir                     {   // auf dieser Ebene geht es noch nach rechts
98cdf0e10cSrcweir                         ++nOldSearchChildIndex;
99cdf0e10cSrcweir                         // also das naechste Child
100cdf0e10cSrcweir                         ::com::sun::star::uno::Any aElement(xContainerAccess->getByIndex(nOldSearchChildIndex));
101cdf0e10cSrcweir                         xSearchLoop = *(::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface>*) aElement.getValue();
102cdf0e10cSrcweir                         bCheckingStartingPoint = sal_False;
103cdf0e10cSrcweir                         // und dessen Position auf den 'Stack'
104cdf0e10cSrcweir                         m_arrChildIndizies.push_back((sal_Int32)nOldSearchChildIndex);
105cdf0e10cSrcweir 
106cdf0e10cSrcweir                         break;
107cdf0e10cSrcweir                     }
108cdf0e10cSrcweir                     // hierher komme ich, wenn es auf der aktuellen Ebene nicht nach rechts geht, dann mache ich eine darueber weiter
109cdf0e10cSrcweir                     xSearchLoop = xParent;
110cdf0e10cSrcweir                     bCheckingStartingPoint = sal_False;
111cdf0e10cSrcweir                 }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir                 if ((m_arrChildIndizies.size() == 0) && !bCheckingStartingPoint)
114cdf0e10cSrcweir                 {   // das ist genau dann der Fall, wenn ich keinen rechten Nachbarn fuer irgendeinen der direkten Vorfahren des
115cdf0e10cSrcweir                     // urspruenglichen xSearchLoop gefunden habe
116cdf0e10cSrcweir                     bHasMoreToSearch = sal_False;
117cdf0e10cSrcweir                 }
118cdf0e10cSrcweir             }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             if (bHasMoreToSearch)
121cdf0e10cSrcweir             {   // ich habe in xSearchLoop jetzt ein Interface eines 'Knotens' meines 'Baumes', den ich noch abtesten kann
122cdf0e10cSrcweir                 if (ShouldHandleElement(xSearchLoop))
123cdf0e10cSrcweir                 {
124cdf0e10cSrcweir                     m_xCurrentObject = xSearchLoop;
125cdf0e10cSrcweir                     bFoundSomething = sal_True;
126cdf0e10cSrcweir                 }
127cdf0e10cSrcweir                 else
128cdf0e10cSrcweir                     if (bCheckingStartingPoint)
129cdf0e10cSrcweir                         // ich bin noch am Anfang, konnte nicht absteigen, und habe an diesem Anfang nix gefunden -> nix mehr zu tun
130cdf0e10cSrcweir                         bHasMoreToSearch = sal_False;
131cdf0e10cSrcweir                 bAlreadyCheckedCurrent = sal_True;
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     if (!bFoundSomething)
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         OSL_ENSURE(m_arrChildIndizies.size() == 0, "IndexAccessIterator::Next : items left on stack ! how this ?");
139cdf0e10cSrcweir         Invalidate();
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     return m_xCurrentObject;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir //.........................................................................
146cdf0e10cSrcweir }   // namespace comphelper
147cdf0e10cSrcweir //.........................................................................
148