1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <itemdel.hxx>
23 #include <svl/poolitem.hxx>
24 #include <vcl/idle.hxx>
25 
26 #include <tools/debug.hxx>
27 
28 class SfxItemDisruptor_Impl
29 {
30     std::unique_ptr<SfxPoolItem> pItem;
31     Idle m_Idle;
32 
33 private:
34     DECL_LINK(Delete, Timer*, void);
35 
36 public:
37     explicit SfxItemDisruptor_Impl(std::unique_ptr<SfxPoolItem> pItemToDesrupt);
38     void LaunchDeleteOnIdle();
39     ~SfxItemDisruptor_Impl();
40     SfxItemDisruptor_Impl(const SfxItemDisruptor_Impl&) = delete;
41     SfxItemDisruptor_Impl& operator=(const SfxItemDisruptor_Impl&) = delete;
42 };
43 
SfxItemDisruptor_Impl(std::unique_ptr<SfxPoolItem> pItemToDisrupt)44 SfxItemDisruptor_Impl::SfxItemDisruptor_Impl(std::unique_ptr<SfxPoolItem> pItemToDisrupt)
45     : pItem(std::move(pItemToDisrupt))
46     , m_Idle("sfx SfxItemDisruptor_Impl::Delete")
47 {
48     m_Idle.SetInvokeHandler(LINK(this, SfxItemDisruptor_Impl, Delete));
49     m_Idle.SetPriority(TaskPriority::DEFAULT_IDLE);
50     m_Idle.SetDebugName("sfx::SfxItemDisruptor_Impl m_Idle");
51 
52     DBG_ASSERT(0 == pItem->GetRefCount(), "disrupting pooled item");
53     pItem->SetKind(SfxItemKind::DeleteOnIdle);
54 }
55 
LaunchDeleteOnIdle()56 void SfxItemDisruptor_Impl::LaunchDeleteOnIdle() { m_Idle.Start(); }
57 
~SfxItemDisruptor_Impl()58 SfxItemDisruptor_Impl::~SfxItemDisruptor_Impl()
59 {
60     m_Idle.Stop();
61 
62     // reset RefCount (was set to SFX_ITEMS_SPECIAL before!)
63     pItem->SetRefCount(0);
64 
65     pItem.reset();
66 }
67 
IMPL_LINK_NOARG(SfxItemDisruptor_Impl,Delete,Timer *,void)68 IMPL_LINK_NOARG(SfxItemDisruptor_Impl, Delete, Timer*, void) { delete this; }
69 
DeleteItemOnIdle(std::unique_ptr<SfxPoolItem> pItem)70 void DeleteItemOnIdle(std::unique_ptr<SfxPoolItem> pItem)
71 {
72     DBG_ASSERT(0 == pItem->GetRefCount(), "deleting item in use");
73     SfxItemDisruptor_Impl* pDesruptor = new SfxItemDisruptor_Impl(std::move(pItem));
74     pDesruptor->LaunchDeleteOnIdle();
75     // coverity[leaked_storage] - pDesruptor takes care of its own destruction at idle time
76 }
77 
78 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
79