update dev300-m58
[ooovba.git] / slideshow / source / inc / interruptabledelayevent.hxx
blob3829bf7e18e1cfd3a0f9ab4b3de5fb822f3fd35b
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: interruptabledelayevent.hxx,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
31 #ifndef INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX
32 #define INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX
34 #include "delayevent.hxx"
36 namespace slideshow
38 namespace internal
40 /** Event, which delays calling passed Event's fire() method
41 the given amount of time.
43 This is actually a facade around the passed event object,
44 that passes on all calls to that object, and the sole
45 contribution of itself is the delay.
47 class DelayFacade : public Event
49 public:
50 DelayFacade( const EventSharedPtr& rEvent,
51 double nTimeout ) :
52 mpEvent( rEvent ),
53 mnTimeout( nTimeout )
57 virtual bool fire()
59 if( mpEvent && isCharged() )
61 // pass on directly - we're supposed to be called
62 // from EventQueue here, anyway - and if not,
63 // we're only keeping that incorrect transitively.
64 return mpEvent->fire();
67 return false;
70 virtual bool isCharged() const
72 // pass on to wrappee - this ensures that we return
73 // false on isCharged(), even if the other event has
74 // been fired outside our own fire() method
75 return !mpEvent ? false : mpEvent->isCharged();
78 virtual double getActivationTime( double nCurrentTime ) const
80 // enforce _our_ timeout to our clients (this
81 // overrides any timeout possibly set at the wrappee!)
82 return nCurrentTime + mnTimeout;
85 virtual void dispose()
87 mpEvent.reset();
90 private:
91 EventSharedPtr mpEvent;
92 double mnTimeout;
95 /// Return value for makeInterruptableDelay()
96 struct InterruptableEventPair
98 /** This member contains a pointer to the timeout
99 event. When enqueued, this event will fire the
100 requested action only after the specified timeout.
102 EventSharedPtr mpTimeoutEvent;
104 /** This member contains a pointer to the interruption
105 event. When enqueued, this event will fire
106 immediately, interrupting a potentially waiting
107 timeout event.
109 EventSharedPtr mpImmediateEvent;
112 /** Generate an interruptable delay event.
114 This function generates a pair of events, that are
115 especially tailored to achieve the following behaviour: By
116 default, the given functor is called after the specified
117 timeout (after insertion of the event into the EventQueue,
118 of course). But optionally, when the interruption event
119 InterruptableEventPair::mpImmediateEvent is fired, the
120 given functor is called <em>at once</em>, and the delay is
121 ignored (that means, the given functor is guaranteed to be
122 called at utmost once, and never twice. Furthermore, it is
123 ensured that both events return false on isCharged(), once
124 anyone of them has been fired already).
126 @param rFunctor
127 Functor to call when the event fires.
129 @param nTimeout
130 Timeout in seconds, to wait until functor is called.
132 @returns a pair of events, where the first one waits the
133 specified amount of time, and the other fires the given
134 functor immediately.
136 template< typename Functor > InterruptableEventPair makeInterruptableDelay( const Functor& rFunctor,
137 double nTimeout )
139 InterruptableEventPair aRes;
141 aRes.mpImmediateEvent = makeEvent( rFunctor );
142 aRes.mpTimeoutEvent.reset( new DelayFacade( aRes.mpImmediateEvent,
143 nTimeout ) );
145 return aRes;
150 #endif /* INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX */