#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2008 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Nuclex.Support.Collections;
namespace Nuclex.Support.Tracking {
/// Forms a single progression from a set of progressions
/// Type of progressions to manage as a set
public class SetProgression : Waitable, IDisposable, IProgressReporter
where ProgressionType : Waitable {
/// will be triggered to report when progress has been achieved
public event EventHandler AsyncProgressChanged;
/// Initializes a new set progression
/// Progressions to track with this set
///
/// Uses a default weighting factor of 1.0 for all progressions.
///
public SetProgression(IEnumerable childs)
: this() {
// Construct a WeightedProgression with the default weight for each
// progression and wrap it in an ObservedProgression
foreach(ProgressionType progression in childs) {
this.children.Add(
new ObservedWeightedWaitable(
new WeightedWaitable(progression),
new ObservedWeightedWaitable.ReportDelegate(asyncProgressUpdated),
new ObservedWeightedWaitable.ReportDelegate(asyncEnded)
)
);
}
// Since all progressions have a weight of 1.0, the total weight is
// equal to the number of progressions in our list
this.totalWeight = (float)this.children.Count;
}
/// Initializes a new set progression
/// Progressions to track with this set
public SetProgression(
IEnumerable> childs
)
: this() {
// Construct an ObservedProgression around each of the WeightedProgressions
foreach(WeightedWaitable progression in childs) {
this.children.Add(
new ObservedWeightedWaitable(
progression,
new ObservedWeightedWaitable.ReportDelegate(asyncProgressUpdated),
new ObservedWeightedWaitable.ReportDelegate(asyncEnded)
)
);
// Sum up the total weight
this.totalWeight += progression.Weight;
}
}
/// Performs common initialization for the public constructors
private SetProgression() {
this.children = new List>();
}
/// Immediately releases all resources owned by the object
public void Dispose() {
if(this.children != null) {
// Dispose all the observed progressions, disconnecting the events from the
// actual progressions so the GC can more easily collect this class
for(int index = 0; index < this.children.Count; ++index)
this.children[index].Dispose();
this.children = null;
this.wrapper = null;
}
}
/// Childs contained in the progression set
public IList> Children {
get {
// The wrapper is constructed only when needed. Most of the time, users will
// just create a SetProgression and monitor its progress without ever using
// the Childs collection.
if(this.wrapper == null) {
// This doesn't need a lock because it's a stateless wrapper.
// If it is constructed twice, then so be it, no problem at all.
this.wrapper = new WeightedProgressionWrapperCollection(
this.children
);
}
return this.wrapper;
}
}
/// Fires the progress update event
/// Progress to report (ranging from 0.0 to 1.0)
///
/// Informs the observers of this progression about the achieved progress.
///
protected virtual void OnAsyncProgressChanged(float progress) {
OnAsyncProgressChanged(new ProgressReportEventArgs(progress));
}
/// Fires the progress update event
/// Progress to report (ranging from 0.0 to 1.0)
///
/// Informs the observers of this progression about the achieved progress.
/// Allows for classes derived from the Progression class to easily provide
/// a custom event arguments class that has been derived from the
/// Progression's ProgressUpdateEventArgs class.
///
protected virtual void OnAsyncProgressChanged(ProgressReportEventArgs eventArguments) {
EventHandler copy = AsyncProgressChanged;
if(copy != null)
copy(this, eventArguments);
}
///
/// Called when the progress of one of the observed progressions changes
///
private void asyncProgressUpdated() {
float totalProgress = 0.0f;
// Calculate the sum of the progress reported by our child progressions,
// scaled to the weight each progression has assigned to it.
for(int index = 0; index < this.children.Count; ++index) {
totalProgress +=
this.children[index].Progress * this.children[index].WeightedWaitable.Weight;
}
// Calculate the actual combined progress
if(this.totalWeight > 0.0f)
totalProgress /= this.totalWeight;
// Send out the progress update
OnAsyncProgressChanged(totalProgress);
}
///
/// Called when an observed progression ends
///
private void asyncEnded() {
// If there's still at least one progression going, don't report that
// the SetProgression has finished yet.
for(int index = 0; index < this.children.Count; ++index)
if(!this.children[index].WeightedWaitable.Waitable.Ended)
return;
// All child progressions have ended, so the set has now ended as well
OnAsyncEnded();
}
/// Progressions being managed in the set
private List> children;
///
/// Wrapper collection for exposing the child progressions under the
/// WeightedProgression interface
///
private volatile WeightedProgressionWrapperCollection wrapper;
/// Summed weight of all progressions in the set
private float totalWeight;
}
} // namespace Nuclex.Support.Tracking