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 <global.hxx>
29 #include <document.hxx>
30 #include <sc.hrc>
31 #include <osl/diagnose.h>
32 #include <osl/thread.h>
33 
35 
36 extern "C" {
37 void CALLTYPE ScAddInAsyncCallBack( double& nHandle, void* pData )
38 {
39  ScAddInAsync::CallBack( sal_uLong( nHandle ), pData );
40 }
41 }
42 
45  pStr( nullptr ),
46  mpFuncData(pFuncData),
47  nHandle( nHandleP ),
48  meType(pFuncData->GetAsyncType()),
49  bValid( false )
50 {
51  pDocs.reset(new ScAddInDocs);
52  pDocs->insert( pDoc );
53  theAddInAsyncTbl.emplace( this );
54 }
55 
57 {
58  // in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
59  mpFuncData->Unadvice( static_cast<double>(nHandle) );
60  if ( meType == ParamType::PTR_STRING && pStr ) // include type comparison because of union
61  delete pStr;
62  pDocs.reset();
63 }
64 
66 {
67  ScAddInAsync* pRet = nullptr;
68  auto it = std::find_if(
69  theAddInAsyncTbl.begin(), theAddInAsyncTbl.end(),
70  [nHandleP](std::unique_ptr<ScAddInAsync> const & el)
71  { return el->nHandle == nHandleP; });
72  if ( it != theAddInAsyncTbl.end() )
73  pRet = it->get();
74  return pRet;
75 }
76 
77 void ScAddInAsync::CallBack( sal_uLong nHandleP, void* pData )
78 {
79  auto asyncIt = std::find_if(
80  theAddInAsyncTbl.begin(), theAddInAsyncTbl.end(),
81  [nHandleP](std::unique_ptr<ScAddInAsync> const & el)
82  { return el->nHandle == nHandleP; });
83  if ( asyncIt == theAddInAsyncTbl.end() )
84  return;
85  ScAddInAsync* p = asyncIt->get();
86 
87  if ( !p->HasListeners() )
88  {
89  // not in dTor because of theAddInAsyncTbl.DeleteAndDestroy in ScGlobal::Clear
90  theAddInAsyncTbl.erase( asyncIt );
91  return ;
92  }
93  switch ( p->meType )
94  {
96  p->nVal = *static_cast<double*>(pData);
97  break;
99  {
100  char* pChar = static_cast<char*>(pData);
101  if ( p->pStr )
102  *p->pStr = OUString( pChar, strlen(pChar),osl_getThreadTextEncoding() );
103  else
104  p->pStr = new OUString( pChar, strlen(pChar), osl_getThreadTextEncoding() );
105  break;
106  }
107  default :
108  OSL_FAIL( "unknown AsyncType" );
109  return;
110  }
111  p->bValid = true;
112  p->Broadcast( ScHint(SfxHintId::ScDataChanged, ScAddress()) );
113 
114  for ( ScDocument* pDoc : *p->pDocs )
115  {
116  pDoc->TrackFormulas();
117  pDoc->GetDocumentShell()->Broadcast( SfxHint( SfxHintId::ScDataChanged ) );
118  }
119 }
120 
122 {
123  for( ScAddInAsyncs::reverse_iterator iter1 = theAddInAsyncTbl.rbegin(); iter1 != theAddInAsyncTbl.rend(); ++iter1 )
124  { // backwards because of pointer-movement in array
125  ScAddInAsync* pAsync = iter1->get();
126  ScAddInDocs* p = pAsync->pDocs.get();
127  ScAddInDocs::iterator iter2 = p->find( pDocumentP );
128  if( iter2 != p->end() )
129  {
130  p->erase( iter2 );
131  if ( p->empty() )
132  { // this AddIn is not used anymore
133  theAddInAsyncTbl.erase( --(iter1.base()) );
134  }
135  }
136  }
137 }
138 
139 /* 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:34
static void CallBack(sal_uLong nHandle, void *pData)
Definition: adiasync.cxx:77
sal_uIntPtr sal_uLong
virtual ~ScAddInAsync() override
Definition: adiasync.cxx:56
std::unique_ptr< ScAddInDocs > pDocs
Definition: adiasync.hxx:44
ScAddInAsync(sal_uLong nHandle, LegacyFuncData *pFuncData, ScDocument *pDoc)
Definition: adiasync.cxx:43
ParamType meType
Definition: adiasync.hxx:47
sal_Int32 nHandle
void Broadcast(const SfxHint &rHint)
void TrackFormulas(SfxHintId nHintId=SfxHintId::ScDataChanged)
Definition: documen7.cxx:527
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:324
static ScAddInAsync * Get(sal_uLong nHandle)
Definition: adiasync.cxx:65
#define CALLTYPE
Definition: callform.hxx:32
void * p
const char * pChar
static void RemoveDocument(ScDocument *pDocument)
Definition: adiasync.cxx:121
bool HasListeners() const
sal_uLong nHandle
Definition: adiasync.hxx:46
SfxObjectShell * GetDocumentShell() const
Definition: document.hxx:1058
void CALLTYPE ScAddInAsyncCallBack(double &nHandle, void *pData)
Definition: adiasync.cxx:37
RedlineType meType