1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski #ifndef INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX
25*b1cdbd2cSJim Jagielski #define INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski #include "delayevent.hxx"
28*b1cdbd2cSJim Jagielski 
29*b1cdbd2cSJim Jagielski namespace slideshow
30*b1cdbd2cSJim Jagielski {
31*b1cdbd2cSJim Jagielski     namespace internal
32*b1cdbd2cSJim Jagielski     {
33*b1cdbd2cSJim Jagielski         /** Event, which delays calling passed Event's fire() method
34*b1cdbd2cSJim Jagielski             the given amount of time.
35*b1cdbd2cSJim Jagielski 
36*b1cdbd2cSJim Jagielski             This is actually a facade around the passed event object,
37*b1cdbd2cSJim Jagielski             that passes on all calls to that object, and the sole
38*b1cdbd2cSJim Jagielski             contribution of itself is the delay.
39*b1cdbd2cSJim Jagielski         */
40*b1cdbd2cSJim Jagielski         class DelayFacade : public Event
41*b1cdbd2cSJim Jagielski         {
42*b1cdbd2cSJim Jagielski         public:
DelayFacade(const EventSharedPtr & rEvent,double nTimeout)43*b1cdbd2cSJim Jagielski             DelayFacade( const EventSharedPtr& 	rEvent,
44*b1cdbd2cSJim Jagielski                          double					nTimeout	) :
45*b1cdbd2cSJim Jagielski #if OSL_DEBUG_LEVEL > 1
46*b1cdbd2cSJim Jagielski                 Event(::rtl::OUString::createFromAscii("DelayFacade")),
47*b1cdbd2cSJim Jagielski #endif
48*b1cdbd2cSJim Jagielski                 mpEvent( rEvent ),
49*b1cdbd2cSJim Jagielski                 mnTimeout( nTimeout )
50*b1cdbd2cSJim Jagielski             {
51*b1cdbd2cSJim Jagielski             }
52*b1cdbd2cSJim Jagielski 
fire()53*b1cdbd2cSJim Jagielski             virtual bool fire()
54*b1cdbd2cSJim Jagielski             {
55*b1cdbd2cSJim Jagielski                 if( mpEvent && isCharged() )
56*b1cdbd2cSJim Jagielski                 {
57*b1cdbd2cSJim Jagielski                     // pass on directly - we're supposed to be called
58*b1cdbd2cSJim Jagielski                     // from EventQueue here, anyway - and if not,
59*b1cdbd2cSJim Jagielski                     // we're only keeping that incorrect transitively.
60*b1cdbd2cSJim Jagielski                     return mpEvent->fire();
61*b1cdbd2cSJim Jagielski                 }
62*b1cdbd2cSJim Jagielski 
63*b1cdbd2cSJim Jagielski                 return false;
64*b1cdbd2cSJim Jagielski             }
65*b1cdbd2cSJim Jagielski 
isCharged() const66*b1cdbd2cSJim Jagielski             virtual bool isCharged() const
67*b1cdbd2cSJim Jagielski             {
68*b1cdbd2cSJim Jagielski                 // pass on to wrappee - this ensures that we return
69*b1cdbd2cSJim Jagielski                 // false on isCharged(), even if the other event has
70*b1cdbd2cSJim Jagielski                 // been fired outside our own fire() method
71*b1cdbd2cSJim Jagielski                 return !mpEvent ? false : mpEvent->isCharged();
72*b1cdbd2cSJim Jagielski             }
73*b1cdbd2cSJim Jagielski 
getActivationTime(double nCurrentTime) const74*b1cdbd2cSJim Jagielski             virtual double getActivationTime( double nCurrentTime ) const
75*b1cdbd2cSJim Jagielski             {
76*b1cdbd2cSJim Jagielski                 // enforce _our_ timeout to our clients (this
77*b1cdbd2cSJim Jagielski                 // overrides any timeout possibly set at the wrappee!)
78*b1cdbd2cSJim Jagielski                 return nCurrentTime + mnTimeout;
79*b1cdbd2cSJim Jagielski             }
80*b1cdbd2cSJim Jagielski 
dispose()81*b1cdbd2cSJim Jagielski             virtual void dispose()
82*b1cdbd2cSJim Jagielski             {
83*b1cdbd2cSJim Jagielski                 mpEvent.reset();
84*b1cdbd2cSJim Jagielski             }
85*b1cdbd2cSJim Jagielski 
86*b1cdbd2cSJim Jagielski         private:
87*b1cdbd2cSJim Jagielski             EventSharedPtr	mpEvent;
88*b1cdbd2cSJim Jagielski             double			mnTimeout;
89*b1cdbd2cSJim Jagielski         };
90*b1cdbd2cSJim Jagielski 
91*b1cdbd2cSJim Jagielski         /// Return value for makeInterruptableDelay()
92*b1cdbd2cSJim Jagielski         struct InterruptableEventPair
93*b1cdbd2cSJim Jagielski         {
94*b1cdbd2cSJim Jagielski             /** This member contains a pointer to the timeout
95*b1cdbd2cSJim Jagielski                 event. When enqueued, this event will fire the
96*b1cdbd2cSJim Jagielski                 requested action only after the specified timeout.
97*b1cdbd2cSJim Jagielski              */
98*b1cdbd2cSJim Jagielski             EventSharedPtr	mpTimeoutEvent;
99*b1cdbd2cSJim Jagielski 
100*b1cdbd2cSJim Jagielski             /** This member contains a pointer to the interruption
101*b1cdbd2cSJim Jagielski                 event. When enqueued, this event will fire
102*b1cdbd2cSJim Jagielski                 immediately, interrupting a potentially waiting
103*b1cdbd2cSJim Jagielski                 timeout event.
104*b1cdbd2cSJim Jagielski              */
105*b1cdbd2cSJim Jagielski             EventSharedPtr	mpImmediateEvent;
106*b1cdbd2cSJim Jagielski         };
107*b1cdbd2cSJim Jagielski 
108*b1cdbd2cSJim Jagielski         /** Generate an interruptable delay event.
109*b1cdbd2cSJim Jagielski 
110*b1cdbd2cSJim Jagielski         	This function generates a pair of events, that are
111*b1cdbd2cSJim Jagielski         	especially tailored to achieve the following behaviour: By
112*b1cdbd2cSJim Jagielski         	default, the given functor is called after the specified
113*b1cdbd2cSJim Jagielski         	timeout (after insertion of the event into the EventQueue,
114*b1cdbd2cSJim Jagielski         	of course). But optionally, when the interruption event
115*b1cdbd2cSJim Jagielski         	InterruptableEventPair::mpImmediateEvent is fired, the
116*b1cdbd2cSJim Jagielski         	given functor is called <em>at once</em>, and the delay is
117*b1cdbd2cSJim Jagielski         	ignored (that means, the given functor is guaranteed to be
118*b1cdbd2cSJim Jagielski         	called at utmost once, and never twice. Furthermore, it is
119*b1cdbd2cSJim Jagielski         	ensured that both events return false on isCharged(), once
120*b1cdbd2cSJim Jagielski         	anyone of them has been fired already).
121*b1cdbd2cSJim Jagielski 
122*b1cdbd2cSJim Jagielski         	@param rFunctor
123*b1cdbd2cSJim Jagielski             Functor to call when the event fires.
124*b1cdbd2cSJim Jagielski 
125*b1cdbd2cSJim Jagielski             @param nTimeout
126*b1cdbd2cSJim Jagielski             Timeout in seconds, to wait until functor is called.
127*b1cdbd2cSJim Jagielski 
128*b1cdbd2cSJim Jagielski 			@returns a pair of events, where the first one waits the
129*b1cdbd2cSJim Jagielski 			specified amount of time, and the other fires the given
130*b1cdbd2cSJim Jagielski 			functor immediately.
131*b1cdbd2cSJim Jagielski          */
makeInterruptableDelay(const Functor & rFunctor,double nTimeout)132*b1cdbd2cSJim Jagielski         template< typename Functor > InterruptableEventPair makeInterruptableDelay( const Functor& 	rFunctor,
133*b1cdbd2cSJim Jagielski                                                                                     double			nTimeout	)
134*b1cdbd2cSJim Jagielski         {
135*b1cdbd2cSJim Jagielski             InterruptableEventPair aRes;
136*b1cdbd2cSJim Jagielski 
137*b1cdbd2cSJim Jagielski             aRes.mpImmediateEvent = makeEvent( rFunctor, "makeInterruptableDelay");
138*b1cdbd2cSJim Jagielski             aRes.mpTimeoutEvent.reset( new DelayFacade( aRes.mpImmediateEvent,
139*b1cdbd2cSJim Jagielski                                                         nTimeout ) );
140*b1cdbd2cSJim Jagielski 
141*b1cdbd2cSJim Jagielski             return aRes;
142*b1cdbd2cSJim Jagielski         }
143*b1cdbd2cSJim Jagielski     }
144*b1cdbd2cSJim Jagielski }
145*b1cdbd2cSJim Jagielski 
146*b1cdbd2cSJim Jagielski #endif /* INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX */
147