Use o3tl::convert in Math
[LibreOffice.git] / slideshow / source / engine / activitiesqueue.cxx
blob2e3b29d9dbc7393302d49ff75f7b516b4c25bc6f
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 #include <tools/diagnose_ex.h>
22 #include <osl/diagnose.h>
23 #include <sal/log.hxx>
25 #include <slideshowexceptions.hxx>
26 #include <activity.hxx>
27 #include <activitiesqueue.hxx>
29 #include <algorithm>
30 #include <memory>
33 using namespace ::com::sun::star;
35 namespace slideshow::internal
37 ActivitiesQueue::ActivitiesQueue(
38 const std::shared_ptr< ::canvas::tools::ElapsedTime >& pPresTimer ) :
39 mpTimer( pPresTimer ),
40 maCurrentActivitiesWaiting(),
41 maCurrentActivitiesReinsert(),
42 maDequeuedActivities()
46 ActivitiesQueue::~ActivitiesQueue()
48 // dispose all queue entries
49 try
51 for( const auto& pActivity : maCurrentActivitiesWaiting )
52 pActivity->dispose();
53 for( const auto& pActivity : maCurrentTailActivitiesWaiting )
54 pActivity->dispose();
55 for( const auto& pActivity : maCurrentActivitiesReinsert )
56 pActivity->dispose();
58 catch (const uno::Exception&)
60 TOOLS_WARN_EXCEPTION("slideshow", "");
64 bool ActivitiesQueue::addActivity( const ActivitySharedPtr& pActivity )
66 OSL_ENSURE( pActivity, "ActivitiesQueue::addActivity: activity ptr NULL" );
68 if( !pActivity )
69 return false;
71 // add entry to waiting list
72 maCurrentActivitiesWaiting.push_back( pActivity );
74 return true;
77 bool ActivitiesQueue::addTailActivity(const ActivitySharedPtr &pActivity)
79 OSL_ENSURE( pActivity, "ActivitiesQueue::addTailActivity: activity ptr NULL" );
81 if( !pActivity )
82 return false;
84 // Activities that should be processed last are kept in a different
85 // ActivityQueue, and later appended to the end of the maCurrentActivitiesWaiting
86 // on the beginning of ActivitiesQueue::process()
87 maCurrentTailActivitiesWaiting.push_back( pActivity );
89 return true;
92 void ActivitiesQueue::process()
94 SAL_INFO("slideshow.verbose", "ActivitiesQueue: outer loop heartbeat" );
96 // If there are activities to be processed last append them to the end of the ActivitiesQueue
97 maCurrentActivitiesWaiting.insert( maCurrentActivitiesWaiting.end(),
98 maCurrentTailActivitiesWaiting.begin(),
99 maCurrentTailActivitiesWaiting.end() );
100 maCurrentTailActivitiesWaiting.clear();
102 // accumulate time lag for all activities, and lag time
103 // base if necessary:
104 double fLag = 0.0;
105 for ( const auto& rxActivity : maCurrentActivitiesWaiting )
106 fLag = std::max<double>( fLag, rxActivity->calcTimeLag() );
107 if (fLag > 0.0)
109 mpTimer->adjustTimer( -fLag );
112 // process list of activities
113 while( !maCurrentActivitiesWaiting.empty() )
115 // process topmost activity
116 ActivitySharedPtr pActivity( maCurrentActivitiesWaiting.front() );
117 maCurrentActivitiesWaiting.pop_front();
119 bool bReinsert( false );
123 // fire up activity
124 bReinsert = pActivity->perform();
126 catch( uno::RuntimeException& )
128 throw;
130 catch( uno::Exception& )
132 // catch anything here, we don't want
133 // to leave this scope under _any_
134 // circumstance. Although, do _not_
135 // reinsert an activity that threw
136 // once.
138 // NOTE: we explicitly don't catch(...) here,
139 // since this will also capture segmentation
140 // violations and the like. In such a case, we
141 // still better let our clients now...
142 TOOLS_WARN_EXCEPTION( "slideshow", "" );
144 catch( SlideShowException& )
146 // catch anything here, we don't want
147 // to leave this scope under _any_
148 // circumstance. Although, do _not_
149 // reinsert an activity that threw
150 // once.
152 // NOTE: we explicitly don't catch(...) here,
153 // since this will also capture segmentation
154 // violations and the like. In such a case, we
155 // still better let our clients now...
156 SAL_WARN("slideshow", "::presentation::internal::ActivitiesQueue: Activity threw a SlideShowException, removing from ring" );
159 if( bReinsert )
160 maCurrentActivitiesReinsert.push_back( pActivity );
161 else
162 maDequeuedActivities.push_back( pActivity );
164 SAL_INFO("slideshow.verbose", "ActivitiesQueue: inner loop heartbeat" );
167 if( !maCurrentActivitiesReinsert.empty() )
169 // reinsert all processed, but not finished
170 // activities back to waiting queue. With swap(),
171 // we kill two birds with one stone: we reuse the
172 // list nodes, and we clear the
173 // maCurrentActivitiesReinsert list
174 maCurrentActivitiesWaiting.swap( maCurrentActivitiesReinsert );
178 void ActivitiesQueue::processDequeued()
180 // notify all dequeued activities from last round
181 for( const auto& pActivity : maDequeuedActivities )
182 pActivity->dequeued();
183 maDequeuedActivities.clear();
186 bool ActivitiesQueue::isEmpty() const
188 return maCurrentActivitiesWaiting.empty() && maCurrentActivitiesReinsert.empty();
191 void ActivitiesQueue::clear()
193 // dequeue all entries:
194 for( const auto& pActivity : maCurrentActivitiesWaiting )
195 pActivity->dequeued();
196 ActivityQueue().swap( maCurrentActivitiesWaiting );
198 for( const auto& pActivity : maCurrentActivitiesReinsert )
199 pActivity->dequeued();
200 ActivityQueue().swap( maCurrentActivitiesReinsert );
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */