/src/libreoffice/vcl/inc/impdel.hxx
Line | Count | Source (jump to first uncovered line) |
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 | | #pragma once |
21 | | |
22 | | #include <algorithm> |
23 | | #include <vector> |
24 | | |
25 | | namespace vcl |
26 | | { |
27 | | |
28 | | class DeletionListener; |
29 | | |
30 | | class DeletionNotifier |
31 | | { |
32 | | std::vector< DeletionListener* > m_aListeners; |
33 | | protected: |
34 | 12.7k | DeletionNotifier() {} |
35 | | |
36 | | ~DeletionNotifier() |
37 | 8.47k | { notifyDelete(); } |
38 | | |
39 | | inline void notifyDelete(); |
40 | | |
41 | | public: |
42 | | void addDel( DeletionListener* pListener ) |
43 | 0 | { m_aListeners.push_back( pListener ); } |
44 | | |
45 | | void removeDel( DeletionListener* pListener ) |
46 | 0 | { std::erase(m_aListeners, pListener); } |
47 | | }; |
48 | | |
49 | | class DeletionListener |
50 | | { |
51 | | DeletionNotifier* m_pNotifier; |
52 | | public: |
53 | | DeletionListener( DeletionNotifier* pNotifier ) |
54 | | : m_pNotifier( pNotifier ) |
55 | 0 | { |
56 | 0 | if( m_pNotifier ) |
57 | 0 | m_pNotifier->addDel( this ); |
58 | 0 | } |
59 | | DeletionListener(const DeletionListener& rOther) |
60 | | : m_pNotifier(rOther.m_pNotifier) |
61 | 0 | { |
62 | 0 | if (m_pNotifier) |
63 | 0 | m_pNotifier->addDel(this); |
64 | 0 | } |
65 | | ~DeletionListener() |
66 | 0 | { |
67 | 0 | if( m_pNotifier ) |
68 | 0 | m_pNotifier->removeDel( this ); |
69 | 0 | } |
70 | 0 | void deleted() { m_pNotifier = nullptr; } |
71 | 0 | bool isDeleted() const { return (m_pNotifier == nullptr); } |
72 | | }; |
73 | | |
74 | | inline void DeletionNotifier::notifyDelete() |
75 | 8.47k | { |
76 | 8.47k | for( auto& rListener : m_aListeners ) |
77 | 0 | rListener->deleted(); |
78 | | |
79 | 8.47k | m_aListeners.clear(); |
80 | 8.47k | } |
81 | | |
82 | | } // namespace vcl |
83 | | |
84 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |