Reorganized the directory structure a bit; Created a new transforming collection for exposing the types in a collection under a different interface; moved Operation and associated classes to a new namespace; Implemented the basics of the SetProgression's observation mechanics

git-svn-id: file:///srv/devel/repo-conversion/nusu@11 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-04-18 20:20:03 +00:00
parent f38a0bc1ea
commit cefbc78868
15 changed files with 946 additions and 91 deletions

View file

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Interface for abortable processes</summary>
public interface IAbortable {
/// <summary>Aborts the running process. Can be called from any thread.</summary>
void AsyncAbort();
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Progression being observed by another class</summary>
/// <typeparam name="ProgressionType">
/// Type of the progression that is being observed
/// </typeparam>
internal class ObservedProgression<ProgressionType>
where ProgressionType : Progression {
/// <summary>Initializes a new observed progression</summary>
/// <param name="weightedProgression">Weighted progression being observed</param>
internal ObservedProgression(
WeightedProgression<ProgressionType> weightedProgression
) {
this.weightedProgression = weightedProgression;
}
/// <summary>Weighted progression being observed</summary>
public WeightedProgression<ProgressionType> WeightedProgression {
get { return this.weightedProgression; }
}
/*
internal void AsyncProgressUpdated(object sender, ProgressUpdateEventArgs e) {
this.Progress = e.Progress;
}
internal void AsyncEnded(object sender, EventArgs e) { }
*/
/// <summary>The weighted progression that is being observed</summary>
private WeightedProgression<ProgressionType> weightedProgression;
/*
/// <summary>Amount of progress this progression has achieved so far</summary>
private volatile float progress;
*/
}
} // namespace Nuclex.Support.Tracking

View file

@ -0,0 +1,60 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Nuclex.Support.Collections;
namespace Nuclex.Support.Tracking {
/// <summary>Collection of progressions with a weighting value</summary>
/// <typeparam name="ProgressionType">Type of progressions to manage</typeparam>
/// <remarks>
/// <para>
/// This collection is exposed as a read-only collection to the user that
/// stores WeightedProgressions. Internally, it merely wraps a collection of
/// an internal type used to keep track of the individual progression's
/// progress in the SetProgression and QueueOperation classes.
/// </para>
/// <para>
/// It is read-only because the design requires a progression to only ever
/// finish once. If it was possible eg. to add items after a SetProgression
/// had signalled itself as being finished, it would be moved into an
/// unfinished state again. Also, an empty SetProgression is, by definition,
/// finished (simply because there is no work to do) - unless the contents
/// of set are passed to the SetProgression's constructor and never modified
/// at all, the design would be violated as soon as ab instance of the
/// SetProgression or QueueOperation classes was created.
/// </para>
/// </remarks>
internal class WeightedProgressionCollection<ProgressionType> :
TransformingReadOnlyCollection<
ObservedProgression<ProgressionType>, WeightedProgression<ProgressionType>
>
where ProgressionType : Progression {
/// <summary>Initializes a new weighted progression collection wrapper</summary>
/// <param name="items">Items to be exposed as weighted progressions</param>
internal WeightedProgressionCollection(
IList<ObservedProgression<ProgressionType>> items
)
: base(items) { }
/// <summary>Transforms an item into the exposed type</summary>
/// <param name="item">Item to be transformed</param>
/// <returns>The transformed item</returns>
/// <remarks>
/// This method is used to transform an item in the wrapped collection into
/// the exposed item type whenever the user accesses an item. Expect it to
/// be called frequently, because the TransformingReadOnlyCollection does
/// not cache otherwise store the transformed items.
/// </remarks>
protected override WeightedProgression<ProgressionType> Transform(
ObservedProgression<ProgressionType> item
) {
return item.WeightedProgression;
}
}
} // namespace Nuclex.Support.Tracking

View file

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Base class for observable operations running in the background</summary>
public abstract class Operation : Progression {
/// <summary>Executes the operation</summary>
public abstract void Execute();
}
} // namespace Nuclex.Support.Tracking

