LCOV - code coverage report
Current view: top level - slideshow/source/engine - activitiesqueue.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 13 49 26.5 %
Date: 2015-06-13 12:38:46 Functions: 3 7 42.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : // must be first
      22             : #include <canvas/debug.hxx>
      23             : #include <canvas/verbosetrace.hxx>
      24             : 
      25             : #include <comphelper/anytostring.hxx>
      26             : #include <cppuhelper/exc_hlp.hxx>
      27             : #include <osl/diagnose.h>
      28             : 
      29             : #include "slideshowexceptions.hxx"
      30             : #include "activity.hxx"
      31             : #include "activitiesqueue.hxx"
      32             : 
      33             : #include <boost/mem_fn.hpp>
      34             : #include <boost/shared_ptr.hpp>
      35             : #include <algorithm>
      36             : 
      37             : 
      38             : using namespace ::com::sun::star;
      39             : 
      40             : namespace slideshow
      41             : {
      42             :     namespace internal
      43             :     {
      44           1 :         ActivitiesQueue::ActivitiesQueue(
      45             :           const ::boost::shared_ptr< ::canvas::tools::ElapsedTime >& pPresTimer ) :
      46             :             mpTimer( pPresTimer ),
      47             :             maCurrentActivitiesWaiting(),
      48             :             maCurrentActivitiesReinsert(),
      49           1 :             maDequeuedActivities()
      50             :         {
      51           1 :         }
      52             : 
      53           2 :         ActivitiesQueue::~ActivitiesQueue()
      54             :         {
      55             :             // dispose all queue entries
      56             :             try
      57             :             {
      58             :                 std::for_each( maCurrentActivitiesWaiting.begin(),
      59             :                                maCurrentActivitiesWaiting.end(),
      60           1 :                                boost::mem_fn( &Disposable::dispose ) );
      61             :                 std::for_each( maCurrentActivitiesReinsert.begin(),
      62             :                                maCurrentActivitiesReinsert.end(),
      63           1 :                                boost::mem_fn( &Disposable::dispose ) );
      64             :             }
      65           0 :             catch (uno::Exception &)
      66             :             {
      67             :                 OSL_FAIL( OUStringToOString(
      68             :                                 comphelper::anyToString(
      69             :                                     cppu::getCaughtException() ),
      70             :                                 RTL_TEXTENCODING_UTF8 ).getStr() );
      71             :             }
      72           1 :         }
      73             : 
      74           0 :         bool ActivitiesQueue::addActivity( const ActivitySharedPtr& pActivity )
      75             :         {
      76             :             OSL_ENSURE( pActivity, "ActivitiesQueue::addActivity: activity ptr NULL" );
      77             : 
      78           0 :             if( !pActivity )
      79           0 :                 return false;
      80             : 
      81             :             // add entry to waiting list
      82           0 :             maCurrentActivitiesWaiting.push_back( pActivity );
      83             : 
      84           0 :             return true;
      85             :         }
      86             : 
      87           0 :         void ActivitiesQueue::process()
      88             :         {
      89             :             VERBOSE_TRACE( "ActivitiesQueue: outer loop heartbeat" );
      90             : 
      91             :             // accumulate time lag for all activities, and lag time
      92             :             // base if necessary:
      93             :             ActivityQueue::const_iterator iPos(
      94           0 :                 maCurrentActivitiesWaiting.begin() );
      95             :             const ActivityQueue::const_iterator iEnd(
      96           0 :                 maCurrentActivitiesWaiting.end() );
      97           0 :             double fLag = 0.0;
      98           0 :             for ( ; iPos != iEnd; ++iPos )
      99           0 :                 fLag = std::max<double>( fLag, (*iPos)->calcTimeLag() );
     100           0 :             if (fLag > 0.0)
     101             :             {
     102           0 :                 mpTimer->adjustTimer( -fLag );
     103             :             }
     104             : 
     105             :             // process list of activities
     106           0 :             while( !maCurrentActivitiesWaiting.empty() )
     107             :             {
     108             :                 // process topmost activity
     109           0 :                 ActivitySharedPtr pActivity( maCurrentActivitiesWaiting.front() );
     110           0 :                 maCurrentActivitiesWaiting.pop_front();
     111             : 
     112           0 :                 bool bReinsert( false );
     113             : 
     114             :                 try
     115             :                 {
     116             :                     // fire up activity
     117           0 :                     bReinsert = pActivity->perform();
     118             :                 }
     119           0 :                 catch( uno::RuntimeException& )
     120             :                 {
     121           0 :                     throw;
     122             :                 }
     123           0 :                 catch( uno::Exception& )
     124             :                 {
     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.
     130             : 
     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() );
     138             :                 }
     139           0 :                 catch( SlideShowException& )
     140             :                 {
     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.
     146             : 
     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" );
     152             :                 }
     153             : 
     154           0 :                 if( bReinsert )
     155           0 :                     maCurrentActivitiesReinsert.push_back( pActivity );
     156             :                 else
     157           0 :                     maDequeuedActivities.push_back( pActivity );
     158             : 
     159             :                 VERBOSE_TRACE( "ActivitiesQueue: inner loop heartbeat" );
     160           0 :             }
     161             : 
     162           0 :             if( !maCurrentActivitiesReinsert.empty() )
     163             :             {
     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           0 :                 maCurrentActivitiesWaiting.swap( maCurrentActivitiesReinsert );
     170             :             }
     171           0 :         }
     172             : 
     173           0 :         void ActivitiesQueue::processDequeued()
     174             :         {
     175             :             // notify all dequeued activities from last round
     176             :             ::std::for_each( maDequeuedActivities.begin(),
     177             :                              maDequeuedActivities.end(),
     178           0 :                              ::boost::mem_fn( &Activity::dequeued ) );
     179           0 :             maDequeuedActivities.clear();
     180           0 :         }
     181             : 
     182           0 :         bool ActivitiesQueue::isEmpty() const
     183             :         {
     184           0 :             return maCurrentActivitiesWaiting.empty() && maCurrentActivitiesReinsert.empty();
     185             :         }
     186             : 
     187           1 :         void ActivitiesQueue::clear()
     188             :         {
     189             :             // dequeue all entries:
     190             :             std::for_each( maCurrentActivitiesWaiting.begin(),
     191             :                            maCurrentActivitiesWaiting.end(),
     192           1 :                            boost::mem_fn( &Activity::dequeued ) );
     193           1 :             ActivityQueue().swap( maCurrentActivitiesWaiting );
     194             : 
     195             :             std::for_each( maCurrentActivitiesReinsert.begin(),
     196             :                            maCurrentActivitiesReinsert.end(),
     197           1 :                            boost::mem_fn( &Activity::dequeued ) );
     198           1 :             ActivityQueue().swap( maCurrentActivitiesReinsert );
     199           1 :         }
     200             :     }
     201             : }
     202             : 
     203             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11