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 : using namespace ::rtl;
32 :
33 : // constructor
34 :
35 0 : OComponentEnumeration::OComponentEnumeration( const Sequence< css::uno::Reference< XComponent > >& seqComponents )
36 : : m_nPosition ( 0 ) // 0 is the first position for a valid list and the right value for an invalid list to!
37 0 : , m_seqComponents ( seqComponents )
38 : {
39 : // Safe impossible states
40 : // "Method" not defined for ALL parameters!
41 : SAL_WARN_IF( !impldbg_checkParameter_OComponentEnumerationCtor( seqComponents ), "fwk", "OComponentEnumeration::OComponentEnumeration(): Invalid parameter detected!" );
42 0 : }
43 :
44 : // destructor
45 :
46 0 : OComponentEnumeration::~OComponentEnumeration()
47 : {
48 : // Reset instance, free memory ....
49 0 : impl_resetObject();
50 0 : }
51 :
52 : // XEventListener
53 0 : void SAL_CALL OComponentEnumeration::disposing( const EventObject& aEvent ) throw( RuntimeException, std::exception )
54 : {
55 0 : SolarMutexGuard g;
56 :
57 : // Safe impossible cases
58 : // This method is not specified for all incoming parameters.
59 : (void) aEvent;
60 : SAL_WARN_IF( !impldbg_checkParameter_disposing( aEvent ), "fwk", "OComponentEnumeration::disposing(): Invalid parameter detected!" );
61 :
62 : // Reset instance to defaults, release references and free memory.
63 0 : impl_resetObject();
64 0 : }
65 :
66 : // XEnumeration
67 0 : sal_Bool SAL_CALL OComponentEnumeration::hasMoreElements() throw( RuntimeException, std::exception )
68 : {
69 0 : SolarMutexGuard g;
70 :
71 : // First position in a valid list is 0.
72 : // => The last one is getLength() - 1!
73 : // m_nPosition's current value is the position for the next element, which will be return, if user call "nextElement()"
74 : // => We have more elements if current position less then the length of the list!
75 0 : return ( m_nPosition < (sal_uInt32)(m_seqComponents.getLength()) );
76 : }
77 :
78 : // XEnumeration
79 :
80 0 : Any SAL_CALL OComponentEnumeration::nextElement() throw( NoSuchElementException ,
81 : WrappedTargetException ,
82 : RuntimeException, std::exception )
83 : {
84 0 : SolarMutexGuard g;
85 :
86 : // If we have no elements or end of enumeration is arrived ...
87 0 : if ( hasMoreElements() == sal_False )
88 : {
89 : // .. throw an exception!
90 0 : throw NoSuchElementException();
91 : }
92 :
93 : // Else; Get next element from list ...
94 0 : Any aComponent;
95 0 : aComponent <<= m_seqComponents[m_nPosition];
96 : // ... and step to next element!
97 0 : ++m_nPosition;
98 :
99 : // Return listitem.
100 0 : return aComponent;
101 : }
102 :
103 : // proteced method
104 :
105 0 : void OComponentEnumeration::impl_resetObject()
106 : {
107 : // Attention:
108 : // Write this for multiple calls - NOT AT THE SAME TIME - but for more then one call again)!
109 : // It exist two ways to call this method. From destructor and from disposing().
110 : // I can't say, which one is the first. Normaly the disposing-call - but other way ....
111 :
112 : // Delete list of components.
113 0 : m_seqComponents.realloc( 0 );
114 : // Reset position in list.
115 : // The list has no elements anymore. m_nPosition is normaly the current position in list for nextElement!
116 : // But a position of 0 in a list of 0 items is an invalid state. This constellation can't work in future.
117 : // End of enumeration is arrived!
118 : // (see hasMoreElements() for more details...)
119 0 : m_nPosition = 0;
120 0 : }
121 :
122 : // debug methods
123 :
124 : /*-----------------------------------------------------------------------------------------------------------------
125 : The follow methods checks the parameter for other functions. If a parameter or his value is non valid,
126 : we return "sal_False". (else sal_True) This mechanism is used to throw an ASSERT!
127 :
128 : ATTENTION
129 :
130 : If you miss a test for one of this parameters, contact the autor or add it himself !(?)
131 : But ... look for right testing! See using of this methods!
132 : -----------------------------------------------------------------------------------------------------------------*/
133 :
134 : // An empty list is allowed ... hasMoreElements() will return false then!
135 0 : bool OComponentEnumeration::impldbg_checkParameter_OComponentEnumerationCtor( const Sequence< css::uno::Reference< XComponent > >& seqComponents )
136 : {
137 : // Set default return value.
138 0 : bool bOK = true;
139 : // Check parameter.
140 0 : if (
141 : ( &seqComponents == NULL )
142 : )
143 : {
144 0 : bOK = false;
145 : }
146 : // Return result of check.
147 0 : return bOK;
148 : }
149 :
150 0 : bool OComponentEnumeration::impldbg_checkParameter_disposing( const EventObject& aEvent )
151 : {
152 0 : return aEvent.Source.is();
153 : }
154 :
155 : } // namespace framework
156 :
157 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|