Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ext / macromigration / progressmixer.cxx
blob87c7d84579d11e96a24f9f47ce4289bed377c89f
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 .
20 #include "progressmixer.hxx"
22 #include <osl/diagnose.h>
24 #include <map>
26 namespace dbmm
28 #define OVERALL_RANGE 100000
30 // misc types
31 struct PhaseData
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
42 sal_uInt32 nGlobalRange;
44 PhaseData()
45 :nWeight(1)
46 ,nRange(100)
47 ,nGlobalStart(0)
48 ,nGlobalRange(100)
52 PhaseData( const PhaseWeight _nWeight )
53 :nWeight( _nWeight )
54 ,nRange(100)
55 ,nGlobalStart(0)
56 ,nGlobalRange(100)
61 typedef ::std::map< PhaseID, PhaseData > Phases;
63 // ProgressMixer_Data
64 struct ProgressMixer_Data
66 Phases aPhases;
67 Phases::iterator pCurrentPhase;
68 sal_uInt32 nWeightSum; /// the cached sum of the weights
69 double nOverallStretch;
70 IProgressConsumer& rConsumer;
72 ProgressMixer_Data( IProgressConsumer& _rConsumer )
73 :aPhases()
74 ,pCurrentPhase( aPhases.end() )
75 ,nWeightSum( 0 )
76 ,nOverallStretch( 0 )
77 ,rConsumer( _rConsumer )
82 namespace
84 #if OSL_DEBUG_LEVEL > 0
85 bool lcl_isRunning( const ProgressMixer_Data& _rData )
87 return _rData.pCurrentPhase != _rData.aPhases.end();
89 #endif
90 void lcl_ensureInitialized( ProgressMixer_Data& _rData )
92 OSL_PRECOND( _rData.nWeightSum, "lcl_ensureInitialized: we have no phases, this will crash!" );
94 if ( _rData.nOverallStretch )
95 return;
97 _rData.nOverallStretch = 1.0 * OVERALL_RANGE / _rData.nWeightSum;
99 // tell the single phases their "overall starting point"
100 PhaseWeight nRunningWeight( 0 );
101 for ( Phases::iterator phase = _rData.aPhases.begin();
102 phase != _rData.aPhases.end();
103 ++phase
106 phase->second.nGlobalStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
107 nRunningWeight += phase->second.nWeight;
109 sal_uInt32 nNextPhaseStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
110 phase->second.nGlobalRange = nNextPhaseStart - phase->second.nGlobalStart;
113 _rData.rConsumer.start( OVERALL_RANGE );
117 // ProgressMixer
118 ProgressMixer::ProgressMixer( IProgressConsumer& _rConsumer )
119 :m_pData( new ProgressMixer_Data( _rConsumer ) )
123 ProgressMixer::~ProgressMixer()
127 void ProgressMixer::registerPhase( const PhaseID _nID, const PhaseWeight _nWeight )
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 m_pData->aPhases[ _nID ] = PhaseData( _nWeight );
133 m_pData->nWeightSum += _nWeight;
136 void ProgressMixer::startPhase( const PhaseID _nID, const sal_uInt32 _nPhaseRange )
138 OSL_ENSURE( m_pData->aPhases.find( _nID ) != m_pData->aPhases.end(),
139 "ProgresMixer::startPhase: unknown phase!" );
141 m_pData->aPhases[ _nID ].nRange = _nPhaseRange;
142 m_pData->pCurrentPhase = m_pData->aPhases.find( _nID );
145 void ProgressMixer::advancePhase( const sal_uInt32 _nPhaseProgress )
147 OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::advancePhase: not running!" );
149 // in case this is the first call, ensure all the ranges/weights are calculated
150 // correctly
151 lcl_ensureInitialized( *m_pData );
153 const PhaseData& rPhase( m_pData->pCurrentPhase->second );
155 double nLocalProgress = 1.0 * _nPhaseProgress / rPhase.nRange;
156 sal_uInt32 nOverallProgress = (sal_uInt32)
157 ( rPhase.nGlobalStart + nLocalProgress * rPhase.nGlobalRange );
159 m_pData->rConsumer.advance( nOverallProgress );
162 void ProgressMixer::endPhase()
164 OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::endPhase: not running!" );
166 // in case this is the first call, ensure all the ranges/weights are calculated
167 // correctly
168 lcl_ensureInitialized( *m_pData );
170 // simply assume the phase's complete range is over
171 advancePhase( m_pData->pCurrentPhase->second.nRange );
173 // if that's the last phase, this is the "global end", too
174 Phases::const_iterator pNextPhase( m_pData->pCurrentPhase );
175 ++pNextPhase;
176 if ( pNextPhase == m_pData->aPhases.end() )
177 m_pData->rConsumer.end();
180 } // namespace dbmm
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */