File: | home/maarten/src/libreoffice/core/vcl/source/window/scrwnd.cxx |
Warning: | line 385, column 10 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 <limits.h> | |||
21 | ||||
22 | #include <o3tl/float_int_conversion.hxx> | |||
23 | #include <tools/time.hxx> | |||
24 | ||||
25 | #include <bitmaps.hlst> | |||
26 | #include <svdata.hxx> | |||
27 | #include <scrwnd.hxx> | |||
28 | ||||
29 | #include <vcl/timer.hxx> | |||
30 | #include <vcl/commandevent.hxx> | |||
31 | #include <vcl/event.hxx> | |||
32 | #include <vcl/ptrstyle.hxx> | |||
33 | #include <sal/log.hxx> | |||
34 | ||||
35 | #include <math.h> | |||
36 | ||||
37 | #define WHEEL_WIDTH25 25 | |||
38 | #define WHEEL_RADIUS((25) >> 1 ) ((WHEEL_WIDTH25) >> 1 ) | |||
39 | #define MAX_TIME300 300 | |||
40 | #define MIN_TIME20 20 | |||
41 | #define DEF_TIMEOUT50 50 | |||
42 | ||||
43 | ImplWheelWindow::ImplWheelWindow( vcl::Window* pParent ) : | |||
44 | FloatingWindow ( pParent, 0 ), | |||
45 | mnRepaintTime ( 1 ), | |||
46 | mnTimeout ( DEF_TIMEOUT50 ), | |||
47 | mnWheelMode ( WheelMode::NONE ), | |||
48 | mnActDist ( 0 ), | |||
49 | mnActDeltaX ( 0 ), | |||
50 | mnActDeltaY ( 0 ) | |||
51 | { | |||
52 | // we need a parent | |||
53 | SAL_WARN_IF( !pParent, "vcl", "ImplWheelWindow::ImplWheelWindow(): Parent not set!" )do { if (true && (!pParent)) { switch (sal_detail_log_report (::SAL_DETAIL_LOG_LEVEL_WARN, "vcl")) { case SAL_DETAIL_LOG_ACTION_IGNORE : break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail ::getResult( ::sal::detail::StreamStart() << "ImplWheelWindow::ImplWheelWindow(): Parent not set!" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/window/scrwnd.cxx" ":" "53" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "ImplWheelWindow::ImplWheelWindow(): Parent not set!" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "ImplWheelWindow::ImplWheelWindow(): Parent not set!" ; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/window/scrwnd.cxx" ":" "53" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "ImplWheelWindow::ImplWheelWindow(): Parent not set!" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl" ), ("/home/maarten/src/libreoffice/core/vcl/source/window/scrwnd.cxx" ":" "53" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "ImplWheelWindow::ImplWheelWindow(): Parent not set!" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "ImplWheelWindow::ImplWheelWindow(): Parent not set!" ; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("vcl"), ("/home/maarten/src/libreoffice/core/vcl/source/window/scrwnd.cxx" ":" "53" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | |||
54 | ||||
55 | const Size aSize( pParent->GetOutputSizePixel() ); | |||
56 | const StartAutoScrollFlags nFlags = ImplGetSVData()->mpWinData->mnAutoScrollFlags; | |||
57 | const bool bHorz( nFlags & StartAutoScrollFlags::Horz ); | |||
58 | const bool bVert( nFlags & StartAutoScrollFlags::Vert ); | |||
59 | ||||
60 | // calculate maximum speed distance | |||
61 | mnMaxWidth = static_cast<sal_uLong>( 0.4 * hypot( static_cast<double>(aSize.Width()), aSize.Height() ) ); | |||
62 | ||||
63 | // create wheel window | |||
64 | SetTitleType( FloatWinTitleType::NONE ); | |||
65 | ImplCreateImageList(); | |||
66 | BitmapEx aBmp(SV_RESID_BITMAP_SCROLLMSK"vcl/res/scrmsk.png"); | |||
67 | ImplSetRegion(aBmp.GetBitmap()); | |||
68 | ||||
69 | // set wheel mode | |||
70 | if( bHorz && bVert ) | |||
71 | ImplSetWheelMode( WheelMode::VH ); | |||
72 | else if( bHorz ) | |||
73 | ImplSetWheelMode( WheelMode::H ); | |||
74 | else | |||
75 | ImplSetWheelMode( WheelMode::V ); | |||
76 | ||||
77 | // init timer | |||
78 | mpTimer.reset(new Timer("WheelWindowTimer")); | |||
79 | mpTimer->SetInvokeHandler( LINK( this, ImplWheelWindow, ImplScrollHdl )::tools::detail::makeLink( ::tools::detail::castTo<ImplWheelWindow *>(this), &ImplWheelWindow::LinkStubImplScrollHdl) ); | |||
80 | mpTimer->SetTimeout( mnTimeout ); | |||
81 | mpTimer->Start(); | |||
82 | ||||
83 | CaptureMouse(); | |||
84 | } | |||
85 | ||||
86 | ImplWheelWindow::~ImplWheelWindow() | |||
87 | { | |||
88 | disposeOnce(); | |||
89 | } | |||
90 | ||||
91 | void ImplWheelWindow::dispose() | |||
92 | { | |||
93 | ImplStop(); | |||
94 | mpTimer.reset(); | |||
95 | FloatingWindow::dispose(); | |||
96 | } | |||
97 | ||||
98 | void ImplWheelWindow::ImplStop() | |||
99 | { | |||
100 | ReleaseMouse(); | |||
101 | mpTimer->Stop(); | |||
102 | Show(false); | |||
103 | } | |||
104 | ||||
105 | void ImplWheelWindow::ImplSetRegion( const Bitmap& rRegionBmp ) | |||
106 | { | |||
107 | Point aPos( GetPointerPosPixel() ); | |||
108 | const Size aSize( rRegionBmp.GetSizePixel() ); | |||
109 | const tools::Rectangle aRect( Point(), aSize ); | |||
110 | ||||
111 | maCenter = maLastMousePos = aPos; | |||
112 | aPos.AdjustX( -(aSize.Width() >> 1) ); | |||
113 | aPos.AdjustY( -(aSize.Height() >> 1) ); | |||
114 | ||||
115 | SetPosSizePixel( aPos, aSize ); | |||
116 | SetWindowRegionPixel( rRegionBmp.CreateRegion( COL_BLACK, aRect ) ); | |||
117 | } | |||
118 | ||||
119 | void ImplWheelWindow::ImplCreateImageList() | |||
120 | { | |||
121 | maImgList.emplace_back(Image(StockImage::Yes, SV_RESID_BITMAP_SCROLLVH"vcl/res/scrollvh.png")); | |||
122 | maImgList.emplace_back(Image(StockImage::Yes, SV_RESID_BITMAP_SCROLLV"vcl/res/scrollv.png")); | |||
123 | maImgList.emplace_back(Image(StockImage::Yes, SV_RESID_BITMAP_SCROLLH"vcl/res/scrollh.png")); | |||
124 | maImgList.emplace_back(Image(StockImage::Yes, SV_RESID_BITMAP_WHEELVH"vcl/res/wheelvh.png")); | |||
125 | maImgList.emplace_back(Image(StockImage::Yes, SV_RESID_BITMAP_WHEELV"vcl/res/wheelv.png")); | |||
126 | maImgList.emplace_back(Image(StockImage::Yes, SV_RESID_BITMAP_WHEELH"vcl/res/wheelh.png")); | |||
127 | } | |||
128 | ||||
129 | void ImplWheelWindow::ImplSetWheelMode( WheelMode nWheelMode ) | |||
130 | { | |||
131 | if( nWheelMode == mnWheelMode ) | |||
132 | return; | |||
133 | ||||
134 | mnWheelMode = nWheelMode; | |||
135 | ||||
136 | if( WheelMode::NONE == mnWheelMode ) | |||
137 | { | |||
138 | if( IsVisible() ) | |||
139 | Hide(); | |||
140 | } | |||
141 | else | |||
142 | { | |||
143 | if( !IsVisible() ) | |||
144 | Show(); | |||
145 | ||||
146 | Invalidate(); | |||
147 | } | |||
148 | } | |||
149 | ||||
150 | void ImplWheelWindow::ImplDrawWheel(vcl::RenderContext& rRenderContext) | |||
151 | { | |||
152 | int nIndex; | |||
153 | ||||
154 | switch (mnWheelMode) | |||
155 | { | |||
156 | case WheelMode::VH: | |||
157 | nIndex = 0; | |||
158 | break; | |||
159 | case WheelMode::V: | |||
160 | nIndex = 1; | |||
161 | break; | |||
162 | case WheelMode::H: | |||
163 | nIndex = 2; | |||
164 | break; | |||
165 | case WheelMode::ScrollVH: | |||
166 | nIndex = 3; | |||
167 | break; | |||
168 | case WheelMode::ScrollV: | |||
169 | nIndex = 4; | |||
170 | break; | |||
171 | case WheelMode::ScrollH: | |||
172 | nIndex = 5; | |||
173 | break; | |||
174 | default: | |||
175 | nIndex = -1; | |||
176 | break; | |||
177 | } | |||
178 | ||||
179 | if (nIndex >= 0) | |||
180 | rRenderContext.DrawImage(Point(), maImgList[nIndex]); | |||
181 | } | |||
182 | ||||
183 | void ImplWheelWindow::ImplRecalcScrollValues() | |||
184 | { | |||
185 | if( mnActDist < WHEEL_RADIUS((25) >> 1 ) ) | |||
186 | { | |||
187 | mnActDeltaX = mnActDeltaY = 0; | |||
188 | mnTimeout = DEF_TIMEOUT50; | |||
189 | } | |||
190 | else | |||
191 | { | |||
192 | sal_uInt64 nCurTime; | |||
193 | ||||
194 | // calc current time | |||
195 | if( mnMaxWidth ) | |||
196 | { | |||
197 | const double fExp = ( static_cast<double>(mnActDist) / mnMaxWidth ) * log10( double(MAX_TIME300) / MIN_TIME20 ); | |||
198 | nCurTime = static_cast<sal_uInt64>( MAX_TIME300 / pow( 10., fExp ) ); | |||
199 | } | |||
200 | else | |||
201 | nCurTime = MAX_TIME300; | |||
202 | ||||
203 | if( !nCurTime ) | |||
204 | nCurTime = 1; | |||
205 | ||||
206 | if( mnRepaintTime <= nCurTime ) | |||
207 | mnTimeout = nCurTime - mnRepaintTime; | |||
208 | else | |||
209 | { | |||
210 | sal_uInt64 nMult = mnRepaintTime / nCurTime; | |||
211 | ||||
212 | if( !( mnRepaintTime % nCurTime ) ) | |||
213 | mnTimeout = 0; | |||
214 | else | |||
215 | mnTimeout = ++nMult * nCurTime - mnRepaintTime; | |||
216 | ||||
217 | double fValX = static_cast<double>(mnActDeltaX) * nMult; | |||
218 | double fValY = static_cast<double>(mnActDeltaY) * nMult; | |||
219 | ||||
220 | if( !o3tl::convertsToAtMost(fValX, LONG_MAX9223372036854775807L) ) | |||
221 | mnActDeltaX = LONG_MAX9223372036854775807L; | |||
222 | else if( !o3tl::convertsToAtLeast(fValX, LONG_MIN(-9223372036854775807L -1L)) ) | |||
223 | mnActDeltaX = LONG_MIN(-9223372036854775807L -1L); | |||
224 | else | |||
225 | mnActDeltaX = static_cast<long>(fValX); | |||
226 | ||||
227 | if( !o3tl::convertsToAtMost(fValY, LONG_MAX9223372036854775807L) ) | |||
228 | mnActDeltaY = LONG_MAX9223372036854775807L; | |||
229 | else if( !o3tl::convertsToAtLeast(fValY, LONG_MIN(-9223372036854775807L -1L)) ) | |||
230 | mnActDeltaY = LONG_MIN(-9223372036854775807L -1L); | |||
231 | else | |||
232 | mnActDeltaY = static_cast<long>(fValY); | |||
233 | } | |||
234 | } | |||
235 | } | |||
236 | ||||
237 | PointerStyle ImplWheelWindow::ImplGetMousePointer( long nDistX, long nDistY ) | |||
238 | { | |||
239 | PointerStyle eStyle; | |||
240 | const StartAutoScrollFlags nFlags = ImplGetSVData()->mpWinData->mnAutoScrollFlags; | |||
241 | const bool bHorz( nFlags & StartAutoScrollFlags::Horz ); | |||
242 | const bool bVert( nFlags & StartAutoScrollFlags::Vert ); | |||
243 | ||||
244 | if( bHorz || bVert ) | |||
245 | { | |||
246 | if( mnActDist < WHEEL_RADIUS((25) >> 1 ) ) | |||
247 | { | |||
248 | if( bHorz && bVert ) | |||
249 | eStyle = PointerStyle::AutoScrollNSWE; | |||
250 | else if( bHorz ) | |||
251 | eStyle = PointerStyle::AutoScrollWE; | |||
252 | else | |||
253 | eStyle = PointerStyle::AutoScrollNS; | |||
254 | } | |||
255 | else | |||
256 | { | |||
257 | double fAngle = basegfx::rad2deg(atan2(static_cast<double>(-nDistY), nDistX)); | |||
258 | ||||
259 | if( fAngle < 0.0 ) | |||
260 | fAngle += 360.; | |||
261 | ||||
262 | if( bHorz && bVert ) | |||
263 | { | |||
264 | if( fAngle >= 22.5 && fAngle <= 67.5 ) | |||
265 | eStyle = PointerStyle::AutoScrollNE; | |||
266 | else if( fAngle >= 67.5 && fAngle <= 112.5 ) | |||
267 | eStyle = PointerStyle::AutoScrollN; | |||
268 | else if( fAngle >= 112.5 && fAngle <= 157.5 ) | |||
269 | eStyle = PointerStyle::AutoScrollNW; | |||
270 | else if( fAngle >= 157.5 && fAngle <= 202.5 ) | |||
271 | eStyle = PointerStyle::AutoScrollW; | |||
272 | else if( fAngle >= 202.5 && fAngle <= 247.5 ) | |||
273 | eStyle = PointerStyle::AutoScrollSW; | |||
274 | else if( fAngle >= 247.5 && fAngle <= 292.5 ) | |||
275 | eStyle = PointerStyle::AutoScrollS; | |||
276 | else if( fAngle >= 292.5 && fAngle <= 337.5 ) | |||
277 | eStyle = PointerStyle::AutoScrollSE; | |||
278 | else | |||
279 | eStyle = PointerStyle::AutoScrollE; | |||
280 | } | |||
281 | else if( bHorz ) | |||
282 | { | |||
283 | if( fAngle >= 270. || fAngle <= 90. ) | |||
284 | eStyle = PointerStyle::AutoScrollE; | |||
285 | else | |||
286 | eStyle = PointerStyle::AutoScrollW; | |||
287 | } | |||
288 | else | |||
289 | { | |||
290 | if( fAngle >= 0. && fAngle <= 180. ) | |||
291 | eStyle = PointerStyle::AutoScrollN; | |||
292 | else | |||
293 | eStyle = PointerStyle::AutoScrollS; | |||
294 | } | |||
295 | } | |||
296 | } | |||
297 | else | |||
298 | eStyle = PointerStyle::Arrow; | |||
299 | ||||
300 | return eStyle; | |||
301 | } | |||
302 | ||||
303 | void ImplWheelWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&) | |||
304 | { | |||
305 | ImplDrawWheel(rRenderContext); | |||
306 | } | |||
307 | ||||
308 | void ImplWheelWindow::MouseMove( const MouseEvent& rMEvt ) | |||
309 | { | |||
310 | FloatingWindow::MouseMove( rMEvt ); | |||
311 | ||||
312 | const Point aMousePos( OutputToScreenPixel( rMEvt.GetPosPixel() ) ); | |||
313 | const long nDistX = aMousePos.X() - maCenter.X(); | |||
314 | const long nDistY = aMousePos.Y() - maCenter.Y(); | |||
315 | ||||
316 | mnActDist = static_cast<sal_uLong>(hypot( static_cast<double>(nDistX), nDistY )); | |||
317 | ||||
318 | const PointerStyle eActStyle = ImplGetMousePointer( nDistX, nDistY ); | |||
319 | const StartAutoScrollFlags nFlags = ImplGetSVData()->mpWinData->mnAutoScrollFlags; | |||
320 | const bool bHorz( nFlags & StartAutoScrollFlags::Horz ); | |||
321 | const bool bVert( nFlags & StartAutoScrollFlags::Vert ); | |||
322 | const bool bOuter = mnActDist > WHEEL_RADIUS((25) >> 1 ); | |||
323 | ||||
324 | if( bOuter && ( maLastMousePos != aMousePos ) ) | |||
325 | { | |||
326 | switch( eActStyle ) | |||
327 | { | |||
328 | case PointerStyle::AutoScrollN: mnActDeltaX = +0; mnActDeltaY = +1; break; | |||
329 | case PointerStyle::AutoScrollS: mnActDeltaX = +0; mnActDeltaY = -1; break; | |||
330 | case PointerStyle::AutoScrollW: mnActDeltaX = +1; mnActDeltaY = +0; break; | |||
331 | case PointerStyle::AutoScrollE: mnActDeltaX = -1; mnActDeltaY = +0; break; | |||
332 | case PointerStyle::AutoScrollNW: mnActDeltaX = +1; mnActDeltaY = +1; break; | |||
333 | case PointerStyle::AutoScrollNE: mnActDeltaX = -1; mnActDeltaY = +1; break; | |||
334 | case PointerStyle::AutoScrollSW: mnActDeltaX = +1; mnActDeltaY = -1; break; | |||
335 | case PointerStyle::AutoScrollSE: mnActDeltaX = -1; mnActDeltaY = -1; break; | |||
336 | ||||
337 | default: | |||
338 | break; | |||
339 | } | |||
340 | } | |||
341 | ||||
342 | ImplRecalcScrollValues(); | |||
343 | maLastMousePos = aMousePos; | |||
344 | SetPointer( eActStyle ); | |||
345 | ||||
346 | if( bHorz && bVert ) | |||
347 | ImplSetWheelMode( bOuter ? WheelMode::ScrollVH : WheelMode::VH ); | |||
348 | else if( bHorz ) | |||
349 | ImplSetWheelMode( bOuter ? WheelMode::ScrollH : WheelMode::H ); | |||
350 | else | |||
351 | ImplSetWheelMode( bOuter ? WheelMode::ScrollV : WheelMode::V ); | |||
352 | } | |||
353 | ||||
354 | void ImplWheelWindow::MouseButtonUp( const MouseEvent& rMEvt ) | |||
355 | { | |||
356 | if( mnActDist > WHEEL_RADIUS((25) >> 1 ) ) | |||
357 | GetParent()->EndAutoScroll(); | |||
358 | else | |||
359 | FloatingWindow::MouseButtonUp( rMEvt ); | |||
360 | } | |||
361 | ||||
362 | IMPL_LINK_NOARG(ImplWheelWindow, ImplScrollHdl, Timer *, void)void ImplWheelWindow::LinkStubImplScrollHdl(void * instance, Timer * data) { return static_cast<ImplWheelWindow *>(instance )->ImplScrollHdl(data); } void ImplWheelWindow::ImplScrollHdl (__attribute__ ((unused)) Timer *) | |||
| ||||
363 | { | |||
364 | if ( mnActDeltaX || mnActDeltaY ) | |||
365 | { | |||
366 | vcl::Window* pWindow = GetParent(); | |||
367 | const Point aMousePos( pWindow->OutputToScreenPixel( pWindow->GetPointerPosPixel() ) ); | |||
368 | Point aCmdMousePos( pWindow->ImplFrameToOutput( aMousePos ) ); | |||
369 | CommandScrollData aScrollData( mnActDeltaX, mnActDeltaY ); | |||
370 | CommandEvent aCEvt( aCmdMousePos, CommandEventId::AutoScroll, true, &aScrollData ); | |||
371 | NotifyEvent aNCmdEvt( MouseNotifyEvent::COMMAND, pWindow, &aCEvt ); | |||
372 | ||||
373 | if ( !ImplCallPreNotify( aNCmdEvt ) ) | |||
374 | { | |||
375 | const sal_uInt64 nTime = tools::Time::GetSystemTicks(); | |||
376 | VclPtr<ImplWheelWindow> xWin(this); | |||
377 | pWindow->Command( aCEvt ); | |||
378 | if( xWin->IsDisposed() ) | |||
379 | return; | |||
380 | mnRepaintTime = std::max( tools::Time::GetSystemTicks() - nTime, sal_uInt64(1) ); | |||
381 | ImplRecalcScrollValues(); | |||
382 | } | |||
383 | } | |||
384 | ||||
385 | if ( mnTimeout != mpTimer->GetTimeout() ) | |||
| ||||
386 | mpTimer->SetTimeout( mnTimeout ); | |||
387 | mpTimer->Start(); | |||
388 | } | |||
389 | ||||
390 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | |||||||
34 | namespace rtl | ||||||
35 | { | ||||||
36 | |||||||
37 | /** Template reference class for reference type. | ||||||
38 | */ | ||||||
39 | template <class reference_type> | ||||||
40 | class Reference | ||||||
41 | { | ||||||
42 | /** The <b>reference_type</b> body pointer. | ||||||
43 | */ | ||||||
44 | reference_type * m_pBody; | ||||||
45 | |||||||
46 | |||||||
47 | public: | ||||||
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) | ||||||
177 | { | ||||||
178 | reference_type * const pOld = m_pBody; | ||||||
179 | m_pBody = NULL__null; | ||||||
180 | pOld->release(); | ||||||
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; | ||||||
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 | ||||||
277 | namespace 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 | */ | ||||||
286 | template<typename T> | ||||||
287 | struct 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: */ |
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 | |
25 | class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase |
26 | { |
27 | mutable oslInterlockedCount mnRefCnt; |
28 | |
29 | template<typename T> friend class VclPtr; |
30 | |
31 | public: |
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) |
40 | delete this; |
41 | } |
42 | #ifdef DBG_UTIL |
43 | #ifndef _WIN32 |
44 | sal_Int32 getRefCount() const { return mnRefCnt; } |
45 | #endif |
46 | #endif |
47 | |
48 | |
49 | private: |
50 | VclReferenceBase(const VclReferenceBase&) = delete; |
51 | VclReferenceBase& operator=(const VclReferenceBase&) = delete; |
52 | |
53 | bool mbDisposed : 1; |
54 | |
55 | protected: |
56 | VclReferenceBase(); |
57 | protected: |
58 | virtual ~VclReferenceBase(); |
59 | |
60 | protected: |
61 | virtual void dispose(); |
62 | |
63 | public: |
64 | void disposeOnce(); |
65 | bool isDisposed() const { return mbDisposed; } |
66 | |
67 | }; |
68 | #endif |