xref: /AOO41X/main/slideshow/source/inc/interruptabledelayevent.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX
29*cdf0e10cSrcweir #define INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "delayevent.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir namespace slideshow
34*cdf0e10cSrcweir {
35*cdf0e10cSrcweir     namespace internal
36*cdf0e10cSrcweir     {
37*cdf0e10cSrcweir         /** Event, which delays calling passed Event's fire() method
38*cdf0e10cSrcweir             the given amount of time.
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir             This is actually a facade around the passed event object,
41*cdf0e10cSrcweir             that passes on all calls to that object, and the sole
42*cdf0e10cSrcweir             contribution of itself is the delay.
43*cdf0e10cSrcweir         */
44*cdf0e10cSrcweir         class DelayFacade : public Event
45*cdf0e10cSrcweir         {
46*cdf0e10cSrcweir         public:
47*cdf0e10cSrcweir             DelayFacade( const EventSharedPtr& 	rEvent,
48*cdf0e10cSrcweir                          double					nTimeout	) :
49*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
50*cdf0e10cSrcweir                 Event(::rtl::OUString::createFromAscii("DelayFacade")),
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir                 mpEvent( rEvent ),
53*cdf0e10cSrcweir                 mnTimeout( nTimeout )
54*cdf0e10cSrcweir             {
55*cdf0e10cSrcweir             }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir             virtual bool fire()
58*cdf0e10cSrcweir             {
59*cdf0e10cSrcweir                 if( mpEvent && isCharged() )
60*cdf0e10cSrcweir                 {
61*cdf0e10cSrcweir                     // pass on directly - we're supposed to be called
62*cdf0e10cSrcweir                     // from EventQueue here, anyway - and if not,
63*cdf0e10cSrcweir                     // we're only keeping that incorrect transitively.
64*cdf0e10cSrcweir                     return mpEvent->fire();
65*cdf0e10cSrcweir                 }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir                 return false;
68*cdf0e10cSrcweir             }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir             virtual bool isCharged() const
71*cdf0e10cSrcweir             {
72*cdf0e10cSrcweir                 // pass on to wrappee - this ensures that we return
73*cdf0e10cSrcweir                 // false on isCharged(), even if the other event has
74*cdf0e10cSrcweir                 // been fired outside our own fire() method
75*cdf0e10cSrcweir                 return !mpEvent ? false : mpEvent->isCharged();
76*cdf0e10cSrcweir             }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir             virtual double getActivationTime( double nCurrentTime ) const
79*cdf0e10cSrcweir             {
80*cdf0e10cSrcweir                 // enforce _our_ timeout to our clients (this
81*cdf0e10cSrcweir                 // overrides any timeout possibly set at the wrappee!)
82*cdf0e10cSrcweir                 return nCurrentTime + mnTimeout;
83*cdf0e10cSrcweir             }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir             virtual void dispose()
86*cdf0e10cSrcweir             {
87*cdf0e10cSrcweir                 mpEvent.reset();
88*cdf0e10cSrcweir             }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         private:
91*cdf0e10cSrcweir             EventSharedPtr	mpEvent;
92*cdf0e10cSrcweir             double			mnTimeout;
93*cdf0e10cSrcweir         };
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         /// Return value for makeInterruptableDelay()
96*cdf0e10cSrcweir         struct InterruptableEventPair
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             /** This member contains a pointer to the timeout
99*cdf0e10cSrcweir                 event. When enqueued, this event will fire the
100*cdf0e10cSrcweir                 requested action only after the specified timeout.
101*cdf0e10cSrcweir              */
102*cdf0e10cSrcweir             EventSharedPtr	mpTimeoutEvent;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir             /** This member contains a pointer to the interruption
105*cdf0e10cSrcweir                 event. When enqueued, this event will fire
106*cdf0e10cSrcweir                 immediately, interrupting a potentially waiting
107*cdf0e10cSrcweir                 timeout event.
108*cdf0e10cSrcweir              */
109*cdf0e10cSrcweir             EventSharedPtr	mpImmediateEvent;
110*cdf0e10cSrcweir         };
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         /** Generate an interruptable delay event.
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         	This function generates a pair of events, that are
115*cdf0e10cSrcweir         	especially tailored to achieve the following behaviour: By
116*cdf0e10cSrcweir         	default, the given functor is called after the specified
117*cdf0e10cSrcweir         	timeout (after insertion of the event into the EventQueue,
118*cdf0e10cSrcweir         	of course). But optionally, when the interruption event
119*cdf0e10cSrcweir         	InterruptableEventPair::mpImmediateEvent is fired, the
120*cdf0e10cSrcweir         	given functor is called <em>at once</em>, and the delay is
121*cdf0e10cSrcweir         	ignored (that means, the given functor is guaranteed to be
122*cdf0e10cSrcweir         	called at utmost once, and never twice. Furthermore, it is
123*cdf0e10cSrcweir         	ensured that both events return false on isCharged(), once
124*cdf0e10cSrcweir         	anyone of them has been fired already).
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir         	@param rFunctor
127*cdf0e10cSrcweir             Functor to call when the event fires.
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir             @param nTimeout
130*cdf0e10cSrcweir             Timeout in seconds, to wait until functor is called.
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 			@returns a pair of events, where the first one waits the
133*cdf0e10cSrcweir 			specified amount of time, and the other fires the given
134*cdf0e10cSrcweir 			functor immediately.
135*cdf0e10cSrcweir          */
136*cdf0e10cSrcweir         template< typename Functor > InterruptableEventPair makeInterruptableDelay( const Functor& 	rFunctor,
137*cdf0e10cSrcweir                                                                                     double			nTimeout	)
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             InterruptableEventPair aRes;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir             aRes.mpImmediateEvent = makeEvent( rFunctor, "makeInterruptableDelay");
142*cdf0e10cSrcweir             aRes.mpTimeoutEvent.reset( new DelayFacade( aRes.mpImmediateEvent,
143*cdf0e10cSrcweir                                                         nTimeout ) );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir             return aRes;
146*cdf0e10cSrcweir         }
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir #endif /* INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX */
151