Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 192, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name transobj.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -isystem /usr/include/libxml2 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D SC_DLLIMPLEMENTATION -D SC_INFO_OSVERSION="LINUX" -D SYSTEM_LIBXML -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/liborcus/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/mdds/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/clew/source/include -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/sc/source/core/inc -I /home/maarten/src/libreoffice/core/sc/source/filter/inc -I /home/maarten/src/libreoffice/core/sc/source/ui/inc -I /home/maarten/src/libreoffice/core/sc/inc -I /home/maarten/src/libreoffice/core/workdir/SdiTarget/sc/sdi -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/oovbaapi/normal -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx

/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx

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 <scitems.hxx>
21#include <editeng/justifyitem.hxx>
22
23#include <com/sun/star/uno/Sequence.hxx>
24#include <com/sun/star/embed/XTransactedObject.hpp>
25
26#include <osl/diagnose.h>
27#include <unotools/tempfile.hxx>
28#include <unotools/ucbstreamhelper.hxx>
29#include <comphelper/fileformat.h>
30#include <comphelper/storagehelper.hxx>
31#include <comphelper/servicehelper.hxx>
32#include <sot/storage.hxx>
33#include <vcl/gdimtf.hxx>
34#include <vcl/jobset.hxx>
35#include <vcl/svapp.hxx>
36#include <vcl/virdev.hxx>
37#include <sfx2/docfile.hxx>
38
39#include <transobj.hxx>
40#include <patattr.hxx>
41#include <cellvalue.hxx>
42#include <cellform.hxx>
43#include <document.hxx>
44#include <viewopti.hxx>
45#include <editutil.hxx>
46#include <impex.hxx>
47#include <formulacell.hxx>
48#include <printfun.hxx>
49#include <docfunc.hxx>
50#include <scmod.hxx>
51#include <dragdata.hxx>
52
53#include <editeng/paperinf.hxx>
54#include <editeng/sizeitem.hxx>
55#include <formula/errorcodes.hxx>
56#include <docsh.hxx>
57#include <markdata.hxx>
58#include <stlpool.hxx>
59#include <viewdata.hxx>
60#include <dociter.hxx>
61#include <cellsuno.hxx>
62#include <stringutil.hxx>
63#include <formulaiter.hxx>
64
65using namespace com::sun::star;
66
67constexpr sal_uInt32 SCTRANS_TYPE_IMPEX = 1;
68constexpr sal_uInt32 SCTRANS_TYPE_EDIT_RTF = 2;
69constexpr sal_uInt32 SCTRANS_TYPE_EDIT_BIN = 3;
70constexpr sal_uInt32 SCTRANS_TYPE_EMBOBJ = 4;
71constexpr sal_uInt32 SCTRANS_TYPE_EDIT_ODF_TEXT_FLAT = 5;
72
73void ScTransferObj::GetAreaSize( const ScDocument& rDoc, SCTAB nTab1, SCTAB nTab2, SCROW& nRow, SCCOL& nCol )
74{
75 SCCOL nMaxCol = 0;
76 SCROW nMaxRow = 0;
77 for( SCTAB nTab = nTab1; nTab <= nTab2; nTab++ )
78 {
79 SCCOL nLastCol = 0;
80 SCROW nLastRow = 0;
81 // GetPrintArea instead of GetCellArea - include drawing objects
82 if( rDoc.GetPrintArea( nTab, nLastCol, nLastRow ) )
83 {
84 if( nLastCol > nMaxCol )
85 nMaxCol = nLastCol;
86 if( nLastRow > nMaxRow )
87 nMaxRow = nLastRow;
88 }
89 }
90 nRow = nMaxRow;
91 nCol = nMaxCol;
92}
93
94void ScTransferObj::PaintToDev( OutputDevice* pDev, ScDocument& rDoc, double nPrintFactor,
95 const ScRange& rBlock )
96{
97 tools::Rectangle aBound( Point(), pDev->GetOutputSize() ); //! use size from clip area?
98
99 ScViewData aViewData(rDoc);
100
101 aViewData.SetTabNo( rBlock.aEnd.Tab() );
102 aViewData.SetScreen( rBlock.aStart.Col(), rBlock.aStart.Row(),
103 rBlock.aEnd.Col(), rBlock.aEnd.Row() );
104
105 ScPrintFunc::DrawToDev( rDoc, pDev, nPrintFactor, aBound, &aViewData, false/*bMetaFile*/ );
106}
107
108ScTransferObj::ScTransferObj( ScDocumentUniquePtr pClipDoc, const TransferableObjectDescriptor& rDesc ) :
109 m_pDoc( std::move(pClipDoc ) ),
110 m_nNonFiltered(0),
111 m_aObjDesc( rDesc ),
112 m_nDragHandleX( 0 ),
113 m_nDragHandleY( 0 ),
114 m_nSourceCursorX( m_pDoc->MaxCol() + 1 ),
115 m_nSourceCursorY( m_pDoc->MaxRow() + 1 ),
116 m_nDragSourceFlags( ScDragSrc::Undefined ),
117 m_bDragWasInternal( false ),
118 m_bUsedForLink( false ),
119 m_bUseInApi( false )
120{
121 OSL_ENSURE(m_pDoc->IsClipboard(), "wrong document")do { if (true && (!(m_pDoc->IsClipboard()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx"
":" "121" ": "), "%s", "wrong document"); } } while (false)
;
122
123 // get aBlock from clipboard doc
124
125 SCCOL nCol1;
126 SCROW nRow1;
127 SCCOL nCol2;
128 SCROW nRow2;
129 m_pDoc->GetClipStart( nCol1, nRow1 );
130 m_pDoc->GetClipArea( nCol2, nRow2, true ); // real source area - include filtered rows
131 nCol2 = sal::static_int_cast<SCCOL>( nCol2 + nCol1 );
132 nRow2 = sal::static_int_cast<SCROW>( nRow2 + nRow1 );
133
134 SCCOL nDummy;
135 m_pDoc->GetClipArea( nDummy, m_nNonFiltered, false );
136 m_bHasFiltered = (m_nNonFiltered < (nRow2 - nRow1));
137 ++m_nNonFiltered; // to get count instead of diff
138
139 SCTAB nTab1=0;
140 SCTAB nTab2=0;
141 bool bFirst = true;
142 for (SCTAB i=0; i< m_pDoc->GetTableCount(); i++)
143 if (m_pDoc->HasTable(i))
144 {
145 if (bFirst)
146 nTab1 = i;
147 nTab2 = i;
148 bFirst = false;
149 }
150 OSL_ENSURE(!bFirst, "no sheet selected")do { if (true && (!(!bFirst))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx"
":" "150" ": "), "%s", "no sheet selected"); } } while (false
)
;
151
152 // only limit to used cells if whole sheet was marked
153 // (so empty cell areas can be copied)
154 if ( nCol2>=m_pDoc->MaxCol() && nRow2>=m_pDoc->MaxRow() )
155 {
156 SCROW nMaxRow;
157 SCCOL nMaxCol;
158 GetAreaSize( *m_pDoc, nTab1, nTab2, nMaxRow, nMaxCol );
159 if( nMaxRow < nRow2 )
160 nRow2 = nMaxRow;
161 if( nMaxCol < nCol2 )
162 nCol2 = nMaxCol;
163 }
164
165 m_aBlock = ScRange( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
166 m_nVisibleTab = nTab1; // valid table as default
167
168 tools::Rectangle aMMRect = m_pDoc->GetMMRect( nCol1,nRow1, nCol2,nRow2, nTab1 );
169 m_aObjDesc.maSize = aMMRect.GetSize();
170 PrepareOLE( m_aObjDesc );
171}
172
173ScTransferObj::~ScTransferObj()
174{
175 SolarMutexGuard aSolarGuard;
176
177 ScModule* pScMod = SC_MOD()( static_cast<ScModule*>(SfxApplication::GetModule(SfxToolsModule
::Calc)) )
;
178 if (pScMod && pScMod->GetDragData().pCellTransfer == this)
179 {
180 OSL_FAIL("ScTransferObj wasn't released")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx"
":" "180" ": "), "%s", "ScTransferObj wasn't released"); } }
while (false)
;
181 pScMod->ResetDragObject();
182 }
183
184 m_pDoc.reset(); // ScTransferObj is owner of clipboard document
185
186 m_aDocShellRef.clear(); // before releasing the mutex
187
188 m_aDrawPersistRef.clear(); // after the model
189
190}
191
192ScTransferObj* ScTransferObj::GetOwnClipboard(const uno::Reference<datatransfer::XTransferable2>& xTransferable)
193{
194 ScTransferObj* pObj = nullptr;
195 if (xTransferable.is())
196 {
197 uno::Reference<XUnoTunnel> xTunnel( xTransferable, uno::UNO_QUERY );
198 if ( xTunnel.is() )
199 {
200 sal_Int64 nHandle = xTunnel->getSomething( getUnoTunnelId() );
201 if ( nHandle )
202 pObj = dynamic_cast<ScTransferObj*>(reinterpret_cast<TransferableHelper*>( static_cast<sal_IntPtr>(nHandle) ));
203 }
204 }
205
206 return pObj;
207}
208
209void ScTransferObj::AddSupportedFormats()
210{
211 AddFormat( SotClipboardFormatId::EMBED_SOURCE );
212 AddFormat( SotClipboardFormatId::OBJECTDESCRIPTOR );
213 AddFormat( SotClipboardFormatId::GDIMETAFILE );
214 AddFormat( SotClipboardFormatId::PNG );
215 AddFormat( SotClipboardFormatId::BITMAP );
216
217 // ScImportExport formats
218 AddFormat( SotClipboardFormatId::HTML );
219 AddFormat( SotClipboardFormatId::SYLK );
220 AddFormat( SotClipboardFormatId::LINK );
221 AddFormat( SotClipboardFormatId::DIF );
222 AddFormat( SotClipboardFormatId::STRING );
223 AddFormat( SotClipboardFormatId::STRING_TSVC );
224
225 AddFormat( SotClipboardFormatId::RTF );
226 AddFormat( SotClipboardFormatId::RICHTEXT );
227 if ( m_aBlock.aStart == m_aBlock.aEnd )
228 {
229 AddFormat( SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT );
230 }
231}
232
233static ScRange lcl_reduceBlock(const ScDocument& rDoc, ScRange aReducedBlock, bool bIncludeVisual = false)
234{
235 if ((aReducedBlock.aEnd.Col() == rDoc.MaxCol() || aReducedBlock.aEnd.Row() == rDoc.MaxRow()) &&
236 aReducedBlock.aStart.Tab() == aReducedBlock.aEnd.Tab())
237 {
238 // Shrink the block here so we don't waste time creating huge
239 // output when whole columns or rows are selected.
240
241 SCCOL nPrintAreaEndCol = 0;
242 SCROW nPrintAreaEndRow = 0;
243 if (bIncludeVisual)
244 rDoc.GetPrintArea( aReducedBlock.aStart.Tab(), nPrintAreaEndCol, nPrintAreaEndRow, true );
245
246 // Shrink the area to allow pasting to external applications.
247 // Shrink to real data area for HTML, RTF and RICHTEXT, but include
248 // all objects and top-left area for BITMAP and PNG.
249 SCCOL nStartCol = aReducedBlock.aStart.Col();
250 SCROW nStartRow = aReducedBlock.aStart.Row();
251 SCCOL nEndCol = aReducedBlock.aEnd.Col();
252 SCROW nEndRow = aReducedBlock.aEnd.Row();
253 bool bShrunk = false;
254 rDoc.ShrinkToUsedDataArea( bShrunk, aReducedBlock.aStart.Tab(), nStartCol, nStartRow, nEndCol, nEndRow,
255 false, bIncludeVisual /*bStickyTopRow*/, bIncludeVisual /*bStickyLeftCol*/,
256 bIncludeVisual /*bConsiderCellNotes*/, bIncludeVisual /*bConsiderCellDrawObjects*/);
257
258 if ( nPrintAreaEndRow > nEndRow )
259 nEndRow = nPrintAreaEndRow;
260
261 if ( nPrintAreaEndCol > nEndCol )
262 nEndCol = nPrintAreaEndCol;
263
264 aReducedBlock = ScRange(nStartCol, nStartRow, aReducedBlock.aStart.Tab(), nEndCol, nEndRow, aReducedBlock.aEnd.Tab());
265 }
266 return aReducedBlock;
267}
268
269bool ScTransferObj::GetData( const datatransfer::DataFlavor& rFlavor, const OUString& /*rDestDoc*/ )
270{
271 SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor );
272 bool bOK = false;
273
274 if( HasFormat( nFormat ) )
1
Assuming the condition is true
2
Taking true branch
275 {
276 ScRange aReducedBlock = m_aBlock;
277
278 bool bReduceBlockFormat =
279 nFormat == SotClipboardFormatId::HTML
3
Assuming 'nFormat' is not equal to HTML
280 || nFormat == SotClipboardFormatId::RTF
4
Assuming 'nFormat' is not equal to RTF
281 || nFormat == SotClipboardFormatId::RICHTEXT
5
Assuming 'nFormat' is not equal to RICHTEXT
282 || nFormat == SotClipboardFormatId::BITMAP
6
Assuming 'nFormat' is not equal to BITMAP
283 || nFormat == SotClipboardFormatId::PNG;
7
Assuming 'nFormat' is not equal to PNG
284
285 const bool bIncludeVisual = (nFormat
7.1
'nFormat' is not equal to BITMAP
7.1
'nFormat' is not equal to BITMAP
7.1
'nFormat' is not equal to BITMAP
7.1
'nFormat' is not equal to BITMAP
== SotClipboardFormatId::BITMAP ||
286 nFormat
7.2
'nFormat' is not equal to PNG
7.2
'nFormat' is not equal to PNG
7.2
'nFormat' is not equal to PNG
7.2
'nFormat' is not equal to PNG
== SotClipboardFormatId::PNG);
287
288 if (bReduceBlockFormat
7.3
'bReduceBlockFormat' is false
7.3
'bReduceBlockFormat' is false
7.3
'bReduceBlockFormat' is false
7.3
'bReduceBlockFormat' is false
)
8
Taking false branch
289 aReducedBlock = lcl_reduceBlock(*m_pDoc, m_aBlock, bIncludeVisual);
290
291 if ( nFormat == SotClipboardFormatId::LINKSRCDESCRIPTOR || nFormat == SotClipboardFormatId::OBJECTDESCRIPTOR )
9
Assuming 'nFormat' is not equal to LINKSRCDESCRIPTOR
10
Assuming 'nFormat' is not equal to OBJECTDESCRIPTOR
11
Taking false branch
292 {
293 bOK = SetTransferableObjectDescriptor( m_aObjDesc );
294 }
295 else if ( ( nFormat
11.1
'nFormat' is not equal to RTF
11.1
'nFormat' is not equal to RTF
11.1
'nFormat' is not equal to RTF
11.1
'nFormat' is not equal to RTF
== SotClipboardFormatId::RTF || nFormat
11.2
'nFormat' is not equal to RICHTEXT
11.2
'nFormat' is not equal to RICHTEXT
11.2
'nFormat' is not equal to RICHTEXT
11.2
'nFormat' is not equal to RICHTEXT
== SotClipboardFormatId::RICHTEXT ||
296 nFormat == SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT ) && m_aBlock.aStart == m_aBlock.aEnd )
12
Assuming 'nFormat' is not equal to EDITENGINE_ODF_TEXT_FLAT
297 {
298 // RTF from a single cell is handled by EditEngine
299
300 SCCOL nCol = m_aBlock.aStart.Col();
301 SCROW nRow = m_aBlock.aStart.Row();
302 SCTAB nTab = m_aBlock.aStart.Tab();
303 ScAddress aPos(nCol, nRow, nTab);
304
305 const ScPatternAttr* pPattern = m_pDoc->GetPattern( nCol, nRow, nTab );
306 ScTabEditEngine aEngine( *pPattern, m_pDoc->GetEditPool(), m_pDoc.get() );
307 ScRefCellValue aCell(*m_pDoc, aPos);
308 if (aCell.meType == CELLTYPE_EDIT)
309 {
310 const EditTextObject* pObj = aCell.mpEditText;
311 aEngine.SetTextCurrentDefaults(*pObj);
312 }
313 else
314 {
315 SvNumberFormatter* pFormatter = m_pDoc->GetFormatTable();
316 sal_uInt32 nNumFmt = pPattern->GetNumberFormat(pFormatter);
317 OUString aText;
318 const Color* pColor;
319 ScCellFormat::GetString(aCell, nNumFmt, aText, &pColor, *pFormatter, *m_pDoc);
320 if (!aText.isEmpty())
321 aEngine.SetTextCurrentDefaults(aText);
322 }
323
324 bOK = SetObject( &aEngine,
325 ((nFormat == SotClipboardFormatId::RTF) ? SCTRANS_TYPE_EDIT_RTF :
326 ((nFormat == SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT) ?
327 SCTRANS_TYPE_EDIT_ODF_TEXT_FLAT : SCTRANS_TYPE_EDIT_BIN)),
328 rFlavor );
329 }
330 else if ( ScImportExport::IsFormatSupported( nFormat ) || nFormat
13.1
'nFormat' is not equal to RTF
13.1
'nFormat' is not equal to RTF
13.1
'nFormat' is not equal to RTF
13.1
'nFormat' is not equal to RTF
== SotClipboardFormatId::RTF
13
Assuming the condition is false
14
Taking false branch
331 || nFormat
13.2
'nFormat' is not equal to RICHTEXT
13.2
'nFormat' is not equal to RICHTEXT
13.2
'nFormat' is not equal to RICHTEXT
13.2
'nFormat' is not equal to RICHTEXT
== SotClipboardFormatId::RICHTEXT )
332 {
333 // if this transfer object was used to create a DDE link, filtered rows
334 // have to be included for subsequent calls (to be consistent with link data)
335 if ( nFormat == SotClipboardFormatId::LINK )
336 m_bUsedForLink = true;
337
338 bool bIncludeFiltered = m_pDoc->IsCutMode() || m_bUsedForLink;
339
340 ScImportExport aObj( *m_pDoc, aReducedBlock );
341 // Plain text ("Unformatted text") may contain embedded tabs and
342 // line breaks but is not enclosed in quotes. Which makes it
343 // unsuitable for multiple cells, especially if one of them is
344 // multi-line, but otherwise is expected behavior for plain text.
345 // For multiple cells replace embedded line breaks (and tabs) with
346 // space character, otherwise pasting would yield odd results.
347 /* XXX: it's debatable whether this is actually expected, but
348 * there's no way to satisfy all possible requirements when
349 * copy/pasting unformatted text. */
350 const bool bPlainMulti = (nFormat == SotClipboardFormatId::STRING &&
351 aReducedBlock.aStart != aReducedBlock.aEnd);
352 // Add quotes only for STRING_TSVC.
353 /* TODO: a possible future STRING_TSV should not contain embedded
354 * line breaks nor tab (separator) characters and not be quoted.
355 * A possible STRING_CSV should. */
356 ScExportTextOptions aTextOptions( ScExportTextOptions::None, 0,
357 (nFormat == SotClipboardFormatId::STRING_TSVC));
358 if ( bPlainMulti || m_bUsedForLink )
359 {
360 // For a DDE link or plain text multiple cells, convert line
361 // breaks and separators to space.
362 aTextOptions.meNewlineConversion = ScExportTextOptions::ToSpace;
363 aTextOptions.mcSeparatorConvertTo = ' ';
364 aTextOptions.mbAddQuotes = false;
365 }
366 aObj.SetExportTextOptions(aTextOptions);
367 aObj.SetFormulas( m_pDoc->GetViewOptions().GetOption( VOPT_FORMULAS ) );
368 aObj.SetIncludeFiltered( bIncludeFiltered );
369
370 // DataType depends on format type:
371
372 if ( rFlavor.DataType.equals( ::cppu::UnoType<OUString>::get() ) )
373 {
374 OUString aString;
375 if ( aObj.ExportString( aString, nFormat ) )
376 bOK = SetString( aString, rFlavor );
377 }
378 else if ( rFlavor.DataType.equals( cppu::UnoType<uno::Sequence< sal_Int8 >>::get() ) )
379 {
380 // SetObject converts a stream into an Int8-Sequence
381 bOK = SetObject( &aObj, SCTRANS_TYPE_IMPEX, rFlavor );
382 }
383 else
384 {
385 OSL_FAIL("unknown DataType")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx"
":" "385" ": "), "%s", "unknown DataType"); } } while (false
)
;
386 }
387 }
388 else if ( nFormat
14.1
'nFormat' is not equal to BITMAP
14.1
'nFormat' is not equal to BITMAP
14.1
'nFormat' is not equal to BITMAP
14.1
'nFormat' is not equal to BITMAP
== SotClipboardFormatId::BITMAP || nFormat
14.2
'nFormat' is not equal to PNG
14.2
'nFormat' is not equal to PNG
14.2
'nFormat' is not equal to PNG
14.2
'nFormat' is not equal to PNG
== SotClipboardFormatId::PNG )
15
Taking false branch
389 {
390 tools::Rectangle aMMRect = m_pDoc->GetMMRect( aReducedBlock.aStart.Col(), aReducedBlock.aStart.Row(),
391 aReducedBlock.aEnd.Col(), aReducedBlock.aEnd.Row(),
392 aReducedBlock.aStart.Tab() );
393 ScopedVclPtrInstance< VirtualDevice > pVirtDev;
394 pVirtDev->SetOutputSizePixel(pVirtDev->LogicToPixel(aMMRect.GetSize(), MapMode(MapUnit::Map100thMM)));
395
396 PaintToDev( pVirtDev, *m_pDoc, 1.0, aReducedBlock );
397
398 pVirtDev->SetMapMode( MapMode( MapUnit::MapPixel ) );
399 BitmapEx aBmp = pVirtDev->GetBitmapEx( Point(), pVirtDev->GetOutputSize() );
400 bOK = SetBitmapEx( aBmp, rFlavor );
401 }
402 else if ( nFormat == SotClipboardFormatId::GDIMETAFILE )
16
Assuming 'nFormat' is equal to GDIMETAFILE
17
Taking true branch
403 {
404 // #i123405# Do not limit visual size calculation for metafile creation.
405 // It seems unlikely that removing the limitation causes problems since
406 // metafile creation means that no real pixel device in the needed size is
407 // created.
408 InitDocShell(false);
409
410 SfxObjectShell* pEmbObj = m_aDocShellRef.get();
411
412 // like SvEmbeddedTransfer::GetData:
413 GDIMetaFile aMtf;
414 ScopedVclPtrInstance< VirtualDevice > pVDev;
18
Calling default constructor for 'ScopedVclPtrInstance<VirtualDevice>'
20
Returning from default constructor for 'ScopedVclPtrInstance<VirtualDevice>'
415 MapMode aMapMode( pEmbObj->GetMapUnit() );
416 tools::Rectangle aVisArea( pEmbObj->GetVisArea( ASPECT_CONTENT1 ) );
417
418 pVDev->EnableOutput( false );
419 pVDev->SetMapMode( aMapMode );
420 aMtf.SetPrefSize( aVisArea.GetSize() );
421 aMtf.SetPrefMapMode( aMapMode );
422 aMtf.Record( pVDev );
423
424 pEmbObj->DoDraw( pVDev, Point(), aVisArea.GetSize(), JobSetup() );
425
426 aMtf.Stop();
427 aMtf.WindStart();
428
429 bOK = SetGDIMetaFile( aMtf );
430 }
21
Calling implicit destructor for 'ScopedVclPtrInstance<VirtualDevice>'
22
Calling '~ScopedVclPtr'
431 else if ( nFormat == SotClipboardFormatId::EMBED_SOURCE )
432 {
433 //TODO/LATER: differentiate between formats?!
434 // #i123405# Do limit visual size calculation to PageSize
435 InitDocShell(true); // set aDocShellRef
436
437 SfxObjectShell* pEmbObj = m_aDocShellRef.get();
438 bOK = SetObject( pEmbObj, SCTRANS_TYPE_EMBOBJ, rFlavor );
439 }
440 }
441 return bOK;
442}
443
444bool ScTransferObj::WriteObject( tools::SvRef<SotStorageStream>& rxOStm, void* pUserObject, sal_uInt32 nUserObjectId,
445 const datatransfer::DataFlavor& rFlavor )
446{
447 // called from SetObject, put data into stream
448
449 bool bRet = false;
450 switch (nUserObjectId)
451 {
452 case SCTRANS_TYPE_IMPEX:
453 {
454 ScImportExport* pImpEx = static_cast<ScImportExport*>(pUserObject);
455
456 SotClipboardFormatId nFormat = SotExchange::GetFormat( rFlavor );
457 // mba: no BaseURL for data exchange
458 if ( pImpEx->ExportStream( *rxOStm, OUString(), nFormat ) )
459 bRet = ( rxOStm->GetError() == ERRCODE_NONEErrCode(0) );
460 }
461 break;
462
463 case SCTRANS_TYPE_EDIT_RTF:
464 case SCTRANS_TYPE_EDIT_BIN:
465 {
466 ScTabEditEngine* pEngine = static_cast<ScTabEditEngine*>(pUserObject);
467 if ( nUserObjectId == SCTRANS_TYPE_EDIT_RTF )
468 {
469 pEngine->Write( *rxOStm, EETextFormat::Rtf );
470 bRet = ( rxOStm->GetError() == ERRCODE_NONEErrCode(0) );
471 }
472 else
473 {
474 // can't use Write for EditEngine format because that would
475 // write old format without support for unicode characters.
476 // Get the data from the EditEngine's transferable instead.
477
478 sal_Int32 nParCnt = pEngine->GetParagraphCount();
479 if ( nParCnt == 0 )
480 nParCnt = 1;
481 ESelection aSel( 0, 0, nParCnt-1, pEngine->GetTextLen(nParCnt-1) );
482
483 uno::Reference<datatransfer::XTransferable> xEditTrans = pEngine->CreateTransferable( aSel );
484 TransferableDataHelper aEditHelper( xEditTrans );
485
486 bRet = aEditHelper.GetSotStorageStream( rFlavor, rxOStm );
487 }
488 }
489 break;
490
491 case SCTRANS_TYPE_EDIT_ODF_TEXT_FLAT:
492 {
493 ScTabEditEngine* pEngine = static_cast<ScTabEditEngine*>(pUserObject);
494 pEngine->Write(*rxOStm, EETextFormat::Xml);
495 bRet = (rxOStm->GetError() == ERRCODE_NONEErrCode(0));
496 }
497 break;
498
499 case SCTRANS_TYPE_EMBOBJ:
500 {
501 // TODO/MBA: testing
502 SfxObjectShell* pEmbObj = static_cast<SfxObjectShell*>(pUserObject);
503 ::utl::TempFile aTempFile;
504 aTempFile.EnableKillingFile();
505 uno::Reference< embed::XStorage > xWorkStore =
506 ::comphelper::OStorageHelper::GetStorageFromURL( aTempFile.GetURL(), embed::ElementModes::READWRITE );
507
508 // write document storage
509 pEmbObj->SetupStorage( xWorkStore, SOFFICE_FILEFORMAT_CURRENT6800, false );
510
511 // mba: no relative URLs for clipboard!
512 SfxMedium aMedium( xWorkStore, OUString() );
513 pEmbObj->DoSaveObjectAs( aMedium, false );
514 pEmbObj->DoSaveCompleted();
515
516 uno::Reference< embed::XTransactedObject > xTransact( xWorkStore, uno::UNO_QUERY );
517 if ( xTransact.is() )
518 xTransact->commit();
519
520 std::unique_ptr<SvStream> pSrcStm = ::utl::UcbStreamHelper::CreateStream( aTempFile.GetURL(), StreamMode::READ );
521 if( pSrcStm )
522 {
523 rxOStm->SetBufferSize( 0xff00 );
524 rxOStm->WriteStream( *pSrcStm );
525 pSrcStm.reset();
526 }
527
528 bRet = true;
529
530 xWorkStore->dispose();
531 xWorkStore.clear();
532 rxOStm->Commit();
533 }
534 break;
535
536 default:
537 OSL_FAIL("unknown object id")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx"
":" "537" ": "), "%s", "unknown object id"); } } while (false
)
;
538 }
539 return bRet;
540}
541
542sal_Bool SAL_CALL ScTransferObj::isComplex()
543{
544 ScRange aReduced = lcl_reduceBlock(*m_pDoc, m_aBlock);
545 size_t nCells = (aReduced.aEnd.Col() - aReduced.aStart.Col() + 1) *
546 (aReduced.aEnd.Row() - aReduced.aStart.Row() + 1) *
547 (aReduced.aEnd.Tab() - aReduced.aStart.Tab() + 1);
548 return nCells > 1000;
549}
550
551void ScTransferObj::DragFinished( sal_Int8 nDropAction )
552{
553 if ( nDropAction == DND_ACTION_MOVEcss::datatransfer::dnd::DNDConstants::ACTION_MOVE && !m_bDragWasInternal && !(m_nDragSourceFlags & ScDragSrc::Navigator) )
554 {
555 // move: delete source data
556 ScDocShell* pSourceSh = GetSourceDocShell();
557 if (pSourceSh)
558 {
559 ScMarkData aMarkData = GetSourceMarkData();
560 // external drag&drop doesn't copy objects, so they also aren't deleted:
561 // bApi=TRUE, don't show error messages from drag&drop
562 pSourceSh->GetDocFunc().DeleteContents( aMarkData, InsertDeleteFlags::ALL & ~InsertDeleteFlags::OBJECTS, true, true );
563 }
564 }
565
566 ScModule* pScMod = SC_MOD()( static_cast<ScModule*>(SfxApplication::GetModule(SfxToolsModule
::Calc)) )
;
567 if ( pScMod->GetDragData().pCellTransfer == this )
568 pScMod->ResetDragObject();
569
570 m_xDragSourceRanges = nullptr; // don't keep source after dropping
571
572 TransferDataContainer::DragFinished( nDropAction );
573}
574
575void ScTransferObj::SetDragHandlePos( SCCOL nX, SCROW nY )
576{
577 m_nDragHandleX = nX;
578 m_nDragHandleY = nY;
579}
580
581void ScTransferObj::SetSourceCursorPos( SCCOL nX, SCROW nY )
582{
583 m_nSourceCursorX = nX;
584 m_nSourceCursorY = nY;
585}
586
587bool ScTransferObj::WasSourceCursorInSelection() const
588{
589 return
590 m_nSourceCursorX >= m_aBlock.aStart.Col() && m_nSourceCursorX <= m_aBlock.aEnd.Col() &&
591 m_nSourceCursorY >= m_aBlock.aStart.Row() && m_nSourceCursorY <= m_aBlock.aEnd.Row();
592}
593
594void ScTransferObj::SetVisibleTab( SCTAB nNew )
595{
596 m_nVisibleTab = nNew;
597}
598
599void ScTransferObj::SetDrawPersist( const SfxObjectShellRef& rRef )
600{
601 m_aDrawPersistRef = rRef;
602}
603
604void ScTransferObj::SetDragSource( ScDocShell* pSourceShell, const ScMarkData& rMark )
605{
606 ScRangeList aRanges;
607 rMark.FillRangeListWithMarks( &aRanges, false );
608 m_xDragSourceRanges = new ScCellRangesObj( pSourceShell, aRanges );
609}
610
611void ScTransferObj::SetDragSourceFlags(ScDragSrc nFlags)
612{
613 m_nDragSourceFlags = nFlags;
614}
615
616void ScTransferObj::SetDragWasInternal()
617{
618 m_bDragWasInternal = true;
619}
620
621void ScTransferObj::SetUseInApi( bool bSet )
622{
623 m_bUseInApi = bSet;
624}
625
626ScDocument* ScTransferObj::GetSourceDocument()
627{
628 ScDocShell* pSourceDocSh = GetSourceDocShell();
629 if (pSourceDocSh)
630 return &pSourceDocSh->GetDocument();
631 return nullptr;
632}
633
634ScDocShell* ScTransferObj::GetSourceDocShell()
635{
636 ScCellRangesBase* pRangesObj = comphelper::getUnoTunnelImplementation<ScCellRangesBase>( m_xDragSourceRanges );
637 if (pRangesObj)
638 return pRangesObj->GetDocShell();
639
640 return nullptr; // none set
641}
642
643ScMarkData ScTransferObj::GetSourceMarkData() const
644{
645 ScMarkData aMarkData(m_pDoc->GetSheetLimits());
646 ScCellRangesBase* pRangesObj = comphelper::getUnoTunnelImplementation<ScCellRangesBase>( m_xDragSourceRanges );
647 if (pRangesObj)
648 {
649 const ScRangeList& rRanges = pRangesObj->GetRangeList();
650 aMarkData.MarkFromRangeList( rRanges, false );
651 }
652 return aMarkData;
653}
654
655// initialize aDocShellRef with a live document from the ClipDoc
656
657// #i123405# added parameter to allow size calculation without limitation
658// to PageSize, e.g. used for Metafile creation for clipboard.
659
660void ScTransferObj::InitDocShell(bool bLimitToPageSize)
661{
662 if ( m_aDocShellRef.is() )
663 return;
664
665 ScDocShell* pDocSh = new ScDocShell;
666 m_aDocShellRef = pDocSh; // ref must be there before InitNew
667
668 pDocSh->DoInitNew();
669
670 ScDocument& rDestDoc = pDocSh->GetDocument();
671 ScMarkData aDestMark(rDestDoc.GetSheetLimits());
672 aDestMark.SelectTable( 0, true );
673
674 rDestDoc.SetDocOptions( m_pDoc->GetDocOptions() ); // #i42666#
675
676 OUString aTabName;
677 m_pDoc->GetName( m_aBlock.aStart.Tab(), aTabName );
678 rDestDoc.RenameTab( 0, aTabName );
679
680 rDestDoc.CopyStdStylesFrom(*m_pDoc);
681
682 SCCOL nStartX = m_aBlock.aStart.Col();
683 SCROW nStartY = m_aBlock.aStart.Row();
684 SCCOL nEndX = m_aBlock.aEnd.Col();
685 SCROW nEndY = m_aBlock.aEnd.Row();
686
687 // widths / heights
688 // (must be copied before CopyFromClip, for drawing objects)
689
690 SCCOL nCol;
691 SCTAB nSrcTab = m_aBlock.aStart.Tab();
692 rDestDoc.SetLayoutRTL(0, m_pDoc->IsLayoutRTL(nSrcTab));
693 for (nCol=nStartX; nCol<=nEndX; nCol++)
694 if ( m_pDoc->ColHidden(nCol, nSrcTab) )
695 rDestDoc.ShowCol( nCol, 0, false );
696 else
697 rDestDoc.SetColWidth( nCol, 0, m_pDoc->GetColWidth( nCol, nSrcTab ) );
698
699 if (nStartY > 0)
700 {
701 // Set manual height for all previous rows so we can ensure
702 // that visible area will not change due to autoheight
703 rDestDoc.SetManualHeight(0, nStartY - 1, 0, true);
704 }
705 for (SCROW nRow = nStartY; nRow <= nEndY; ++nRow)
706 {
707 if ( m_pDoc->RowHidden(nRow, nSrcTab) )
708 rDestDoc.ShowRow( nRow, 0, false );
709 else
710 {
711 rDestDoc.SetRowHeight( nRow, 0, m_pDoc->GetOriginalHeight( nRow, nSrcTab ) );
712
713 // if height was set manually, that flag has to be copied, too
714 bool bManual = m_pDoc->IsManualRowHeight(nRow, nSrcTab);
715 rDestDoc.SetManualHeight(nRow, nRow, 0, bManual);
716 }
717 }
718
719 if (m_pDoc->GetDrawLayer() || m_pDoc->HasNotes())
720 pDocSh->MakeDrawLayer();
721
722 // cell range is copied to the original position, but on the first sheet
723 // -> bCutMode must be set
724 // pDoc is always a Clipboard-document
725
726 ScRange aDestRange( nStartX,nStartY,0, nEndX,nEndY,0 );
727 bool bWasCut = m_pDoc->IsCutMode();
728 if (!bWasCut)
729 m_pDoc->SetClipArea( aDestRange, true ); // Cut
730 rDestDoc.CopyFromClip( aDestRange, aDestMark, InsertDeleteFlags::ALL, nullptr, m_pDoc.get(), false );
731 m_pDoc->SetClipArea( aDestRange, bWasCut );
732
733 StripRefs(*m_pDoc, nStartX,nStartY, nEndX,nEndY, rDestDoc);
734
735 ScRange aMergeRange = aDestRange;
736 rDestDoc.ExtendMerge( aMergeRange, true );
737
738 m_pDoc->CopyDdeLinks( rDestDoc ); // copy values of DDE Links
739
740 // page format (grid etc) and page size (maximum size for ole object)
741
742 Size aPaperSize = SvxPaperInfo::GetPaperSize( PAPER_A4 ); // Twips
743 ScStyleSheetPool* pStylePool = m_pDoc->GetStyleSheetPool();
744 OUString aStyleName = m_pDoc->GetPageStyle( m_aBlock.aStart.Tab() );
745 SfxStyleSheetBase* pStyleSheet = pStylePool->Find( aStyleName, SfxStyleFamily::Page );
746 if (pStyleSheet)
747 {
748 const SfxItemSet& rSourceSet = pStyleSheet->GetItemSet();
749 aPaperSize = rSourceSet.Get(ATTR_PAGE_SIZE).GetSize();
750
751 // CopyStyleFrom copies SetItems with correct pool
752 ScStyleSheetPool* pDestPool = rDestDoc.GetStyleSheetPool();
753 pDestPool->CopyStyleFrom( pStylePool, aStyleName, SfxStyleFamily::Page );
754 }
755
756 ScViewData aViewData( *pDocSh, nullptr );
757 aViewData.SetScreen( nStartX,nStartY, nEndX,nEndY );
758 aViewData.SetCurX( nStartX );
759 aViewData.SetCurY( nStartY );
760
761 rDestDoc.SetViewOptions( m_pDoc->GetViewOptions() );
762
763 // Size
764 //! get while copying sizes
765
766 long nPosX = 0;
767 long nPosY = 0;
768
769 for (nCol=0; nCol<nStartX; nCol++)
770 nPosX += rDestDoc.GetColWidth( nCol, 0 );
771 nPosY += rDestDoc.GetRowHeight( 0, nStartY-1, 0 );
772 nPosX = static_cast<long>( nPosX * HMM_PER_TWIPS((2.54 / (20.0 * 72.0)) * 1000.0) );
773 nPosY = static_cast<long>( nPosY * HMM_PER_TWIPS((2.54 / (20.0 * 72.0)) * 1000.0) );
774
775 aPaperSize.setWidth( aPaperSize.Width() * 2 ); // limit OLE object to double of page size
776 aPaperSize.setHeight( aPaperSize.Height() * 2 );
777
778 long nSizeX = 0;
779 long nSizeY = 0;
780 for (nCol=nStartX; nCol<=nEndX; nCol++)
781 {
782 long nAdd = rDestDoc.GetColWidth( nCol, 0 );
783 if ( bLimitToPageSize && nSizeX+nAdd > aPaperSize.Width() && nSizeX ) // above limit?
784 break;
785 nSizeX += nAdd;
786 }
787 for (SCROW nRow=nStartY; nRow<=nEndY; nRow++)
788 {
789 long nAdd = rDestDoc.GetRowHeight( nRow, 0 );
790 if ( bLimitToPageSize && nSizeY+nAdd > aPaperSize.Height() && nSizeY ) // above limit?
791 break;
792 nSizeY += nAdd;
793 }
794 nSizeX = static_cast<long>( nSizeX * HMM_PER_TWIPS((2.54 / (20.0 * 72.0)) * 1000.0) );
795 nSizeY = static_cast<long>( nSizeY * HMM_PER_TWIPS((2.54 / (20.0 * 72.0)) * 1000.0) );
796
797// pDocSh->SetVisAreaSize( Size(nSizeX,nSizeY) );
798
799 tools::Rectangle aNewArea( Point(nPosX,nPosY), Size(nSizeX,nSizeY) );
800 //TODO/LATER: why twice?!
801 //pDocSh->SvInPlaceObject::SetVisArea( aNewArea );
802 pDocSh->SetVisArea( aNewArea );
803
804 pDocSh->UpdateOle(aViewData, true);
805
806 //! SetDocumentModified?
807 if ( rDestDoc.IsChartListenerCollectionNeedsUpdate() )
808 rDestDoc.UpdateChartListenerCollection();
809}
810
811SfxObjectShell* ScTransferObj::SetDrawClipDoc( bool bAnyOle )
812{
813 // update ScGlobal::xDrawClipDocShellRef
814
815 ScGlobal::xDrawClipDocShellRef.clear();
816 if (bAnyOle)
817 {
818 ScGlobal::xDrawClipDocShellRef = new ScDocShell(SfxModelFlags::EMBEDDED_OBJECT | SfxModelFlags::DISABLE_EMBEDDED_SCRIPTS); // there must be a ref
819 ScGlobal::xDrawClipDocShellRef->DoInitNew();
820 return ScGlobal::xDrawClipDocShellRef.get();
821 }
822 else
823 {
824 ScGlobal::xDrawClipDocShellRef.clear();
825 return nullptr;
826 }
827}
828
829void ScTransferObj::StripRefs( ScDocument& rDoc,
830 SCCOL nStartX, SCROW nStartY, SCCOL nEndX, SCROW nEndY,
831 ScDocument& rDestDoc )
832{
833 // In a clipboard doc the data don't have to be on the first sheet
834
835 SCTAB nSrcTab = 0;
836 while (nSrcTab < rDoc.GetTableCount() && !rDoc.HasTable(nSrcTab))
837 ++nSrcTab;
838 SCTAB nDestTab = 0;
839 while (nDestTab < rDestDoc.GetTableCount() && !rDestDoc.HasTable(nDestTab))
840 ++nDestTab;
841
842 if (!rDoc.HasTable(nSrcTab) || !rDestDoc.HasTable(nDestTab))
843 {
844 OSL_FAIL("Sheet not found in ScTransferObj::StripRefs")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sc/source/ui/app/transobj.cxx"
":" "844" ": "), "%s", "Sheet not found in ScTransferObj::StripRefs"
); } } while (false)
;
845 return;
846 }
847
848 ScRange aRef;
849
850 ScCellIterator aIter( rDoc, ScRange(nStartX, nStartY, nSrcTab, nEndX, nEndY, nSrcTab) );
851 for (bool bHas = aIter.first(); bHas; bHas = aIter.next())
852 {
853 if (aIter.getType() != CELLTYPE_FORMULA)
854 continue;
855
856 ScFormulaCell* pFCell = aIter.getFormulaCell();
857 bool bOut = false;
858 ScDetectiveRefIter aRefIter( rDoc, pFCell );
859 while ( !bOut && aRefIter.GetNextRef( aRef ) )
860 {
861 if ( aRef.aStart.Tab() != nSrcTab || aRef.aEnd.Tab() != nSrcTab ||
862 aRef.aStart.Col() < nStartX || aRef.aEnd.Col() > nEndX ||
863 aRef.aStart.Row() < nStartY || aRef.aEnd.Row() > nEndY )
864 bOut = true;
865 }
866 if (bOut)
867 {
868 SCCOL nCol = aIter.GetPos().Col();
869 SCROW nRow = aIter.GetPos().Row();
870
871 FormulaError nErrCode = pFCell->GetErrCode();
872 ScAddress aPos(nCol, nRow, nDestTab);
873 if (nErrCode != FormulaError::NONE)
874 {
875 if ( rDestDoc.GetAttr( nCol,nRow,nDestTab, ATTR_HOR_JUSTIFY)->GetValue() ==
876 SvxCellHorJustify::Standard )
877 rDestDoc.ApplyAttr( nCol,nRow,nDestTab,
878 SvxHorJustifyItem(SvxCellHorJustify::Right, ATTR_HOR_JUSTIFY) );
879
880 ScSetStringParam aParam;
881 aParam.setTextInput();
882 rDestDoc.SetString(aPos, ScGlobal::GetErrorString(nErrCode), &aParam);
883 }
884 else if (pFCell->IsValue())
885 {
886 rDestDoc.SetValue(aPos, pFCell->GetValue());
887 }
888 else
889 {
890 OUString aStr = pFCell->GetString().getString();
891 if ( pFCell->IsMultilineResult() )
892 {
893 ScFieldEditEngine& rEngine = rDestDoc.GetEditEngine();
894 rEngine.SetTextCurrentDefaults(aStr);
895 rDestDoc.SetEditText(ScAddress(nCol,nRow,nDestTab), rEngine.CreateTextObject());
896 }
897 else
898 {
899 ScSetStringParam aParam;
900 aParam.setTextInput();
901 rDestDoc.SetString(aPos, aStr, &aParam);
902 }
903 }
904 }
905 }
906}
907
908namespace
909{
910 class theScTransferUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theScTransferUnoTunnelId> {};
911}
912
913const css::uno::Sequence< sal_Int8 >& ScTransferObj::getUnoTunnelId()
914{
915 return theScTransferUnoTunnelId::get().getSeq();
916}
917
918sal_Int64 SAL_CALL ScTransferObj::getSomething( const css::uno::Sequence< sal_Int8 >& rId )
919{
920 sal_Int64 nRet;
921 if( isUnoTunnelId<ScTransferObj>(rId) )
922 {
923 nRet = reinterpret_cast< sal_Int64 >( this );
924 }
925 else
926 nRet = TransferDataContainer::getSomething(rId);
927 return nRet;
928}
929
930/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx

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#ifndef INCLUDED_VCL_PTR_HXX
21#define INCLUDED_VCL_PTR_HXX
22
23#include <sal/config.h>
24
25#include <rtl/ref.hxx>
26
27#include <utility>
28#include <type_traits>
29
30#ifdef DBG_UTIL
31#ifndef _WIN32
32#include <vcl/vclmain.hxx>
33#endif
34#endif
35
36class VclReferenceBase;
37
38namespace vcl::detail {
39
40template<typename>
41constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
42
43template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase(
44 int (*)[sizeof(T)])
45{ return std::is_base_of<VclReferenceBase, T>::value; }
46
47} // namespace vcl::detail
48
49/**
50 * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses.
51 *
52 * For more details on the design please see vcl/README.lifecycle
53 *
54 * @param reference_type must be a subclass of vcl::Window
55 */
56template <class reference_type>
57class VclPtr
58{
59 static_assert(
60 vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>(
61 nullptr),
62 "template argument type must be derived from VclReferenceBase");
63
64 ::rtl::Reference<reference_type> m_rInnerRef;
65
66public:
67 /** Constructor...
68 */
69 VclPtr()
70 : m_rInnerRef()
71 {}
72
73 /** Constructor...
74 */
75 VclPtr (reference_type * pBody)
76 : m_rInnerRef(pBody)
77 {}
78
79 /** Constructor... that doesn't take a ref.
80 */
81 VclPtr (reference_type * pBody, __sal_NoAcquire)
82 : m_rInnerRef(pBody, SAL_NO_ACQUIRE)
83 {}
84
85 /** Up-casting conversion constructor: Copies interface reference.
86
87 Does not work for up-casts to ambiguous bases. For the special case of
88 up-casting to Reference< XInterface >, see the corresponding conversion
89 operator.
90
91 @param rRef another reference
92 */
93 template< class derived_type >
94 VclPtr(
95 const VclPtr< derived_type > & rRef,
96 typename std::enable_if<
97 std::is_base_of<reference_type, derived_type>::value, int>::type
98 = 0 )
99 : m_rInnerRef( static_cast<reference_type*>(rRef) )
100 {
101 }
102
103#if defined(DBG_UTIL) && !defined(_WIN32)
104 virtual ~VclPtr()
105 {
106 assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain
::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 106, __extension__ __PRETTY_FUNCTION__))
;
107 // We can be one of the intermediate counts, but if we are the last
108 // VclPtr keeping this object alive, then something forgot to call dispose().
109 assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
110 && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef
->isDisposed() || m_rInnerRef->getRefCount() > 1) &&
"someone forgot to call dispose()") ? void (0) : __assert_fail
("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\""
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 110, __extension__ __PRETTY_FUNCTION__))
;
111 }
112 VclPtr(VclPtr const &) = default;
113 VclPtr(VclPtr &&) = default;
114 VclPtr & operator =(VclPtr const &) = default;
115 VclPtr & operator =(VclPtr &&) = default;
116#endif
117
118 /**
119 * A construction helper for VclPtr. Since VclPtr types are created
120 * with a reference-count of one - to help fit into the existing
121 * code-flow; this helps us to construct them easily.
122 *
123 * For more details on the design please see vcl/README.lifecycle
124 *
125 * @tparam reference_type must be a subclass of vcl::Window
126 */
127 template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg)
128 {
129 return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE );
130 }
131
132 /** Probably most common used: handle->someBodyOp().
133 */
134 reference_type * operator->() const
135 {
136 return m_rInnerRef.get();
137 }
138
139 /** Get the body. Can be used instead of operator->().
140 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
141 are the same.
142 */
143 reference_type * get() const
144 {
145 return m_rInnerRef.get();
146 }
147
148 void set(reference_type *pBody)
149 {
150 m_rInnerRef.set(pBody);
151 }
152
153 void reset(reference_type *pBody)
154 {
155 m_rInnerRef.set(pBody);
156 }
157
158 /** Up-casting copy assignment operator.
159
160 Does not work for up-casts to ambiguous bases.
161
162 @param rRef another reference
163 */
164 template<typename derived_type>
165 typename std::enable_if<
166 std::is_base_of<reference_type, derived_type>::value,
167 VclPtr &>::type
168 operator =(VclPtr<derived_type> const & rRef)
169 {
170 m_rInnerRef.set(rRef.get());
171 return *this;
172 }
173
174 VclPtr & operator =(reference_type * pBody)
175 {
176 m_rInnerRef.set(pBody);
177 return *this;
178 }
179
180 operator reference_type * () const
181 {
182 return m_rInnerRef.get();
183 }
184
185 explicit operator bool () const
186 {
187 return m_rInnerRef.get() != nullptr;
188 }
189
190 void clear()
191 {
192 m_rInnerRef.clear();
193 }
194
195 void reset()
196 {
197 m_rInnerRef.clear();
198 }
199
200 void disposeAndClear()
201 {
202 // hold it alive for the lifetime of this method
203 ::rtl::Reference<reference_type> aTmp(m_rInnerRef);
204 m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-)
24
Calling 'Reference::clear'
31
Returning; memory was released
205 if (aTmp.get()) {
32
Calling 'Reference::get'
206 aTmp->disposeOnce();
207 }
208 }
209
210 /** Needed to place VclPtr's into STL collection.
211 */
212 bool operator< (const VclPtr<reference_type> & handle) const
213 {
214 return (m_rInnerRef < handle.m_rInnerRef);
215 }
216}; // class VclPtr
217
218template<typename T1, typename T2>
219inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
220 return p1.get() == p2.get();
221}
222
223template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2)
224{
225 return p1.get() == p2;
226}
227
228template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) {
229 return p1.get() == p2;
230}
231
232template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2)
233{
234 return p1 == p2.get();
235}
236
237template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) {
238 return p1 == p2.get();
239}
240
241template<typename T1, typename T2>
242inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
243 return !(p1 == p2);
244}
245
246template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2)
247{
248 return !(p1 == p2);
249}
250
251template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) {
252 return !(p1 == p2);
253}
254
255template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2)
256{
257 return !(p1 == p2);
258}
259
260template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) {
261 return !(p1 == p2);
262}
263
264/**
265 * A construction helper for a temporary VclPtr. Since VclPtr types
266 * are created with a reference-count of one - to help fit into
267 * the existing code-flow; this helps us to construct them easily.
268 * see also VclPtr::Create and ScopedVclPtr
269 *
270 * For more details on the design please see vcl/README.lifecycle
271 *
272 * @param reference_type must be a subclass of vcl::Window
273 */
274template <class reference_type>
275class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type>
276{
277public:
278 template<typename... Arg> VclPtrInstance(Arg &&... arg)
279 : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
280 {
281 }
282
283 /**
284 * Override and disallow this, to prevent people accidentally calling it and actually
285 * getting VclPtr::Create and getting a naked VclPtr<> instance
286 */
287 template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete;
288};
289
290template <class reference_type>
291class ScopedVclPtr : public VclPtr<reference_type>
292{
293public:
294 /** Constructor...
295 */
296 ScopedVclPtr()
297 : VclPtr<reference_type>()
298 {}
299
300 /** Constructor
301 */
302 ScopedVclPtr (reference_type * pBody)
303 : VclPtr<reference_type>(pBody)
304 {}
305
306 /** Copy constructor...
307 */
308 ScopedVclPtr (const VclPtr<reference_type> & handle)
309 : VclPtr<reference_type>(handle)
310 {}
311
312 /**
313 Assignment that releases the last reference.
314 */
315 void disposeAndReset(reference_type *pBody)
316 {
317 if (pBody != this->get()) {
318 VclPtr<reference_type>::disposeAndClear();
319 VclPtr<reference_type>::set(pBody);
320 }
321 }
322
323 /**
324 Assignment that releases the last reference.
325 */
326 ScopedVclPtr<reference_type>& operator = (reference_type * pBody)
327 {
328 disposeAndReset(pBody);
329 return *this;
330 }
331
332 /** Up-casting conversion constructor: Copies interface reference.
333
334 Does not work for up-casts to ambiguous bases. For the special case of
335 up-casting to Reference< XInterface >, see the corresponding conversion
336 operator.
337
338 @param rRef another reference
339 */
340 template< class derived_type >
341 ScopedVclPtr(
342 const VclPtr< derived_type > & rRef,
343 typename std::enable_if<
344 std::is_base_of<reference_type, derived_type>::value, int>::type
345 = 0 )
346 : VclPtr<reference_type>( rRef )
347 {
348 }
349
350 /** Up-casting assignment operator.
351
352 Does not work for up-casts to ambiguous bases.
353
354 @param rRef another VclPtr
355 */
356 template<typename derived_type>
357 typename std::enable_if<
358 std::is_base_of<reference_type, derived_type>::value,
359 ScopedVclPtr &>::type
360 operator =(VclPtr<derived_type> const & rRef)
361 {
362 disposeAndReset(rRef.get());
363 return *this;
364 }
365
366 /**
367 * Override and disallow this, to prevent people accidentally calling it and actually
368 * getting VclPtr::Create and getting a naked VclPtr<> instance
369 */
370 template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete;
371
372 ~ScopedVclPtr()
373 {
374 VclPtr<reference_type>::disposeAndClear();
23
Calling 'VclPtr::disposeAndClear'
375 assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get(
) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr"
, "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx"
, 375, __extension__ __PRETTY_FUNCTION__))
; // make sure there are no lingering references
376 }
377
378private:
379 // Most likely we don't want this default copy-constructor.
380 ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete;
381 // And certainly we don't want a default assignment operator.
382 ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete;
383 // And disallow reset as that doesn't call disposeAndClear on the original reference
384 void reset() = delete;
385 void reset(reference_type *pBody) = delete;
386
387protected:
388 ScopedVclPtr (reference_type * pBody, __sal_NoAcquire)
389 : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE)
390 {}
391};
392
393/**
394 * A construction helper for ScopedVclPtr. Since VclPtr types are created
395 * with a reference-count of one - to help fit into the existing
396 * code-flow; this helps us to construct them easily.
397 *
398 * For more details on the design please see vcl/README.lifecycle
399 *
400 * @param reference_type must be a subclass of vcl::Window
401 */
402#if defined _MSC_VER
403#pragma warning(push)
404#pragma warning(disable: 4521) // " multiple copy constructors specified"
405#endif
406template <class reference_type>
407class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type>
408{
409public:
410 template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg)
411 : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE )
19
Memory is allocated
412 {
413 }
414
415 /**
416 * Override and disallow this, to prevent people accidentally calling it and actually
417 * getting VclPtr::Create and getting a naked VclPtr<> instance
418 */
419 template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete;
420
421private:
422 // Prevent the above perfect forwarding ctor from hijacking (accidental)
423 // attempts at ScopedVclPtrInstance copy construction (where the hijacking
424 // would typically lead to somewhat obscure error messages); both non-const
425 // and const variants are needed here, as the ScopedVclPtr base class has a
426 // const--variant copy ctor, so the implicitly declared copy ctor for
427 // ScopedVclPtrInstance would also be the const variant, so non-const copy
428 // construction attempts would be hijacked by the perfect forwarding ctor;
429 // but if we only declared a non-const variant here, the const variant would
430 // no longer be implicitly declared (as there would already be an explicitly
431 // declared copy ctor), so const copy construction attempts would then be
432 // hijacked by the perfect forwarding ctor:
433 ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete;
434 ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete;
435};
436#if defined _MSC_VER
437#pragma warning(pop)
438#endif
439
440#endif // INCLUDED_VCL_PTR_HXX
441
442/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY1
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
48 /** Constructor...
49 */
50 Reference()
51 : m_pBody (NULL__null)
52 {}
53
54
55 /** Constructor...
56 */
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
62 /** Constructor...
63 */
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
71 /** Copy constructor...
72 */
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY1
81 /** Move constructor...
82 */
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY1
91 /** Up-casting conversion constructor: Copies interface reference.
92
93 Does not work for up-casts to ambiguous bases.
94
95 @param rRef another reference
96 */
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
108 /** Destructor...
109 */
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody)
113 m_pBody->release();
114 }
115
116 /** Set...
117 Similar to assignment.
118 */
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld)
127 pOld->release();
128 return *this;
129 }
130
131 /** Assignment.
132 Unbinds this instance from its body (if bound) and
133 bind it to the body represented by the handle.
134 */
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
139 }
140
141#ifdef LIBO_INTERNAL_ONLY1
142 /** Assignment.
143 * Unbinds this instance from its body (if bound),
144 * bind it to the body represented by the handle, and
145 * set the body represented by the handle to nullptr.
146 */
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
159 /** Assignment...
160 */
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
167 /** Unbind the body from this handle.
168 Note that for a handle representing a large body,
169 "handle.clear().set(new body());" _might_
170 perform a little bit better than "handle.set(new body());",
171 since in the second case two large objects exist in memory
172 (the old body and the new body).
173 */
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody
24.1
Field 'm_pBody' is non-null
24.1
Field 'm_pBody' is non-null
24.1
Field 'm_pBody' is non-null
24.1
Field 'm_pBody' is non-null
)
25
Taking true branch
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
26
Calling 'VclReferenceBase::release'
30
Returning; memory was released
181 }
182 return *this;
183 }
184
185
186 /** Get the body. Can be used instead of operator->().
187 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
188 are the same.
189 */
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
33
Use of memory after it is freed
193 }
194
195
196 /** Probably most common used: handle->someBodyOp().
197 */
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 200, __extension__ __PRETTY_FUNCTION__))
;
201 return m_pBody;
202 }
203
204
205 /** Allows (*handle).someBodyOp().
206 */
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 209, __extension__ __PRETTY_FUNCTION__))
;
210 return *m_pBody;
211 }
212
213
214 /** Returns True if the handle does point to a valid body.
215 */
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL__null);
219 }
220
221#if defined LIBO_INTERNAL_ONLY1
222 /** Returns True if the handle does point to a valid body.
223 */
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
230 /** Returns True if this points to pBody.
231 */
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
238 /** Returns True if handle points to the same body.
239 */
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
247 /** Needed to place References into STL collection.
248 */
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
256 /** Needed to place References into STL collection.
257 */
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
265 /** Needed to place References into STL collection.
266 */
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY1
277namespace std
278{
279
280/// @cond INTERNAL
281/**
282 Make rtl::Reference hashable by default for use in STL containers.
283
284 @since LibreOffice 6.3
285*/
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
292/// @endcond
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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#ifndef INCLUDED_VCL_Reference_HXX
20#define INCLUDED_VCL_Reference_HXX
21
22#include <vcl/dllapi.h>
23#include <osl/interlck.h>
24
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
32 void acquire() const
33 {
34 osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1);
35 }
36
37 void release() const
38 {
39 if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0)
27
Assuming the condition is true
28
Taking true branch
40 delete this;
29
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif