bump product version to 5.0.4.1
[LibreOffice.git] / slideshow / source / engine / activitiesqueue.cxx
blob3cca2ef469294f072873bde4afafc49ec14ed5e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 // must be first
22 #include <canvas/debug.hxx>
23 #include <canvas/verbosetrace.hxx>
25 #include <comphelper/anytostring.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
27 #include <osl/diagnose.h>
29 #include "slideshowexceptions.hxx"
30 #include "activity.hxx"
31 #include "activitiesqueue.hxx"
33 #include <boost/mem_fn.hpp>
34 #include <boost/shared_ptr.hpp>
35 #include <algorithm>
38 using namespace ::com::sun::star;
40 namespace slideshow
42 namespace internal
44 ActivitiesQueue::ActivitiesQueue(
45 const ::boost::shared_ptr< ::canvas::tools::ElapsedTime >& pPresTimer ) :
46 mpTimer( pPresTimer ),
47 maCurrentActivitiesWaiting(),
48 maCurrentActivitiesReinsert(),
49 maDequeuedActivities()
53 ActivitiesQueue::~ActivitiesQueue()
55 // dispose all queue entries
56 try
58 std::for_each( maCurrentActivitiesWaiting.begin(),
59 maCurrentActivitiesWaiting.end(),
60 boost::mem_fn( &Disposable::dispose ) );
61 std::for_each( maCurrentActivitiesReinsert.begin(),
62 maCurrentActivitiesReinsert.end(),
63 boost::mem_fn( &Disposable::dispose ) );
65 catch (uno::Exception &)
67 OSL_FAIL( OUStringToOString(
68 comphelper::anyToString(
69 cppu::getCaughtException() ),
70 RTL_TEXTENCODING_UTF8 ).getStr() );
74 bool ActivitiesQueue::addActivity( const ActivitySharedPtr& pActivity )
76 OSL_ENSURE( pActivity, "ActivitiesQueue::addActivity: activity ptr NULL" );
78 if( !pActivity )
79 return false;
81 // add entry to waiting list
82 maCurrentActivitiesWaiting.push_back( pActivity );
84 return true;
87 void ActivitiesQueue::process()
89 VERBOSE_TRACE( "ActivitiesQueue: outer loop heartbeat" );
91 // accumulate time lag for all activities, and lag time
92 // base if necessary:
93 ActivityQueue::const_iterator iPos(
94 maCurrentActivitiesWaiting.begin() );
95 const ActivityQueue::const_iterator iEnd(
96 maCurrentActivitiesWaiting.end() );
97 double fLag = 0.0;
98 for ( ; iPos != iEnd; ++iPos )
99 fLag = std::max<double>( fLag, (*iPos)->calcTimeLag() );
100 if (fLag > 0.0)
102 mpTimer->adjustTimer( -fLag );
105 // process list of activities
106 while( !maCurrentActivitiesWaiting.empty() )
108 // process topmost activity
109 ActivitySharedPtr pActivity( maCurrentActivitiesWaiting.front() );
110 maCurrentActivitiesWaiting.pop_front();
112 bool bReinsert( false );
116 // fire up activity
117 bReinsert = pActivity->perform();
119 catch( uno::RuntimeException& )
121 throw;
123 catch( uno::Exception& )
125 // catch anything here, we don't want
126 // to leave this scope under _any_
127 // circumstance. Although, do _not_
128 // reinsert an activity that threw
129 // once.
131 // NOTE: we explicitly don't catch(...) here,
132 // since this will also capture segmentation
133 // violations and the like. In such a case, we
134 // still better let our clients now...
135 OSL_FAIL( OUStringToOString(
136 comphelper::anyToString( cppu::getCaughtException() ),
137 RTL_TEXTENCODING_UTF8 ).getStr() );
139 catch( SlideShowException& )
141 // catch anything here, we don't want
142 // to leave this scope under _any_
143 // circumstance. Although, do _not_
144 // reinsert an activity that threw
145 // once.
147 // NOTE: we explicitly don't catch(...) here,
148 // since this will also capture segmentation
149 // violations and the like. In such a case, we
150 // still better let our clients now...
151 OSL_TRACE( "::presentation::internal::ActivitiesQueue: Activity threw a SlideShowException, removing from ring" );
154 if( bReinsert )
155 maCurrentActivitiesReinsert.push_back( pActivity );
156 else
157 maDequeuedActivities.push_back( pActivity );
159 VERBOSE_TRACE( "ActivitiesQueue: inner loop heartbeat" );
162 if( !maCurrentActivitiesReinsert.empty() )
164 // reinsert all processed, but not finished
165 // activities back to waiting queue. With swap(),
166 // we kill two birds with one stone: we reuse the
167 // list nodes, and we clear the
168 // maCurrentActivitiesReinsert list
169 maCurrentActivitiesWaiting.swap( maCurrentActivitiesReinsert );
173 void ActivitiesQueue::processDequeued()
175 // notify all dequeued activities from last round
176 ::std::for_each( maDequeuedActivities.begin(),
177 maDequeuedActivities.end(),
178 ::boost::mem_fn( &Activity::dequeued ) );
179 maDequeuedActivities.clear();
182 bool ActivitiesQueue::isEmpty() const
184 return maCurrentActivitiesWaiting.empty() && maCurrentActivitiesReinsert.empty();
187 void ActivitiesQueue::clear()
189 // dequeue all entries:
190 std::for_each( maCurrentActivitiesWaiting.begin(),
191 maCurrentActivitiesWaiting.end(),
192 boost::mem_fn( &Activity::dequeued ) );
193 ActivityQueue().swap( maCurrentActivitiesWaiting );
195 std::for_each( maCurrentActivitiesReinsert.begin(),
196 maCurrentActivitiesReinsert.end(),
197 boost::mem_fn( &Activity::dequeued ) );
198 ActivityQueue().swap( maCurrentActivitiesReinsert );
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */