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