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