1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: listenercontainerimpl.hxx,v $
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>
39 ////////////////////////////////////////////////////////////////////////////
43 struct EmptyGuard
{ explicit EmptyGuard(EmptyBase
) {} };
44 struct EmptyClearableGuard
46 explicit EmptyClearableGuard(EmptyBase
) {}
51 typedef EmptyGuard Guard
;
52 typedef EmptyClearableGuard ClearableGuard
;
58 struct Guard
: public osl::MutexGuard
60 explicit Guard(MutexBase
const& rBase
) :
61 osl::MutexGuard(rBase
.maMutex
)
64 struct ClearableGuard
: public osl::ClearableMutexGuard
66 explicit ClearableGuard(MutexBase
const& rBase
) :
67 osl::ClearableMutexGuard(rBase
.maMutex
)
71 mutable osl::Mutex maMutex
;
74 ////////////////////////////////////////////////////////////////////////////
76 template< typename result_type
, typename ListenerTargetT
> struct FunctionApply
78 template<typename FuncT
> static bool apply(
80 ListenerTargetT
const& rArg
)
86 template<typename ListenerTargetT
> struct FunctionApply
<void,ListenerTargetT
>
88 template<typename FuncT
> static bool apply(
90 ListenerTargetT
const& rArg
)
97 ////////////////////////////////////////////////////////////////////////////
99 template< typename ListenerT
> struct ListenerOperations
101 /// Notify a single one of the listeners
102 template< typename ContainerT
,
104 static bool notifySingleListener( ContainerT
& rContainer
,
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(),
116 /// Notify all listeners
117 template< typename ContainerT
,
119 static bool notifyAllListeners( ContainerT
& rContainer
,
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(
138 // true: at least one handler returned true
139 // false: not a single handler returned true
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
,
157 static bool notifySingleListener( ContainerT
& rContainer
,
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
) )
175 template< typename ContainerT
,
177 static bool notifyAllListeners( ContainerT
& rContainer
,
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
) )
200 template< typename ContainerT
>
201 static void pruneListeners( ContainerT
& rContainer
,
202 size_t nSizeThreshold
)
204 if( rContainer
.size() <= nSizeThreshold
)
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
);
220 std::swap( rContainer
, aAliveListeners
);
224 } // namespace internal
225 } // namespace Presentation
227 #endif /* INCLUDED_SLIDESHOW_LISTENERCONTAINERIMPL_HXX */