1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 _VCL_LAZYDELETE_HXX
21 #define _VCL_LAZYDELETE_HXX
26 #include <boost/unordered_map.hpp>
29 #if OSL_DEBUG_LEVEL > 2
34 #include <com/sun/star/lang/XComponent.hpp>
38 /* Helpers for lazy object deletion
40 With vcl it is often necessary to delete objects (especially Windows)
41 in the right order as well as in a way ensuring that the deleted objects
42 are not still on the stack (e.g. deleting a Window in its key handler). To
43 make this easier a helper class is given here which takes care of both
44 sorting as well as lazy deletion.
47 LazyDelete is a class that LazyDeletor register to. When vcl's event
48 loop (that is Application::Yield or Application::Reschedule) comes out
49 of the last level, the LazyDelete::flush is called. This will cause
50 LazyDelete to delete all registered LazyDeletor objects.
52 LazyDeletor<T> is a one instance object that contains a list of
53 <T> objects to be deleted in sorted order. It is derived from
54 LazyDeletorBase as to be able to register itself in LazyDelete.
56 The user calls the static method LazyDeletor<T>::Delete( T* ) with the
57 object to be destroyed lazy. The static method creates the LazyDeletor<T>
58 (which in turn registers itself in LazyDelete) if this is the first time
59 a T* is to be destroyed lazy. It then inserts the object. When the LazyDeletor<T>
60 gets delte it will delete the stored objects in a fashion
61 that will ensure the correct order of deletion via the specialized is_less method
62 (e.g. if a Window is a child of another Window and therefore should be destroyed
63 first it is "less" in this sense)
65 LazyDelete::flush will be called when the top of the nested event loop is
66 reached again and will then destroy each registered LazyDeletor<T> which
67 in turn destroys the objects needed to be destroyed lazily. After this
68 the state is as before entering the event loop.
71 - The class <T> of which objects are to be destroyed needs a virtual
72 destructor or must be final, else the wrong type will be destroyed.
73 - The destructor of <T> should call LazyDeletor<T>::Undelete( this ). This
74 prevents duplicate deletionin case someone destroys the object prematurely.
77 class LazyDeletorBase
;
78 class VCL_DLLPUBLIC LazyDelete
81 /** flush all registered object lists
84 /** register an object list to be destroyed
86 static void addDeletor( LazyDeletorBase
* pDeletor
);
89 class VCL_DLLPUBLIC LazyDeletorBase
91 friend void LazyDelete::flush();
94 virtual ~LazyDeletorBase();
97 template < typename T
>
98 class VCL_DLLPUBLIC LazyDeletor
: public LazyDeletorBase
100 static LazyDeletor
< T
>* s_pOneInstance
;
102 struct DeleteObjectEntry
107 DeleteObjectEntry() :
112 DeleteObjectEntry( T
* i_pObject
) :
113 m_pObject( i_pObject
),
118 std::vector
< DeleteObjectEntry
> m_aObjects
;
119 typedef boost::unordered_map
< sal_IntPtr
, unsigned int > PtrToIndexMap
;
120 PtrToIndexMap m_aPtrToIndex
;
122 /** strict weak ordering funtion to bring objects to be destroyed lazily
123 in correct order, e.g. for Window objects children before parents
125 static bool is_less( T
* left
, T
* right
);
127 LazyDeletor() { LazyDelete::addDeletor( this ); }
128 virtual ~LazyDeletor()
130 #if OSL_DEBUG_LEVEL > 2
131 fprintf( stderr
, "%s %p deleted\n",
132 typeid(*this).name(), this );
134 if( s_pOneInstance
== this ) // sanity check
135 s_pOneInstance
= NULL
;
137 // do the actual work
138 unsigned int nCount
= m_aObjects
.size();
139 std::vector
<T
*> aRealDelete
;
140 aRealDelete
.reserve( nCount
);
141 for( unsigned int i
= 0; i
< nCount
; i
++ )
143 if( ! m_aObjects
[i
].m_bDeleted
)
145 aRealDelete
.push_back( m_aObjects
[i
].m_pObject
);
148 // sort the vector of objects to be destroyed
149 std::sort( aRealDelete
.begin(), aRealDelete
.end(), is_less
);
150 nCount
= aRealDelete
.size();
151 for( unsigned int n
= 0; n
< nCount
; n
++ )
153 #if OSL_DEBUG_LEVEL > 2
154 fprintf( stderr
, "%s deletes object %p of type %s\n",
155 typeid(*this).name(),
157 typeid(*aRealDelete
[n
]).name() );
159 // check if the object to be deleted is not already destroyed
160 // as a side effect of a previous lazily destroyed object
161 if( ! m_aObjects
[ m_aPtrToIndex
[ reinterpret_cast<sal_IntPtr
>(aRealDelete
[n
]) ] ].m_bDeleted
)
162 delete aRealDelete
[n
];
167 /** mark an object for lazy deletion
169 static void Delete( T
* i_pObject
)
171 if( s_pOneInstance
== NULL
)
172 s_pOneInstance
= new LazyDeletor
<T
>();
174 // is this object already in the list ?
175 // if so mark it as not to be deleted; else insert it
176 PtrToIndexMap::const_iterator dup
= s_pOneInstance
->m_aPtrToIndex
.find( reinterpret_cast<sal_IntPtr
>(i_pObject
) );
177 if( dup
!= s_pOneInstance
->m_aPtrToIndex
.end() )
179 s_pOneInstance
->m_aObjects
[ dup
->second
].m_bDeleted
= false;
183 s_pOneInstance
->m_aPtrToIndex
[ reinterpret_cast<sal_IntPtr
>(i_pObject
) ] = s_pOneInstance
->m_aObjects
.size();
184 s_pOneInstance
->m_aObjects
.push_back( DeleteObjectEntry( i_pObject
) );
187 /** unmark an object already marked for lazy deletion
189 static void Undelete( T
* i_pObject
)
193 PtrToIndexMap::const_iterator it
= s_pOneInstance
->m_aPtrToIndex
.find( reinterpret_cast<sal_IntPtr
>(i_pObject
) );
194 if( it
!= s_pOneInstance
->m_aPtrToIndex
.end() )
195 s_pOneInstance
->m_aObjects
[ it
->second
].m_bDeleted
= true;
201 class DeleteOnDeinit matches a similar need as LazyDelete for static objects:
202 you may not access vcl objects after DeInitVCL has been called this includes their destruction
203 therefore disallowing the existance of static vcl object like e.g. a static BitmapEx
204 To work around this use DeleteOnDeinit<BitmapEx> which will allow you to have a static object container,
205 that will have its contents destroyed on DeinitVCL. The single drawback is that you need to check on the
206 container object whether it still contains content before actually accessing it.
208 caveat: when constructing a vcl object, you certainly want to ensure that InitVCL has run already.
209 However this is not necessarily the case when using a class static member or a file level static variable.
210 In these cases make judicious use of the set() method of DeleteOnDeinit, but beware of the changing
213 example use case: use a lazy initialized on call BitmapEx in a paint method. Of course a paint method
214 would not normally be called after DeInitVCL anyway, so the check might not be necessary in a
215 Window::Paint implementation, but always checking is a good idea.
219 static vcl::DeleteOnDeinit< BitmapEx > aBmp( new BitmapEx( ResId( 1000, myResMgr ) ) );
221 if( aBmp.get() ) // check whether DeInitVCL has been called already
222 DrawBitmapEx( Point( 10, 10 ), *aBmp.get() );
226 class VCL_DLLPUBLIC DeleteOnDeinitBase
229 static void SAL_DLLPRIVATE
ImplDeleteOnDeInit();
230 virtual ~DeleteOnDeinitBase();
232 static void addDeinitContainer( DeleteOnDeinitBase
* i_pContainer
);
234 virtual void doCleanup() = 0;
237 template < typename T
>
238 class DeleteOnDeinit
: public DeleteOnDeinitBase
241 virtual void doCleanup() { delete m_pT
; m_pT
= NULL
; }
243 DeleteOnDeinit( T
* i_pT
) : m_pT( i_pT
) { addDeinitContainer( this ); }
244 virtual ~DeleteOnDeinit() {}
247 T
* get() { return m_pT
; }
249 // set contents, returning old contents
250 // ownership is transferred !
251 T
* set( T
* i_pNew
) { T
* pOld
= m_pT
; m_pT
= i_pNew
; return pOld
; }
253 // set contents, deleting old contents
254 // ownership is transferred !
255 void reset( T
* i_pNew
= NULL
)
256 { OSL_ASSERT( i_pNew
!= m_pT
|| i_pNew
== NULL
); T
* pOld
= m_pT
; m_pT
= i_pNew
; delete pOld
; }
259 /** Similar to DeleteOnDeinit, the DeleteUnoReferenceOnDeinit
260 template class makes sure that a static UNO object is disposed
261 and released at the right time.
264 static DeleteUnoReferenceOnDeinit<lang::XMultiServiceFactory>
265 xStaticFactory (<create factory object>);
266 Reference<lang::XMultiServiceFactory> xFactory (xStaticFactory.get());
268 <do something with xFactory>
270 template <typename I
>
271 class DeleteUnoReferenceOnDeinit
: public ::vcl::DeleteOnDeinitBase
273 ::com::sun::star::uno::Reference
<I
> m_xI
;
274 virtual void doCleanup() { set(NULL
); }
276 DeleteUnoReferenceOnDeinit(const ::com::sun::star::uno::Reference
<I
>& r_xI
) : m_xI( r_xI
) {
277 addDeinitContainer( this ); }
278 virtual ~DeleteUnoReferenceOnDeinit() {}
280 ::com::sun::star::uno::Reference
<I
> get (void) { return m_xI
; }
282 void set (const ::com::sun::star::uno::Reference
<I
>& r_xNew
)
284 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> xComponent (m_xI
, ::com::sun::star::uno::UNO_QUERY
);
286 if (xComponent
.is()) try
288 xComponent
->dispose();
290 catch( ::com::sun::star::uno::Exception
& )
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */