xref: /trunk/main/slideshow/source/inc/eventqueue.hxx (revision e8df90a080f9dc7518c4b937d7dcd461d7b4d178)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_SLIDESHOW_EVENTQUEUE_HXX
25 #define INCLUDED_SLIDESHOW_EVENTQUEUE_HXX
26 
27 #include <canvas/elapsedtime.hxx>
28 #include <osl/mutex.hxx>
29 
30 #include "event.hxx"
31 
32 #include <boost/noncopyable.hpp>
33 #include <functional>
34 #include <queue>
35 #include <vector>
36 
37 
38 /* Definition of ActivitiesQueue class */
39 
40 namespace slideshow
41 {
42     namespace internal
43     {
44         /** This class handles events in a presentation. Events are
45             time instants where e.g. effects start.
46          */
47         class EventQueue : private ::boost::noncopyable
48         {
49         public:
50             EventQueue(
51                 ::boost::shared_ptr< ::canvas::tools::ElapsedTime >
52                 const & pPresTimer );
53 
54             ~EventQueue();
55 
56             /** Add the given event to the queue. The event is fired
57                 at, or shortly after, its Event::getActivationTime instant.
58              */
59             bool addEvent( const EventSharedPtr& event );
60 
61             /** Add the given event to the queue. The event is fired
62                 at, or shortly after, its Event::getActivationTime instant.
63                 The difference to addEvent() is that events added during
64                 process() are postponed to next process().
65              */
66             bool addEventForNextRound( const EventSharedPtr& event );
67 
68             /** Another way to control the order of asynchronous event
69                 exeqution.  Use this method to schedule events that are to
70                 be executed after all regular events that have no delay,
71                 even when they schedule new regular events without delay.
72             */
73             bool addEventWhenQueueIsEmpty (const EventSharedPtr& rpEvent);
74 
75             /** Process the event queue.
76 
77                 This method executes all events whose timeout has
78                 expired when calling this method (i.e. all events
79                 whose scheduled time is less or equal the current
80                 time).
81 
82                 Check for the next available event's timeout via
83                 nextTimeout(), or whether the queue is empty
84                 altogether via isEmpty().
85              */
86             void process();
87 
88             /** Query state of the queue
89 
90                 @return false, if queue is empty, true otherwise
91              */
92             bool isEmpty() const;
93 
94             /** Query timeout for the topmost event in the queue.
95 
96                 @return Timeout in seconds, until the next event is
97                 ready. The time returned here is relative to the pres
98                 timer (i.e. the timer specified at the EventQueue
99                 constructor). When the queue is empty (i.e. isEmpty()
100                 returns true), the returned value is the highest
101                 representable double value
102                 (::std::numeric_limits<double>::max()). If the topmost
103                 event in the queue is already pending, the timeout
104                 returned here will actually be negative.
105             */
106             double nextTimeout() const;
107 
108             /** Remove all pending events from the queue.
109              */
110             void clear();
111 
112             /** Forces an empty queue, firing all events immediately
113                 without minding any times.
114                 @attention do only call from event loop, this calls process_()!
115              */
116             void forceEmpty();
117 
118             /** Gets the queue's timer object.
119              */
120             ::boost::shared_ptr< ::canvas::tools::ElapsedTime > const &
getTimer() const121             getTimer() const { return mpTimer; }
122 
123         private:
124             mutable ::osl::Mutex      maMutex;
125 
126             struct EventEntry
127             {
128                 typedef EventEntry argument_type;
129                 typedef bool result_type;
130                 EventSharedPtr  pEvent;
131                 double          nTime;
132 
133                 bool operator<( const EventEntry& ) const; // to leverage priority_queue's default compare
134 
EventEntryslideshow::internal::EventQueue::EventEntry135                 EventEntry( EventSharedPtr const& p, double t )
136                     : pEvent(p), nTime(t) {}
137             };
138 
139             typedef ::std::priority_queue< EventEntry > ImplQueueType;
140             ImplQueueType                   maEvents;
141             typedef ::std::vector<EventEntry> EventEntryVector;
142             EventEntryVector                maNextEvents;
143             ImplQueueType                   maNextNextEvents;
144             void process_( bool bFireAllEvents );
145 
146             // perform timing of events via relative time
147             // measurements. The world time starts, when the
148             // EventQueue object is created
149             ::boost::shared_ptr< ::canvas::tools::ElapsedTime > mpTimer;
150         };
151 
152     }
153 }
154 #endif /* INCLUDED_SLIDESHOW_EVENTQUEUE_HXX */
155