Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / helper / ocomponentenumeration.cxx
blob3d00f75e56c0e337c55c9f22c43fd3e369d8b48d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <helper/ocomponentenumeration.hxx>
22 #include <vcl/svapp.hxx>
23 #include <sal/log.hxx>
25 namespace framework{
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 using namespace ::osl;
33 // constructor
35 OComponentEnumeration::OComponentEnumeration( std::vector< 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 , m_seqComponents ( std::move(seqComponents) )
40 // destructor
42 OComponentEnumeration::~OComponentEnumeration()
44 // Reset instance, free memory...
45 impl_resetObject();
48 // XEventListener
49 void SAL_CALL OComponentEnumeration::disposing( const EventObject& aEvent )
51 SolarMutexGuard g;
53 // Safe impossible cases
54 // This method is not specified for all incoming parameters.
55 SAL_WARN_IF( !aEvent.Source.is(), "fwk", "OComponentEnumeration::disposing(): Invalid parameter detected!" );
57 // Reset instance to defaults, release references and free memory.
58 impl_resetObject();
61 // XEnumeration
62 sal_Bool SAL_CALL OComponentEnumeration::hasMoreElements()
64 SolarMutexGuard g;
66 // First position in a valid list is 0.
67 // => The last one is getLength() - 1!
68 // m_nPosition's current value is the position for the next element, which will be return, if user call "nextElement()"
69 // => We have more elements if current position less than the length of the list!
70 return ( m_nPosition < static_cast<sal_uInt32>(m_seqComponents.size()) );
73 // XEnumeration
75 Any SAL_CALL OComponentEnumeration::nextElement()
77 SolarMutexGuard g;
79 // If we have no elements or end of enumeration is arrived ...
80 if ( !hasMoreElements() )
82 // .. throw an exception!
83 throw NoSuchElementException();
86 // Else; Get next element from list ...
87 Any aComponent;
88 aComponent <<= m_seqComponents[m_nPosition];
89 // ... and step to next element!
90 ++m_nPosition;
92 // Return listitem.
93 return aComponent;
96 // protected method
98 void OComponentEnumeration::impl_resetObject()
100 // Attention:
101 // Write this for multiple calls - NOT AT THE SAME TIME - but for more than one call again)!
102 // It exist two ways to call this method. From destructor and from disposing().
103 // I can't say, which one is the first. Normally the disposing-call - but other way...
105 // Delete list of components.
106 m_seqComponents.clear();
107 // Reset position in list.
108 // The list has no elements anymore. m_nPosition is normally the current position in list for nextElement!
109 // But a position of 0 in a list of 0 items is an invalid state. This constellation can't work in future.
110 // End of enumeration is arrived!
111 // (see hasMoreElements() for more details...)
112 m_nPosition = 0;
115 } // namespace framework
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */