xref: /aoo42x/main/vcl/source/helper/lazydelete.cxx (revision 9f62ea84)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 // MARKER(update_precomp.py): autogen include statement, do not remove
24 #include "precompiled_vcl.hxx"
25 
26 #ifndef LAZYDELETE_CXX
27 #define LAZYDELETE_CXX
28 
29 #include "vcl/window.hxx"
30 #include "vcl/menu.hxx"
31 #include "vcl/lazydelete.hxx"
32 #include "svdata.hxx"
33 
34 namespace vcl {
35 
LazyDeletorBase()36 LazyDeletorBase::LazyDeletorBase()
37 {
38 }
39 
~LazyDeletorBase()40 LazyDeletorBase::~LazyDeletorBase()
41 {
42 }
43 
44 // instantiate instance pointers for LazyDeletor<Window,Menu>
45 template<> LazyDeletor<Window>* LazyDeletor<Window>::s_pOneInstance = NULL;
46 template<> LazyDeletor<Menu>* LazyDeletor<Menu>::s_pOneInstance = NULL;
47 
48 // a list for all LazyeDeletor<T> singletons
49 static std::vector< LazyDeletorBase* > lcl_aDeletors;
50 
addDeletor(LazyDeletorBase * i_pDel)51 void LazyDelete::addDeletor( LazyDeletorBase* i_pDel )
52 {
53     lcl_aDeletors.push_back( i_pDel );
54 }
55 
flush()56 void LazyDelete::flush()
57 {
58     unsigned int nCount = lcl_aDeletors.size();
59     for( unsigned int i = 0; i < nCount; i++ )
60         delete lcl_aDeletors[i];
61     lcl_aDeletors.clear();
62 }
63 
64 // specialized is_less function for Window
is_less(Window * left,Window * right)65 template<> bool LazyDeletor<Window>::is_less( Window* left, Window* right )
66 {
67     return (left != right && right->IsChild( left, sal_True )) ? true : false;
68 }
69 
70 // specialized is_less function for Menu
is_less(Menu * left,Menu * right)71 template<> bool LazyDeletor<Menu>::is_less( Menu* left, Menu* right )
72 {
73     while( left && left != right )
74         left = left->ImplGetStartedFrom();
75     return left != NULL;
76 }
77 
~DeleteOnDeinitBase()78 DeleteOnDeinitBase::~DeleteOnDeinitBase()
79 {
80     ImplSVData* pSVData = ImplGetSVData();
81     if( pSVData && pSVData->mpDeinitDeleteList != NULL )
82         pSVData->mpDeinitDeleteList->remove( this );
83 }
84 
addDeinitContainer(DeleteOnDeinitBase * i_pContainer)85 void DeleteOnDeinitBase::addDeinitContainer( DeleteOnDeinitBase* i_pContainer )
86 {
87     ImplSVData* pSVData = ImplGetSVData();
88     if( ! pSVData )
89     {
90         ImplInitSVData();
91         pSVData = ImplGetSVData();
92     }
93 
94     DBG_ASSERT( ! pSVData->mbDeInit, "DeleteOnDeinit added after DeiInitVCL !" );
95     if( pSVData->mbDeInit )
96         return;
97 
98     if( pSVData->mpDeinitDeleteList == NULL )
99         pSVData->mpDeinitDeleteList = new std::list< DeleteOnDeinitBase* >();
100     pSVData->mpDeinitDeleteList->push_back( i_pContainer );
101 }
102 
ImplDeleteOnDeInit()103 void DeleteOnDeinitBase::ImplDeleteOnDeInit()
104 {
105     ImplSVData* pSVData = ImplGetSVData();
106     if( pSVData->mpDeinitDeleteList )
107     {
108         for( std::list< vcl::DeleteOnDeinitBase* >::iterator it = pSVData->mpDeinitDeleteList->begin();
109              it != pSVData->mpDeinitDeleteList->end(); ++it )
110         {
111             (*it)->doCleanup();
112         }
113         delete pSVData->mpDeinitDeleteList;
114         pSVData->mpDeinitDeleteList = NULL;
115     }
116 }
117 
118 } // namespace vcl
119 
120 #endif
121 
122