xref: /AOO41X/main/slideshow/source/inc/eventqueue.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef INCLUDED_SLIDESHOW_EVENTQUEUE_HXX
29*cdf0e10cSrcweir #define INCLUDED_SLIDESHOW_EVENTQUEUE_HXX
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <canvas/elapsedtime.hxx>
32*cdf0e10cSrcweir #include <osl/mutex.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "event.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <boost/noncopyable.hpp>
37*cdf0e10cSrcweir #include <functional>
38*cdf0e10cSrcweir #include <queue>
39*cdf0e10cSrcweir #include <vector>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /* Definition of ActivitiesQueue class */
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir namespace slideshow
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir     namespace internal
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir 		/** This class handles events in a presentation. Events are
49*cdf0e10cSrcweir             time instants where e.g. effects start.
50*cdf0e10cSrcweir          */
51*cdf0e10cSrcweir         class EventQueue : private ::boost::noncopyable
52*cdf0e10cSrcweir         {
53*cdf0e10cSrcweir         public:
54*cdf0e10cSrcweir             EventQueue(
55*cdf0e10cSrcweir                 ::boost::shared_ptr< ::canvas::tools::ElapsedTime >
56*cdf0e10cSrcweir                 const & pPresTimer );
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir             ~EventQueue();
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir             /** Add the given event to the queue. The event is fired
61*cdf0e10cSrcweir                 at, or shortly after, its Event::getActivationTime instant.
62*cdf0e10cSrcweir              */
63*cdf0e10cSrcweir             bool addEvent( const EventSharedPtr& event );
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir             /** Add the given event to the queue. The event is fired
66*cdf0e10cSrcweir                 at, or shortly after, its Event::getActivationTime instant.
67*cdf0e10cSrcweir                 The difference to addEvent() is that events added during
68*cdf0e10cSrcweir                 process() are postponed to next process().
69*cdf0e10cSrcweir              */
70*cdf0e10cSrcweir             bool addEventForNextRound( const EventSharedPtr& event );
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir             /** Another way to control the order of asynchronous event
73*cdf0e10cSrcweir                 exeqution.  Use this method to schedule events that are to
74*cdf0e10cSrcweir                 be executed after all regular events that have no delay,
75*cdf0e10cSrcweir                 even when they schedule new regular events without delay.
76*cdf0e10cSrcweir             */
77*cdf0e10cSrcweir             bool addEventWhenQueueIsEmpty (const EventSharedPtr& rpEvent);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir             /** Process the event queue.
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir             	This method executes all events whose timeout has
82*cdf0e10cSrcweir             	expired when calling this method (i.e. all events
83*cdf0e10cSrcweir             	whose scheduled time is less or equal the current
84*cdf0e10cSrcweir             	time).
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir                 Check for the next available event's timeout via
87*cdf0e10cSrcweir                 nextTimeout(), or whether the queue is empty
88*cdf0e10cSrcweir                 altogether via isEmpty().
89*cdf0e10cSrcweir              */
90*cdf0e10cSrcweir             void process();
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 			/** Query state of the queue
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir                 @return false, if queue is empty, true otherwise
95*cdf0e10cSrcweir              */
96*cdf0e10cSrcweir             bool isEmpty() const;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir             /** Query timeout for the topmost event in the queue.
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir                 @return Timeout in seconds, until the next event is
101*cdf0e10cSrcweir                 ready. The time returned here is relative to the pres
102*cdf0e10cSrcweir                 timer (i.e. the timer specified at the EventQueue
103*cdf0e10cSrcweir                 constructor). When the queue is empty (i.e. isEmpty()
104*cdf0e10cSrcweir                 returns true), the returned value is the highest
105*cdf0e10cSrcweir                 representable double value
106*cdf0e10cSrcweir                 (::std::numeric_limits<double>::max()). If the topmost
107*cdf0e10cSrcweir                 event in the queue is already pending, the timeout
108*cdf0e10cSrcweir                 returned here will actually be negative.
109*cdf0e10cSrcweir             */
110*cdf0e10cSrcweir             double nextTimeout() const;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir             /** Remove all pending events from the queue.
113*cdf0e10cSrcweir              */
114*cdf0e10cSrcweir             void clear();
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             /** Forces an empty queue, firing all events immediately
117*cdf0e10cSrcweir                 without minding any times.
118*cdf0e10cSrcweir                 @attention do only call from event loop, this calls process_()!
119*cdf0e10cSrcweir              */
120*cdf0e10cSrcweir             void forceEmpty();
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir             /** Gets the queue's timer object.
123*cdf0e10cSrcweir              */
124*cdf0e10cSrcweir             ::boost::shared_ptr< ::canvas::tools::ElapsedTime > const &
125*cdf0e10cSrcweir             getTimer() const { return mpTimer; }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir         private:
128*cdf0e10cSrcweir             mutable ::osl::Mutex      maMutex;
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             struct EventEntry : public ::std::unary_function<EventEntry, bool>
131*cdf0e10cSrcweir             {
132*cdf0e10cSrcweir                 EventSharedPtr	pEvent;
133*cdf0e10cSrcweir                 double			nTime;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir                 bool operator<( const EventEntry& ) const; // to leverage priority_queue's default compare
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir                 EventEntry( EventSharedPtr const& p, double t )
138*cdf0e10cSrcweir                     : pEvent(p), nTime(t) {}
139*cdf0e10cSrcweir             };
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir             typedef ::std::priority_queue< EventEntry > ImplQueueType;
142*cdf0e10cSrcweir             ImplQueueType					maEvents;
143*cdf0e10cSrcweir             typedef ::std::vector<EventEntry> EventEntryVector;
144*cdf0e10cSrcweir             EventEntryVector                maNextEvents;
145*cdf0e10cSrcweir             ImplQueueType                   maNextNextEvents;
146*cdf0e10cSrcweir             void process_( bool bFireAllEvents );
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             // perform timing of events via relative time
149*cdf0e10cSrcweir             // measurements. The world time starts, when the
150*cdf0e10cSrcweir             // EventQueue object is created
151*cdf0e10cSrcweir             ::boost::shared_ptr< ::canvas::tools::ElapsedTime > mpTimer;
152*cdf0e10cSrcweir         };
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir #endif /* INCLUDED_SLIDESHOW_EVENTQUEUE_HXX */
157