View file

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Nuclex.Support.Collections;
namespace Nuclex.Support.Tracking {
/// <summary>Non-generic version of the progression collection</summary>
public class ProgressionCollection : ProgressionCollection<Progression> { }
/// <summary>Generic collection of progressions</summary>
public class ProgressionCollection<ProgressionType>
: ObservableCollection<Progression> where ProgressionType : Progression { }
} // namespace Nuclex.Support.Tracking

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Nuclex.Support.Collections;
@ -10,15 +11,31 @@ namespace Nuclex.Support.Tracking {
public class SetProgression<ProgressionType> : Progression
where ProgressionType : Progression {
/// <summary>Initializes a new set progression</summary>
/// <param name="progressions">Progressions to track with this set</param>
/// <remarks>
/// Uses a default weighting factor of 1.0 for all progressions.
/// </remarks>
public SetProgression(IEnumerable<ProgressionType> progressions) {
}
// TODO
/// <summary>Initializes a new set progression</summary>
/// <param name="progressions">Progressions to track with this set</param>
public SetProgression(IEnumerable<WeightedProgression<ProgressionType>> progressions) {
}
/// <summary>Childs contained in the progression set</summary>
public ReadOnlyCollection<WeightedProgression<ProgressionType>> Childs {
get { return null; }
}
/*
/// <summary>Progressions being managed in the set</summary>
private ReadOnlyCollection<ProgressionType> progressions;
/// <summary>whether the progress needs to be recalculated</summary>
private volatile bool needProgressRecalculation;
/// <summary>Total progress achieved by the progressions in this collection</summary>
private volatile float totalProgress;
*/
}
/*
public class SetOperation<OperationType> : Operation
where OperationType : OperationType
QueueOperation
*/
} // namespace Nuclex.Support.Tracking

View file

@ -1,10 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Tracking {
/*
public class ThreadedMethodOperation : Operation {
}
*/
} // namespace Nuclex.Support.Tracking

View file

@ -5,36 +5,20 @@ using System.Text;
namespace Nuclex.Support.Tracking {
/// <summary>Progression with an associated weight for the total progress</summary>
internal class WeightedProgression<ProgressionType> : IDisposable
where ProgressionType : Progression {
public class WeightedProgression<ProgressionType> where ProgressionType : Progression {
/// <summary>
/// Initializes a new weighted progression with a default weight of 1.0
/// </summary>
/// <param name="progression">Progression whose progress to monitor</param>
public WeightedProgression(ProgressionType progression) : this(progression, 1.0f) { }
/// <summary>Initializes a new weighted progression</summary>
/// <param name="callback">Callback to pass progress updates on to</param>
/// <param name="progression">Progression whose progress to monitor</param>
/// <param name="weight">Weighting of the progression's progress</param>
public WeightedProgression(
ProgressionType progression,
EventHandler<ProgressUpdateEventArgs> callback,
float weight
) {
public WeightedProgression(ProgressionType progression, float weight) {
this.progression = progression;
this.weight = weight;
this.callback = callback;
progression.AsyncProgressUpdated += new EventHandler<ProgressUpdateEventArgs>(
asyncProgressUpdated
);
}
/// <summary>Disposes of the resources used by this instance immediately</summary>
public void Dispose() {
if(this.progression != null) {
progression.AsyncProgressUpdated -= new EventHandler<ProgressUpdateEventArgs>(
asyncProgressUpdated
);
this.progression = null;
}
this.weight = weight;
}
/// <summary>Progression being wrapped by this weighted progression</summary>
@ -42,30 +26,13 @@ namespace Nuclex.Support.Tracking {
get { return this.progression; }
}
/// <summary>Progress this progression has achieved so far</summary>
public float Progress {
get { return this.progress; }
}
/// <summary>The weighting of this progression in the total progress</summary>
/// <summary>The contribution of this progression to the total progress</summary>
public float Weight {
get { return this.weight; }
}
/// <summary>Handles progress reports by the progression</summary>
/// <param name="sender">Progression that has made progress</param>
/// <param name="e">Contains the currently achieved progress</param>
private void asyncProgressUpdated(object sender, ProgressUpdateEventArgs e) {
this.progress = e.Progress;
this.callback(sender, e);
}
/// <summary>Progression whose progress we're tracking</summary>
private ProgressionType progression;
/// <summary>Callback to which any progress reports will be passed on</summary>
private EventHandler<ProgressUpdateEventArgs> callback;
/// <summary>Most recent progress reported by the progression</summary>
private volatile float progress;
/// <summary>Weighting of this progression in the total progress</summary>
private float weight;

View file

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using Nuclex.Support.Collections;
namespace Nuclex.Support.Tracking {
internal class WeightedProgressionCollection<ProgressionType>
: ObservableCollection<WeightedProgression<ProgressionType>>
where ProgressionType : Progression { }
} // namespace Nuclex.Support.Tracking