1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_VCL_LAZYDELETE_HXX
21 #define INCLUDED_VCL_LAZYDELETE_HXX
23 #include <vcl/dllapi.h>
25 #include <com/sun/star/lang/XComponent.hpp>
32 You may not access vcl objects after DeInitVCL has been called this includes their destruction
33 therefore disallowing the existence of static vcl object like e.g. a static BitmapEx
34 To work around this use DeleteOnDeinit<BitmapEx> which will allow you to have a static object container,
35 that will have its contents destroyed on DeinitVCL. The single drawback is that you need to check on the
36 container object whether it still contains content before actually accessing it.
38 caveat: when constructing a vcl object, you certainly want to ensure that InitVCL has run already.
39 However this is not necessarily the case when using a class static member or a file level static variable.
40 In these cases make judicious use of the set() method of DeleteOnDeinit, but beware of the changing
43 example use case: use a lazy initialized on call BitmapEx in a paint method. Of course a paint method
44 would not normally be called after DeInitVCL anyway, so the check might not be necessary in a
45 Window::Paint implementation, but always checking is a good idea.
49 static vcl::DeleteOnDeinit< BitmapEx > aBmp( new BitmapEx( ... ) );
51 if( aBmp.get() ) // check whether DeInitVCL has been called already
52 DrawBitmapEx( Point( 10, 10 ), *aBmp );
56 class VCL_DLLPUBLIC DeleteOnDeinitBase
59 static void SAL_DLLPRIVATE
ImplDeleteOnDeInit();
60 virtual ~DeleteOnDeinitBase();
62 static void addDeinitContainer( DeleteOnDeinitBase
* i_pContainer
);
64 virtual void doCleanup() = 0;
67 template < typename T
>
68 class DeleteOnDeinit final
: public DeleteOnDeinitBase
70 std::unique_ptr
<T
> m_pT
;
71 virtual void doCleanup() override
{ m_pT
.reset(); }
73 DeleteOnDeinit( T
* i_pT
) : m_pT( i_pT
) { addDeinitContainer( this ); }
76 T
* get() { return m_pT
.get(); }
78 // set contents, returning old contents
79 // ownership is transferred !
80 std::unique_ptr
<T
> set( std::unique_ptr
<T
> i_pNew
) { auto pOld
= std::move(m_pT
); m_pT
= std::move(i_pNew
); return pOld
; }
83 /** Similar to DeleteOnDeinit, the DeleteUnoReferenceOnDeinit
84 template class makes sure that a static UNO object is disposed
85 and released at the right time.
88 static DeleteUnoReferenceOnDeinit<lang::XMultiServiceFactory>
89 xStaticFactory (\<create factory object>);
90 Reference<lang::XMultiServiceFactory> xFactory (xStaticFactory.get());
92 \<do something with xFactory>
95 class DeleteUnoReferenceOnDeinit final
: public vcl::DeleteOnDeinitBase
97 css::uno::Reference
<I
> m_xI
;
98 virtual void doCleanup() override
{ set(nullptr); }
100 DeleteUnoReferenceOnDeinit(const css::uno::Reference
<I
>& r_xI
) : m_xI( r_xI
) {
101 addDeinitContainer( this ); }
103 css::uno::Reference
<I
> get() { return m_xI
; }
105 void set (const css::uno::Reference
<I
>& r_xNew
)
107 css::uno::Reference
< css::lang::XComponent
> xComponent (m_xI
, css::uno::UNO_QUERY
);
109 if (xComponent
.is()) try
111 xComponent
->dispose();
113 catch( css::uno::Exception
& )
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */