2007-05-11 21:15:35 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
|
|
|
Copyright (C) 2002-2007 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
|
2007-04-16 18:31:59 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2007-04-18 20:20:03 +00:00
|
|
|
using System.Collections.ObjectModel;
|
2007-04-16 18:31:59 +00:00
|
|
|
|
|
|
|
using Nuclex.Support.Collections;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Tracking {
|
|
|
|
|
|
|
|
/// <summary>Forms a single progression from a set of progressions</summary>
|
|
|
|
/// <typeparam name="ProgressionType">Type of progressions to manage as a set</typeparam>
|
2007-04-20 18:04:19 +00:00
|
|
|
public class SetProgression<ProgressionType> : Progression, IDisposable
|
2007-04-16 18:31:59 +00:00
|
|
|
where ProgressionType : Progression {
|
|
|
|
|
2007-04-19 18:18:34 +00:00
|
|
|
/// <summary>Performs common initialization for the public constructors</summary>
|
|
|
|
private SetProgression() {
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children = new List<ObservedWeightedProgression<ProgressionType>>();
|
2007-04-19 18:18:34 +00:00
|
|
|
}
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Initializes a new set progression</summary>
|
2007-04-19 18:18:34 +00:00
|
|
|
/// <param name="childs">Progressions to track with this set</param>
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <remarks>
|
|
|
|
/// Uses a default weighting factor of 1.0 for all progressions.
|
|
|
|
/// </remarks>
|
2007-04-19 18:18:34 +00:00
|
|
|
public SetProgression(IEnumerable<ProgressionType> childs)
|
|
|
|
: this() {
|
|
|
|
|
|
|
|
// Construct a WeightedProgression with the default weight for each
|
|
|
|
// progression and wrap it in an ObservedProgression
|
|
|
|
foreach(ProgressionType progression in childs) {
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children.Add(
|
2007-07-05 20:02:02 +00:00
|
|
|
new ObservedWeightedProgression<ProgressionType>(
|
2007-04-19 18:18:34 +00:00
|
|
|
new WeightedProgression<ProgressionType>(progression),
|
2007-07-05 20:02:02 +00:00
|
|
|
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncProgressUpdated),
|
|
|
|
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncEnded)
|
2007-04-19 18:18:34 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since all progressions have a weight of 1.0, the total weight is
|
|
|
|
// equal to the number of progressions in our list
|
2007-07-10 19:25:18 +00:00
|
|
|
this.totalWeight = (float)this.children.Count;
|
2007-04-19 18:18:34 +00:00
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
2007-04-16 18:31:59 +00:00
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Initializes a new set progression</summary>
|
2007-04-19 18:18:34 +00:00
|
|
|
/// <param name="childs">Progressions to track with this set</param>
|
|
|
|
public SetProgression(
|
|
|
|
IEnumerable<WeightedProgression<ProgressionType>> childs
|
|
|
|
)
|
|
|
|
: this() {
|
|
|
|
|
|
|
|
// Construct an ObservedProgression around each of the WeightedProgressions
|
|
|
|
foreach(WeightedProgression<ProgressionType> progression in childs) {
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children.Add(
|
2007-07-05 20:02:02 +00:00
|
|
|
new ObservedWeightedProgression<ProgressionType>(
|
2007-04-19 18:18:34 +00:00
|
|
|
progression,
|
2007-07-05 20:02:02 +00:00
|
|
|
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncProgressUpdated),
|
|
|
|
new ObservedWeightedProgression<ProgressionType>.ReportDelegate(asyncEnded)
|
2007-04-19 18:18:34 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Sum up the total weight
|
2007-07-04 19:19:48 +00:00
|
|
|
this.totalWeight += progression.Weight;
|
2007-04-19 18:18:34 +00:00
|
|
|
}
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
2007-04-16 18:31:59 +00:00
|
|
|
|
2007-04-20 18:04:19 +00:00
|
|
|
/// <summary>Immediately releases all resources owned by the object</summary>
|
|
|
|
public void Dispose() {
|
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
if(this.children != null) {
|
2007-04-20 18:04:19 +00:00
|
|
|
|
|
|
|
// Dispose all the observed progressions, disconnecting the events from the
|
|
|
|
// actual progressions so the GC can more easily collect this class
|
2007-07-10 19:25:18 +00:00
|
|
|
for(int index = 0; index < this.children.Count; ++index)
|
|
|
|
this.children[index].Dispose();
|
2007-04-20 18:04:19 +00:00
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children = null;
|
2007-04-20 18:04:19 +00:00
|
|
|
this.wrapper = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Childs contained in the progression set</summary>
|
2007-07-10 19:25:18 +00:00
|
|
|
public IList<WeightedProgression<ProgressionType>> Children {
|
2007-04-19 18:18:34 +00:00
|
|
|
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) {
|
|
|
|
|
2007-07-05 20:02:02 +00:00
|
|
|
// 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.
|
2007-04-19 18:18:34 +00:00
|
|
|
this.wrapper = new WeightedProgressionWrapperCollection<ProgressionType>(
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children
|
2007-04-19 18:18:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.wrapper;
|
|
|
|
|
|
|
|
}
|
2007-04-18 20:20:03 +00:00
|
|
|
}
|
2007-04-19 18:18:34 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Called when the progress of one of the observed progressions changes
|
|
|
|
/// </summary>
|
|
|
|
private void asyncProgressUpdated() {
|
2007-07-05 20:02:02 +00:00
|
|
|
float totalProgress = 0.0f;
|
2007-04-19 18:18:34 +00:00
|
|
|
|
|
|
|
// Calculate the sum of the progress reported by our child progressions,
|
|
|
|
// scaled to the weight each progression has assigned to it.
|
2007-07-10 19:25:18 +00:00
|
|
|
for(int index = 0; index < this.children.Count; ++index) {
|
2007-04-19 18:18:34 +00:00
|
|
|
totalProgress +=
|
2007-07-10 19:25:18 +00:00
|
|
|
this.children[index].Progress * this.children[index].WeightedProgression.Weight;
|
2007-04-19 18:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the actual combined progress
|
|
|
|
if(this.totalWeight > 0.0f)
|
|
|
|
totalProgress /= this.totalWeight;
|
|
|
|
|
|
|
|
// Send out the progress update
|
|
|
|
OnAsyncProgressUpdated(totalProgress);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2007-07-04 19:19:48 +00:00
|
|
|
/// Called when an observed progression ends
|
2007-04-19 18:18:34 +00:00
|
|
|
/// </summary>
|
|
|
|
private void asyncEnded() {
|
|
|
|
|
2007-04-20 18:04:19 +00:00
|
|
|
// If there's still at least one progression going, don't report that
|
|
|
|
// the SetProgression has finished yet.
|
2007-07-10 19:25:18 +00:00
|
|
|
for(int index = 0; index < this.children.Count; ++index)
|
|
|
|
if(!this.children[index].WeightedProgression.Progression.Ended)
|
2007-04-19 18:18:34 +00:00
|
|
|
return;
|
|
|
|
|
2007-04-20 18:04:19 +00:00
|
|
|
// All child progressions have ended, so the set has now ended as well
|
2007-04-19 18:18:34 +00:00
|
|
|
OnAsyncEnded();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-04-18 20:20:03 +00:00
|
|
|
/// <summary>Progressions being managed in the set</summary>
|
2007-07-10 19:25:18 +00:00
|
|
|
private List<ObservedWeightedProgression<ProgressionType>> children;
|
2007-04-19 18:18:34 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Wrapper collection for exposing the child progressions under the
|
|
|
|
/// WeightedProgression interface
|
|
|
|
/// </summary>
|
|
|
|
private volatile WeightedProgressionWrapperCollection<ProgressionType> wrapper;
|
2007-07-10 18:15:34 +00:00
|
|
|
/// <summary>Summed weight of all progressions in the set</summary>
|
2007-04-19 18:18:34 +00:00
|
|
|
private float totalWeight;
|
|
|
|
|
2007-04-16 18:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Tracking
|