bump product version to 7.6.3.2-android
[LibreOffice.git] / include / vcl / lazydelete.hxx
blobe700582f12975873a09b09bd7dfc356afaa9800b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_VCL_LAZYDELETE_HXX
21 #define INCLUDED_VCL_LAZYDELETE_HXX
23 #include <vcl/dllapi.h>
25 #include <com/sun/star/lang/XComponent.hpp>
27 #include <optional>
28 #include <utility>
30 namespace vcl
33 You may not access vcl objects after DeInitVCL has been called this includes their destruction
34 therefore disallowing the existence of static vcl object like e.g. a static BitmapEx
35 To work around this use DeleteOnDeinit<BitmapEx> which will allow you to have a static object container,
36 that will have its contents destroyed on DeinitVCL. The single drawback is that you need to check on the
37 container object whether it still contains content before actually accessing it.
39 caveat: when constructing a vcl object, you certainly want to ensure that InitVCL has run already.
40 However this is not necessarily the case when using a class static member or a file level static variable.
41 In these cases make judicious use of the set() method of DeleteOnDeinit, but beware of the changing
42 ownership.
44 example use case: use a lazy initialized on call BitmapEx in a paint method. Of course a paint method
45 would not normally be called after DeInitVCL anyway, so the check might not be necessary in a
46 Window::Paint implementation, but always checking is a good idea.
48 SomeWindow::Paint()
50 static vcl::DeleteOnDeinit< BitmapEx > aBmp( ... );
52 if( aBmp.get() ) // check whether DeInitVCL has been called already
53 DrawBitmapEx( Point( 10, 10 ), *aBmp );
57 class VCL_DLLPUBLIC DeleteOnDeinitBase
59 public:
60 static void SAL_DLLPRIVATE ImplDeleteOnDeInit();
61 virtual ~DeleteOnDeinitBase();
62 protected:
63 static void addDeinitContainer( DeleteOnDeinitBase* i_pContainer );
65 virtual void doCleanup() = 0;
68 enum class DeleteOnDeinitFlag { Empty };
70 template < typename T >
71 class DeleteOnDeinit final : public DeleteOnDeinitBase
73 std::optional<T> m_pT;
74 virtual void doCleanup() override { m_pT.reset(); }
75 public:
76 template <class... Args >
77 DeleteOnDeinit(Args&&... args )
79 m_pT.emplace(args...);
80 addDeinitContainer( this );
82 DeleteOnDeinit(DeleteOnDeinitFlag)
84 addDeinitContainer( this );
87 // get contents
88 T* get() { return m_pT ? &*m_pT : nullptr; }
90 // set contents, returning old contents
91 // ownership is transferred !
92 template <class... Args >
93 std::optional<T> set(Args&&... args)
95 auto pOld = std::move(m_pT);
96 m_pT.emplace(args...);
97 return pOld;
101 /** Similar to DeleteOnDeinit, the DeleteUnoReferenceOnDeinit
102 template class makes sure that a static UNO object is disposed
103 and released at the right time.
105 Use like
106 static DeleteUnoReferenceOnDeinit<lang::XMultiServiceFactory>
107 xStaticFactory (\<create factory object>);
108 Reference<lang::XMultiServiceFactory> xFactory (xStaticFactory.get());
109 if (xFactory.is())
110 \<do something with xFactory>
112 template <typename I>
113 class DeleteUnoReferenceOnDeinit final : public vcl::DeleteOnDeinitBase
115 css::uno::Reference<I> m_xI;
116 virtual void doCleanup() override { set(nullptr); }
117 public:
118 DeleteUnoReferenceOnDeinit(css::uno::Reference<I> _xI ) : m_xI(std::move( _xI )) {
119 addDeinitContainer( this ); }
121 css::uno::Reference<I> get() { return m_xI; }
123 void set (const css::uno::Reference<I>& r_xNew )
125 css::uno::Reference< css::lang::XComponent> xComponent (m_xI, css::uno::UNO_QUERY);
126 m_xI = r_xNew;
127 if (xComponent.is()) try
129 xComponent->dispose();
131 catch( css::uno::Exception& )
138 #endif
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */