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 :
21 : #include <tools/debug.hxx>
22 :
23 : #include <svl/hint.hxx>
24 : #include <svl/SfxBroadcaster.hxx>
25 :
26 : #include <svl/lstner.hxx>
27 : #include <algorithm>
28 :
29 10503955 : TYPEINIT0(SfxListener);
30 :
31 : // simple ctor of class SfxListener
32 :
33 2145835 : SfxListener::SfxListener()
34 : {
35 2145835 : }
36 :
37 : // copy ctor of class SfxListener
38 :
39 4 : SfxListener::SfxListener( const SfxListener &rListener )
40 : {
41 4 : for ( sal_uInt16 n = 0; n < rListener.aBCs.size(); ++n )
42 0 : StartListening( *rListener.aBCs[n] );
43 4 : }
44 :
45 : // unregisters the SfxListener from its SfxBroadcasters
46 :
47 4271502 : SfxListener::~SfxListener()
48 : {
49 : // unregister at all remaining broadcasters
50 2220941 : for ( sal_uInt16 nPos = 0; nPos < aBCs.size(); ++nPos )
51 : {
52 85190 : SfxBroadcaster *pBC = aBCs[nPos];
53 85190 : pBC->RemoveListener(*this);
54 : }
55 2135751 : }
56 :
57 :
58 : // unregisters a specific SfxBroadcaster
59 :
60 54056 : void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBroadcaster )
61 : {
62 54056 : aBCs.erase( std::find( aBCs.begin(), aBCs.end(), &rBroadcaster ) );
63 54056 : }
64 :
65 :
66 : // registers a specific SfxBroadcaster
67 :
68 1038731 : void SfxListener::StartListening( SfxBroadcaster& rBroadcaster, bool bPreventDups )
69 : {
70 1038731 : if ( !bPreventDups || !IsListening( rBroadcaster ) )
71 : {
72 1033249 : rBroadcaster.AddListener(*this);
73 1033249 : aBCs.push_back( &rBroadcaster );
74 :
75 : DBG_ASSERT( IsListening(rBroadcaster), "StartListening failed" );
76 : }
77 1038731 : }
78 :
79 :
80 : // unregisters a specific SfxBroadcaster
81 :
82 1089808 : void SfxListener::EndListening( SfxBroadcaster& rBroadcaster, bool bAllDups )
83 : {
84 879098 : do
85 : {
86 1089808 : SfxBroadcasterArr_Impl::iterator it = std::find( aBCs.begin(), aBCs.end(), &rBroadcaster );
87 1089808 : if ( it == aBCs.end() )
88 : {
89 210710 : break;
90 : }
91 879098 : rBroadcaster.RemoveListener(*this);
92 879098 : aBCs.erase( it );
93 : }
94 : while ( bAllDups );
95 889898 : }
96 :
97 :
98 : // unregisters all Broadcasters
99 :
100 127926 : void SfxListener::EndListeningAll()
101 : {
102 : // Attention: when optimizing this: Respect sideffects of RemoveListener!
103 256235 : while ( !aBCs.empty() )
104 : {
105 383 : SfxBroadcaster *pBC = aBCs.front();
106 383 : pBC->RemoveListener(*this);
107 383 : aBCs.pop_front();
108 : }
109 127926 : }
110 :
111 :
112 229166 : bool SfxListener::IsListening( SfxBroadcaster& rBroadcaster ) const
113 : {
114 229166 : return aBCs.end() != std::find( aBCs.begin(), aBCs.end(), &rBroadcaster );
115 : }
116 :
117 :
118 : // base implementation of notification handler
119 :
120 : #ifdef DBG_UTIL
121 : void SfxListener::Notify( SfxBroadcaster& rBroadcaster, const SfxHint& )
122 : #else
123 503 : void SfxListener::Notify( SfxBroadcaster&, const SfxHint& )
124 : #endif
125 : {
126 : #ifdef DBG_UTIL
127 : DBG_ASSERT(aBCs.end() != std::find(aBCs.begin(), aBCs.end(), &rBroadcaster),
128 : "notification from unregistered broadcaster" );
129 : #endif
130 503 : }
131 :
132 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|