source: XMLIO_V2/external/include/Poco/Task.h @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

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