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