source: trunk/eraser6/Eraser.Manager/Executor.cs @ 1079

Revision 1012, 5.9 KB checked in by lowjoel, 6 years ago (diff)

Document the fact that LoadFromStream? is meant to append tasks instead of overwrite tasks in the list.

  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.IO;
26
27namespace Eraser.Manager
28{
29    /// <summary>
30    /// Executor base class. This class will manage the tasks currently scheduled
31    /// to be run and will run them when they are set to be run. This class is
32    /// abstract as they each will have their own ways of dealing with tasks.
33    /// </summary>
34    public abstract class Executor : IDisposable
35    {
36        #region IDisposable members
37        ~Executor()
38        {
39            Dispose(false);
40        }
41
42        protected virtual void Dispose(bool disposing)
43        {
44        }
45
46        public void Dispose()
47        {
48            Dispose(true);
49            GC.SuppressFinalize(this);
50        }
51        #endregion
52
53        /// <summary>
54        /// Starts the execution of tasks queued.
55        /// </summary>
56        public abstract void Run();
57
58        /// <summary>
59        /// Queues the task for execution.
60        /// </summary>
61        /// <param name="task">The task to queue.</param>
62        public abstract void QueueTask(Task task);
63
64        /// <summary>
65        /// Schedules the given task for execution.
66        /// </summary>
67        /// <param name="task">The task to schedule</param>
68        public abstract void ScheduleTask(Task task);
69
70        /// <summary>
71        /// Removes the given task from the execution queue.
72        /// </summary>
73        /// <param name="task">The task to cancel.</param>
74        public abstract void UnqueueTask(Task task);
75
76        /// <summary>
77        /// Queues all tasks in the task list which are meant for restart execution.
78        /// This is a separate function rather than just running them by default on
79        /// task load because creating a new instance and loading the task list
80        /// may just be a program restart and may not necessarily be a system
81        /// restart. Therefore this fuction has to be explicitly called by clients.
82        /// </summary>
83        public abstract void QueueRestartTasks();
84
85        /// <summary>
86        /// Retrieves the current task list for the executor.
87        /// </summary>
88        /// <returns>A list of tasks which the executor has registered.</returns>
89        public abstract ExecutorTasksCollection Tasks { get; protected set; }
90
91        /// <summary>
92        /// The task added event object.
93        /// </summary>
94        public EventHandler<TaskEventArgs> TaskAdded { get; set; }
95
96        /// <summary>
97        /// Helper function for the task added event.
98        /// </summary>
99        internal void OnTaskAdded(TaskEventArgs e)
100        {
101            if (TaskAdded != null)
102                TaskAdded(this, e);
103        }
104
105        /// <summary>
106        /// The task added event object.
107        /// </summary>
108        public EventHandler<TaskEventArgs> TaskDeleted { get; set; }
109
110        /// <summary>
111        /// Helper function for the task deleted event.
112        /// </summary>
113        internal void OnTaskDeleted(TaskEventArgs e)
114        {
115            if (TaskDeleted != null)
116                TaskDeleted(this, e);
117        }
118
119        /// <summary>
120        /// The task processing event object.
121        /// </summary>
122        public EventHandler<TaskEventArgs> TaskProcessing { get; set; }
123
124        /// <summary>
125        /// Helper function for the Task processing event.
126        /// </summary>
127        protected void OnTaskProcessing(TaskEventArgs e)
128        {
129            if (TaskProcessing != null)
130                TaskProcessing(this, e);
131        }
132
133        /// <summary>
134        /// The task processed event object.
135        /// </summary>
136        public EventHandler<TaskEventArgs> TaskProcessed { get; set; }
137
138        /// <summary>
139        /// Helper function for the Task processed event.
140        /// </summary>
141        protected void OnTaskProcessed(TaskEventArgs e)
142        {
143            if (TaskProcessed != null)
144                TaskProcessed(this, e);
145        }
146    }
147
148    public abstract class ExecutorTasksCollection : IList<Task>, ICollection<Task>,
149        IEnumerable<Task>
150    {
151        /// <summary>
152        /// Constructor.
153        /// </summary>
154        /// <param name="executor">The <seealso cref="Executor"/> object owning
155        /// this task list.</param>
156        protected ExecutorTasksCollection(Executor executor)
157        {
158            Owner = executor;
159        }
160
161        #region IList<Task> Members
162        public abstract int IndexOf(Task item);
163        public abstract void Insert(int index, Task item);
164        public abstract void RemoveAt(int index);
165        public abstract Task this[int index] { get; set; }
166        #endregion
167
168        #region ICollection<Task> Members
169        public abstract void Add(Task item);
170        public abstract void Clear();
171        public abstract bool Contains(Task item);
172        public abstract void CopyTo(Task[] array, int arrayIndex);
173        public abstract int Count { get; }
174        public bool IsReadOnly { get { return false; } }
175        public abstract bool Remove(Task item);
176        #endregion
177
178        #region IEnumerable<Task> Members
179        public abstract IEnumerator<Task> GetEnumerator();
180        #endregion
181
182        #region IEnumerable Members
183        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
184        {
185            return GetEnumerator();
186        }
187        #endregion
188
189        /// <param name="stream">The stream to save to.</param>
190        public abstract void SaveToStream(Stream stream);
191
192        /// <summary>
193        /// Loads the task list from the given stream.
194        /// </summary>
195        /// <remarks>This will append the tasks in the given stream to the current list of
196        /// tasks instead of overwriting it.</remarks>
197        /// <param name="stream">The stream to save to.</param>
198        public abstract void LoadFromStream(Stream stream);
199
200        /// <summary>
201        /// The owner of this task list.
202        /// </summary>
203        protected Executor Owner { get; private set; }
204    }
205}
Note: See TracBrowser for help on using the repository browser.