1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "progressmixer.hxx"
22 #include <osl/diagnose.h>
28 #define OVERALL_RANGE 100000
33 // the weight of the phase, relative to all other phases
35 // the "local" range of the phase
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
42 sal_uInt32 nGlobalRange
;
52 explicit PhaseData( const PhaseWeight _nWeight
)
61 typedef ::std::map
< PhaseID
, PhaseData
> Phases
;
64 struct ProgressMixer_Data
67 Phases::iterator pCurrentPhase
;
68 sal_uInt32 nWeightSum
; /// the cached sum of the weights
69 double nOverallStretch
;
70 IProgressConsumer
& rConsumer
;
72 explicit ProgressMixer_Data( IProgressConsumer
& _rConsumer
)
74 ,pCurrentPhase( aPhases
.end() )
77 ,rConsumer( _rConsumer
)
84 bool lcl_isRunning( const ProgressMixer_Data
& _rData
)
86 return _rData
.pCurrentPhase
!= _rData
.aPhases
.end();
89 void lcl_ensureInitialized( ProgressMixer_Data
& _rData
)
91 OSL_PRECOND( _rData
.nWeightSum
, "lcl_ensureInitialized: we have no phases, this will crash!" );
93 if ( _rData
.nOverallStretch
)
96 _rData
.nOverallStretch
= 1.0 * OVERALL_RANGE
/ _rData
.nWeightSum
;
98 // tell the single phases their "overall starting point"
99 PhaseWeight
nRunningWeight( 0 );
100 for ( Phases::iterator phase
= _rData
.aPhases
.begin();
101 phase
!= _rData
.aPhases
.end();
105 phase
->second
.nGlobalStart
= (sal_uInt32
)( nRunningWeight
* _rData
.nOverallStretch
);
106 nRunningWeight
+= phase
->second
.nWeight
;
108 sal_uInt32 nNextPhaseStart
= (sal_uInt32
)( nRunningWeight
* _rData
.nOverallStretch
);
109 phase
->second
.nGlobalRange
= nNextPhaseStart
- phase
->second
.nGlobalStart
;
112 _rData
.rConsumer
.start( OVERALL_RANGE
);
117 ProgressMixer::ProgressMixer( IProgressConsumer
& _rConsumer
)
118 :m_pData( new ProgressMixer_Data( _rConsumer
) )
122 ProgressMixer::~ProgressMixer()
126 void ProgressMixer::registerPhase( const PhaseID _nID
, const PhaseWeight _nWeight
)
128 OSL_PRECOND( !lcl_isRunning( *m_pData
), "ProgressMixer::registerPhase: already running!" );
129 OSL_ENSURE( m_pData
->aPhases
.find( _nID
) == m_pData
->aPhases
.end(),
130 "ProgressMixer::registerPhase: ID already used!" );
131 m_pData
->aPhases
[ _nID
] = PhaseData( _nWeight
);
132 m_pData
->nWeightSum
+= _nWeight
;
135 void ProgressMixer::startPhase( const PhaseID _nID
, const sal_uInt32 _nPhaseRange
)
137 OSL_ENSURE( m_pData
->aPhases
.find( _nID
) != m_pData
->aPhases
.end(),
138 "ProgresMixer::startPhase: unknown phase!" );
140 m_pData
->aPhases
[ _nID
].nRange
= _nPhaseRange
;
141 m_pData
->pCurrentPhase
= m_pData
->aPhases
.find( _nID
);
144 void ProgressMixer::advancePhase( const sal_uInt32 _nPhaseProgress
)
146 OSL_PRECOND( lcl_isRunning( *m_pData
), "ProgresMixer::advancePhase: not running!" );
148 // in case this is the first call, ensure all the ranges/weights are calculated
150 lcl_ensureInitialized( *m_pData
);
152 const PhaseData
& rPhase( m_pData
->pCurrentPhase
->second
);
154 double nLocalProgress
= 1.0 * _nPhaseProgress
/ rPhase
.nRange
;
155 sal_uInt32 nOverallProgress
= (sal_uInt32
)
156 ( rPhase
.nGlobalStart
+ nLocalProgress
* rPhase
.nGlobalRange
);
158 m_pData
->rConsumer
.advance( nOverallProgress
);
161 void ProgressMixer::endPhase()
163 OSL_PRECOND( lcl_isRunning( *m_pData
), "ProgresMixer::endPhase: not running!" );
165 // in case this is the first call, ensure all the ranges/weights are calculated
167 lcl_ensureInitialized( *m_pData
);
169 // simply assume the phase's complete range is over
170 advancePhase( m_pData
->pCurrentPhase
->second
.nRange
);
172 // if that's the last phase, this is the "global end", too
173 Phases::const_iterator
pNextPhase( m_pData
->pCurrentPhase
);
175 if ( pNextPhase
== m_pData
->aPhases
.end() )
176 m_pData
->rConsumer
.end();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */