Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 192, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name popuptoolbarcontroller.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D FWK_DLLIMPLEMENTATION -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/framework/source/inc -I /home/maarten/src/libreoffice/core/framework/inc -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx

/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx

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 <bitmaps.hlst>
21
22#include <cppuhelper/implbase.hxx>
23#include <cppuhelper/supportsservice.hxx>
24#include <comphelper/propertyvalue.hxx>
25#include <menuconfiguration.hxx>
26#include <svtools/imagemgr.hxx>
27#include <svtools/miscopt.hxx>
28#include <svtools/toolboxcontroller.hxx>
29#include <toolkit/awt/vclxmenu.hxx>
30#include <toolkit/helper/vclunohelper.hxx>
31#include <tools/diagnose_ex.h>
32#include <tools/urlobj.hxx>
33#include <unotools/moduleoptions.hxx>
34#include <vcl/commandinfoprovider.hxx>
35#include <vcl/menu.hxx>
36#include <vcl/svapp.hxx>
37#include <vcl/toolbox.hxx>
38
39#include <com/sun/star/awt/PopupMenuDirection.hpp>
40#include <com/sun/star/awt/XPopupMenu.hpp>
41#include <com/sun/star/frame/thePopupMenuControllerFactory.hpp>
42#include <com/sun/star/frame/XPopupMenuController.hpp>
43#include <com/sun/star/frame/XStorable.hpp>
44#include <com/sun/star/frame/XSubToolbarController.hpp>
45#include <com/sun/star/frame/XUIControllerFactory.hpp>
46#include <com/sun/star/frame/XController.hpp>
47#include <com/sun/star/lang/XServiceInfo.hpp>
48#include <com/sun/star/ucb/CommandFailedException.hpp>
49#include <com/sun/star/ucb/ContentCreationException.hpp>
50#include <com/sun/star/util/XModifiable.hpp>
51
52using namespace framework;
53
54namespace
55{
56
57typedef cppu::ImplInheritanceHelper< svt::ToolboxController,
58 css::lang::XServiceInfo >
59 ToolBarBase;
60
61class PopupMenuToolbarController : public ToolBarBase
62{
63public:
64 // XComponent
65 virtual void SAL_CALL dispose() override;
66 // XInitialization
67 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
68 // XToolbarController
69 virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override;
70 // XStatusListener
71 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
72
73protected:
74 PopupMenuToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
75 const OUString &rPopupCommand = OUString() );
76 virtual void functionExecuted( const OUString &rCommand );
77 virtual ToolBoxItemBits getDropDownStyle() const;
78 void createPopupMenuController();
79
80 bool m_bHasController;
81 bool m_bResourceURL;
82 OUString m_aPopupCommand;
83 css::uno::Reference< css::awt::XPopupMenu > m_xPopupMenu;
84
85private:
86 css::uno::Reference< css::frame::XUIControllerFactory > m_xPopupMenuFactory;
87 css::uno::Reference< css::frame::XPopupMenuController > m_xPopupMenuController;
88};
89
90PopupMenuToolbarController::PopupMenuToolbarController(
91 const css::uno::Reference< css::uno::XComponentContext >& xContext,
92 const OUString &rPopupCommand )
93 : ToolBarBase( xContext, css::uno::Reference< css::frame::XFrame >(), /*aCommandURL*/OUString() )
94 , m_bHasController( false )
95 , m_bResourceURL( false )
96 , m_aPopupCommand( rPopupCommand )
97{
98}
99
100void SAL_CALL PopupMenuToolbarController::dispose()
101{
102 svt::ToolboxController::dispose();
103
104 osl::MutexGuard aGuard( m_aMutex );
105 if( m_xPopupMenuController.is() )
106 {
107 css::uno::Reference< css::lang::XComponent > xComponent(
108 m_xPopupMenuController, css::uno::UNO_QUERY );
109 if( xComponent.is() )
110 {
111 try
112 {
113 xComponent->dispose();
114 }
115 catch (...)
116 {}
117 }
118 m_xPopupMenuController.clear();
119 }
120
121 m_xContext.clear();
122 m_xPopupMenuFactory.clear();
123 m_xPopupMenu.clear();
124}
125
126void SAL_CALL PopupMenuToolbarController::initialize(
127 const css::uno::Sequence< css::uno::Any >& aArguments )
128{
129 ToolboxController::initialize( aArguments );
130
131 osl::MutexGuard aGuard( m_aMutex );
132 if ( !m_aPopupCommand.getLength() )
133 m_aPopupCommand = m_aCommandURL;
134
135 try
136 {
137 m_xPopupMenuFactory.set(
138 css::frame::thePopupMenuControllerFactory::get( m_xContext ) );
139 m_bHasController = m_xPopupMenuFactory->hasController(
140 m_aPopupCommand, getModuleName() );
141 }
142 catch (const css::uno::Exception&)
143 {
144 TOOLS_INFO_EXCEPTION( "fwk.uielement", "" )do { css::uno::Any tools_warn_exception( DbgGetCaughtException
() ); do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "fwk.uielement")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break
; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "" << " " <<
exceptionToString(tools_warn_exception)) == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "144" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "" << " " << exceptionToString
(tools_warn_exception)), 0); } else { ::std::ostringstream sal_detail_stream
; sal_detail_stream << "" << " " << exceptionToString
(tools_warn_exception); ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "144" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "" << " " << exceptionToString(tools_warn_exception
)) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), (
"fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "144" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "" << " " << exceptionToString
(tools_warn_exception)), 0); } else { ::std::ostringstream sal_detail_stream
; sal_detail_stream << "" << " " << exceptionToString
(tools_warn_exception); ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "144" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false); } while (false)
;
145 }
146
147 if ( !m_bHasController && m_aPopupCommand.startsWith( "private:resource/" ) )
148 {
149 m_bResourceURL = true;
150 m_bHasController = true;
151 }
152
153 SolarMutexGuard aSolarLock;
154 VclPtr< ToolBox > pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ).get() );
155 if ( pToolBox )
156 {
157 ToolBoxItemBits nCurStyle( pToolBox->GetItemBits( m_nToolBoxId ) );
158 ToolBoxItemBits nSetStyle( getDropDownStyle() );
159 pToolBox->SetItemBits( m_nToolBoxId,
160 m_bHasController ?
161 nCurStyle | nSetStyle :
162 nCurStyle & ~nSetStyle );
163 }
164
165}
166
167void SAL_CALL PopupMenuToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
168{
169 if ( m_bResourceURL )
170 return;
171
172 ToolBox* pToolBox = nullptr;
173 sal_uInt16 nItemId = 0;
174 if ( getToolboxId( nItemId, &pToolBox ) )
175 {
176 SolarMutexGuard aSolarLock;
177 pToolBox->EnableItem( nItemId, rEvent.IsEnabled );
178 bool bValue;
179 if ( rEvent.State >>= bValue )
180 pToolBox->CheckItem( nItemId, bValue );
181 }
182}
183
184css::uno::Reference< css::awt::XWindow > SAL_CALL
185PopupMenuToolbarController::createPopupWindow()
186{
187 css::uno::Reference< css::awt::XWindow > xRet;
188
189 osl::MutexGuard aGuard( m_aMutex );
190 if ( !m_bHasController )
191 return xRet;
192
193 createPopupMenuController();
194
195 SolarMutexGuard aSolarLock;
196 VclPtr< ToolBox > pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ).get() );
197 if ( !pToolBox )
198 return xRet;
199
200 pToolBox->SetItemDown( m_nToolBoxId, true );
201 WindowAlign eAlign( pToolBox->GetAlign() );
202
203 // If the parent ToolBox is in popup mode (e.g. sub toolbar, overflow popup),
204 // its ToolBarManager can be disposed along with our controller, destroying
205 // m_xPopupMenu, while the latter still in execute. This should be fixed at a
206 // different level, for now just hold it here so it won't crash.
207 css::uno::Reference< css::awt::XPopupMenu > xPopupMenu ( m_xPopupMenu );
208 sal_uInt16 nId = xPopupMenu->execute(
209 css::uno::Reference< css::awt::XWindowPeer >( getParent(), css::uno::UNO_QUERY ),
210 VCLUnoHelper::ConvertToAWTRect( pToolBox->GetItemRect( m_nToolBoxId ) ),
211 ( eAlign == WindowAlign::Top || eAlign == WindowAlign::Bottom ) ?
212 css::awt::PopupMenuDirection::EXECUTE_DOWN :
213 css::awt::PopupMenuDirection::EXECUTE_RIGHT );
214 pToolBox->SetItemDown( m_nToolBoxId, false );
215
216 if ( nId )
217 functionExecuted( xPopupMenu->getCommand( nId ) );
218
219 return xRet;
220}
221
222void PopupMenuToolbarController::functionExecuted( const OUString &/*rCommand*/)
223{
224}
225
226ToolBoxItemBits PopupMenuToolbarController::getDropDownStyle() const
227{
228 return ToolBoxItemBits::DROPDOWN;
229}
230
231void PopupMenuToolbarController::createPopupMenuController()
232{
233 if( !m_bHasController )
234 return;
235
236 if ( m_xPopupMenuController.is() )
237 {
238 m_xPopupMenuController->updatePopupMenu();
239 }
240 else
241 {
242 css::uno::Sequence<css::uno::Any> aArgs {
243 css::uno::makeAny(comphelper::makePropertyValue("Frame", m_xFrame)),
244 css::uno::makeAny(comphelper::makePropertyValue("ModuleIdentifier", m_sModuleName)),
245 css::uno::makeAny(comphelper::makePropertyValue("InToolbar", true))
246 };
247
248 try
249 {
250 m_xPopupMenu.set(
251 m_xContext->getServiceManager()->createInstanceWithContext(
252 "com.sun.star.awt.PopupMenu", m_xContext ),
253 css::uno::UNO_QUERY_THROW );
254
255 if (m_bResourceURL)
256 {
257 sal_Int32 nAppendIndex = aArgs.getLength();
258 aArgs.realloc(nAppendIndex + 1);
259 aArgs[nAppendIndex] <<= comphelper::makePropertyValue("ResourceURL", m_aPopupCommand);
260
261 m_xPopupMenuController.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
262 "com.sun.star.comp.framework.ResourceMenuController", aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
263 }
264 else
265 {
266 m_xPopupMenuController.set( m_xPopupMenuFactory->createInstanceWithArgumentsAndContext(
267 m_aPopupCommand, aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
268 }
269
270 m_xPopupMenuController->setPopupMenu( m_xPopupMenu );
271 }
272 catch ( const css::uno::Exception & )
273 {
274 TOOLS_INFO_EXCEPTION( "fwk.uielement", "" )do { css::uno::Any tools_warn_exception( DbgGetCaughtException
() ); do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "fwk.uielement")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break
; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "" << " " <<
exceptionToString(tools_warn_exception)) == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "274" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "" << " " << exceptionToString
(tools_warn_exception)), 0); } else { ::std::ostringstream sal_detail_stream
; sal_detail_stream << "" << " " << exceptionToString
(tools_warn_exception); ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "274" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "" << " " << exceptionToString(tools_warn_exception
)) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), (
"fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "274" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "" << " " << exceptionToString
(tools_warn_exception)), 0); } else { ::std::ostringstream sal_detail_stream
; sal_detail_stream << "" << " " << exceptionToString
(tools_warn_exception); ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("fwk.uielement"), ("/home/maarten/src/libreoffice/core/framework/source/uielement/popuptoolbarcontroller.cxx"
":" "274" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false); } while (false)
;
275 m_xPopupMenu.clear();
276 }
277 }
278}
279
280class GenericPopupToolbarController : public PopupMenuToolbarController
281{
282public:
283 GenericPopupToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
284 const css::uno::Sequence< css::uno::Any >& rxArgs );
285
286 // XInitialization
287 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& rxArgs ) override;
288
289 // XStatusListener
290 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
291
292 // XServiceInfo
293 virtual OUString SAL_CALL getImplementationName() override;
294
295 virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
296
297 virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
298
299private:
300 bool m_bSplitButton, m_bReplaceWithLast;
301 void functionExecuted(const OUString &rCommand) override;
302 ToolBoxItemBits getDropDownStyle() const override;
303};
304
305GenericPopupToolbarController::GenericPopupToolbarController(
306 const css::uno::Reference< css::uno::XComponentContext >& xContext,
307 const css::uno::Sequence< css::uno::Any >& rxArgs )
308 : PopupMenuToolbarController( xContext )
309 , m_bReplaceWithLast( false )
310{
311 css::beans::PropertyValue aPropValue;
312 for ( const auto& arg: rxArgs )
313 {
314 if ( ( arg >>= aPropValue ) && aPropValue.Name == "Value" )
315 {
316 sal_Int32 nIdx{ 0 };
317 OUString aValue;
318 aPropValue.Value >>= aValue;
319 m_aPopupCommand = aValue.getToken(0, ';', nIdx);
320 m_bReplaceWithLast = aValue.getToken(0, ';', nIdx).toBoolean();
321 break;
322 }
323 }
324 m_bSplitButton = m_bReplaceWithLast || !m_aPopupCommand.isEmpty();
325}
326
327OUString GenericPopupToolbarController::getImplementationName()
328{
329 return "com.sun.star.comp.framework.GenericPopupToolbarController";
330}
331
332sal_Bool GenericPopupToolbarController::supportsService(OUString const & rServiceName)
333{
334 return cppu::supportsService( this, rServiceName );
335}
336
337css::uno::Sequence<OUString> GenericPopupToolbarController::getSupportedServiceNames()
338{
339 return {"com.sun.star.frame.ToolbarController"};
340}
341
342void GenericPopupToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& rxArgs )
343{
344 PopupMenuToolbarController::initialize( rxArgs );
345 if ( m_bReplaceWithLast )
346 // Create early, so we can use the menu is statusChanged method.
347 createPopupMenuController();
348}
349
350void GenericPopupToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
351{
352 SolarMutexGuard aGuard;
353
354 if ( m_bReplaceWithLast && !rEvent.IsEnabled && m_xPopupMenu.is() )
355 {
356 Menu* pVclMenu = comphelper::getUnoTunnelImplementation<VCLXMenu>( m_xPopupMenu )->GetMenu();
357
358 ToolBox* pToolBox = nullptr;
359 sal_uInt16 nId = 0;
360 if ( getToolboxId( nId, &pToolBox ) && pToolBox->IsItemEnabled( nId ) )
361 {
362 pVclMenu->Activate();
363 pVclMenu->Deactivate();
364 }
365
366 for ( sal_uInt16 i = 0; i < pVclMenu->GetItemCount(); ++i )
367 {
368 sal_uInt16 nItemId = pVclMenu->GetItemId( i );
369 if ( nItemId && pVclMenu->IsItemEnabled( nItemId ) && !pVclMenu->GetPopupMenu( nItemId ) )
370 {
371 functionExecuted( pVclMenu->GetItemCommand( nItemId ) );
372 return;
373 }
374 }
375 }
376
377 PopupMenuToolbarController::statusChanged( rEvent );
378}
379
380void GenericPopupToolbarController::functionExecuted( const OUString& rCommand )
381{
382 if ( !m_bReplaceWithLast )
383 return;
384
385 removeStatusListener( m_aCommandURL );
386
387 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(rCommand, m_sModuleName);
388 OUString aRealCommand( vcl::CommandInfoProvider::GetRealCommandForCommand(aProperties) );
389 m_aCommandURL = aRealCommand.isEmpty() ? rCommand : aRealCommand;
390 addStatusListener( m_aCommandURL );
391
392 ToolBox* pToolBox = nullptr;
393 sal_uInt16 nId = 0;
394 if ( getToolboxId( nId, &pToolBox ) )
395 {
396 pToolBox->SetItemCommand( nId, rCommand );
397 pToolBox->SetHelpText( nId, OUString() ); // Will retrieve the new one from help.
398 pToolBox->SetItemText(nId, vcl::CommandInfoProvider::GetLabelForCommand(aProperties));
399 pToolBox->SetQuickHelpText(nId, vcl::CommandInfoProvider::GetTooltipForCommand(rCommand, aProperties, m_xFrame));
400
401 Image aImage = vcl::CommandInfoProvider::GetImageForCommand(rCommand, m_xFrame, pToolBox->GetImageSize());
402 if ( !!aImage )
403 pToolBox->SetItemImage( nId, aImage );
404 }
405}
406
407ToolBoxItemBits GenericPopupToolbarController::getDropDownStyle() const
408{
409 return m_bSplitButton ? ToolBoxItemBits::DROPDOWN : ToolBoxItemBits::DROPDOWNONLY;
410}
411
412class SaveToolbarController : public cppu::ImplInheritanceHelper< PopupMenuToolbarController,
413 css::frame::XSubToolbarController,
414 css::util::XModifyListener >
415{
416public:
417 explicit SaveToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
418
419 // XInitialization
420 virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
421
422 // XSubToolbarController
423 // Make ToolBarManager ask our controller for updated image, in case of icon theme change.
424 virtual sal_Bool SAL_CALL opensSubToolbar() override;
425 virtual OUString SAL_CALL getSubToolbarName() override;
426 virtual void SAL_CALL functionSelected( const OUString& aCommand ) override;
427 virtual void SAL_CALL updateImage() override;
428
429 // XStatusListener
430 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
431
432 // XModifyListener
433 virtual void SAL_CALL modified( const css::lang::EventObject& rEvent ) override;
434
435 // XEventListener
436 virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) override;
437
438 // XComponent
439 virtual void SAL_CALL dispose() override;
440
441 // XServiceInfo
442 virtual OUString SAL_CALL getImplementationName() override;
443 virtual sal_Bool SAL_CALL supportsService( OUString const & rServiceName ) override;
444 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
445
446private:
447 bool m_bReadOnly;
448 bool m_bModified;
449 css::uno::Reference< css::frame::XStorable > m_xStorable;
450 css::uno::Reference< css::util::XModifiable > m_xModifiable;
451};
452
453SaveToolbarController::SaveToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
454 : ImplInheritanceHelper( rxContext, ".uno:SaveAsMenu" )
455 , m_bReadOnly( false )
456 , m_bModified( false )
457{
458}
459
460void SaveToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
461{
462 PopupMenuToolbarController::initialize( aArguments );
463
464 ToolBox* pToolBox = nullptr;
465 sal_uInt16 nId = 0;
466 if ( !getToolboxId( nId, &pToolBox ) )
467 return;
468
469 css::uno::Reference< css::frame::XController > xController = m_xFrame->getController();
470 if ( xController.is() )
471 m_xModifiable.set( xController->getModel(), css::uno::UNO_QUERY );
472
473 if ( m_xModifiable.is() && pToolBox->GetItemCommand( nId ) == m_aCommandURL )
474 // Will also enable the save as only mode.
475 m_xStorable.set( m_xModifiable, css::uno::UNO_QUERY );
476 else if ( !m_xModifiable.is() )
477 // Can be in table/query design.
478 m_xModifiable.set( xController, css::uno::UNO_QUERY );
479 else
480 // Simple save button, without the dropdown.
481 pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) & ~ ToolBoxItemBits::DROPDOWN );
482
483 if ( m_xModifiable.is() )
484 {
485 m_xModifiable->addModifyListener( this );
486 modified( css::lang::EventObject() );
487 }
488}
489
490sal_Bool SaveToolbarController::opensSubToolbar()
491{
492 return true;
493}
494
495OUString SaveToolbarController::getSubToolbarName()
496{
497 return OUString();
498}
499
500void SaveToolbarController::functionSelected( const OUString& /*aCommand*/ )
501{
502}
503
504void SaveToolbarController::updateImage()
505{
506 SolarMutexGuard aGuard;
507 ToolBox* pToolBox = nullptr;
508 sal_uInt16 nId = 0;
509 if ( !getToolboxId( nId, &pToolBox ) )
510 return;
511
512 vcl::ImageType eImageType = pToolBox->GetImageSize();
513
514 Image aImage;
515
516 if ( m_bReadOnly )
517 {
518 aImage = vcl::CommandInfoProvider::GetImageForCommand(".uno:SaveAs", m_xFrame, eImageType);
519 }
520 else if ( m_bModified )
521 {
522 if (eImageType == vcl::ImageType::Size26)
523 aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_LARGE"res/savemodified_large.png");
524 else if (eImageType == vcl::ImageType::Size32)
525 aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_EXTRALARGE"res/savemodified_extralarge.png");
526 else
527 aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_SMALL"res/savemodified_small.png");
528 }
529
530 if ( !aImage )
531 aImage = vcl::CommandInfoProvider::GetImageForCommand(m_aCommandURL, m_xFrame, eImageType);
532
533 if ( !!aImage )
534 pToolBox->SetItemImage( nId, aImage );
535}
536
537void SaveToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
538{
539 ToolBox* pToolBox = nullptr;
540 sal_uInt16 nId = 0;
541 if ( !getToolboxId( nId, &pToolBox ) )
542 return;
543
544 bool bLastReadOnly = m_bReadOnly;
545 m_bReadOnly = m_xStorable.is() && m_xStorable->isReadonly();
546 if ( bLastReadOnly != m_bReadOnly )
547 {
548 OUString sCommand = m_bReadOnly ? OUString( ".uno:SaveAs" ) : m_aCommandURL;
549 auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(sCommand,
550 vcl::CommandInfoProvider::GetModuleIdentifier(m_xFrame));
551 pToolBox->SetQuickHelpText( nId,
552 vcl::CommandInfoProvider::GetTooltipForCommand(sCommand, aProperties, m_xFrame) );
553 pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) & ~( m_bReadOnly ? ToolBoxItemBits::DROPDOWN : ToolBoxItemBits::DROPDOWNONLY ) );
554 pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) | ( m_bReadOnly ? ToolBoxItemBits::DROPDOWNONLY : ToolBoxItemBits::DROPDOWN ) );
555 updateImage();
556 }
557
558 if ( !m_bReadOnly )
559 pToolBox->EnableItem( nId, rEvent.IsEnabled );
560}
561
562void SaveToolbarController::modified( const css::lang::EventObject& /*rEvent*/ )
563{
564 bool bLastModified = m_bModified;
565 m_bModified = m_xModifiable->isModified();
566 if ( bLastModified != m_bModified )
567 updateImage();
568}
569
570void SaveToolbarController::disposing( const css::lang::EventObject& rEvent )
571{
572 if ( rEvent.Source == m_xModifiable )
573 {
574 m_xModifiable.clear();
575 m_xStorable.clear();
576 }
577 else
578 PopupMenuToolbarController::disposing( rEvent );
579}
580
581void SaveToolbarController::dispose()
582{
583 PopupMenuToolbarController::dispose();
584 if ( m_xModifiable.is() )
585 {
586 m_xModifiable->removeModifyListener( this );
587 m_xModifiable.clear();
588 }
589 m_xStorable.clear();
590}
591
592OUString SaveToolbarController::getImplementationName()
593{
594 return "com.sun.star.comp.framework.SaveToolbarController";
595}
596
597sal_Bool SaveToolbarController::supportsService( OUString const & rServiceName )
598{
599 return cppu::supportsService( this, rServiceName );
600}
601
602css::uno::Sequence< OUString > SaveToolbarController::getSupportedServiceNames()
603{
604 return {"com.sun.star.frame.ToolbarController"};
605}
606
607class NewToolbarController : public cppu::ImplInheritanceHelper<PopupMenuToolbarController, css::frame::XSubToolbarController>
608{
609public:
610 explicit NewToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
611
612 // XServiceInfo
613 OUString SAL_CALL getImplementationName() override;
614
615 virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
616
617 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
618
619 void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
620
621 // XSubToolbarController
622 // Make ToolBarManager ask our controller for updated image, in case of icon theme change.
623 sal_Bool SAL_CALL opensSubToolbar() override { return true; }
624 OUString SAL_CALL getSubToolbarName() override { return OUString(); }
625 void SAL_CALL functionSelected( const OUString& ) override {}
626 void SAL_CALL updateImage() override;
627
628private:
629 void functionExecuted( const OUString &rCommand ) override;
630 void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
631 void SAL_CALL execute( sal_Int16 KeyModifier ) override;
632 sal_uInt16 getMenuIdForCommand( const OUString &rCommand );
633
634 sal_uInt16 m_nMenuId;
635};
636
637NewToolbarController::NewToolbarController(
638 const css::uno::Reference< css::uno::XComponentContext >& xContext )
639 : ImplInheritanceHelper( xContext )
640 , m_nMenuId( 0 )
641{
642}
643
644OUString NewToolbarController::getImplementationName()
645{
646 return "org.apache.openoffice.comp.framework.NewToolbarController";
647}
648
649sal_Bool NewToolbarController::supportsService(OUString const & rServiceName)
650{
651 return cppu::supportsService( this, rServiceName );
652}
653
654css::uno::Sequence<OUString> NewToolbarController::getSupportedServiceNames()
655{
656 return {"com.sun.star.frame.ToolbarController"};
657}
658
659void SAL_CALL NewToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
660{
661 PopupMenuToolbarController::initialize( aArguments );
662
663 osl::MutexGuard aGuard( m_aMutex );
664 createPopupMenuController();
665}
666
667void SAL_CALL NewToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
668{
669 if ( rEvent.IsEnabled )
1
Assuming field 'IsEnabled' is not equal to 0
2
Taking true branch
670 {
671 OUString aState;
672 rEvent.State >>= aState;
673 try
674 {
675 // set the image even if the state is not a string
676 // the toolbar item command will be used as a fallback
677 functionExecuted( aState );
3
Calling 'NewToolbarController::functionExecuted'
678 }
679 catch (const css::ucb::CommandFailedException&)
680 {
681 }
682 catch (const css::ucb::ContentCreationException&)
683 {
684 }
685 }
686
687 enable( rEvent.IsEnabled );
688}
689
690void SAL_CALL NewToolbarController::execute( sal_Int16 /*KeyModifier*/ )
691{
692 osl::MutexGuard aGuard( m_aMutex );
693
694 OUString aURL, aTarget;
695 if ( m_xPopupMenu.is() && m_nMenuId )
696 {
697 // TODO investigate how to wrap Get/SetUserValue in css::awt::XMenu
698 SolarMutexGuard aSolarMutexGuard;
699 Menu* pVclMenu = comphelper::getUnoTunnelImplementation<VCLXMenu>( m_xPopupMenu )->GetMenu();
700 aURL = pVclMenu->GetItemCommand( m_nMenuId );
701
702 MenuAttributes* pMenuAttributes( static_cast<MenuAttributes*>( pVclMenu->GetUserValue( m_nMenuId ) ) );
703 if ( pMenuAttributes )
704 aTarget = pMenuAttributes->aTargetFrame;
705 else
706 aTarget = "_default";
707 }
708 else
709 aURL = m_aCommandURL;
710
711 css::uno::Sequence< css::beans::PropertyValue > aArgs( 1 );
712 aArgs[0].Name = "Referer";
713 aArgs[0].Value <<= OUString( "private:user" );
714
715 dispatchCommand( aURL, aArgs, aTarget );
716}
717
718void NewToolbarController::functionExecuted( const OUString &rCommand )
719{
720 m_nMenuId = getMenuIdForCommand( rCommand );
721 updateImage();
4
Calling 'NewToolbarController::updateImage'
722}
723
724sal_uInt16 NewToolbarController::getMenuIdForCommand( const OUString &rCommand )
725{
726 if ( m_xPopupMenu.is() && !rCommand.isEmpty() )
727 {
728 Menu* pVclMenu( comphelper::getUnoTunnelImplementation<VCLXMenu>( m_xPopupMenu )->GetMenu() );
729 sal_uInt16 nCount = pVclMenu->GetItemCount();
730 for ( sal_uInt16 n = 0; n < nCount; ++n )
731 {
732 sal_uInt16 nId = pVclMenu->GetItemId( n );
733 OUString aCmd( pVclMenu->GetItemCommand( nId ) );
734
735 // match even if the menu command is more detailed
736 // (maybe an additional query) #i28667#
737 if ( aCmd.match( rCommand ) )
738 return nId;
739 }
740 }
741
742 return 0;
743}
744
745void SAL_CALL NewToolbarController::updateImage()
746{
747 SolarMutexGuard aSolarLock;
748 VclPtr< ToolBox> pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ).get() );
5
Calling constructor for 'VclPtr<ToolBox>'
10
Returning from constructor for 'VclPtr<ToolBox>'
11
Calling implicit destructor for 'VclPtr<vcl::Window>'
12
Calling '~Reference'
19
Returning from '~Reference'
20
Returning from destructor for 'VclPtr<vcl::Window>'
749 if ( !pToolBox )
21
Calling 'VclPtr::operator bool'
750 return;
751
752 OUString aURL, aImageId;
753 if ( m_xPopupMenu.is() && m_nMenuId )
754 {
755 Menu* pVclMenu = comphelper::getUnoTunnelImplementation<VCLXMenu>( m_xPopupMenu )->GetMenu();
756 aURL = pVclMenu->GetItemCommand( m_nMenuId );
757
758 MenuAttributes* pMenuAttributes( static_cast<MenuAttributes*>( pVclMenu->GetUserValue( m_nMenuId ) ) );
759 if ( pMenuAttributes )
760 aImageId = pMenuAttributes->aImageId;
761 }
762 else
763 aURL = m_aCommandURL;
764
765 INetURLObject aURLObj( aImageId.isEmpty() ? aURL : aImageId );
766 vcl::ImageType eImageType( pToolBox->GetImageSize() );
767 Image aImage = SvFileInformationManager::GetImageNoDefault( aURLObj, eImageType );
768 if ( !aImage )
769 aImage = vcl::CommandInfoProvider::GetImageForCommand( aURL, m_xFrame, eImageType );
770
771 if ( !aImage )
772 return;
773
774 pToolBox->SetItemImage( m_nToolBoxId, aImage );
775}
776
777}
778
779extern "C" SAL_DLLPUBLIC_EXPORT__attribute__ ((visibility("default"))) css::uno::XInterface *
780com_sun_star_comp_framework_GenericPopupToolbarController_get_implementation(
781 css::uno::XComponentContext *context,
782 css::uno::Sequence<css::uno::Any> const &args)
783{
784 return cppu::acquire(new GenericPopupToolbarController(context, args));
785}
786
787extern "C" SAL_DLLPUBLIC_EXPORT__attribute__ ((visibility("default"))) css::uno::XInterface *
788com_sun_star_comp_framework_SaveToolbarController_get_implementation(
789 css::uno::XComponentContext *context,
790 css::uno::Sequence<css::uno::Any> const &)
791{
792 return cppu::acquire(new SaveToolbarController(context));
793}
794
795extern "C" SAL_DLLPUBLIC_EXPORT__attribute__ ((visibility("default"))) css::uno::XInterface *
796org_apache_openoffice_comp_framework_NewToolbarController_get_implementation(
797 css::uno::XComponentContext *context,
798 css::uno::Sequence<css::uno::Any> const &)
799{
800 return cppu::acquire(new NewToolbarController(context));
801}
802
803/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx

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_VCL_PTR_HXX
21#define INCLUDED_VCL_PTR_HXX
22
23#include <sal/config.h>
24
25#include <rtl/ref.hxx>
26
27#include <utility>
28#include <type_traits>
29
30#ifdef DBG_UTIL
31#ifndef _WIN32
32#include <vcl/vclmain.hxx>
33#endif
34#endif
35
36class VclReferenceBase;
37
38namespace vcl::detail {
39
40template<typename>
41constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
42
43template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase(
44 int (*)[sizeof(T)])
45{ return std::is_base_of<VclReferenceBase, T>::value; }
46
47} // namespace vcl::detail
48
49/**
50 * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses.
51 *
52 * For more details on the design please see vcl/README.lifecycle
53 *
54 * @param reference_type must be a subclass of vcl::Window
55 */
56template <class reference_type>
57class VclPtr
58{
59 static_assert(
60 vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>(
61 nullptr),
62 "template argument type must be derived from VclReferenceBase");
63
64 ::rtl::Reference<reference_type> m_rInnerRef;
65
66public:
67 /** Constructor...
68 */
69 VclPtr()
70 : m_rInnerRef()
71 {}
72
73 /** Constructor...
74 */
75 VclPtr (reference_type * pBody)
76 : m_rInnerRef(pBody)
6
Calling constructor for 'Reference<ToolBox>'
9
Returning from constructor for 'Reference<ToolBox>'
77 {}
78
79 /** Constructor... that doesn't take a ref.
80 */
81 VclPtr (reference_type * pBody, __sal_NoAcquire)
82 : m_rInnerRef(pBody, SAL_NO_ACQUIRE)
83 {}
84
85 /** Up-casting conversion constructor: Copies interface reference.
86
87 Does not work for up-casts to ambiguous bases. For the special case of
88 up-casting to Reference< XInterface >, see the corresponding conversion
89 operator.
90
91 @param rRef another reference
92 */
93 template< class derived_type >
94 VclPtr(
95 const VclPtr< derived_type > & rRef,
96 typename std::enable_if<
97 std::is_base_of<reference_type, derived_type>::value, int>::type
98 = 0 )
99 : m_rInnerRef( static_cast<reference_type*>(rRef) )
100 {
101 }
102
103#if defined(DBG_UTIL) && !defined(_WIN32)
104 virtual ~VclPtr()
105 {
106 assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain
::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 106, __extension__ __PRETTY_FUNCTION__))
;
107 // We can be one of the intermediate counts, but if we are the last
108 // VclPtr keeping this object alive, then something forgot to call dispose().
109 assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
110 && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
;
111 }
112 VclPtr(VclPtr const &) = default;
113 VclPtr(VclPtr &&) = default;
114 VclPtr & operator =(VclPtr const &) = default;
115 VclPtr & operator =(VclPtr &&) = default;
116#endif
117
118 /**
119 * A construction helper for VclPtr. Since VclPtr types are created
120 * with a reference-count of one - to help fit into the existing
121 * code-flow; this helps us to construct them easily.
122 *
123 * For more details on the design please see vcl/README.lifecycle
124 *
125 * @tparam reference_type must be a subclass of vcl::Window
126 */
127 template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg)
128 {
129 return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE );
130 }
131
132 /** Probably most common used: handle->someBodyOp().
133 */
134 reference_type * operator->() const
135 {
136 return m_rInnerRef.get();
137 }
138
139 /** Get the body. Can be used instead of operator->().
140 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
141 are the same.
142 */
143 reference_type * get() const
144 {
145 return m_rInnerRef.get();
146 }
147
148 void set(reference_type *pBody)
149 {
150 m_rInnerRef.set(pBody);
151 }
152
153 void reset(reference_type *pBody)
154 {
155 m_rInnerRef.set(pBody);
156 }
157
158 /** Up-casting copy assignment operator.
159
160 Does not work for up-casts to ambiguous bases.
161
162 @param rRef another reference
163 */
164 template<typename derived_type>
165 typename std::enable_if<
166 std::is_base_of<reference_type, derived_type>::value,
167 VclPtr &>::type
168 operator =(VclPtr<derived_type> const & rRef)
169 {
170 m_rInnerRef.set(rRef.get());
171 return *this;
172 }
173
174 VclPtr & operator =(reference_type * pBody)
175 {
176 m_rInnerRef.set(pBody);
177 return *this;
178 }
179
180 operator reference_type * () const
181 {
182 return m_rInnerRef.get();
183 }
184
185 explicit operator bool () const
186 {
187 return m_rInnerRef.get() != nullptr;
22
Calling 'Reference::get'
188 }
189
190 void clear()
191 {
192 m_rInnerRef.clear();
193 }
194
195 void reset()
196 {
197 m_rInnerRef.clear();
198 }
199
200 void disposeAndClear()
201 {
202 // hold it alive for the lifetime of this method
203 ::rtl::Reference<reference_type> aTmp(m_rInnerRef);
204 m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-)
205 if (aTmp.get()) {
206 aTmp->disposeOnce();
207 }
208 }
209
210 /** Needed to place VclPtr's into STL collection.
211 */
212 bool operator< (const VclPtr<reference_type> & handle) const
213 {
214 return (m_rInnerRef < handle.m_rInnerRef);
215 }
216}; // class VclPtr
217
218template<typename T1, typename T2>
219inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
220 return p1.get() == p2.get();
221}
222
223template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2)
224{
225 return p1.get() == p2;
226}
227
228template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) {
229 return p1.get() == p2;
230}
231
232template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2)
233{
234 return p1 == p2.get();
235}
236
237template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) {
238 return p1 == p2.get();
239}
240
241template<typename T1, typename T2>
242inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
243 return !(p1 == p2);
244}
245
246template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2)
247{
248 return !(p1 == p2);
249}
250
251template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) {
252 return !(p1 == p2);
253}
254
255template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2)
256{
257 return !(p1 == p2);
258}
259
260template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) {
261 return !(p1 == p2);
262}
263
264/**
265 * A construction helper for a temporary VclPtr. Since VclPtr types
266 * are created with a reference-count of one - to help fit into
267 * the existing code-flow; this helps us to construct them easily.
268 * see also VclPtr::Create and ScopedVclPtr
269 *
270 * For more details on the design please see vcl/README.lifecycle
271 *
272 * @param reference_type must be a subclass of vcl::Window
273 */
274template <class reference_type>
275class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type>
276{
277public:
278 template<typename... Arg> VclPtrInstance(Arg &&... arg)
279 : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
280 {
281 }
282
283 /**
284 * Override and disallow this, to prevent people accidentally calling it and actually
285 * getting VclPtr::Create and getting a naked VclPtr<> instance
286 */
287 template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete;
288};
289
290template <class reference_type>
291class ScopedVclPtr : public VclPtr<reference_type>
292{
293public:
294 /** Constructor...
295 */
296 ScopedVclPtr()
297 : VclPtr<reference_type>()
298 {}
299
300 /** Constructor
301 */
302 ScopedVclPtr (reference_type * pBody)
303 : VclPtr<reference_type>(pBody)
304 {}
305
306 /** Copy constructor...
307 */
308 ScopedVclPtr (const VclPtr<reference_type> & handle)
309 : VclPtr<reference_type>(handle)
310 {}
311
312 /**
313 Assignment that releases the last reference.
314 */
315 void disposeAndReset(reference_type *pBody)
316 {
317 if (pBody != this->get()) {
318 VclPtr<reference_type>::disposeAndClear();
319 VclPtr<reference_type>::set(pBody);
320 }
321 }
322
323 /**
324 Assignment that releases the last reference.
325 */
326 ScopedVclPtr<reference_type>& operator = (reference_type * pBody)
327 {
328 disposeAndReset(pBody);
329 return *this;
330 }
331
332 /** Up-casting conversion constructor: Copies interface reference.
333
334 Does not work for up-casts to ambiguous bases. For the special case of
335 up-casting to Reference< XInterface >, see the corresponding conversion
336 operator.
337
338 @param rRef another reference
339 */
340 template< class derived_type >
341 ScopedVclPtr(
342 const VclPtr< derived_type > & rRef,
343 typename std::enable_if<
344 std::is_base_of<reference_type, derived_type>::value, int>::type
345 = 0 )
346 : VclPtr<reference_type>( rRef )
347 {
348 }
349
350 /** Up-casting assignment operator.
351
352 Does not work for up-casts to ambiguous bases.
353
354 @param rRef another VclPtr
355 */
356 template<typename derived_type>
357 typename std::enable_if<
358 std::is_base_of<reference_type, derived_type>::value,
359 ScopedVclPtr &>::type
360 operator =(VclPtr<derived_type> const & rRef)
361 {
362 disposeAndReset(rRef.get());
363 return *this;
364 }
365
366 /**
367 * Override and disallow this, to prevent people accidentally calling it and actually
368 * getting VclPtr::Create and getting a naked VclPtr<> instance
369 */
370 template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete;
371
372 ~ScopedVclPtr()
373 {
374 VclPtr<reference_type>::disposeAndClear();
375 assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get(
) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 375, __extension__ __PRETTY_FUNCTION__))
; // make sure there are no lingering references
376 }
377
378private:
379 // Most likely we don't want this default copy-constructor.
380 ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete;
381 // And certainly we don't want a default assignment operator.
382 ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete;
383 // And disallow reset as that doesn't call disposeAndClear on the original reference
384 void reset() = delete;
385 void reset(reference_type *pBody) = delete;
386
387protected:
388 ScopedVclPtr (reference_type * pBody, __sal_NoAcquire)
389 : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE)
390 {}
391};
392
393/**
394 * A construction helper for ScopedVclPtr. Since VclPtr types are created
395 * with a reference-count of one - to help fit into the existing
396 * code-flow; this helps us to construct them easily.
397 *
398 * For more details on the design please see vcl/README.lifecycle
399 *
400 * @param reference_type must be a subclass of vcl::Window
401 */
402#if defined _MSC_VER
403#pragma warning(push)
404#pragma warning(disable: 4521) // " multiple copy constructors specified"
405#endif
406template <class reference_type>
407class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type>
408{
409public:
410 template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg)
411 : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
412 {
413 }
414
415 /**
416 * Override and disallow this, to prevent people accidentally calling it and actually
417 * getting VclPtr::Create and getting a naked VclPtr<> instance
418 */
419 template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete;
420
421private:
422 // Prevent the above perfect forwarding ctor from hijacking (accidental)
423 // attempts at ScopedVclPtrInstance copy construction (where the hijacking
424 // would typically lead to somewhat obscure error messages); both non-const
425 // and const variants are needed here, as the ScopedVclPtr base class has a
426 // const--variant copy ctor, so the implicitly declared copy ctor for
427 // ScopedVclPtrInstance would also be the const variant, so non-const copy
428 // construction attempts would be hijacked by the perfect forwarding ctor;
429 // but if we only declared a non-const variant here, the const variant would
430 // no longer be implicitly declared (as there would already be an explicitly
431 // declared copy ctor), so const copy construction attempts would then be
432 // hijacked by the perfect forwarding ctor:
433 ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete;
434 ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete;
435};
436#if defined _MSC_VER
437#pragma warning(pop)
438#endif
439
440#endif // INCLUDED_VCL_PTR_HXX
441
442/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
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)
7
Assuming field 'm_pBody' is non-null
8
Taking true branch
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
12.1
Field 'm_pBody' is non-null
12.1
Field 'm_pBody' is non-null
12.1
Field 'm_pBody' is non-null
12.1
Field 'm_pBody' is non-null
)
13
Taking true branch
113 m_pBody->release();
14
Calling 'VclReferenceBase::release'
18
Returning; memory was released
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;
23
Use of memory after it is freed
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
277namespace 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*/
286template<typename T>
287struct 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: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
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)
15
Assuming the condition is true
16
Taking true branch
40 delete this;
17
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif