Line data Source code
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 <svl/broadcast.hxx>
21 : #include <svl/listener.hxx>
22 : #include <svl/smplhint.hxx>
23 :
24 60090 : void SvtBroadcaster::Normalize()
25 : {
26 60090 : if (!mbNormalized)
27 : {
28 30255 : std::sort(maListeners.begin(), maListeners.end());
29 30255 : ListenersType::iterator itUniqueEnd = std::unique(maListeners.begin(), maListeners.end());
30 30255 : maListeners.erase(itUniqueEnd, maListeners.end());
31 30255 : mbNormalized = true;
32 : }
33 :
34 60090 : if (!mbDestNormalized)
35 : {
36 24316 : std::sort(maDestructedListeners.begin(), maDestructedListeners.end());
37 24316 : ListenersType::iterator itUniqueEnd = std::unique(maDestructedListeners.begin(), maDestructedListeners.end());
38 24316 : maDestructedListeners.erase(itUniqueEnd, maDestructedListeners.end());
39 24316 : mbDestNormalized = true;
40 : }
41 60090 : }
42 :
43 41296 : void SvtBroadcaster::Add( SvtListener* p )
44 : {
45 41296 : maListeners.push_back(p);
46 41296 : mbNormalized = false;
47 41296 : }
48 :
49 41296 : void SvtBroadcaster::Remove( SvtListener* p )
50 : {
51 41296 : if (mbDisposing)
52 41984 : return;
53 :
54 30892 : if (mbAboutToDie)
55 : {
56 21176 : maDestructedListeners.push_back(p);
57 21176 : mbDestNormalized = false;
58 21176 : return;
59 : }
60 :
61 9716 : Normalize();
62 : std::pair<ListenersType::iterator,ListenersType::iterator> r =
63 9716 : std::equal_range(maListeners.begin(), maListeners.end(), p);
64 :
65 9716 : if (r.first != r.second)
66 9716 : maListeners.erase(r.first, r.second);
67 9716 : if (maListeners.empty())
68 4722 : ListenersGone();
69 : }
70 :
71 24022 : SvtBroadcaster::SvtBroadcaster() : mbAboutToDie(false), mbDisposing(false), mbNormalized(false), mbDestNormalized(false) {}
72 :
73 0 : SvtBroadcaster::SvtBroadcaster( const SvtBroadcaster &rBC ) :
74 : maListeners(rBC.maListeners), maDestructedListeners(rBC.maDestructedListeners),
75 : mbAboutToDie(rBC.mbAboutToDie), mbDisposing(false),
76 0 : mbNormalized(rBC.mbNormalized), mbDestNormalized(rBC.mbDestNormalized)
77 : {
78 0 : if (mbAboutToDie)
79 0 : Normalize();
80 :
81 0 : ListenersType::iterator dest(maDestructedListeners.begin());
82 0 : for (ListenersType::iterator it(maListeners.begin()); it != maListeners.end(); ++it)
83 : {
84 0 : bool bStart = true;
85 :
86 0 : if (mbAboutToDie)
87 : {
88 : // skip the destructed ones
89 0 : while (dest != maDestructedListeners.end() && (*dest < *it))
90 0 : ++dest;
91 :
92 0 : bStart = (dest == maDestructedListeners.end() || *dest != *it);
93 : }
94 :
95 0 : if (bStart)
96 0 : (*it)->StartListening(*this);
97 : }
98 0 : }
99 :
100 64822 : SvtBroadcaster::~SvtBroadcaster()
101 : {
102 24022 : mbDisposing = true;
103 24022 : Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
104 :
105 24022 : Normalize();
106 :
107 : // now when both lists are sorted, we can linearly unregister all
108 : // listeners, with the exception of those that already asked to be removed
109 : // during their own destruction
110 24022 : ListenersType::iterator dest(maDestructedListeners.begin());
111 55602 : for (ListenersType::iterator it(maListeners.begin()); it != maListeners.end(); ++it)
112 : {
113 : // skip the destructed ones
114 69496 : while (dest != maDestructedListeners.end() && (*dest < *it))
115 6336 : ++dest;
116 :
117 31580 : if (dest == maDestructedListeners.end() || *dest != *it)
118 10404 : (*it)->EndListening(*this);
119 : }
120 40800 : }
121 :
122 26352 : void SvtBroadcaster::Broadcast( const SfxHint &rHint )
123 : {
124 26352 : Normalize();
125 :
126 26352 : ListenersType::iterator dest(maDestructedListeners.begin());
127 26352 : ListenersType aListeners(maListeners); // this copy is important to avoid erasing entries while iterating
128 144166 : for (ListenersType::iterator it(aListeners.begin()); it != aListeners.end(); ++it)
129 : {
130 : // skip the destructed ones
131 241964 : while (dest != maDestructedListeners.end() && (*dest < *it))
132 6336 : ++dest;
133 :
134 117814 : if (dest == maDestructedListeners.end() || *dest != *it)
135 96638 : (*it)->Notify(rHint);
136 26352 : }
137 26352 : }
138 :
139 4716 : void SvtBroadcaster::ListenersGone() {}
140 :
141 :
142 3528 : bool SvtBroadcaster::HasListeners() const
143 : {
144 3528 : return !maListeners.empty();
145 : }
146 :
147 15568 : void SvtBroadcaster::PrepareForDestruction()
148 : {
149 15568 : mbAboutToDie = true;
150 15568 : maDestructedListeners.reserve(maListeners.size());
151 15568 : }
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|