summaryrefslogtreecommitdiff
path: root/vcl/inc/vcl/lazydelete.hxx
diff options
context:
space:
mode:
Diffstat (limited to 'vcl/inc/vcl/lazydelete.hxx')
-rw-r--r--vcl/inc/vcl/lazydelete.hxx50
1 files changed, 25 insertions, 25 deletions
diff --git a/vcl/inc/vcl/lazydelete.hxx b/vcl/inc/vcl/lazydelete.hxx
index 46804f3b54cb..713e22ef022a 100644
--- a/vcl/inc/vcl/lazydelete.hxx
+++ b/vcl/inc/vcl/lazydelete.hxx
@@ -2,7 +2,7 @@
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
+ *
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
@@ -43,23 +43,23 @@
namespace vcl
{
/* Helpers for lazy object deletion
-
+
With vcl it is often necessary to delete objects (especially Windows)
in the right order as well as in a way ensuring that the deleted objects
are not still on the stack (e.g. deleting a Window in its key handler). To
make this easier a helper class is given here which takes care of both
sorting as well as lazy deletion.
-
+
The grisly details:
LazyDelete is a class that LazyDeletor register to. When vcl's event
loop (that is Application::Yield or Application::Reschedule) comes out
of the last level, the LazyDelete::flush is called. This will cause
LazyDelete to delete all registered LazyDeletor objects.
-
+
LazyDeletor<T> is a one instance object that contains a list of
<T> objects to be deleted in sorted order. It is derived from
LazyDeletorBase as to be able to register itself in LazyDelete.
-
+
The user calls the static method LazyDeletor<T>::Delete( T* ) with the
object to be destroyed lazy. The static method creates the LazyDeletor<T>
(which in turn registers itself in LazyDelete) if this is the first time
@@ -68,19 +68,19 @@ namespace vcl
that will ensure the correct order of deletion via the specialized is_less method
(e.g. if a Window is a child of another Window and therefore should be destroyed
first it is "less" in this sense)
-
+
LazyDelete::flush will be called when the top of the nested event loop is
reached again and will then destroy each registered LazyDeletor<T> which
in turn destroys the objects needed to be destroyed lazily. After this
the state is as before entering the event loop.
-
+
Preconditions:
- The class <T> of which objects are to be destroyed needs a virtual
destructor or must be final, else the wrong type will be destroyed.
- The destructor of <T> should call LazyDeletor<T>::Undelete( this ). This
prevents duplicate deletionin case someone destroys the object prematurely.
*/
-
+
class LazyDeletorBase;
class VCL_DLLPUBLIC LazyDelete
{
@@ -105,27 +105,27 @@ namespace vcl
class VCL_DLLPUBLIC LazyDeletor : public LazyDeletorBase
{
static LazyDeletor< T >* s_pOneInstance;
-
+
struct DeleteObjectEntry
{
T* m_pObject;
bool m_bDeleted;
-
+
DeleteObjectEntry() :
m_pObject( NULL ),
m_bDeleted( false )
{}
-
+
DeleteObjectEntry( T* i_pObject ) :
m_pObject( i_pObject ),
m_bDeleted( false )
{}
};
-
+
std::vector< DeleteObjectEntry > m_aObjects;
typedef std::hash_map< sal_IntPtr, unsigned int > PtrToIndexMap;
PtrToIndexMap m_aPtrToIndex;
-
+
/** strict weak ordering funtion to bring objects to be destroyed lazily
in correct order, e.g. for Window objects children before parents
*/
@@ -140,7 +140,7 @@ namespace vcl
#endif
if( s_pOneInstance == this ) // sanity check
s_pOneInstance = NULL;
-
+
// do the actual work
unsigned int nCount = m_aObjects.size();
std::vector<T*> aRealDelete;
@@ -169,7 +169,7 @@ namespace vcl
delete aRealDelete[n];
}
}
-
+
public:
/** mark an object for lazy deletion
*/
@@ -203,7 +203,7 @@ namespace vcl
}
}
};
-
+
/*
class DeleteOnDeinit matches a similar need as LazyDelete for static objects:
you may not access vcl objects after DeInitVCL has been called this includes their destruction
@@ -211,36 +211,36 @@ namespace vcl
To work around this use DeleteOnDeinit<BitmapEx> which will allow you to have a static object container,
that will have its contents destroyed on DeinitVCL. The single drawback is that you need to check on the
container object whether it still contains content before actually accessing it.
-
+
caveat: when constructing a vcl object, you certainly want to ensure that InitVCL has run already.
However this is not necessarily the case when using a class static member or a file level static variable.
In these cases make judicious use of the set() method of DeleteOnDeinit, but beware of the changing
ownership.
-
+
example use case: use a lazy initialized on call BitmapEx in a paint method. Of course a paint method
would not normally be called after DeInitVCL anyway, so the check might not be necessary in a
Window::Paint implementation, but always checking is a good idea.
-
+
SomeWindow::Paint()
{
static vcl::DeleteOnDeinit< BitmapEx > aBmp( new BitmapEx( ResId( 1000, myResMgr ) ) );
-
+
if( aBmp.get() ) // check whether DeInitVCL has been called already
DrawBitmapEx( Point( 10, 10 ), *aBmp.get() );
}
*/
-
+
class VCL_DLLPUBLIC DeleteOnDeinitBase
{
public:
static void SAL_DLLPRIVATE ImplDeleteOnDeInit();
virtual ~DeleteOnDeinitBase();
protected:
- static void addDeinitContainer( DeleteOnDeinitBase* i_pContainer );
+ static void addDeinitContainer( DeleteOnDeinitBase* i_pContainer );
virtual void doCleanup() = 0;
};
-
+
template < typename T >
class DeleteOnDeinit : public DeleteOnDeinitBase
{
@@ -249,10 +249,10 @@ namespace vcl
public:
DeleteOnDeinit( T* i_pT ) : m_pT( i_pT ) { addDeinitContainer( this ); }
virtual ~DeleteOnDeinit() {}
-
+
// get contents
T* get() { return m_pT; }
-
+
// set contents, returning old contents
// ownership is transfered !
T* set( T* i_pNew ) { T* pOld = m_pT; m_pT = i_pNew; return pOld; }