1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_SVL_BROADCAST_HXX
20 #define INCLUDED_SVL_BROADCAST_HXX
22 #include <svl/svldllapi.h>
29 class SVL_DLLPUBLIC SvtBroadcaster
32 friend class SvtListener
;
34 typedef std::vector
<SvtListener
*> ListenersType
;
37 const SvtBroadcaster
& operator=(const SvtBroadcaster
&) = delete;
40 * Ensure that the container doesn't contain any duplicated listener
41 * entries. As a side effect, the listeners get sorted by pointer values
44 void Normalize() const;
46 void Add( SvtListener
* p
);
47 void Remove( SvtListener
* p
);
50 virtual void ListenersGone();
54 : mnEmptySlots(0), mnListenersFirstUnsorted(0), mbAboutToDie(false)
55 , mbDisposing(false), mbDestNormalized(true) {}
56 SvtBroadcaster( const SvtBroadcaster
&rBC
);
57 virtual ~SvtBroadcaster();
59 void Broadcast( const SfxHint
&rHint
);
61 ListenersType
& GetAllListeners();
62 const ListenersType
& GetAllListeners() const;
64 bool HasListeners() const { return (maListeners
.size() - mnEmptySlots
) > 0; }
67 * Listeners and broadcasters are M:N relationship. If you want to
68 * destruct them, you easily end up in O(M*N) situation; where for every
69 * listener, you iterate all broadcasters, to remove that one listener.
71 * To avoid that, use this call to announce to the broadcaster it is going
72 * to die, and the listeners do not have to bother with removing
73 * themselves from the broadcaster - the broadcaster will not broadcast
74 * anything after the PrepareForDestruction() call anyway.
76 void PrepareForDestruction();
79 /// contains only one of each listener, which is enforced by SvtListener
80 mutable ListenersType maListeners
;
82 /// When the broadcaster is about to die, collect listeners that asked for removal.
83 mutable ListenersType maDestructedListeners
;
86 Note that this class is very performance sensitive, so the following fields have been
87 carefully sized and packed so we can access them quickly, while also taking as little CPU cache
88 space as possible (because e.g. calc can have a LOT of them)
90 mutable sal_Int32 mnEmptySlots
;
91 // The first item in maListeners that is not sorted. The container can become large, so this optimizes sorting.
92 mutable sal_Int32 mnListenersFirstUnsorted
;
93 /// Indicate that this broadcaster will be destructed (we indicate this on all ScColumn's broadcasters during the ScTable destruction, eg.)
96 // Whether maDestructedListeners is sorted or not.
97 mutable bool mbDestNormalized
:1;
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */