root / trunk / tbeta / OSX / libs / poco / include / Poco / Task.h @ 5
View | Annotate | Download (5.1 KB)
| 1 | 5 | ss | //
|
|---|---|---|---|
| 2 | 5 | ss | // Task.h
|
| 3 | 5 | ss | //
|
| 4 | 5 | ss | // $Id: //poco/1.3/Foundation/include/Poco/Task.h#1 $
|
| 5 | 5 | ss | //
|
| 6 | 5 | ss | // Library: Foundation
|
| 7 | 5 | ss | // Package: Tasks
|
| 8 | 5 | ss | // Module: Tasks
|
| 9 | 5 | ss | //
|
| 10 | 5 | ss | // Definition of the Task class.
|
| 11 | 5 | ss | //
|
| 12 | 5 | ss | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
| 13 | 5 | ss | // and Contributors.
|
| 14 | 5 | ss | //
|
| 15 | 5 | ss | // Permission is hereby granted, free of charge, to any person or organization
|
| 16 | 5 | ss | // obtaining a copy of the software and accompanying documentation covered by
|
| 17 | 5 | ss | // this license (the "Software") to use, reproduce, display, distribute,
|
| 18 | 5 | ss | // execute, and transmit the Software, and to prepare derivative works of the
|
| 19 | 5 | ss | // Software, and to permit third-parties to whom the Software is furnished to
|
| 20 | 5 | ss | // do so, all subject to the following:
|
| 21 | 5 | ss | //
|
| 22 | 5 | ss | // The copyright notices in the Software and this entire statement, including
|
| 23 | 5 | ss | // the above license grant, this restriction and the following disclaimer,
|
| 24 | 5 | ss | // must be included in all copies of the Software, in whole or in part, and
|
| 25 | 5 | ss | // all derivative works of the Software, unless such copies or derivative
|
| 26 | 5 | ss | // works are solely in the form of machine-executable object code generated by
|
| 27 | 5 | ss | // a source language processor.
|
| 28 | 5 | ss | //
|
| 29 | 5 | ss | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 30 | 5 | ss | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 31 | 5 | ss | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
| 32 | 5 | ss | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
| 33 | 5 | ss | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
| 34 | 5 | ss | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
| 35 | 5 | ss | // DEALINGS IN THE SOFTWARE.
|
| 36 | 5 | ss | //
|
| 37 | 5 | ss | |
| 38 | 5 | ss | |
| 39 | 5 | ss | #ifndef Foundation_Task_INCLUDED
|
| 40 | 5 | ss | #define Foundation_Task_INCLUDED
|
| 41 | 5 | ss | |
| 42 | 5 | ss | |
| 43 | 5 | ss | #include "Poco/Foundation.h" |
| 44 | 5 | ss | #include "Poco/Runnable.h" |
| 45 | 5 | ss | #include "Poco/RefCountedObject.h" |
| 46 | 5 | ss | #include "Poco/Mutex.h" |
| 47 | 5 | ss | #include "Poco/Event.h" |
| 48 | 5 | ss | |
| 49 | 5 | ss | |
| 50 | 5 | ss | namespace Poco {
|
| 51 | 5 | ss | |
| 52 | 5 | ss | |
| 53 | 5 | ss | class TaskManager; |
| 54 | 5 | ss | class Notification; |
| 55 | 5 | ss | class NotificationCenter; |
| 56 | 5 | ss | |
| 57 | 5 | ss | |
| 58 | 5 | ss | class Foundation_API Task: public Runnable, public RefCountedObject
|
| 59 | 5 | ss | /// A Task is a subclass of Runnable that has a name
|
| 60 | 5 | ss | /// and supports progress reporting and cancellation.
|
| 61 | 5 | ss | ///
|
| 62 | 5 | ss | /// A TaskManager object can be used to take care of the
|
| 63 | 5 | ss | /// lifecycle of a Task.
|
| 64 | 5 | ss | {
|
| 65 | 5 | ss | public:
|
| 66 | 5 | ss | enum TaskState
|
| 67 | 5 | ss | {
|
| 68 | 5 | ss | TASK_IDLE, |
| 69 | 5 | ss | TASK_STARTING, |
| 70 | 5 | ss | TASK_RUNNING, |
| 71 | 5 | ss | TASK_CANCELLING, |
| 72 | 5 | ss | TASK_FINISHED |
| 73 | 5 | ss | }; |
| 74 | 5 | ss | |
| 75 | 5 | ss | Task(const std::string& name);
|
| 76 | 5 | ss | /// Creates the Task.
|
| 77 | 5 | ss | |
| 78 | 5 | ss | const std::string& name() const; |
| 79 | 5 | ss | /// Returns the task's name.
|
| 80 | 5 | ss | |
| 81 | 5 | ss | float progress() const; |
| 82 | 5 | ss | /// Returns the task's progress.
|
| 83 | 5 | ss | /// The value will be between 0.0 (just started)
|
| 84 | 5 | ss | /// and 1.0 (completed).
|
| 85 | 5 | ss | |
| 86 | 5 | ss | void cancel();
|
| 87 | 5 | ss | /// Requests the task to cancel itself. For cancellation
|
| 88 | 5 | ss | /// to work, the task's runTask() method must periodically
|
| 89 | 5 | ss | /// call isCancelled() and react accordingly.
|
| 90 | 5 | ss | |
| 91 | 5 | ss | bool isCancelled() const; |
| 92 | 5 | ss | /// Returns true if cancellation of the task has been
|
| 93 | 5 | ss | /// requested.
|
| 94 | 5 | ss | ///
|
| 95 | 5 | ss | /// A Task's runTask() method should periodically
|
| 96 | 5 | ss | /// call this method and stop whatever it is doing in an
|
| 97 | 5 | ss | /// orderly way when this method returns true.
|
| 98 | 5 | ss | |
| 99 | 5 | ss | TaskState state() const;
|
| 100 | 5 | ss | /// Returns the task's current state.
|
| 101 | 5 | ss | |
| 102 | 5 | ss | void reset();
|
| 103 | 5 | ss | /// Sets the task's progress to zero and clears the
|
| 104 | 5 | ss | /// cancel flag.
|
| 105 | 5 | ss | |
| 106 | 5 | ss | virtual void runTask() = 0; |
| 107 | 5 | ss | /// Do whatever the task needs to do. Must
|
| 108 | 5 | ss | /// be overridden by subclasses.
|
| 109 | 5 | ss | |
| 110 | 5 | ss | void run();
|
| 111 | 5 | ss | /// Calls the task's runTask() method and notifies the owner
|
| 112 | 5 | ss | /// of the task's start and completion.
|
| 113 | 5 | ss | |
| 114 | 5 | ss | protected:
|
| 115 | 5 | ss | bool sleep(long milliseconds); |
| 116 | 5 | ss | /// Suspends the current thread for the specified
|
| 117 | 5 | ss | /// amount of time.
|
| 118 | 5 | ss | ///
|
| 119 | 5 | ss | /// If the task is cancelled while it is sleeping,
|
| 120 | 5 | ss | /// sleep() will return immediately and the return
|
| 121 | 5 | ss | /// value will be true. If the time interval
|
| 122 | 5 | ss | /// passes without the task being cancelled, the
|
| 123 | 5 | ss | /// return value is false.
|
| 124 | 5 | ss | ///
|
| 125 | 5 | ss | /// A Task should use this method in favor of Thread::sleep().
|
| 126 | 5 | ss | |
| 127 | 5 | ss | void setProgress(float progress); |
| 128 | 5 | ss | /// Sets the task's progress.
|
| 129 | 5 | ss | /// The value should be between 0.0 (just started)
|
| 130 | 5 | ss | /// and 1.0 (completed).
|
| 131 | 5 | ss | |
| 132 | 5 | ss | virtual void postNotification(Notification* pNf);
|
| 133 | 5 | ss | /// Posts a notification to the task manager's
|
| 134 | 5 | ss | /// notification center.
|
| 135 | 5 | ss | ///
|
| 136 | 5 | ss | /// A task can use this method to post custom
|
| 137 | 5 | ss | /// notifications about its progress.
|
| 138 | 5 | ss | |
| 139 | 5 | ss | void setOwner(TaskManager* pOwner);
|
| 140 | 5 | ss | /// Sets the (optional) owner of the task.
|
| 141 | 5 | ss | |
| 142 | 5 | ss | TaskManager* getOwner() const;
|
| 143 | 5 | ss | /// Returns the owner of the task, which may be NULL.
|
| 144 | 5 | ss | |
| 145 | 5 | ss | void setState(TaskState state);
|
| 146 | 5 | ss | /// Sets the task's state.
|
| 147 | 5 | ss | |
| 148 | 5 | ss | virtual ~Task(); |
| 149 | 5 | ss | /// Destroys the Task.
|
| 150 | 5 | ss | |
| 151 | 5 | ss | private:
|
| 152 | 5 | ss | Task(); |
| 153 | 5 | ss | Task(const Task&);
|
| 154 | 5 | ss | Task& operator = (const Task&);
|
| 155 | 5 | ss | |
| 156 | 5 | ss | std::string _name; |
| 157 | 5 | ss | TaskManager* _pOwner; |
| 158 | 5 | ss | float _progress;
|
| 159 | 5 | ss | TaskState _state; |
| 160 | 5 | ss | Event _cancelEvent; |
| 161 | 5 | ss | mutable FastMutex _mutex; |
| 162 | 5 | ss | |
| 163 | 5 | ss | friend class TaskManager; |
| 164 | 5 | ss | }; |
| 165 | 5 | ss | |
| 166 | 5 | ss | |
| 167 | 5 | ss | //
|
| 168 | 5 | ss | // inlines
|
| 169 | 5 | ss | //
|
| 170 | 5 | ss | inline const std::string& Task::name() const |
| 171 | 5 | ss | {
|
| 172 | 5 | ss | return _name;
|
| 173 | 5 | ss | } |
| 174 | 5 | ss | |
| 175 | 5 | ss | |
| 176 | 5 | ss | inline float Task::progress() const |
| 177 | 5 | ss | {
|
| 178 | 5 | ss | FastMutex::ScopedLock lock(_mutex); |
| 179 | 5 | ss | |
| 180 | 5 | ss | return _progress;
|
| 181 | 5 | ss | } |
| 182 | 5 | ss | |
| 183 | 5 | ss | |
| 184 | 5 | ss | inline bool Task::isCancelled() const |
| 185 | 5 | ss | {
|
| 186 | 5 | ss | return _state == TASK_CANCELLING;
|
| 187 | 5 | ss | } |
| 188 | 5 | ss | |
| 189 | 5 | ss | |
| 190 | 5 | ss | inline Task::TaskState Task::state() const |
| 191 | 5 | ss | {
|
| 192 | 5 | ss | return _state;
|
| 193 | 5 | ss | } |
| 194 | 5 | ss | |
| 195 | 5 | ss | |
| 196 | 5 | ss | inline TaskManager* Task::getOwner() const |
| 197 | 5 | ss | {
|
| 198 | 5 | ss | FastMutex::ScopedLock lock(_mutex); |
| 199 | 5 | ss | |
| 200 | 5 | ss | return _pOwner;
|
| 201 | 5 | ss | } |
| 202 | 5 | ss | |
| 203 | 5 | ss | |
| 204 | 5 | ss | } // namespace Poco
|
| 205 | 5 | ss | |
| 206 | 5 | ss | |
| 207 | 5 | ss | #endif // Foundation_Task_INCLUDED |
