xref: /trunk/main/framework/source/helper/ocomponentenumeration.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_framework.hxx"
30 
31 //_________________________________________________________________________________________________________________
32 //  my own includes
33 //_________________________________________________________________________________________________________________
34 #include <helper/ocomponentenumeration.hxx>
35 
36 #ifndef _FRAMEWORK_THREADHELP_RESETABLEGUARD_HXX_
37 #include <threadhelp/resetableguard.hxx>
38 #endif
39 
40 //_________________________________________________________________________________________________________________
41 //  interface includes
42 //_________________________________________________________________________________________________________________
43 
44 //_________________________________________________________________________________________________________________
45 //  includes of other projects
46 //_________________________________________________________________________________________________________________
47 #include <vcl/svapp.hxx>
48 
49 //_________________________________________________________________________________________________________________
50 //  namespace
51 //_________________________________________________________________________________________________________________
52 
53 namespace framework{
54 
55 using namespace ::com::sun::star::container     ;
56 using namespace ::com::sun::star::lang          ;
57 using namespace ::com::sun::star::uno           ;
58 using namespace ::cppu                          ;
59 using namespace ::osl                           ;
60 using namespace ::rtl                           ;
61 
62 //_________________________________________________________________________________________________________________
63 //  non exported const
64 //_________________________________________________________________________________________________________________
65 
66 //_________________________________________________________________________________________________________________
67 //  non exported definitions
68 //_________________________________________________________________________________________________________________
69 
70 //_________________________________________________________________________________________________________________
71 //  declarations
72 //_________________________________________________________________________________________________________________
73 
74 //*****************************************************************************************************************
75 //  constructor
76 //*****************************************************************************************************************
77 OComponentEnumeration::OComponentEnumeration( const Sequence< css::uno::Reference< XComponent > >& seqComponents )
78         //  Init baseclasses first
79         //  Attention:
80         //      Don't change order of initialization!
81         //      ThreadHelpBase is a struct with a mutex as member. We can't use a mutex as member, while
82         //      we must garant right initialization and a valid value of this! First initialize
83         //      baseclasses and then members. And we need the mutex for other baseclasses !!!
84         :   ThreadHelpBase  ( &Application::GetSolarMutex() )
85         // Init member
86         ,   m_nPosition     ( 0                             )   // 0 is the first position for a valid list and the right value for an invalid list to!
87         ,   m_seqComponents ( seqComponents                 )
88 {
89     // Safe impossible states
90     // "Method" not defined for ALL parameters!
91     LOG_ASSERT( impldbg_checkParameter_OComponentEnumerationCtor( seqComponents ), "OComponentEnumeration::OComponentEnumeration()\nInvalid parameter detected!\n" )
92 }
93 
94 //*****************************************************************************************************************
95 //  destructor
96 //*****************************************************************************************************************
97 OComponentEnumeration::~OComponentEnumeration()
98 {
99     // Reset instance, free memory ....
100     impl_resetObject();
101 }
102 
103 //*****************************************************************************************************************
104 //  XEventListener
105 //*****************************************************************************************************************
106 void SAL_CALL OComponentEnumeration::disposing( const EventObject&
107 #if OSL_DEBUG_LEVEL > 0
108 aEvent
109 #endif
110 ) throw( RuntimeException )
111 {
112     // Ready for multithreading
113     ResetableGuard aGuard( m_aLock );
114 
115     // Safe impossible cases
116     // This method is not specified for all incoming parameters.
117     LOG_ASSERT( impldbg_checkParameter_disposing( aEvent ), "OComponentEnumeration::disposing()\nInvalid parameter detected!\n" )
118 
119     // Reset instance to defaults, release references and free memory.
120     impl_resetObject();
121 }
122 
123 //*****************************************************************************************************************
124 //  XEnumeration
125 //*****************************************************************************************************************
126 sal_Bool SAL_CALL OComponentEnumeration::hasMoreElements() throw( RuntimeException )
127 {
128     // Ready for multithreading
129     ResetableGuard aGuard( m_aLock );
130 
131     // First position in a valid list is 0.
132     // => The last one is getLength() - 1!
133     // m_nPosition's current value is the position for the next element, which will be return, if user call "nextElement()"
134     // => We have more elements if current position less then the length of the list!
135     return ( m_nPosition < (sal_uInt32)(m_seqComponents.getLength()) );
136 }
137 
138 //*****************************************************************************************************************
139 //  XEnumeration
140 //*****************************************************************************************************************
141 Any SAL_CALL OComponentEnumeration::nextElement() throw(    NoSuchElementException  ,
142                                                             WrappedTargetException  ,
143                                                             RuntimeException        )
144 {
145     // Ready for multithreading
146     ResetableGuard aGuard( m_aLock );
147 
148     // If we have no elements or end of enumeration is arrived ...
149     if ( hasMoreElements() == sal_False )
150     {
151         // .. throw an exception!
152         throw NoSuchElementException();
153     }
154 
155     // Else; Get next element from list ...
156     Any aComponent;
157     aComponent <<= m_seqComponents[m_nPosition];
158     // ... and step to next element!
159     ++m_nPosition;
160 
161     // Return listitem.
162     return aComponent;
163 }
164 
165 //*****************************************************************************************************************
166 //  proteced method
167 //*****************************************************************************************************************
168 void OComponentEnumeration::impl_resetObject()
169 {
170     // Attention:
171     // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
172     // It exist two ways to call this method. From destructor and from disposing().
173     // I can't say, which one is the first. Normaly the disposing-call - but other way ....
174 
175     // Delete list of components.
176     m_seqComponents.realloc( 0 );
177     // Reset position in list.
178     // The list has no elements anymore. m_nPosition is normaly the current position in list for nextElement!
179     // But a position of 0 in a list of 0 items is an invalid state. This constellation can't work in future.
180     // End of enumeration is arrived!
181     // (see hasMoreElements() for more details...)
182     m_nPosition = 0 ;
183 }
184 
185 //_________________________________________________________________________________________________________________
186 //  debug methods
187 //_________________________________________________________________________________________________________________
188 
189 /*-----------------------------------------------------------------------------------------------------------------
190     The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
191     we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
192 
193     ATTENTION
194 
195         If you miss a test for one of this parameters, contact the autor or add it himself !(?)
196         But ... look for right testing! See using of this methods!
197 -----------------------------------------------------------------------------------------------------------------*/
198 
199 #ifdef ENABLE_ASSERTIONS
200 
201 //*****************************************************************************************************************
202 // An empty list is allowed ... hasMoreElements() will return false then!
203 sal_Bool OComponentEnumeration::impldbg_checkParameter_OComponentEnumerationCtor( const Sequence< css::uno::Reference< XComponent > >& seqComponents )
204 {
205     // Set default return value.
206     sal_Bool bOK = sal_True;
207     // Check parameter.
208     if  (
209             ( &seqComponents == NULL )
210         )
211     {
212         bOK = sal_False ;
213     }
214     // Return result of check.
215     return bOK ;
216 }
217 
218 //*****************************************************************************************************************
219 sal_Bool OComponentEnumeration::impldbg_checkParameter_disposing( const EventObject& aEvent )
220 {
221     // Set default return value.
222     sal_Bool bOK = sal_True;
223     // Check parameter.
224     if  (
225             ( &aEvent               ==  NULL        )   ||
226             ( aEvent.Source.is()    ==  sal_False   )
227         )
228     {
229         bOK = sal_False ;
230     }
231     // Return result of check.
232     return bOK ;
233 }
234 
235 #endif  //  #ifdef ENABLE_ASSERTIONS
236 
237 }       //  namespace framework
238