Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / adiasync.cxx
blob3232b0e6fb0ab1e7e675ff9ed35160ebb9f92177
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 #include <sfx2/objsh.hxx>
22 #include "adiasync.hxx"
23 #include "brdcst.hxx"
24 #include "global.hxx"
25 #include "document.hxx"
26 #include "sc.hrc"
27 #include <osl/thread.h>
29 ScAddInAsyncs theAddInAsyncTbl;
30 static ScAddInAsync aSeekObj;
32 extern "C" {
33 void CALLTYPE ScAddInAsyncCallBack( double& nHandle, void* pData )
35 ScAddInAsync::CallBack( sal_uLong( nHandle ), pData );
39 ScAddInAsync::ScAddInAsync() :
40 SvtBroadcaster(),
41 nHandle( 0 )
42 { // nur fuer aSeekObj !
45 ScAddInAsync::ScAddInAsync(sal_uLong nHandleP, FuncData* pFuncData, ScDocument* pDoc) :
46 SvtBroadcaster(),
47 pStr( NULL ),
48 mpFuncData(pFuncData),
49 nHandle( nHandleP ),
50 meType(pFuncData->GetAsyncType()),
51 bValid( false )
53 pDocs = new ScAddInDocs();
54 pDocs->insert( pDoc );
55 theAddInAsyncTbl.insert( this );
58 ScAddInAsync::~ScAddInAsync()
60 // aSeekObj does not have that, handle 0 does not exist otherwise
61 if ( nHandle )
63 // in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
64 mpFuncData->Unadvice( (double)nHandle );
65 if ( meType == PTR_STRING && pStr ) // include type comparison because of union
66 delete pStr;
67 delete pDocs;
71 ScAddInAsync* ScAddInAsync::Get( sal_uLong nHandleP )
73 ScAddInAsync* pRet = 0;
74 aSeekObj.nHandle = nHandleP;
75 ScAddInAsyncs::iterator it = theAddInAsyncTbl.find( &aSeekObj );
76 if ( it != theAddInAsyncTbl.end() )
77 pRet = *it;
78 aSeekObj.nHandle = 0;
79 return pRet;
82 void ScAddInAsync::CallBack( sal_uLong nHandleP, void* pData )
84 ScAddInAsync* p;
85 if ( (p = Get( nHandleP )) == NULL )
86 return;
88 if ( !p->HasListeners() )
90 // not in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
91 theAddInAsyncTbl.erase( p );
92 delete p;
93 return ;
95 switch ( p->meType )
97 case PTR_DOUBLE :
98 p->nVal = *(double*)pData;
99 break;
100 case PTR_STRING :
102 sal_Char* pChar = (sal_Char*)pData;
103 if ( p->pStr )
104 *p->pStr = OUString( pChar, strlen(pChar),osl_getThreadTextEncoding() );
105 else
106 p->pStr = new OUString( pChar, strlen(pChar), osl_getThreadTextEncoding() );
107 break;
109 default :
110 OSL_FAIL( "unknown AsyncType" );
111 return;
113 p->bValid = sal_True;
114 p->Broadcast( ScHint(SC_HINT_DATACHANGED, ScAddress()) );
116 for ( ScAddInDocs::iterator it = p->pDocs->begin(); it != p->pDocs->end(); ++it )
118 ScDocument* pDoc = *it;
119 pDoc->TrackFormulas();
120 pDoc->GetDocumentShell()->Broadcast( SfxSimpleHint( FID_DATACHANGED ) );
124 void ScAddInAsync::RemoveDocument( ScDocument* pDocumentP )
126 if ( !theAddInAsyncTbl.empty() )
128 for( ScAddInAsyncs::reverse_iterator iter1 = theAddInAsyncTbl.rbegin(); iter1 != theAddInAsyncTbl.rend(); ++iter1 )
129 { // backwards because of pointer-movement in array
130 ScAddInAsync* pAsync = *iter1;
131 ScAddInDocs* p = pAsync->pDocs;
132 ScAddInDocs::iterator iter2 = p->find( pDocumentP );
133 if( iter2 != p->end() )
135 p->erase( iter2 );
136 if ( p->empty() )
137 { // this AddIn is not used anymore
138 theAddInAsyncTbl.erase( --(iter1.base()) );
139 delete pAsync;
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */