Mark some Calc slots as inactive in readonly mode
[LibreOffice.git] / include / tools / lazydelete.hxx
blobdb820ec75d6e40dc33de76132510c8b57f94852c
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 #pragma once
22 #include <tools/toolsdllapi.h>
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <rtl/ref.hxx>
27 #include <optional>
29 namespace tools
32 You may not access some objects after DeInitVCL has been called this includes their destruction
33 therefore disallowing the existence of static 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
41 ownership.
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.
47 SomeWindow::Paint()
49 static tools::DeleteOnDeinit< BitmapEx > aBmp( ... );
51 if( aBmp.get() ) // check whether DeInitVCL has been called already
52 DrawBitmapEx( Point( 10, 10 ), *aBmp );
56 class TOOLS_DLLPUBLIC DeleteOnDeinitBase
58 public:
59 static void ImplDeleteOnDeInit();
60 virtual ~DeleteOnDeinitBase();
61 protected:
62 static void addDeinitContainer( DeleteOnDeinitBase* i_pContainer );
64 virtual void doCleanup() = 0;
67 enum class DeleteOnDeinitFlag { Empty };
69 template < typename T >
70 class DeleteOnDeinit final : public DeleteOnDeinitBase
72 std::optional<T> m_pT;
73 virtual void doCleanup() override { m_pT.reset(); }
74 public:
75 template <class... Args >
76 DeleteOnDeinit(Args&&... args )
78 m_pT.emplace(args...);
79 addDeinitContainer( this );
81 DeleteOnDeinit(DeleteOnDeinitFlag)
83 addDeinitContainer( this );
86 // get contents
87 T* get() { return m_pT ? &*m_pT : nullptr; }
89 // set contents, returning old contents
90 // ownership is transferred !
91 template <class... Args >
92 std::optional<T> set(Args&&... args)
94 auto pOld = std::move(m_pT);
95 m_pT.emplace(args...);
96 return pOld;
100 /** Similar to DeleteOnDeinit, the DeleteUnoReferenceOnDeinit
101 template class makes sure that a static UNO object is disposed
102 and released at the right time.
104 Use like
105 static DeleteUnoReferenceOnDeinit<lang::XMultiServiceFactory>
106 xStaticFactory (\<create factory object>);
107 Reference<lang::XMultiServiceFactory> xFactory (xStaticFactory.get());
108 if (xFactory.is())
109 \<do something with xFactory>
111 template <typename I>
112 class DeleteUnoReferenceOnDeinit final : public tools::DeleteOnDeinitBase
114 css::uno::Reference<I> m_xI;
115 virtual void doCleanup() override { set(nullptr); }
116 public:
117 DeleteUnoReferenceOnDeinit(css::uno::Reference<I> _xI ) : m_xI(std::move( _xI )) {
118 addDeinitContainer( this ); }
120 css::uno::Reference<I> get() { return m_xI; }
122 void set (const css::uno::Reference<I>& r_xNew )
124 css::uno::Reference< css::lang::XComponent> xComponent (m_xI, css::uno::UNO_QUERY);
125 m_xI = r_xNew;
126 if (xComponent.is()) try
128 xComponent->dispose();
130 catch( css::uno::Exception& )
136 /** Similar to DeleteOnDeinit, the DeleteRtlReferenceOnDeinit
137 template class makes sure that a static rtl::Reference managed object is disposed
138 and released at the right time.
140 Use like
141 static DeleteUnoReferenceOnDeinit<Foo> xStaticFactory (new Foo);
142 rtl::Reference<Foo> xFactory (xStaticFactory.get());
143 if (xFactory.is())
144 \<do something with xFactory>
146 template <typename I>
147 class DeleteRtlReferenceOnDeinit final : public tools::DeleteOnDeinitBase
149 rtl::Reference<I> m_xI;
150 virtual void doCleanup() override { set(nullptr); }
151 public:
152 DeleteRtlReferenceOnDeinit(rtl::Reference<I> _xI ) : m_xI(std::move( _xI )) {
153 addDeinitContainer( this ); }
155 rtl::Reference<I> get() { return m_xI; }
157 void set (const rtl::Reference<I>& r_xNew )
159 m_xI = r_xNew;
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */