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 #include <sal/config.h>
24 #include <sfx2/objsh.hxx>
26 #include <adiasync.hxx>
28 #include <document.hxx>
30 #include <osl/diagnose.h>
31 #include <osl/thread.h>
33 ScAddInAsyncs theAddInAsyncTbl
;
36 void CALLTYPE
ScAddInAsyncCallBack( double& nHandle
, void* pData
)
38 ScAddInAsync::CallBack( sal_uLong( nHandle
), pData
);
42 ScAddInAsync::ScAddInAsync(sal_uLong nHandleP
, LegacyFuncData
* pFuncData
, ScDocument
* pDoc
) :
44 mpFuncData(pFuncData
),
46 meType(pFuncData
->GetAsyncType()),
49 pDocs
.reset(new ScAddInDocs
);
50 pDocs
->insert( pDoc
);
51 theAddInAsyncTbl
.emplace( this );
54 ScAddInAsync::~ScAddInAsync()
56 // in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
57 mpFuncData
->Unadvice( static_cast<double>(nHandle
) );
58 if ( meType
== ParamType::PTR_STRING
&& pStr
) // include type comparison because of union
63 ScAddInAsync
* ScAddInAsync::Get( sal_uLong nHandleP
)
65 ScAddInAsync
* pRet
= nullptr;
66 auto it
= std::find_if(
67 theAddInAsyncTbl
.begin(), theAddInAsyncTbl
.end(),
68 [nHandleP
](std::unique_ptr
<ScAddInAsync
> const & el
)
69 { return el
->nHandle
== nHandleP
; });
70 if ( it
!= theAddInAsyncTbl
.end() )
75 void ScAddInAsync::CallBack( sal_uLong nHandleP
, void* pData
)
77 auto asyncIt
= std::find_if(
78 theAddInAsyncTbl
.begin(), theAddInAsyncTbl
.end(),
79 [nHandleP
](std::unique_ptr
<ScAddInAsync
> const & el
)
80 { return el
->nHandle
== nHandleP
; });
81 if ( asyncIt
== theAddInAsyncTbl
.end() )
83 ScAddInAsync
* p
= asyncIt
->get();
85 if ( !p
->HasListeners() )
87 // not in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
88 theAddInAsyncTbl
.erase( asyncIt
);
93 case ParamType::PTR_DOUBLE
:
94 p
->nVal
= *static_cast<double*>(pData
);
96 case ParamType::PTR_STRING
:
98 char* pChar
= static_cast<char*>(pData
);
100 *p
->pStr
= OUString( pChar
, strlen(pChar
),osl_getThreadTextEncoding() );
102 p
->pStr
= new OUString( pChar
, strlen(pChar
), osl_getThreadTextEncoding() );
106 OSL_FAIL( "unknown AsyncType" );
110 p
->Broadcast( ScHint(SfxHintId::ScDataChanged
, ScAddress()) );
112 for ( ScDocument
* pDoc
: *p
->pDocs
)
114 pDoc
->TrackFormulas();
115 pDoc
->GetDocumentShell()->Broadcast( SfxHint( SfxHintId::ScDataChanged
) );
119 void ScAddInAsync::RemoveDocument( ScDocument
* pDocumentP
)
121 for( ScAddInAsyncs::reverse_iterator iter1
= theAddInAsyncTbl
.rbegin(); iter1
!= theAddInAsyncTbl
.rend(); ++iter1
)
122 { // backwards because of pointer-movement in array
123 ScAddInAsync
* pAsync
= iter1
->get();
124 ScAddInDocs
* p
= pAsync
->pDocs
.get();
125 ScAddInDocs::iterator iter2
= p
->find( pDocumentP
);
126 if( iter2
!= p
->end() )
130 { // this AddIn is not used anymore
131 theAddInAsyncTbl
.erase( --(iter1
.base()) );
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */