Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / classes / framecontainer.cxx
bloba23e3633e1493bec5ca63fe65dc742ddb7c087c6
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 <framework/framecontainer.hxx>
22 #include <com/sun/star/frame/FrameSearchFlag.hpp>
24 #include <vcl/svapp.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <sal/log.hxx>
28 namespace framework
30 /**-***************************************************************************************************************
31 @short initialize an empty container
32 @descr The container will be empty then - special features (e.g. the async quit mechanism) are disabled.
34 @threadsafe not necessary - it's not a singleton
35 *****************************************************************************************************************/
36 FrameContainer::FrameContainer()
37 /*DEPRECATEME
38 , m_bAsyncQuit ( sal_False ) // default must be "disabled"!
39 , m_aAsyncCall ( LINK( this, FrameContainer, implts_asyncQuit ) )
44 /**-***************************************************************************************************************
45 @short deinitialize may a filled container
46 @descr Special features (if the currently are running) will be disabled and we free all used other resources.
48 @threadsafe not necessary - it's not a singleton
49 *****************************************************************************************************************/
50 FrameContainer::~FrameContainer()
52 // Don't forget to free memory!
53 m_aContainer.clear();
54 m_xActiveFrame.clear();
57 /**-***************************************************************************************************************
58 @short append a new frame to the container
59 @descr We accept the incoming frame only, if it is a valid reference and doesn't exist already.
61 @param xFrame
62 frame, which should be added to this container
63 Must be a valid reference.
65 @threadsafe yes
66 *****************************************************************************************************************/
67 void FrameContainer::append(const css::uno::Reference<css::frame::XFrame>& xFrame)
69 if (xFrame.is() && !exist(xFrame))
71 SolarMutexGuard g;
72 m_aContainer.push_back(xFrame);
76 /**-***************************************************************************************************************
77 @short remove a frame from the container
78 @descr In case we remove the last frame and our internal special feature (the async quit mechanism)
79 was enabled by the desktop instance, we start it.
81 @param xFrame
82 frame, which should be deleted from this container
83 Must be a valid reference.
85 @threadsafe yes
86 *****************************************************************************************************************/
87 void FrameContainer::remove(const css::uno::Reference<css::frame::XFrame>& xFrame)
89 SolarMutexGuard g;
91 TFrameContainer::iterator aSearchedItem
92 = ::std::find(m_aContainer.begin(), m_aContainer.end(), xFrame);
93 if (aSearchedItem != m_aContainer.end())
95 m_aContainer.erase(aSearchedItem);
97 // If removed frame was the current active frame - reset state variable.
98 if (m_xActiveFrame == xFrame)
99 m_xActiveFrame.clear();
103 /**-***************************************************************************************************************
104 @short check if the given frame currently exist inside the container
105 @param xFrame
106 reference to the queried frame
108 @return <TRUE/> if frame is part of this container
109 <FALSE/> otherwise
111 @threadsafe yes
112 *****************************************************************************************************************/
113 bool FrameContainer::exist(const css::uno::Reference<css::frame::XFrame>& xFrame) const
115 SolarMutexGuard g;
116 return (::std::find(m_aContainer.begin(), m_aContainer.end(), xFrame) != m_aContainer.end());
119 /**-***************************************************************************************************************
120 @short delete all existing items of the container
121 @threadsafe yes
122 *****************************************************************************************************************/
123 void FrameContainer::clear()
125 SolarMutexGuard g;
126 // Clear the container ...
127 m_aContainer.clear();
128 // ... and don't forget to reset the active frame.
129 // It's a reference to a valid container-item.
130 // But no container item => no active frame!
131 m_xActiveFrame.clear();
134 /**-***************************************************************************************************************
135 @short returns count of all current existing frames
136 @deprecated This value can't be guaranteed for multithreading environments.
137 So it will be marked as deprecated and should be replaced by "getAllElements()".
139 @return the count of existing container items
141 @threadsafe yes
142 *****************************************************************************************************************/
143 sal_uInt32 FrameContainer::getCount() const
145 SolarMutexGuard g;
146 return static_cast<sal_uInt32>(m_aContainer.size());
149 /**-***************************************************************************************************************
150 @short returns one item of this container
151 @deprecated This value can't be guaranteed for multithreading environments.
152 So it will be marked as deprecated and should be replaced by "getAllElements()".
154 @param nIndex
155 a value between 0 and (getCount()-1) to address one container item
157 @return a reference to a frame inside the container, which match with given index
159 @threadsafe yes
160 *****************************************************************************************************************/
161 css::uno::Reference<css::frame::XFrame> FrameContainer::operator[](sal_uInt32 nIndex) const
163 css::uno::Reference<css::frame::XFrame> xFrame;
166 // Get element form container WITH automatic test of ranges!
167 // If index not valid, an out_of_range exception is thrown.
168 SolarMutexGuard g;
169 xFrame = m_aContainer.at(nIndex);
171 catch (const std::out_of_range&)
173 // The index is not valid for current container-content - we must handle this case!
174 // We can return the default value ...
175 SAL_INFO("fwk", "FrameContainer::operator[]: Exception caught: std::out_of_range");
177 return xFrame;
180 /**-***************************************************************************************************************
181 @short returns a snapshot of all currently existing frames inside this container
182 @descr Should be used to replace the deprecated functions getCount()/operator[]!
184 @return a list of all frame references inside this container
186 @threadsafe yes
187 *****************************************************************************************************************/
188 css::uno::Sequence<css::uno::Reference<css::frame::XFrame>> FrameContainer::getAllElements() const
190 SolarMutexGuard g;
191 return comphelper::containerToSequence(m_aContainer);
194 /**-***************************************************************************************************************
195 @short set the given frame as the new active one inside this container
196 @descr We accept this frame only, if it's already a part of this container.
198 @param xFrame
199 reference to the new active frame
200 Must be a valid reference and already part of this container.
202 @threadsafe yes
203 *****************************************************************************************************************/
204 void FrameContainer::setActive(const css::uno::Reference<css::frame::XFrame>& xFrame)
206 if (!xFrame.is() || exist(xFrame))
208 SolarMutexGuard g;
209 m_xActiveFrame = xFrame;
213 /**-***************************************************************************************************************
214 @short return the current active frame of this container
215 @descr Value can be null in case the frame was removed from the container and nobody
216 from outside decide which of all others should be the new one...
218 @return a reference to the current active frame
219 Value can be NULL!
221 @threadsafe yes
222 *****************************************************************************************************************/
223 css::uno::Reference<css::frame::XFrame> FrameContainer::getActive() const
225 SolarMutexGuard g;
226 return m_xActiveFrame;
229 /**-***************************************************************************************************************
230 @short implements a simple search based on current container items
231 @descr It can be used for findFrame() and implements a deep down search.
233 @param sName
234 target name, which is searched
236 @return reference to the found frame or NULL if not.
238 @threadsafe yes
239 *****************************************************************************************************************/
240 css::uno::Reference<css::frame::XFrame>
241 FrameContainer::searchOnAllChildrens(const OUString& sName) const
243 SolarMutexGuard g;
244 // Step over all child frames. But if direct child isn't the right one search on his children first - before
245 // you go to next direct child of this container!
246 css::uno::Reference<css::frame::XFrame> xSearchedFrame;
247 for (auto const& container : m_aContainer)
249 if (container->getName() == sName)
251 xSearchedFrame = container;
252 break;
254 else
256 xSearchedFrame = container->findFrame(sName, css::frame::FrameSearchFlag::CHILDREN);
257 if (xSearchedFrame.is())
258 break;
261 return xSearchedFrame;
264 /**-***************************************************************************************************************
265 @short implements a simple search based on current container items
266 @descr It can be used for findFrame() and search on members of this container only!
268 @param sName
269 target name, which is searched
271 @return reference to the found frame or NULL if not.
273 @threadsafe yes
274 *****************************************************************************************************************/
275 css::uno::Reference<css::frame::XFrame>
276 FrameContainer::searchOnDirectChildrens(std::u16string_view sName) const
278 SolarMutexGuard g;
279 css::uno::Reference<css::frame::XFrame> xSearchedFrame;
280 for (auto const& container : m_aContainer)
282 if (container->getName() == sName)
284 xSearchedFrame = container;
285 break;
288 return xSearchedFrame;
291 } // namespace framework
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */