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