LibreOffice Module vcl (master) 1
lazydelete.hxx
Go to the documentation of this file.
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_LAZYDELETE_HXX
21#define INCLUDED_VCL_LAZYDELETE_HXX
22
23#include <vcl/dllapi.h>
24
25#include <com/sun/star/lang/XComponent.hpp>
26
27#include <optional>
28#include <utility>
29
30namespace vcl
31{
32 /*
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.
38
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.
43
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.
47
48 SomeWindow::Paint()
49 {
50 static vcl::DeleteOnDeinit< BitmapEx > aBmp( ... );
51
52 if( aBmp.get() ) // check whether DeInitVCL has been called already
53 DrawBitmapEx( Point( 10, 10 ), *aBmp );
54 }
55 */
56
58 {
59 public:
60 static void SAL_DLLPRIVATE ImplDeleteOnDeInit();
61 virtual ~DeleteOnDeinitBase();
62 protected:
63 static void addDeinitContainer( DeleteOnDeinitBase* i_pContainer );
64
65 virtual void doCleanup() = 0;
66 };
67
68 enum class DeleteOnDeinitFlag { Empty };
69
70 template < typename T >
72 {
73 std::optional<T> m_pT;
74 virtual void doCleanup() override { m_pT.reset(); }
75 public:
76 template <class... Args >
77 DeleteOnDeinit(Args&&... args )
78 {
79 m_pT.emplace(args...);
80 addDeinitContainer( this );
81 }
83 {
84 addDeinitContainer( this );
85 }
86
87 // get contents
88 T* get() { return m_pT ? &*m_pT : nullptr; }
89
90 // set contents, returning old contents
91 // ownership is transferred !
92 template <class... Args >
93 std::optional<T> set(Args&&... args)
94 {
95 auto pOld = std::move(m_pT);
96 m_pT.emplace(args...);
97 return pOld;
98 }
99 };
100
112 template <typename I>
114 {
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 ); }
120
121 css::uno::Reference<I> get() { return m_xI; }
122
123 void set (const css::uno::Reference<I>& r_xNew )
124 {
125 css::uno::Reference< css::lang::XComponent> xComponent (m_xI, css::uno::UNO_QUERY);
126 m_xI = r_xNew;
127 if (xComponent.is()) try
128 {
129 xComponent->dispose();
130 }
131 catch( css::uno::Exception& )
132 {
133 }
134 }
135 };
136}
137
138#endif
139/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void addDeinitContainer(DeleteOnDeinitBase *i_pContainer)
Definition: lazydelete.cxx:35
virtual void doCleanup()=0
DeleteOnDeinit(Args &&... args)
Definition: lazydelete.hxx:77
std::optional< T > set(Args &&... args)
Definition: lazydelete.hxx:93
std::optional< T > m_pT
Definition: lazydelete.hxx:73
DeleteOnDeinit(DeleteOnDeinitFlag)
Definition: lazydelete.hxx:82
virtual void doCleanup() override
Definition: lazydelete.hxx:74
Similar to DeleteOnDeinit, the DeleteUnoReferenceOnDeinit template class makes sure that a static UNO...
Definition: lazydelete.hxx:114
void set(const css::uno::Reference< I > &r_xNew)
Definition: lazydelete.hxx:123
DeleteUnoReferenceOnDeinit(css::uno::Reference< I > _xI)
Definition: lazydelete.hxx:118
virtual void doCleanup() override
Definition: lazydelete.hxx:116
css::uno::Reference< I > m_xI
Definition: lazydelete.hxx:115
css::uno::Reference< I > get()
Definition: lazydelete.hxx:121
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
args
Empty
DeleteOnDeinitFlag
Definition: lazydelete.hxx:68