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