Branch data 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 : : #if OSL_DEBUG_LEVEL > 1
42 : : Event(::rtl::OUString("DelayFacade")),
43 : : #endif
44 : : mpEvent( rEvent ),
45 : 0 : mnTimeout( nTimeout )
46 : : {
47 : 0 : }
48 : :
49 : 0 : virtual bool fire()
50 : : {
51 : 0 : if( mpEvent && isCharged() )
52 : : {
53 : : // pass on directly - we're supposed to be called
54 : : // from EventQueue here, anyway - and if not,
55 : : // we're only keeping that incorrect transitively.
56 : 0 : return mpEvent->fire();
57 : : }
58 : :
59 : 0 : return false;
60 : : }
61 : :
62 : 0 : virtual bool isCharged() const
63 : : {
64 : : // pass on to wrappee - this ensures that we return
65 : : // false on isCharged(), even if the other event has
66 : : // been fired outside our own fire() method
67 : 0 : return !mpEvent ? false : mpEvent->isCharged();
68 : : }
69 : :
70 : 0 : virtual double getActivationTime( double nCurrentTime ) const
71 : : {
72 : : // enforce _our_ timeout to our clients (this
73 : : // overrides any timeout possibly set at the wrappee!)
74 : 0 : return nCurrentTime + mnTimeout;
75 : : }
76 : :
77 : 0 : virtual void dispose()
78 : : {
79 : 0 : mpEvent.reset();
80 : 0 : }
81 : :
82 : : private:
83 : : EventSharedPtr mpEvent;
84 : : double mnTimeout;
85 : : };
86 : :
87 : : /// Return value for makeInterruptableDelay()
88 : 0 : struct InterruptableEventPair
89 : : {
90 : : /** This member contains a pointer to the timeout
91 : : event. When enqueued, this event will fire the
92 : : requested action only after the specified timeout.
93 : : */
94 : : EventSharedPtr mpTimeoutEvent;
95 : :
96 : : /** This member contains a pointer to the interruption
97 : : event. When enqueued, this event will fire
98 : : immediately, interrupting a potentially waiting
99 : : timeout event.
100 : : */
101 : : EventSharedPtr mpImmediateEvent;
102 : : };
103 : :
104 : : /** Generate an interruptable delay event.
105 : :
106 : : This function generates a pair of events, that are
107 : : especially tailored to achieve the following behaviour: By
108 : : default, the given functor is called after the specified
109 : : timeout (after insertion of the event into the EventQueue,
110 : : of course). But optionally, when the interruption event
111 : : InterruptableEventPair::mpImmediateEvent is fired, the
112 : : given functor is called <em>at once</em>, and the delay is
113 : : ignored (that means, the given functor is guaranteed to be
114 : : called at utmost once, and never twice. Furthermore, it is
115 : : ensured that both events return false on isCharged(), once
116 : : anyone of them has been fired already).
117 : :
118 : : @param rFunctor
119 : : Functor to call when the event fires.
120 : :
121 : : @param nTimeout
122 : : Timeout in seconds, to wait until functor is called.
123 : :
124 : : @returns a pair of events, where the first one waits the
125 : : specified amount of time, and the other fires the given
126 : : functor immediately.
127 : : */
128 : 0 : template< typename Functor > InterruptableEventPair makeInterruptableDelay( const Functor& rFunctor,
129 : : double nTimeout )
130 : : {
131 : 0 : InterruptableEventPair aRes;
132 : :
133 : 0 : aRes.mpImmediateEvent = makeEvent( rFunctor, "makeInterruptableDelay");
134 : 0 : aRes.mpTimeoutEvent.reset( new DelayFacade( aRes.mpImmediateEvent,
135 : : nTimeout ) );
136 : :
137 : 0 : return aRes;
138 : : }
139 : : }
140 : : }
141 : :
142 : : #endif /* INCLUDED_SLIDESHOW_INTERRUPTABLEDELAYEVENT_HXX */
143 : :
144 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|