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 : #ifndef INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX
20 : #define INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX
21 :
22 : #include <sal/config.h>
23 : #include <boost/weak_ptr.hpp>
24 :
25 : namespace slideshow {
26 : namespace internal {
27 :
28 : ////////////////////////////////////////////////////////////////////////////
29 :
30 0 : struct EmptyBase
31 : {
32 0 : struct EmptyGuard{ explicit EmptyGuard(EmptyBase) {} };
33 : struct EmptyClearableGuard
34 : {
35 0 : explicit EmptyClearableGuard(EmptyBase) {}
36 0 : void clear() {}
37 : void reset() {}
38 : };
39 :
40 : typedef EmptyGuard Guard;
41 : typedef EmptyClearableGuard ClearableGuard;
42 : };
43 :
44 : class MutexBase
45 : {
46 : public:
47 : struct Guard : public osl::MutexGuard
48 : {
49 : explicit Guard(MutexBase const& rBase) :
50 : osl::MutexGuard(rBase.maMutex)
51 : {}
52 : };
53 : struct ClearableGuard : public osl::ClearableMutexGuard
54 : {
55 : explicit ClearableGuard(MutexBase const& rBase) :
56 : osl::ClearableMutexGuard(rBase.maMutex)
57 : {}
58 : };
59 :
60 : mutable osl::Mutex maMutex;
61 : };
62 :
63 : ////////////////////////////////////////////////////////////////////////////
64 :
65 : template< typename result_type, typename ListenerTargetT > struct FunctionApply
66 : {
67 0 : template<typename FuncT> static bool apply(
68 : FuncT func,
69 : ListenerTargetT const& rArg )
70 : {
71 0 : return func(rArg);
72 : }
73 : };
74 :
75 : template<typename ListenerTargetT> struct FunctionApply<void,ListenerTargetT>
76 : {
77 0 : template<typename FuncT> static bool apply(
78 : FuncT func,
79 : ListenerTargetT const& rArg )
80 : {
81 0 : func(rArg);
82 0 : return true;
83 : }
84 : };
85 :
86 : ////////////////////////////////////////////////////////////////////////////
87 :
88 : template< typename ListenerT > struct ListenerOperations
89 : {
90 : /// Notify a single one of the listeners
91 : template< typename ContainerT,
92 : typename FuncT >
93 0 : static bool notifySingleListener( ContainerT& rContainer,
94 : FuncT func )
95 : {
96 0 : const typename ContainerT::const_iterator aEnd( rContainer.end() );
97 :
98 : // true: a handler in this queue processed the event
99 : // false: no handler in this queue finally processed the event
100 : return (std::find_if( rContainer.begin(),
101 : aEnd,
102 0 : func ) != aEnd);
103 : }
104 :
105 : /// Notify all listeners
106 : template< typename ContainerT,
107 : typename FuncT >
108 0 : static bool notifyAllListeners( ContainerT& rContainer,
109 : FuncT func )
110 : {
111 0 : bool bRet(false);
112 0 : typename ContainerT::const_iterator aCurr( rContainer.begin() );
113 0 : typename ContainerT::const_iterator const aEnd ( rContainer.end() );
114 0 : while( aCurr != aEnd )
115 : {
116 0 : if( FunctionApply< typename FuncT::result_type,
117 : typename ContainerT::value_type >::apply(
118 : func,
119 : *aCurr) )
120 : {
121 0 : bRet = true;
122 : }
123 :
124 0 : ++aCurr;
125 : }
126 :
127 : // true: at least one handler returned true
128 : // false: not a single handler returned true
129 0 : return bRet;
130 : }
131 :
132 : /// Prune container from deceased listeners
133 : template< typename ContainerT >
134 0 : static void pruneListeners( ContainerT&, size_t )
135 : {
136 0 : }
137 : };
138 :
139 : // specializations for weak_ptr
140 : // ----------------------------
141 : template< typename ListenerTargetT >
142 : struct ListenerOperations< boost::weak_ptr<ListenerTargetT> >
143 : {
144 : template< typename ContainerT,
145 : typename FuncT >
146 : static bool notifySingleListener( ContainerT& rContainer,
147 : FuncT func )
148 : {
149 : typename ContainerT::const_iterator aCurr( rContainer.begin() );
150 : typename ContainerT::const_iterator const aEnd ( rContainer.end() );
151 : while( aCurr != aEnd )
152 : {
153 : boost::shared_ptr<ListenerTargetT> pListener( aCurr->lock() );
154 :
155 : if( pListener && func(pListener) )
156 : return true;
157 :
158 : ++aCurr;
159 : }
160 :
161 : return false;
162 : }
163 :
164 : template< typename ContainerT,
165 : typename FuncT >
166 0 : static bool notifyAllListeners( ContainerT& rContainer,
167 : FuncT func )
168 : {
169 0 : bool bRet(false);
170 0 : typename ContainerT::const_iterator aCurr( rContainer.begin() );
171 0 : typename ContainerT::const_iterator const aEnd ( rContainer.end() );
172 0 : while( aCurr != aEnd )
173 : {
174 0 : boost::shared_ptr<ListenerTargetT> pListener( aCurr->lock() );
175 :
176 0 : if( pListener.get() &&
177 : FunctionApply< typename FuncT::result_type,
178 : boost::shared_ptr<ListenerTargetT> >::apply(func,pListener) )
179 : {
180 0 : bRet = true;
181 : }
182 :
183 0 : ++aCurr;
184 : }
185 :
186 0 : return bRet;
187 : }
188 :
189 : template< typename ContainerT >
190 0 : static void pruneListeners( ContainerT& rContainer,
191 : size_t nSizeThreshold )
192 : {
193 0 : if( rContainer.size() <= nSizeThreshold )
194 0 : return;
195 :
196 0 : ContainerT aAliveListeners;
197 0 : aAliveListeners.reserve(rContainer.size());
198 :
199 0 : typename ContainerT::const_iterator aCurr( rContainer.begin() );
200 0 : typename ContainerT::const_iterator const aEnd ( rContainer.end() );
201 0 : while( aCurr != aEnd )
202 : {
203 0 : if( !aCurr->expired() )
204 0 : aAliveListeners.push_back( *aCurr );
205 :
206 0 : ++aCurr;
207 : }
208 :
209 0 : std::swap( rContainer, aAliveListeners );
210 : }
211 : };
212 :
213 : } // namespace internal
214 : } // namespace Presentation
215 :
216 : #endif /* INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX */
217 :
218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|