Line data Source code
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 <helper/ocomponentenumeration.hxx>
21 :
22 : #include <vcl/svapp.hxx>
23 :
24 : namespace framework{
25 :
26 : using namespace ::com::sun::star::container;
27 : using namespace ::com::sun::star::lang;
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::cppu;
30 : using namespace ::osl;
31 :
32 : // constructor
33 :
34 89 : OComponentEnumeration::OComponentEnumeration( const Sequence< css::uno::Reference< XComponent > >& seqComponents )
35 : : m_nPosition ( 0 ) // 0 is the first position for a valid list and the right value for an invalid list to!
36 89 : , m_seqComponents ( seqComponents )
37 89 : {}
38 :
39 : // destructor
40 :
41 267 : OComponentEnumeration::~OComponentEnumeration()
42 : {
43 : // Reset instance, free memory ....
44 89 : impl_resetObject();
45 178 : }
46 :
47 : // XEventListener
48 0 : void SAL_CALL OComponentEnumeration::disposing( const EventObject& aEvent ) throw( RuntimeException, std::exception )
49 : {
50 0 : SolarMutexGuard g;
51 :
52 : // Safe impossible cases
53 : // This method is not specified for all incoming parameters.
54 : (void) aEvent;
55 : SAL_WARN_IF( !aEvent.Source.is(), "fwk", "OComponentEnumeration::disposing(): Invalid parameter detected!" );
56 :
57 : // Reset instance to defaults, release references and free memory.
58 0 : impl_resetObject();
59 0 : }
60 :
61 : // XEnumeration
62 275 : sal_Bool SAL_CALL OComponentEnumeration::hasMoreElements() throw( RuntimeException, std::exception )
63 : {
64 275 : SolarMutexGuard g;
65 :
66 : // First position in a valid list is 0.
67 : // => The last one is getLength() - 1!
68 : // m_nPosition's current value is the position for the next element, which will be return, if user call "nextElement()"
69 : // => We have more elements if current position less then the length of the list!
70 275 : return ( m_nPosition < (sal_uInt32)(m_seqComponents.getLength()) );
71 : }
72 :
73 : // XEnumeration
74 :
75 93 : Any SAL_CALL OComponentEnumeration::nextElement() throw( NoSuchElementException ,
76 : WrappedTargetException ,
77 : RuntimeException, std::exception )
78 : {
79 93 : SolarMutexGuard g;
80 :
81 : // If we have no elements or end of enumeration is arrived ...
82 93 : if ( hasMoreElements() == sal_False )
83 : {
84 : // .. throw an exception!
85 0 : throw NoSuchElementException();
86 : }
87 :
88 : // Else; Get next element from list ...
89 93 : Any aComponent;
90 93 : aComponent <<= m_seqComponents[m_nPosition];
91 : // ... and step to next element!
92 93 : ++m_nPosition;
93 :
94 : // Return listitem.
95 93 : return aComponent;
96 : }
97 :
98 : // proteced method
99 :
100 89 : void OComponentEnumeration::impl_resetObject()
101 : {
102 : // Attention:
103 : // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
104 : // It exist two ways to call this method. From destructor and from disposing().
105 : // I can't say, which one is the first. Normally the disposing-call - but other way ....
106 :
107 : // Delete list of components.
108 89 : m_seqComponents.realloc( 0 );
109 : // Reset position in list.
110 : // The list has no elements anymore. m_nPosition is normally the current position in list for nextElement!
111 : // But a position of 0 in a list of 0 items is an invalid state. This constellation can't work in future.
112 : // End of enumeration is arrived!
113 : // (see hasMoreElements() for more details...)
114 89 : m_nPosition = 0;
115 89 : }
116 :
117 : } // namespace framework
118 :
119 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|