xref: /aoo41x/main/vcl/source/helper/lazydelete.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 // MARKER(update_precomp.py): autogen include statement, do not remove
28 #include "precompiled_vcl.hxx"
29 
30 #ifndef LAZYDELETE_CXX
31 #define LAZYDELETE_CXX
32 
33 #include "vcl/window.hxx"
34 #include "vcl/menu.hxx"
35 #include "vcl/lazydelete.hxx"
36 #include "svdata.hxx"
37 
38 namespace vcl {
39 
40 LazyDeletorBase::LazyDeletorBase()
41 {
42 }
43 
44 LazyDeletorBase::~LazyDeletorBase()
45 {
46 }
47 
48 // instantiate instance pointers for LazyDeletor<Window,Menu>
49 template<> LazyDeletor<Window>* LazyDeletor<Window>::s_pOneInstance = NULL;
50 template<> LazyDeletor<Menu>* LazyDeletor<Menu>::s_pOneInstance = NULL;
51 
52 // a list for all LazyeDeletor<T> singletons
53 static std::vector< LazyDeletorBase* > lcl_aDeletors;
54 
55 void LazyDelete::addDeletor( LazyDeletorBase* i_pDel )
56 {
57     lcl_aDeletors.push_back( i_pDel );
58 }
59 
60 void LazyDelete::flush()
61 {
62     unsigned int nCount = lcl_aDeletors.size();
63     for( unsigned int i = 0; i < nCount; i++ )
64         delete lcl_aDeletors[i];
65     lcl_aDeletors.clear();
66 }
67 
68 // specialized is_less function for Window
69 template<> bool LazyDeletor<Window>::is_less( Window* left, Window* right )
70 {
71     return (left != right && right->IsChild( left, sal_True )) ? true : false;
72 }
73 
74 // specialized is_less function for Menu
75 template<> bool LazyDeletor<Menu>::is_less( Menu* left, Menu* right )
76 {
77     while( left && left != right )
78         left = left->ImplGetStartedFrom();
79     return left != NULL;
80 }
81 
82 DeleteOnDeinitBase::~DeleteOnDeinitBase()
83 {
84     ImplSVData* pSVData = ImplGetSVData();
85     if( pSVData && pSVData->mpDeinitDeleteList != NULL )
86         pSVData->mpDeinitDeleteList->remove( this );
87 }
88 
89 void DeleteOnDeinitBase::addDeinitContainer( DeleteOnDeinitBase* i_pContainer )
90 {
91     ImplSVData* pSVData = ImplGetSVData();
92     if( ! pSVData )
93     {
94         ImplInitSVData();
95         pSVData = ImplGetSVData();
96     }
97 
98     DBG_ASSERT( ! pSVData->mbDeInit, "DeleteOnDeinit added after DeiInitVCL !" );
99     if( pSVData->mbDeInit )
100         return;
101 
102     if( pSVData->mpDeinitDeleteList == NULL )
103         pSVData->mpDeinitDeleteList = new std::list< DeleteOnDeinitBase* >();
104     pSVData->mpDeinitDeleteList->push_back( i_pContainer );
105 }
106 
107 void DeleteOnDeinitBase::ImplDeleteOnDeInit()
108 {
109     ImplSVData* pSVData = ImplGetSVData();
110     if( pSVData->mpDeinitDeleteList )
111     {
112         for( std::list< vcl::DeleteOnDeinitBase* >::iterator it = pSVData->mpDeinitDeleteList->begin();
113              it != pSVData->mpDeinitDeleteList->end(); ++it )
114         {
115             (*it)->doCleanup();
116         }
117         delete pSVData->mpDeinitDeleteList;
118         pSVData->mpDeinitDeleteList = NULL;
119     }
120 }
121 
122 } // namespace vcl
123 
124 #endif
125 
126