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