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 <memory>
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  template < typename T >
68  class DeleteOnDeinit final : public DeleteOnDeinitBase
69  {
70  std::unique_ptr<T> m_pT;
71  virtual void doCleanup() override { m_pT.reset(); }
72  public:
73  DeleteOnDeinit( T* i_pT ) : m_pT( i_pT ) { addDeinitContainer( this ); }
74 
75  // get contents
76  T* get() { return m_pT.get(); }
77 
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; }
81  };
82 
94  template <typename I>
96  {
97  css::uno::Reference<I> m_xI;
98  virtual void doCleanup() override { set(nullptr); }
99  public:
100  DeleteUnoReferenceOnDeinit(const css::uno::Reference<I>& r_xI ) : m_xI( r_xI ) {
101  addDeinitContainer( this ); }
102 
103  css::uno::Reference<I> get() { return m_xI; }
104 
105  void set (const css::uno::Reference<I>& r_xNew )
106  {
107  css::uno::Reference< css::lang::XComponent> xComponent (m_xI, css::uno::UNO_QUERY);
108  m_xI = r_xNew;
109  if (xComponent.is()) try
110  {
111  xComponent->dispose();
112  }
113  catch( css::uno::Exception& )
114  {
115  }
116  }
117  };
118 }
119 
120 #endif
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< I > m_xI
Definition: lazydelete.hxx:97
Similar to DeleteOnDeinit, the DeleteUnoReferenceOnDeinit template class makes sure that a static UNO...
Definition: lazydelete.hxx:95
DeleteOnDeinit(T *i_pT)
Definition: lazydelete.hxx:73
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
void set(const css::uno::Reference< I > &r_xNew)
Definition: lazydelete.hxx:105
virtual void doCleanup() override
Definition: lazydelete.hxx:71
css::uno::Reference< css::lang::XComponent > xComponent
std::unique_ptr< T > set(std::unique_ptr< T > i_pNew)
Definition: lazydelete.hxx:80
std::unique_ptr< T > m_pT
Definition: lazydelete.hxx:70
virtual void doCleanup() override
Definition: lazydelete.hxx:98
static void addDeinitContainer(DeleteOnDeinitBase *i_pContainer)
Definition: lazydelete.cxx:35
DeleteUnoReferenceOnDeinit(const css::uno::Reference< I > &r_xI)
Definition: lazydelete.hxx:100