Update ooo320-m1
[ooovba.git] / slideshow / source / inc / listenercontainerimpl.hxx
blobc54d2c0edcce21968c1e3047353e6af6ea31c55a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: listenercontainerimpl.hxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX
31 #define INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX
33 #include <sal/config.h>
34 #include <boost/weak_ptr.hpp>
36 namespace slideshow {
37 namespace internal {
39 ////////////////////////////////////////////////////////////////////////////
41 struct EmptyBase
43 struct EmptyGuard{ explicit EmptyGuard(EmptyBase) {} };
44 struct EmptyClearableGuard
46 explicit EmptyClearableGuard(EmptyBase) {}
47 void clear() {}
48 void reset() {}
51 typedef EmptyGuard Guard;
52 typedef EmptyClearableGuard ClearableGuard;
55 class MutexBase
57 public:
58 struct Guard : public osl::MutexGuard
60 explicit Guard(MutexBase const& rBase) :
61 osl::MutexGuard(rBase.maMutex)
62 {}
64 struct ClearableGuard : public osl::ClearableMutexGuard
66 explicit ClearableGuard(MutexBase const& rBase) :
67 osl::ClearableMutexGuard(rBase.maMutex)
68 {}
71 mutable osl::Mutex maMutex;
74 ////////////////////////////////////////////////////////////////////////////
76 template< typename result_type, typename ListenerTargetT > struct FunctionApply
78 template<typename FuncT> static bool apply(
79 FuncT func,
80 ListenerTargetT const& rArg )
82 return func(rArg);
86 template<typename ListenerTargetT> struct FunctionApply<void,ListenerTargetT>
88 template<typename FuncT> static bool apply(
89 FuncT func,
90 ListenerTargetT const& rArg )
92 func(rArg);
93 return true;
97 ////////////////////////////////////////////////////////////////////////////
99 template< typename ListenerT > struct ListenerOperations
101 /// Notify a single one of the listeners
102 template< typename ContainerT,
103 typename FuncT >
104 static bool notifySingleListener( ContainerT& rContainer,
105 FuncT func )
107 const typename ContainerT::const_iterator aEnd( rContainer.end() );
109 // true: a handler in this queue processed the event
110 // false: no handler in this queue finally processed the event
111 return (std::find_if( rContainer.begin(),
112 aEnd,
113 func ) != aEnd);
116 /// Notify all listeners
117 template< typename ContainerT,
118 typename FuncT >
119 static bool notifyAllListeners( ContainerT& rContainer,
120 FuncT func )
122 bool bRet(false);
123 typename ContainerT::const_iterator aCurr( rContainer.begin() );
124 typename ContainerT::const_iterator const aEnd ( rContainer.end() );
125 while( aCurr != aEnd )
127 if( FunctionApply< typename FuncT::result_type,
128 typename ContainerT::value_type >::apply(
129 func,
130 *aCurr) )
132 bRet = true;
135 ++aCurr;
138 // true: at least one handler returned true
139 // false: not a single handler returned true
140 return bRet;
143 /// Prune container from deceased listeners
144 template< typename ContainerT >
145 static void pruneListeners( ContainerT&, size_t )
150 // specializations for weak_ptr
151 // ----------------------------
152 template< typename ListenerTargetT >
153 struct ListenerOperations< boost::weak_ptr<ListenerTargetT> >
155 template< typename ContainerT,
156 typename FuncT >
157 static bool notifySingleListener( ContainerT& rContainer,
158 FuncT func )
160 typename ContainerT::const_iterator aCurr( rContainer.begin() );
161 typename ContainerT::const_iterator const aEnd ( rContainer.end() );
162 while( aCurr != aEnd )
164 boost::shared_ptr<ListenerTargetT> pListener( aCurr->lock() );
166 if( pListener && func(pListener) )
167 return true;
169 ++aCurr;
172 return false;
175 template< typename ContainerT,
176 typename FuncT >
177 static bool notifyAllListeners( ContainerT& rContainer,
178 FuncT func )
180 bool bRet(false);
181 typename ContainerT::const_iterator aCurr( rContainer.begin() );
182 typename ContainerT::const_iterator const aEnd ( rContainer.end() );
183 while( aCurr != aEnd )
185 boost::shared_ptr<ListenerTargetT> pListener( aCurr->lock() );
187 if( pListener.get() &&
188 FunctionApply< typename FuncT::result_type,
189 boost::shared_ptr<ListenerTargetT> >::apply(func,pListener) )
191 bRet = true;
194 ++aCurr;
197 return bRet;
200 template< typename ContainerT >
201 static void pruneListeners( ContainerT& rContainer,
202 size_t nSizeThreshold )
204 if( rContainer.size() <= nSizeThreshold )
205 return;
207 ContainerT aAliveListeners;
208 aAliveListeners.reserve(rContainer.size());
210 typename ContainerT::const_iterator aCurr( rContainer.begin() );
211 typename ContainerT::const_iterator const aEnd ( rContainer.end() );
212 while( aCurr != aEnd )
214 if( !aCurr->expired() )
215 aAliveListeners.push_back( *aCurr );
217 ++aCurr;
220 std::swap( rContainer, aAliveListeners );
224 } // namespace internal
225 } // namespace Presentation
227 #endif /* INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX */