Update ooo320-m1
[ooovba.git] / slideshow / source / engine / activitiesqueue.cxx
blobc9052f7ecec21b3b74008353790ea5fa550ff37b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: activitiesqueue.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_slideshow.hxx"
34 // must be first
35 #include <canvas/debug.hxx>
36 #include <canvas/verbosetrace.hxx>
38 #include <comphelper/anytostring.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
41 #include "slideshowexceptions.hxx"
42 #include "activity.hxx"
43 #include "activitiesqueue.hxx"
45 #include <boost/bind.hpp>
46 #include <algorithm>
49 using namespace ::com::sun::star;
51 namespace slideshow
53 namespace internal
55 ActivitiesQueue::ActivitiesQueue(
56 const ::boost::shared_ptr< ::canvas::tools::ElapsedTime >& pPresTimer ) :
57 mpTimer( pPresTimer ),
58 maCurrentActivitiesWaiting(),
59 maCurrentActivitiesReinsert(),
60 maDequeuedActivities()
64 ActivitiesQueue::~ActivitiesQueue()
66 // dispose all queue entries
67 try
69 std::for_each( maCurrentActivitiesWaiting.begin(),
70 maCurrentActivitiesWaiting.end(),
71 boost::mem_fn( &Disposable::dispose ) );
72 std::for_each( maCurrentActivitiesReinsert.begin(),
73 maCurrentActivitiesReinsert.end(),
74 boost::mem_fn( &Disposable::dispose ) );
76 catch (uno::Exception &)
78 OSL_ENSURE( false, rtl::OUStringToOString(
79 comphelper::anyToString(
80 cppu::getCaughtException() ),
81 RTL_TEXTENCODING_UTF8 ).getStr() );
85 bool ActivitiesQueue::addActivity( const ActivitySharedPtr& pActivity )
87 OSL_ENSURE( pActivity, "ActivitiesQueue::addActivity: activity ptr NULL" );
89 if( !pActivity )
90 return false;
92 // add entry to waiting list
93 maCurrentActivitiesWaiting.push_back( pActivity );
95 return true;
98 void ActivitiesQueue::process()
100 VERBOSE_TRACE( "ActivitiesQueue: outer loop heartbeat" );
102 // accumulate time lag for all activities, and lag time
103 // base if necessary:
104 ActivityQueue::const_iterator iPos(
105 maCurrentActivitiesWaiting.begin() );
106 const ActivityQueue::const_iterator iEnd(
107 maCurrentActivitiesWaiting.end() );
108 double fLag = 0.0;
109 for ( ; iPos != iEnd; ++iPos )
110 fLag = std::max<double>( fLag, (*iPos)->calcTimeLag() );
111 if (fLag > 0.0)
113 mpTimer->adjustTimer( -fLag );
116 // process list of activities
117 while( !maCurrentActivitiesWaiting.empty() )
119 // process topmost activity
120 ActivitySharedPtr pActivity( maCurrentActivitiesWaiting.front() );
121 maCurrentActivitiesWaiting.pop_front();
123 bool bReinsert( false );
127 // fire up activity
128 bReinsert = pActivity->perform();
130 catch( uno::RuntimeException& )
132 throw;
134 catch( uno::Exception& )
136 // catch anything here, we don't want
137 // to leave this scope under _any_
138 // circumstance. Although, do _not_
139 // reinsert an activity that threw
140 // once.
142 // NOTE: we explicitely don't catch(...) here,
143 // since this will also capture segmentation
144 // violations and the like. In such a case, we
145 // still better let our clients now...
146 OSL_ENSURE( false,
147 rtl::OUStringToOString(
148 comphelper::anyToString( cppu::getCaughtException() ),
149 RTL_TEXTENCODING_UTF8 ).getStr() );
151 catch( SlideShowException& )
153 // catch anything here, we don't want
154 // to leave this scope under _any_
155 // circumstance. Although, do _not_
156 // reinsert an activity that threw
157 // once.
159 // NOTE: we explicitely don't catch(...) here,
160 // since this will also capture segmentation
161 // violations and the like. In such a case, we
162 // still better let our clients now...
163 OSL_TRACE( "::presentation::internal::ActivitiesQueue: Activity threw a SlideShowException, removing from ring" );
166 if( bReinsert )
167 maCurrentActivitiesReinsert.push_back( pActivity );
168 else
169 maDequeuedActivities.push_back( pActivity );
171 VERBOSE_TRACE( "ActivitiesQueue: inner loop heartbeat" );
174 if( !maCurrentActivitiesReinsert.empty() )
176 // reinsert all processed, but not finished
177 // activities back to waiting queue. With swap(),
178 // we kill two birds with one stone: we reuse the
179 // list nodes, and we clear the
180 // maCurrentActivitiesReinsert list
181 maCurrentActivitiesWaiting.swap( maCurrentActivitiesReinsert );
185 void ActivitiesQueue::processDequeued()
187 // notify all dequeued activities from last round
188 ::std::for_each( maDequeuedActivities.begin(),
189 maDequeuedActivities.end(),
190 ::boost::mem_fn( &Activity::dequeued ) );
191 maDequeuedActivities.clear();
194 bool ActivitiesQueue::isEmpty() const
196 return maCurrentActivitiesWaiting.empty() && maCurrentActivitiesReinsert.empty();
199 void ActivitiesQueue::clear()
201 // dequeue all entries:
202 std::for_each( maCurrentActivitiesWaiting.begin(),
203 maCurrentActivitiesWaiting.end(),
204 boost::mem_fn( &Activity::dequeued ) );
205 ActivityQueue().swap( maCurrentActivitiesWaiting );
207 std::for_each( maCurrentActivitiesReinsert.begin(),
208 maCurrentActivitiesReinsert.end(),
209 boost::mem_fn( &Activity::dequeued ) );
210 ActivityQueue().swap( maCurrentActivitiesReinsert );