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