bump product version to 4.1.6.2
[LibreOffice.git] / slideshow / source / engine / activitiesqueue.cxx
blob7a3c215a47388a15e3ff0883c8cfa71879b70919
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>
28 #include "slideshowexceptions.hxx"
29 #include "activity.hxx"
30 #include "activitiesqueue.hxx"
32 #include <boost/mem_fn.hpp>
33 #include <boost/shared_ptr.hpp>
34 #include <algorithm>
37 using namespace ::com::sun::star;
39 namespace slideshow
41 namespace internal
43 ActivitiesQueue::ActivitiesQueue(
44 const ::boost::shared_ptr< ::canvas::tools::ElapsedTime >& pPresTimer ) :
45 mpTimer( pPresTimer ),
46 maCurrentActivitiesWaiting(),
47 maCurrentActivitiesReinsert(),
48 maDequeuedActivities()
52 ActivitiesQueue::~ActivitiesQueue()
54 // dispose all queue entries
55 try
57 std::for_each( maCurrentActivitiesWaiting.begin(),
58 maCurrentActivitiesWaiting.end(),
59 boost::mem_fn( &Disposable::dispose ) );
60 std::for_each( maCurrentActivitiesReinsert.begin(),
61 maCurrentActivitiesReinsert.end(),
62 boost::mem_fn( &Disposable::dispose ) );
64 catch (uno::Exception &)
66 OSL_FAIL( OUStringToOString(
67 comphelper::anyToString(
68 cppu::getCaughtException() ),
69 RTL_TEXTENCODING_UTF8 ).getStr() );
73 bool ActivitiesQueue::addActivity( const ActivitySharedPtr& pActivity )
75 OSL_ENSURE( pActivity, "ActivitiesQueue::addActivity: activity ptr NULL" );
77 if( !pActivity )
78 return false;
80 // add entry to waiting list
81 maCurrentActivitiesWaiting.push_back( pActivity );
83 return true;
86 void ActivitiesQueue::process()
88 VERBOSE_TRACE( "ActivitiesQueue: outer loop heartbeat" );
90 // accumulate time lag for all activities, and lag time
91 // base if necessary:
92 ActivityQueue::const_iterator iPos(
93 maCurrentActivitiesWaiting.begin() );
94 const ActivityQueue::const_iterator iEnd(
95 maCurrentActivitiesWaiting.end() );
96 double fLag = 0.0;
97 for ( ; iPos != iEnd; ++iPos )
98 fLag = std::max<double>( fLag, (*iPos)->calcTimeLag() );
99 if (fLag > 0.0)
101 mpTimer->adjustTimer( -fLag );
104 // process list of activities
105 while( !maCurrentActivitiesWaiting.empty() )
107 // process topmost activity
108 ActivitySharedPtr pActivity( maCurrentActivitiesWaiting.front() );
109 maCurrentActivitiesWaiting.pop_front();
111 bool bReinsert( false );
115 // fire up activity
116 bReinsert = pActivity->perform();
118 catch( uno::RuntimeException& )
120 throw;
122 catch( uno::Exception& )
124 // catch anything here, we don't want
125 // to leave this scope under _any_
126 // circumstance. Although, do _not_
127 // reinsert an activity that threw
128 // once.
130 // NOTE: we explicitly don't catch(...) here,
131 // since this will also capture segmentation
132 // violations and the like. In such a case, we
133 // still better let our clients now...
134 OSL_FAIL( OUStringToOString(
135 comphelper::anyToString( cppu::getCaughtException() ),
136 RTL_TEXTENCODING_UTF8 ).getStr() );
138 catch( SlideShowException& )
140 // catch anything here, we don't want
141 // to leave this scope under _any_
142 // circumstance. Although, do _not_
143 // reinsert an activity that threw
144 // once.
146 // NOTE: we explicitly don't catch(...) here,
147 // since this will also capture segmentation
148 // violations and the like. In such a case, we
149 // still better let our clients now...
150 OSL_TRACE( "::presentation::internal::ActivitiesQueue: Activity threw a SlideShowException, removing from ring" );
153 if( bReinsert )
154 maCurrentActivitiesReinsert.push_back( pActivity );
155 else
156 maDequeuedActivities.push_back( pActivity );
158 VERBOSE_TRACE( "ActivitiesQueue: inner loop heartbeat" );
161 if( !maCurrentActivitiesReinsert.empty() )
163 // reinsert all processed, but not finished
164 // activities back to waiting queue. With swap(),
165 // we kill two birds with one stone: we reuse the
166 // list nodes, and we clear the
167 // maCurrentActivitiesReinsert list
168 maCurrentActivitiesWaiting.swap( maCurrentActivitiesReinsert );
172 void ActivitiesQueue::processDequeued()
174 // notify all dequeued activities from last round
175 ::std::for_each( maDequeuedActivities.begin(),
176 maDequeuedActivities.end(),
177 ::boost::mem_fn( &Activity::dequeued ) );
178 maDequeuedActivities.clear();
181 bool ActivitiesQueue::isEmpty() const
183 return maCurrentActivitiesWaiting.empty() && maCurrentActivitiesReinsert.empty();
186 void ActivitiesQueue::clear()
188 // dequeue all entries:
189 std::for_each( maCurrentActivitiesWaiting.begin(),
190 maCurrentActivitiesWaiting.end(),
191 boost::mem_fn( &Activity::dequeued ) );
192 ActivityQueue().swap( maCurrentActivitiesWaiting );
194 std::for_each( maCurrentActivitiesReinsert.begin(),
195 maCurrentActivitiesReinsert.end(),
196 boost::mem_fn( &Activity::dequeued ) );
197 ActivityQueue().swap( maCurrentActivitiesReinsert );
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */