Bump version to 6.0-36
[LibreOffice.git] / slideshow / source / inc / interruptabledelayevent.hxx
blobf3cdd6c67cfaca968a64e15630cad005635c4c41
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SLIDESHOW_SOURCE_INC_INTERRUPTABLEDELAYEVENT_HXX
21 #define INCLUDED_SLIDESHOW_SOURCE_INC_INTERRUPTABLEDELAYEVENT_HXX
23 #include "delayevent.hxx"
25 namespace slideshow
27 namespace internal
29 /** Event, which delays calling passed Event's fire() method
30 the given amount of time.
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.
36 class DelayFacade : public Event
38 public:
39 DelayFacade( const EventSharedPtr& rEvent,
40 double nTimeout ) :
41 Event("DelayFacade"),
42 mpEvent( rEvent ),
43 mnTimeout( nTimeout )
47 virtual bool fire() override
49 if( mpEvent && isCharged() )
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 return mpEvent->fire();
57 return false;
60 virtual bool isCharged() const override
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 return mpEvent && mpEvent->isCharged();
68 virtual double getActivationTime( double nCurrentTime ) const override
70 // enforce _our_ timeout to our clients (this
71 // overrides any timeout possibly set at the wrappee!)
72 return nCurrentTime + mnTimeout;
75 virtual void dispose() override
77 mpEvent.reset();
80 private:
81 EventSharedPtr mpEvent;
82 double mnTimeout;
85 /// Return value for makeInterruptableDelay()
86 struct InterruptableEventPair
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.
92 EventSharedPtr mpTimeoutEvent;
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.
99 EventSharedPtr mpImmediateEvent;
102 /** Generate an interruptable delay event.
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).
116 @param rFunctor
117 Functor to call when the event fires.
119 @param nTimeout
120 Timeout in seconds, to wait until functor is called.
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.
126 template< typename Functor > InterruptableEventPair makeInterruptableDelay( const Functor& rFunctor,
127 double nTimeout )
129 InterruptableEventPair aRes;
131 aRes.mpImmediateEvent = makeEvent( rFunctor, "makeInterruptableDelay");
132 aRes.mpTimeoutEvent.reset( new DelayFacade( aRes.mpImmediateEvent,
133 nTimeout ) );
135 return aRes;
140 #endif // INCLUDED_SLIDESHOW_SOURCE_INC_INTERRUPTABLEDELAYEVENT_HXX
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */