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 : #include "progressmixer.hxx"
21 :
22 : #include <osl/diagnose.h>
23 :
24 : #include <map>
25 :
26 : namespace dbmm
27 : {
28 : #define OVERALL_RANGE 100000
29 :
30 : // misc types
31 : struct PhaseData
32 : {
33 : // the weight of the phase, relative to all other phases
34 : PhaseWeight nWeight;
35 : // the "local" range of the phase
36 : sal_uInt32 nRange;
37 : // this is the point in the "overall range" at which this phase starts
38 : sal_uInt32 nGlobalStart;
39 : /** the "global" range of the phase, i.e. its range after weighting with all other
40 : phases
41 : */
42 : sal_uInt32 nGlobalRange;
43 :
44 0 : PhaseData()
45 : :nWeight(1)
46 : ,nRange(100)
47 : ,nGlobalStart(0)
48 0 : ,nGlobalRange(100)
49 : {
50 0 : }
51 :
52 0 : PhaseData( const PhaseWeight _nWeight )
53 : :nWeight( _nWeight )
54 : ,nRange(100)
55 : ,nGlobalStart(0)
56 0 : ,nGlobalRange(100)
57 : {
58 0 : }
59 : };
60 :
61 : typedef ::std::map< PhaseID, PhaseData > Phases;
62 :
63 : // ProgressMixer_Data
64 0 : struct ProgressMixer_Data
65 : {
66 : Phases aPhases;
67 : Phases::iterator pCurrentPhase;
68 : sal_uInt32 nWeightSum; /// the cached sum of the weights
69 : double nOverallStretch;
70 : IProgressConsumer& rConsumer;
71 :
72 0 : ProgressMixer_Data( IProgressConsumer& _rConsumer )
73 : :aPhases()
74 0 : ,pCurrentPhase( aPhases.end() )
75 : ,nWeightSum( 0 )
76 : ,nOverallStretch( 0 )
77 0 : ,rConsumer( _rConsumer )
78 : {
79 0 : }
80 : };
81 :
82 : namespace
83 : {
84 : #if OSL_DEBUG_LEVEL > 0
85 : bool lcl_isRunning( const ProgressMixer_Data& _rData )
86 : {
87 : return _rData.pCurrentPhase != _rData.aPhases.end();
88 : }
89 : #endif
90 0 : void lcl_ensureInitialized( ProgressMixer_Data& _rData )
91 : {
92 : OSL_PRECOND( _rData.nWeightSum, "lcl_ensureInitialized: we have no phases, this will crash!" );
93 :
94 0 : if ( _rData.nOverallStretch )
95 0 : return;
96 :
97 0 : _rData.nOverallStretch = 1.0 * OVERALL_RANGE / _rData.nWeightSum;
98 :
99 : // tell the single phases their "overall starting point"
100 0 : PhaseWeight nRunningWeight( 0 );
101 0 : for ( Phases::iterator phase = _rData.aPhases.begin();
102 0 : phase != _rData.aPhases.end();
103 : ++phase
104 : )
105 : {
106 0 : phase->second.nGlobalStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
107 0 : nRunningWeight += phase->second.nWeight;
108 :
109 0 : sal_uInt32 nNextPhaseStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
110 0 : phase->second.nGlobalRange = nNextPhaseStart - phase->second.nGlobalStart;
111 : }
112 :
113 0 : _rData.rConsumer.start( OVERALL_RANGE );
114 : }
115 : }
116 :
117 : // ProgressMixer
118 0 : ProgressMixer::ProgressMixer( IProgressConsumer& _rConsumer )
119 0 : :m_pData( new ProgressMixer_Data( _rConsumer ) )
120 : {
121 0 : }
122 :
123 0 : ProgressMixer::~ProgressMixer()
124 : {
125 0 : }
126 :
127 0 : void ProgressMixer::registerPhase( const PhaseID _nID, const PhaseWeight _nWeight )
128 : {
129 : OSL_PRECOND( !lcl_isRunning( *m_pData ), "ProgressMixer::registerPhase: already running!" );
130 : OSL_ENSURE( m_pData->aPhases.find( _nID ) == m_pData->aPhases.end(),
131 : "ProgressMixer::registerPhase: ID already used!" );
132 0 : m_pData->aPhases[ _nID ] = PhaseData( _nWeight );
133 0 : m_pData->nWeightSum += _nWeight;
134 0 : }
135 :
136 0 : void ProgressMixer::startPhase( const PhaseID _nID, const sal_uInt32 _nPhaseRange )
137 : {
138 : OSL_ENSURE( m_pData->aPhases.find( _nID ) != m_pData->aPhases.end(),
139 : "ProgresMixer::startPhase: unknown phase!" );
140 :
141 0 : m_pData->aPhases[ _nID ].nRange = _nPhaseRange;
142 0 : m_pData->pCurrentPhase = m_pData->aPhases.find( _nID );
143 0 : }
144 :
145 0 : void ProgressMixer::advancePhase( const sal_uInt32 _nPhaseProgress )
146 : {
147 : OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::advancePhase: not running!" );
148 :
149 : // in case this is the first call, ensure all the ranges/weights are calculated
150 : // correctly
151 0 : lcl_ensureInitialized( *m_pData );
152 :
153 0 : const PhaseData& rPhase( m_pData->pCurrentPhase->second );
154 :
155 0 : double nLocalProgress = 1.0 * _nPhaseProgress / rPhase.nRange;
156 : sal_uInt32 nOverallProgress = (sal_uInt32)
157 0 : ( rPhase.nGlobalStart + nLocalProgress * rPhase.nGlobalRange );
158 :
159 0 : m_pData->rConsumer.advance( nOverallProgress );
160 0 : }
161 :
162 0 : void ProgressMixer::endPhase()
163 : {
164 : OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::endPhase: not running!" );
165 :
166 : // in case this is the first call, ensure all the ranges/weights are calculated
167 : // correctly
168 0 : lcl_ensureInitialized( *m_pData );
169 :
170 : // simply assume the phase's complete range is over
171 0 : advancePhase( m_pData->pCurrentPhase->second.nRange );
172 :
173 : // if that's the last phase, this is the "global end", too
174 0 : Phases::const_iterator pNextPhase( m_pData->pCurrentPhase );
175 0 : ++pNextPhase;
176 0 : if ( pNextPhase == m_pData->aPhases.end() )
177 0 : m_pData->rConsumer.end();
178 0 : }
179 :
180 : } // namespace dbmm
181 :
182 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|