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