File: | home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx |
Warning: | line 269, column 62 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 <dbaccess/controllerframe.hxx> | ||||||
21 | #include <dbaccess/IController.hxx> | ||||||
22 | |||||||
23 | #include <com/sun/star/awt/XTopWindow.hpp> | ||||||
24 | #include <com/sun/star/awt/XWindow2.hpp> | ||||||
25 | #include <com/sun/star/lang/DisposedException.hpp> | ||||||
26 | #include <com/sun/star/document/XDocumentEventBroadcaster.hpp> | ||||||
27 | #include <com/sun/star/frame/XController2.hpp> | ||||||
28 | |||||||
29 | #include <cppuhelper/implbase.hxx> | ||||||
30 | #include <rtl/ref.hxx> | ||||||
31 | #include <sfx2/objsh.hxx> | ||||||
32 | #include <tools/diagnose_ex.h> | ||||||
33 | #include <toolkit/helper/vclunohelper.hxx> | ||||||
34 | #include <vcl/window.hxx> | ||||||
35 | |||||||
36 | namespace dbaui | ||||||
37 | { | ||||||
38 | |||||||
39 | using ::com::sun::star::uno::Reference; | ||||||
40 | using ::com::sun::star::uno::XInterface; | ||||||
41 | using ::com::sun::star::uno::UNO_QUERY; | ||||||
42 | using ::com::sun::star::uno::UNO_QUERY_THROW; | ||||||
43 | using ::com::sun::star::uno::UNO_SET_THROW; | ||||||
44 | using ::com::sun::star::uno::Exception; | ||||||
45 | using ::com::sun::star::uno::RuntimeException; | ||||||
46 | using ::com::sun::star::uno::Any; | ||||||
47 | using ::com::sun::star::frame::XFrame; | ||||||
48 | using ::com::sun::star::frame::FrameAction; | ||||||
49 | using ::com::sun::star::frame::FrameAction_FRAME_ACTIVATED; | ||||||
50 | using ::com::sun::star::frame::FrameAction_FRAME_UI_ACTIVATED; | ||||||
51 | using ::com::sun::star::frame::FrameAction_FRAME_DEACTIVATING; | ||||||
52 | using ::com::sun::star::frame::FrameAction_FRAME_UI_DEACTIVATING; | ||||||
53 | using ::com::sun::star::frame::XModel; | ||||||
54 | using ::com::sun::star::frame::XController; | ||||||
55 | using ::com::sun::star::frame::XController2; | ||||||
56 | using ::com::sun::star::awt::XTopWindow; | ||||||
57 | using ::com::sun::star::awt::XTopWindowListener; | ||||||
58 | using ::com::sun::star::awt::XWindow2; | ||||||
59 | using ::com::sun::star::lang::DisposedException; | ||||||
60 | using ::com::sun::star::lang::EventObject; | ||||||
61 | using ::com::sun::star::document::XDocumentEventBroadcaster; | ||||||
62 | using ::com::sun::star::awt::XWindow; | ||||||
63 | |||||||
64 | // FrameWindowActivationListener | ||||||
65 | typedef ::cppu::WeakImplHelper< XTopWindowListener | ||||||
66 | > FrameWindowActivationListener_Base; | ||||||
67 | |||||||
68 | namespace { | ||||||
69 | |||||||
70 | class FrameWindowActivationListener : public FrameWindowActivationListener_Base | ||||||
71 | { | ||||||
72 | public: | ||||||
73 | explicit FrameWindowActivationListener( ControllerFrame_Data& _rData ); | ||||||
74 | |||||||
75 | void dispose(); | ||||||
76 | |||||||
77 | protected: | ||||||
78 | virtual ~FrameWindowActivationListener() override; | ||||||
79 | |||||||
80 | // XTopWindowListener | ||||||
81 | virtual void SAL_CALL windowOpened( const css::lang::EventObject& e ) override; | ||||||
82 | virtual void SAL_CALL windowClosing( const css::lang::EventObject& e ) override; | ||||||
83 | virtual void SAL_CALL windowClosed( const css::lang::EventObject& e ) override; | ||||||
84 | virtual void SAL_CALL windowMinimized( const css::lang::EventObject& e ) override; | ||||||
85 | virtual void SAL_CALL windowNormalized( const css::lang::EventObject& e ) override; | ||||||
86 | virtual void SAL_CALL windowActivated( const css::lang::EventObject& e ) override; | ||||||
87 | virtual void SAL_CALL windowDeactivated( const css::lang::EventObject& e ) override; | ||||||
88 | |||||||
89 | // XEventListener | ||||||
90 | virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override; | ||||||
91 | |||||||
92 | private: | ||||||
93 | void impl_checkDisposed_throw() const; | ||||||
94 | void impl_registerOnFrameContainerWindow_nothrow( bool _bRegister ); | ||||||
95 | |||||||
96 | private: | ||||||
97 | ControllerFrame_Data* m_pData; | ||||||
98 | }; | ||||||
99 | |||||||
100 | } | ||||||
101 | |||||||
102 | // ControllerFrame_Data | ||||||
103 | struct ControllerFrame_Data | ||||||
104 | { | ||||||
105 | explicit ControllerFrame_Data( IController& _rController ) | ||||||
106 | :m_rController( _rController ) | ||||||
107 | ,m_xFrame() | ||||||
108 | ,m_xDocEventBroadcaster() | ||||||
109 | ,m_pListener() | ||||||
110 | ,m_bActive( false ) | ||||||
111 | ,m_bIsTopLevelDocumentWindow( false ) | ||||||
112 | { | ||||||
113 | } | ||||||
114 | |||||||
115 | IController& m_rController; | ||||||
116 | Reference< XFrame > m_xFrame; | ||||||
117 | Reference< XDocumentEventBroadcaster > m_xDocEventBroadcaster; | ||||||
118 | ::rtl::Reference< FrameWindowActivationListener > m_pListener; | ||||||
119 | bool m_bActive; | ||||||
120 | bool m_bIsTopLevelDocumentWindow; | ||||||
121 | }; | ||||||
122 | |||||||
123 | // helper | ||||||
124 | static void lcl_setFrame_nothrow( ControllerFrame_Data& _rData, const Reference< XFrame >& _rxFrame ) | ||||||
125 | { | ||||||
126 | // release old listener | ||||||
127 | if (_rData.m_pListener) | ||||||
128 | { | ||||||
129 | _rData.m_pListener->dispose(); | ||||||
130 | _rData.m_pListener = nullptr; | ||||||
131 | } | ||||||
132 | |||||||
133 | // remember new frame | ||||||
134 | _rData.m_xFrame = _rxFrame; | ||||||
135 | |||||||
136 | // create new listener | ||||||
137 | if ( _rData.m_xFrame.is() ) | ||||||
138 | _rData.m_pListener = new FrameWindowActivationListener( _rData ); | ||||||
139 | |||||||
140 | // at this point in time, we can assume the controller also has a model set, if it supports models | ||||||
141 | try | ||||||
142 | { | ||||||
143 | Reference< XController > xController( _rData.m_rController.getXController(), UNO_SET_THROW ); | ||||||
144 | Reference< XModel > xModel( xController->getModel() ); | ||||||
145 | if ( xModel.is() ) | ||||||
146 | _rData.m_xDocEventBroadcaster.set( xModel, UNO_QUERY ); | ||||||
147 | } | ||||||
148 | catch( const Exception& ) | ||||||
149 | { | ||||||
150 | DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "150" ": ", "dbaccess" );; | ||||||
151 | } | ||||||
152 | } | ||||||
153 | |||||||
154 | static bool lcl_isActive_nothrow( const Reference< XFrame >& _rxFrame ) | ||||||
155 | { | ||||||
156 | bool bIsActive = false; | ||||||
157 | try | ||||||
158 | { | ||||||
159 | if ( _rxFrame.is() ) | ||||||
160 | { | ||||||
161 | Reference< XWindow2 > xWindow( _rxFrame->getContainerWindow(), UNO_QUERY_THROW ); | ||||||
162 | bIsActive = xWindow->isActive(); | ||||||
163 | } | ||||||
164 | |||||||
165 | } | ||||||
166 | catch( const Exception& ) | ||||||
167 | { | ||||||
168 | DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "168" ": ", "dbaccess" );; | ||||||
169 | } | ||||||
170 | return bIsActive; | ||||||
171 | } | ||||||
172 | |||||||
173 | /** updates various global and local states with a new active component | ||||||
174 | |||||||
175 | In particular, the following are updated | ||||||
176 | * the global working document (aka Basic's ThisComponent in the application | ||||||
177 | Basic), with our controller's model, or the controller itself if there is no such | ||||||
178 | model. | ||||||
179 | */ | ||||||
180 | static void lcl_updateActiveComponents_nothrow( const ControllerFrame_Data& _rData ) | ||||||
181 | { | ||||||
182 | try | ||||||
183 | { | ||||||
184 | Reference< XController > xCompController( _rData.m_rController.getXController() ); | ||||||
185 | OSL_ENSURE( xCompController.is(), "lcl_updateActiveComponents_nothrow: can't do anything without a controller!" )do { if (true && (!(xCompController.is()))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "185" ": "), "%s", "lcl_updateActiveComponents_nothrow: can't do anything without a controller!" ); } } while (false); | ||||||
186 | if ( !xCompController.is() ) | ||||||
187 | return; | ||||||
188 | |||||||
189 | if ( _rData.m_bActive && _rData.m_bIsTopLevelDocumentWindow ) | ||||||
190 | { | ||||||
191 | // set the "current component" at the SfxObjectShell | ||||||
192 | Reference< XModel > xModel( xCompController->getModel() ); | ||||||
193 | Reference< XInterface > xCurrentComponent; | ||||||
194 | if ( xModel.is() ) | ||||||
195 | xCurrentComponent = xModel; | ||||||
196 | else | ||||||
197 | xCurrentComponent = xCompController; | ||||||
198 | SfxObjectShell::SetCurrentComponent( xCurrentComponent ); | ||||||
199 | } | ||||||
200 | } | ||||||
201 | catch( const Exception& ) | ||||||
202 | { | ||||||
203 | DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "203" ": ", "dbaccess" );; | ||||||
204 | } | ||||||
205 | } | ||||||
206 | |||||||
207 | /** broadcasts the OnFocus resp. OnUnfocus event | ||||||
208 | */ | ||||||
209 | static void lcl_notifyFocusChange_nothrow( ControllerFrame_Data& _rData, bool _bActive ) | ||||||
210 | { | ||||||
211 | try | ||||||
212 | { | ||||||
213 | if ( _rData.m_xDocEventBroadcaster.is() ) | ||||||
214 | { | ||||||
215 | OUString sEventName = _bActive ? OUString("OnFocus") : OUString("OnUnfocus"); | ||||||
216 | Reference< XController2 > xController( _rData.m_rController.getXController(), UNO_QUERY_THROW ); | ||||||
217 | _rData.m_xDocEventBroadcaster->notifyDocumentEvent( sEventName, xController, Any() ); | ||||||
218 | } | ||||||
219 | } | ||||||
220 | catch( const Exception& ) | ||||||
221 | { | ||||||
222 | DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "222" ": ", "dbaccess" );; | ||||||
223 | } | ||||||
224 | } | ||||||
225 | |||||||
226 | static void lcl_updateActive_nothrow( ControllerFrame_Data& _rData, bool _bActive ) | ||||||
227 | { | ||||||
228 | if ( _rData.m_bActive == _bActive ) | ||||||
229 | return; | ||||||
230 | _rData.m_bActive = _bActive; | ||||||
231 | |||||||
232 | lcl_updateActiveComponents_nothrow( _rData ); | ||||||
233 | lcl_notifyFocusChange_nothrow( _rData, _bActive ); | ||||||
234 | } | ||||||
235 | |||||||
236 | FrameWindowActivationListener::FrameWindowActivationListener( ControllerFrame_Data& _rData ) | ||||||
237 | :m_pData( &_rData ) | ||||||
238 | { | ||||||
239 | impl_registerOnFrameContainerWindow_nothrow( true ); | ||||||
240 | } | ||||||
241 | |||||||
242 | FrameWindowActivationListener::~FrameWindowActivationListener() | ||||||
243 | { | ||||||
244 | } | ||||||
245 | |||||||
246 | void FrameWindowActivationListener::dispose() | ||||||
247 | { | ||||||
248 | impl_registerOnFrameContainerWindow_nothrow( false ); | ||||||
249 | m_pData = nullptr; | ||||||
250 | } | ||||||
251 | |||||||
252 | void FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow( bool _bRegister ) | ||||||
253 | { | ||||||
254 | OSL_ENSURE( m_pData && m_pData->m_xFrame.is(), "FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow: no frame!" )do { if (true && (!(m_pData && m_pData->m_xFrame .is()))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ( "legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "254" ": "), "%s", "FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow: no frame!" ); } } while (false); | ||||||
255 | if ( !m_pData
| ||||||
256 | return; | ||||||
257 | |||||||
258 | try | ||||||
259 | { | ||||||
260 | void ( SAL_CALL XTopWindow::*pListenerAction )( const Reference< XTopWindowListener >& ) = | ||||||
261 | _bRegister
| ||||||
262 | |||||||
263 | const Reference< XWindow > xContainerWindow( m_pData->m_xFrame->getContainerWindow(), UNO_SET_THROW ); | ||||||
264 | if ( _bRegister
| ||||||
265 | { | ||||||
266 | const vcl::Window* pContainerWindow = VCLUnoHelper::GetWindow( xContainerWindow ); | ||||||
267 | ENSURE_OR_THROW( pContainerWindow, "no Window implementation for the frame's container window!" )if( !(pContainerWindow) ){ do { if (true && (!(pContainerWindow ))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl" ), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "267" ": "), "%s", "no Window implementation for the frame's container window!" ); } } while (false); throw css::uno::RuntimeException( __func__ + OUStringLiteral(u",\n" "no Window implementation for the frame's container window!" ), css::uno::Reference< css::uno::XInterface >() ); }; | ||||||
268 | |||||||
269 | m_pData->m_bIsTopLevelDocumentWindow = bool( pContainerWindow->GetExtendedStyle() & WindowExtendedStyle::Document ); | ||||||
| |||||||
270 | } | ||||||
271 | |||||||
272 | const Reference< XTopWindow > xFrameContainer( xContainerWindow, UNO_QUERY ); | ||||||
273 | if ( xFrameContainer.is() ) | ||||||
274 | (xFrameContainer.get()->*pListenerAction)( this ); | ||||||
275 | } | ||||||
276 | catch( const Exception& ) | ||||||
277 | { | ||||||
278 | DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/controllerframe.cxx" ":" "278" ": ", "dbaccess" );; | ||||||
279 | } | ||||||
280 | } | ||||||
281 | |||||||
282 | void FrameWindowActivationListener::impl_checkDisposed_throw() const | ||||||
283 | { | ||||||
284 | if ( !m_pData ) | ||||||
285 | throw DisposedException( OUString(), *const_cast< FrameWindowActivationListener* >( this ) ); | ||||||
286 | } | ||||||
287 | |||||||
288 | void SAL_CALL FrameWindowActivationListener::windowOpened( const EventObject& /*_rEvent*/ ) | ||||||
289 | { | ||||||
290 | // not interested in | ||||||
291 | } | ||||||
292 | |||||||
293 | void SAL_CALL FrameWindowActivationListener::windowClosing( const EventObject& /*_rEvent*/ ) | ||||||
294 | { | ||||||
295 | // not interested in | ||||||
296 | } | ||||||
297 | |||||||
298 | void SAL_CALL FrameWindowActivationListener::windowClosed( const EventObject& /*_rEvent*/ ) | ||||||
299 | { | ||||||
300 | // not interested in | ||||||
301 | } | ||||||
302 | |||||||
303 | void SAL_CALL FrameWindowActivationListener::windowMinimized( const EventObject& /*_rEvent*/ ) | ||||||
304 | { | ||||||
305 | // not interested in | ||||||
306 | } | ||||||
307 | |||||||
308 | void SAL_CALL FrameWindowActivationListener::windowNormalized( const EventObject& /*_rEvent*/ ) | ||||||
309 | { | ||||||
310 | // not interested in | ||||||
311 | } | ||||||
312 | |||||||
313 | void SAL_CALL FrameWindowActivationListener::windowActivated( const EventObject& /*_rEvent*/ ) | ||||||
314 | { | ||||||
315 | impl_checkDisposed_throw(); | ||||||
316 | lcl_updateActive_nothrow( *m_pData, true ); | ||||||
317 | } | ||||||
318 | |||||||
319 | void SAL_CALL FrameWindowActivationListener::windowDeactivated( const EventObject& /*_rEvent*/ ) | ||||||
320 | { | ||||||
321 | impl_checkDisposed_throw(); | ||||||
322 | lcl_updateActive_nothrow( *m_pData, false ); | ||||||
323 | } | ||||||
324 | |||||||
325 | void SAL_CALL FrameWindowActivationListener::disposing( const EventObject& /*_rEvent*/ ) | ||||||
326 | { | ||||||
327 | dispose(); | ||||||
328 | } | ||||||
329 | |||||||
330 | // ControllerFrame | ||||||
331 | ControllerFrame::ControllerFrame( IController& _rController ) | ||||||
332 | :m_pData( new ControllerFrame_Data( _rController ) ) | ||||||
333 | { | ||||||
334 | } | ||||||
335 | |||||||
336 | ControllerFrame::~ControllerFrame() | ||||||
337 | { | ||||||
338 | } | ||||||
339 | |||||||
340 | const Reference< XFrame >& ControllerFrame::attachFrame( const Reference< XFrame >& _rxFrame ) | ||||||
341 | { | ||||||
342 | // set new frame, including listener handling | ||||||
343 | lcl_setFrame_nothrow( *m_pData, _rxFrame ); | ||||||
| |||||||
344 | |||||||
345 | // determine whether we're active | ||||||
346 | m_pData->m_bActive = lcl_isActive_nothrow( m_pData->m_xFrame ); | ||||||
347 | |||||||
348 | // update active component | ||||||
349 | if ( m_pData->m_bActive ) | ||||||
350 | { | ||||||
351 | lcl_updateActiveComponents_nothrow( *m_pData ); | ||||||
352 | lcl_notifyFocusChange_nothrow( *m_pData, true ); | ||||||
353 | } | ||||||
354 | |||||||
355 | return m_pData->m_xFrame; | ||||||
356 | } | ||||||
357 | |||||||
358 | const Reference< XFrame >& ControllerFrame::getFrame() const | ||||||
359 | { | ||||||
360 | return m_pData->m_xFrame; | ||||||
361 | } | ||||||
362 | |||||||
363 | bool ControllerFrame::isActive() const | ||||||
364 | { | ||||||
365 | return m_pData->m_bActive; | ||||||
366 | } | ||||||
367 | |||||||
368 | void ControllerFrame::frameAction( FrameAction _eAction ) | ||||||
369 | { | ||||||
370 | bool bActive = m_pData->m_bActive; | ||||||
371 | |||||||
372 | switch ( _eAction ) | ||||||
373 | { | ||||||
374 | case FrameAction_FRAME_ACTIVATED: | ||||||
375 | case FrameAction_FRAME_UI_ACTIVATED: | ||||||
376 | bActive = true; | ||||||
377 | break; | ||||||
378 | |||||||
379 | case FrameAction_FRAME_DEACTIVATING: | ||||||
380 | case FrameAction_FRAME_UI_DEACTIVATING: | ||||||
381 | bActive = false; | ||||||
382 | break; | ||||||
383 | |||||||
384 | default: | ||||||
385 | break; | ||||||
386 | } | ||||||
387 | |||||||
388 | lcl_updateActive_nothrow( *m_pData, bActive ); | ||||||
389 | } | ||||||
390 | |||||||
391 | } // namespace dbaui | ||||||
392 | |||||||
393 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_RTL_REF_HXX |
21 | #define INCLUDED_RTL_REF_HXX |
22 | |
23 | #include "sal/config.h" |
24 | |
25 | #include <cassert> |
26 | #include <cstddef> |
27 | #include <functional> |
28 | #ifdef LIBO_INTERNAL_ONLY1 |
29 | #include <type_traits> |
30 | #endif |
31 | |
32 | #include "sal/types.h" |
33 | |
34 | namespace rtl |
35 | { |
36 | |
37 | /** Template reference class for reference type. |
38 | */ |
39 | template <class reference_type> |
40 | class Reference |
41 | { |
42 | /** The <b>reference_type</b> body pointer. |
43 | */ |
44 | reference_type * m_pBody; |
45 | |
46 | |
47 | public: |
48 | /** Constructor... |
49 | */ |
50 | Reference() |
51 | : m_pBody (NULL__null) |
52 | {} |
53 | |
54 | |
55 | /** Constructor... |
56 | */ |
57 | Reference (reference_type * pBody, __sal_NoAcquire) |
58 | : m_pBody (pBody) |
59 | { |
60 | } |
61 | |
62 | /** Constructor... |
63 | */ |
64 | Reference (reference_type * pBody) |
65 | : m_pBody (pBody) |
66 | { |
67 | if (m_pBody) |
68 | m_pBody->acquire(); |
69 | } |
70 | |
71 | /** Copy constructor... |
72 | */ |
73 | Reference (const Reference<reference_type> & handle) |
74 | : m_pBody (handle.m_pBody) |
75 | { |
76 | if (m_pBody) |
77 | m_pBody->acquire(); |
78 | } |
79 | |
80 | #ifdef LIBO_INTERNAL_ONLY1 |
81 | /** Move constructor... |
82 | */ |
83 | Reference (Reference<reference_type> && handle) noexcept |
84 | : m_pBody (handle.m_pBody) |
85 | { |
86 | handle.m_pBody = nullptr; |
87 | } |
88 | #endif |
89 | |
90 | #if defined LIBO_INTERNAL_ONLY1 |
91 | /** Up-casting conversion constructor: Copies interface reference. |
92 | |
93 | Does not work for up-casts to ambiguous bases. |
94 | |
95 | @param rRef another reference |
96 | */ |
97 | template< class derived_type > |
98 | inline Reference( |
99 | const Reference< derived_type > & rRef, |
100 | std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 ) |
101 | : m_pBody (rRef.get()) |
102 | { |
103 | if (m_pBody) |
104 | m_pBody->acquire(); |
105 | } |
106 | #endif |
107 | |
108 | /** Destructor... |
109 | */ |
110 | ~Reference() COVERITY_NOEXCEPT_FALSE |
111 | { |
112 | if (m_pBody) |
113 | m_pBody->release(); |
114 | } |
115 | |
116 | /** Set... |
117 | Similar to assignment. |
118 | */ |
119 | Reference<reference_type> & |
120 | SAL_CALL set (reference_type * pBody) |
121 | { |
122 | if (pBody) |
123 | pBody->acquire(); |
124 | reference_type * const pOld = m_pBody; |
125 | m_pBody = pBody; |
126 | if (pOld) |
127 | pOld->release(); |
128 | return *this; |
129 | } |
130 | |
131 | /** Assignment. |
132 | Unbinds this instance from its body (if bound) and |
133 | bind it to the body represented by the handle. |
134 | */ |
135 | Reference<reference_type> & |
136 | SAL_CALL operator= (const Reference<reference_type> & handle) |
137 | { |
138 | return set( handle.m_pBody ); |
139 | } |
140 | |
141 | #ifdef LIBO_INTERNAL_ONLY1 |
142 | /** Assignment. |
143 | * Unbinds this instance from its body (if bound), |
144 | * bind it to the body represented by the handle, and |
145 | * set the body represented by the handle to nullptr. |
146 | */ |
147 | Reference<reference_type> & |
148 | operator= (Reference<reference_type> && handle) |
149 | { |
150 | // self-movement guts ourself |
151 | if (m_pBody) |
152 | m_pBody->release(); |
153 | m_pBody = handle.m_pBody; |
154 | handle.m_pBody = nullptr; |
155 | return *this; |
156 | } |
157 | #endif |
158 | |
159 | /** Assignment... |
160 | */ |
161 | Reference<reference_type> & |
162 | SAL_CALL operator= (reference_type * pBody) |
163 | { |
164 | return set( pBody ); |
165 | } |
166 | |
167 | /** Unbind the body from this handle. |
168 | Note that for a handle representing a large body, |
169 | "handle.clear().set(new body());" _might_ |
170 | perform a little bit better than "handle.set(new body());", |
171 | since in the second case two large objects exist in memory |
172 | (the old body and the new body). |
173 | */ |
174 | Reference<reference_type> & SAL_CALL clear() |
175 | { |
176 | if (m_pBody) |
177 | { |
178 | reference_type * const pOld = m_pBody; |
179 | m_pBody = NULL__null; |
180 | pOld->release(); |
181 | } |
182 | return *this; |
183 | } |
184 | |
185 | |
186 | /** Get the body. Can be used instead of operator->(). |
187 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() |
188 | are the same. |
189 | */ |
190 | reference_type * SAL_CALL get() const |
191 | { |
192 | return m_pBody; |
193 | } |
194 | |
195 | |
196 | /** Probably most common used: handle->someBodyOp(). |
197 | */ |
198 | reference_type * SAL_CALL operator->() const |
199 | { |
200 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 200, __extension__ __PRETTY_FUNCTION__)); |
201 | return m_pBody; |
202 | } |
203 | |
204 | |
205 | /** Allows (*handle).someBodyOp(). |
206 | */ |
207 | reference_type & SAL_CALL operator*() const |
208 | { |
209 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 209, __extension__ __PRETTY_FUNCTION__)); |
210 | return *m_pBody; |
211 | } |
212 | |
213 | |
214 | /** Returns True if the handle does point to a valid body. |
215 | */ |
216 | bool SAL_CALL is() const |
217 | { |
218 | return (m_pBody != NULL__null); |
219 | } |
220 | |
221 | #if defined LIBO_INTERNAL_ONLY1 |
222 | /** Returns True if the handle does point to a valid body. |
223 | */ |
224 | explicit operator bool() const |
225 | { |
226 | return is(); |
227 | } |
228 | #endif |
229 | |
230 | /** Returns True if this points to pBody. |
231 | */ |
232 | bool SAL_CALL operator== (const reference_type * pBody) const |
233 | { |
234 | return (m_pBody == pBody); |
235 | } |
236 | |
237 | |
238 | /** Returns True if handle points to the same body. |
239 | */ |
240 | bool |
241 | SAL_CALL operator== (const Reference<reference_type> & handle) const |
242 | { |
243 | return (m_pBody == handle.m_pBody); |
244 | } |
245 | |
246 | |
247 | /** Needed to place References into STL collection. |
248 | */ |
249 | bool |
250 | SAL_CALL operator!= (const Reference<reference_type> & handle) const |
251 | { |
252 | return (m_pBody != handle.m_pBody); |
253 | } |
254 | |
255 | |
256 | /** Needed to place References into STL collection. |
257 | */ |
258 | bool |
259 | SAL_CALL operator< (const Reference<reference_type> & handle) const |
260 | { |
261 | return (m_pBody < handle.m_pBody); |
262 | } |
263 | |
264 | |
265 | /** Needed to place References into STL collection. |
266 | */ |
267 | bool |
268 | SAL_CALL operator> (const Reference<reference_type> & handle) const |
269 | { |
270 | return (m_pBody > handle.m_pBody); |
271 | } |
272 | }; |
273 | |
274 | } // namespace rtl |
275 | |
276 | #if defined LIBO_INTERNAL_ONLY1 |
277 | namespace std |
278 | { |
279 | |
280 | /// @cond INTERNAL |
281 | /** |
282 | Make rtl::Reference hashable by default for use in STL containers. |
283 | |
284 | @since LibreOffice 6.3 |
285 | */ |
286 | template<typename T> |
287 | struct hash<::rtl::Reference<T>> |
288 | { |
289 | std::size_t operator()(::rtl::Reference<T> const & s) const |
290 | { return std::size_t(s.get()); } |
291 | }; |
292 | /// @endcond |
293 | |
294 | } |
295 | |
296 | #endif |
297 | |
298 | #endif /* ! INCLUDED_RTL_REF_HXX */ |
299 | |
300 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_VCL_Reference_HXX |
20 | #define INCLUDED_VCL_Reference_HXX |
21 | |
22 | #include <vcl/dllapi.h> |
23 | #include <osl/interlck.h> |
24 | |
25 | class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase |
26 | { |
27 | mutable oslInterlockedCount mnRefCnt; |
28 | |
29 | template<typename T> friend class VclPtr; |
30 | |
31 | public: |
32 | void acquire() const |
33 | { |
34 | osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1); |
35 | } |
36 | |
37 | void release() const |
38 | { |
39 | if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0) |
40 | delete this; |
41 | } |
42 | #ifdef DBG_UTIL |
43 | #ifndef _WIN32 |
44 | sal_Int32 getRefCount() const { return mnRefCnt; } |
45 | #endif |
46 | #endif |
47 | |
48 | |
49 | private: |
50 | VclReferenceBase(const VclReferenceBase&) = delete; |
51 | VclReferenceBase& operator=(const VclReferenceBase&) = delete; |
52 | |
53 | bool mbDisposed : 1; |
54 | |
55 | protected: |
56 | VclReferenceBase(); |
57 | protected: |
58 | virtual ~VclReferenceBase(); |
59 | |
60 | protected: |
61 | virtual void dispose(); |
62 | |
63 | public: |
64 | void disposeOnce(); |
65 | bool isDisposed() const { return mbDisposed; } |
66 | |
67 | }; |
68 | #endif |