LibreOffice Module sc (master)  1
adiasync.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <algorithm>
23 
24 #include <sfx2/objsh.hxx>
25 
26 #include <adiasync.hxx>
27 #include <brdcst.hxx>
28 #include <document.hxx>
29 #include <osl/diagnose.h>
30 #include <osl/thread.h>
31 
33 
34 extern "C" {
35 void CALLTYPE ScAddInAsyncCallBack( double& nHandle, void* pData )
36 {
37  ScAddInAsync::CallBack( sal_uLong( nHandle ), pData );
38 }
39 }
40 
43  pStr( nullptr ),
44  mpFuncData(pFuncData),
45  nHandle( nHandleP ),
46  meType(pFuncData->GetAsyncType()),
47  bValid( false )
48 {
49  pDocs.reset(new ScAddInDocs);
50  pDocs->insert( pDoc );
51  theAddInAsyncTbl.emplace( this );
52 }
53 
55 {
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
59  delete pStr;
60  pDocs.reset();
61 }
62 
64 {
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() )
71  pRet = it->get();
72  return pRet;
73 }
74 
75 void ScAddInAsync::CallBack( sal_uLong nHandleP, void* pData )
76 {
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() )
82  return;
83  ScAddInAsync* p = asyncIt->get();
84 
85  if ( !p->HasListeners() )
86  {
87  // not in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
88  theAddInAsyncTbl.erase( asyncIt );
89  return ;
90  }
91  switch ( p->meType )
92  {
94  p->nVal = *static_cast<double*>(pData);
95  break;
97  {
98  char* pChar = static_cast<char*>(pData);
99  if ( p->pStr )
100  *p->pStr = OUString( pChar, strlen(pChar),osl_getThreadTextEncoding() );
101  else
102  p->pStr = new OUString( pChar, strlen(pChar), osl_getThreadTextEncoding() );
103  break;
104  }
105  default :
106  OSL_FAIL( "unknown AsyncType" );
107  return;
108  }
109  p->bValid = true;
110  p->Broadcast( ScHint(SfxHintId::ScDataChanged, ScAddress()) );
111 
112  for ( ScDocument* pDoc : *p->pDocs )
113  {
114  pDoc->TrackFormulas();
115  pDoc->GetDocumentShell()->Broadcast( SfxHint( SfxHintId::ScDataChanged ) );
116  }
117 }
118 
120 {
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() )
127  {
128  p->erase( iter2 );
129  if ( p->empty() )
130  { // this AddIn is not used anymore
131  theAddInAsyncTbl.erase( --(iter1.base()) );
132  }
133  }
134  }
135 }
136 
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString * pStr
Definition: adiasync.hxx:42
double nVal
Definition: adiasync.hxx:41
std::set< ScDocument * > ScAddInDocs
Definition: adiasync.hxx:34
ScAddInAsyncs theAddInAsyncTbl
Definition: adiasync.cxx:32
static void CallBack(sal_uLong nHandle, void *pData)
Definition: adiasync.cxx:75
sal_uIntPtr sal_uLong
virtual ~ScAddInAsync() override
Definition: adiasync.cxx:54
std::unique_ptr< ScAddInDocs > pDocs
Definition: adiasync.hxx:44
ScAddInAsync(sal_uLong nHandle, LegacyFuncData *pFuncData, ScDocument *pDoc)
Definition: adiasync.cxx:41
ParamType meType
Definition: adiasync.hxx:47
sal_Int32 nHandle
void Broadcast(const SfxHint &rHint)
void TrackFormulas(SfxHintId nHintId=SfxHintId::ScDataChanged)
Definition: documen7.cxx:526
std::set< std::unique_ptr< ScAddInAsync >, CompareScAddInAsync > ScAddInAsyncs
Definition: adiasync.hxx:74
LegacyFuncData * mpFuncData
Definition: adiasync.hxx:45
void Unadvice(double nHandle)
Definition: callform.cxx:323
static ScAddInAsync * Get(sal_uLong nHandle)
Definition: adiasync.cxx:63
#define CALLTYPE
Definition: callform.hxx:32
void * p
const char * pChar
static void RemoveDocument(ScDocument *pDocument)
Definition: adiasync.cxx:119
bool HasListeners() const
sal_uLong nHandle
Definition: adiasync.hxx:46
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1055
void CALLTYPE ScAddInAsyncCallBack(double &nHandle, void *pData)
Definition: adiasync.cxx:35
RedlineType meType