xref: /trunk/main/framework/source/helper/ocomponentenumeration.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 )
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()
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()
138 {
139     // Ready for multithreading
140     ResetableGuard aGuard( m_aLock );
141 
142     // If we have no elements or end of enumeration is arrived ...
143     if ( hasMoreElements() == sal_False )
144     {
145         // .. throw an exception!
146         throw NoSuchElementException();
147     }
148 
149     // Else; Get next element from list ...
150     Any aComponent;
151     aComponent <<= m_seqComponents[m_nPosition];
152     // ... and step to next element!
153     ++m_nPosition;
154 
155     // Return listitem.
156     return aComponent;
157 }
158 
159 //*****************************************************************************************************************
160 //  proteced method
161 //*****************************************************************************************************************
impl_resetObject()162 void OComponentEnumeration::impl_resetObject()
163 {
164     // Attention:
165     // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
166     // It exist two ways to call this method. From destructor and from disposing().
167     // I can't say, which one is the first. Normally the disposing-call - but other way ....
168 
169     // Delete list of components.
170     m_seqComponents.realloc( 0 );
171     // Reset position in list.
172     // The list has no elements anymore. m_nPosition is normally the current position in list for nextElement!
173     // But a position of 0 in a list of 0 items is an invalid state. This constellation can't work in future.
174     // End of enumeration is arrived!
175     // (see hasMoreElements() for more details...)
176     m_nPosition = 0 ;
177 }
178 
179 //_________________________________________________________________________________________________________________
180 //  debug methods
181 //_________________________________________________________________________________________________________________
182 
183 /*-----------------------------------------------------------------------------------------------------------------
184     The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
185     we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
186 
187     ATTENTION
188 
189         If you miss a test for one of this parameters, contact the author or add it himself !(?)
190         But ... look for right testing! See using of this methods!
191 -----------------------------------------------------------------------------------------------------------------*/
192 
193 #ifdef ENABLE_ASSERTIONS
194 
195 //*****************************************************************************************************************
196 // An empty list is allowed ... hasMoreElements() will return false then!
impldbg_checkParameter_OComponentEnumerationCtor(const Sequence<css::uno::Reference<XComponent>> & seqComponents)197 sal_Bool OComponentEnumeration::impldbg_checkParameter_OComponentEnumerationCtor( const Sequence< css::uno::Reference< XComponent > >& seqComponents )
198 {
199     // Set default return value.
200     sal_Bool bOK = sal_True;
201     // Check parameter.
202     if  (
203             ( &seqComponents == NULL )
204         )
205     {
206         bOK = sal_False ;
207     }
208     // Return result of check.
209     return bOK ;
210 }
211 
212 //*****************************************************************************************************************
impldbg_checkParameter_disposing(const EventObject & aEvent)213 sal_Bool OComponentEnumeration::impldbg_checkParameter_disposing( const EventObject& aEvent )
214 {
215     // Set default return value.
216     sal_Bool bOK = sal_True;
217     // Check parameter.
218     if  (
219             ( &aEvent               ==  NULL        )   ||
220             ( aEvent.Source.is()    ==  sal_False   )
221         )
222     {
223         bOK = sal_False ;
224     }
225     // Return result of check.
226     return bOK ;
227 }
228 
229 #endif  //  #ifdef ENABLE_ASSERTIONS
230 
231 }       //  namespace framework
232