source: XMLIO_V2/external/src/POCO/Foundation.save/Poco/ActiveResult.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: 11.6 KB
Line 
1//
2// ActiveResult.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/ActiveResult.h#3 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  ActiveObjects
9//
10// Definition of the ActiveResult class.
11//
12// Copyright (c) 2004-2007, 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_ActiveResult_INCLUDED
40#define Foundation_ActiveResult_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/Mutex.h"
45#include "Poco/Event.h"
46#include "Poco/RefCountedObject.h"
47#include "Poco/Exception.h"
48#include <algorithm>
49
50
51namespace Poco {
52
53
54template <class ResultType>
55class ActiveResultHolder: public RefCountedObject
56        /// This class holds the result of an asynchronous method
57        /// invocation. It is used to pass the result from the
58        /// execution thread back to the invocation thread.
59        /// The class uses reference counting for memory management.
60        /// Do not use this class directly, use ActiveResult instead.
61{
62public:
63        ActiveResultHolder():
64                _pData(0),
65                _pExc(0),
66                _event(false)
67                /// Creates an ActiveResultHolder.
68        {
69        }
70               
71        ResultType& data()
72                /// Returns a reference to the actual result.
73        {
74                poco_check_ptr(_pData);
75                return *_pData;
76        }
77       
78        void data(ResultType* pData)
79        {
80                delete _pData;
81                _pData = pData;
82        }
83       
84        void wait()
85                /// Pauses the caller until the result becomes available.
86        {
87                _event.wait();
88        }
89       
90        bool tryWait(long milliseconds)
91                /// Waits up to the specified interval for the result to
92                /// become available. Returns true if the result became
93                /// available, false otherwise.
94        {
95                return _event.tryWait(milliseconds);
96        }
97       
98        void wait(long milliseconds)
99                /// Waits up to the specified interval for the result to
100                /// become available. Throws a TimeoutException if the
101                /// result did not became available.
102        {
103                _event.wait(milliseconds);
104        }
105       
106        void notify()
107                /// Notifies the invoking thread that the result became available.
108        {
109                _event.set();
110        }
111       
112        bool failed() const
113                /// Returns true if the active method failed (and threw an exception).
114                /// Information about the exception can be obtained by calling error().
115        {
116                return _pExc != 0;
117        }
118       
119        std::string error() const
120                /// If the active method threw an exception, a textual representation
121                /// of the exception is returned. An empty string is returned if the
122                /// active method completed successfully.
123        {
124                if (_pExc)
125                        return _pExc->message();
126                else
127                        return std::string();
128        }
129       
130        Exception* exception() const
131                /// If the active method threw an exception, a clone of the exception
132                /// object is returned, otherwise null.
133        {
134                return _pExc;
135        }
136       
137        void error(const Exception& exc)
138                /// Sets the exception.
139        {
140                delete _pExc;
141                _pExc = exc.clone();
142        }
143       
144        void error(const std::string& msg)
145                /// Sets the exception.
146        {
147                delete _pExc;
148                _pExc = new UnhandledException(msg);
149        }
150
151protected:
152        ~ActiveResultHolder()
153        {
154                delete _pData;
155                delete _pExc;
156        }
157
158private:
159        ResultType* _pData;
160        Exception*  _pExc;
161        Event       _event;
162};
163
164
165
166template <>
167class ActiveResultHolder<void>: public RefCountedObject
168{
169public:
170        ActiveResultHolder():
171                _pExc(0),
172                _event(false)
173                /// Creates an ActiveResultHolder.
174        {
175        }
176       
177        void wait()
178                /// Pauses the caller until the result becomes available.
179        {
180                _event.wait();
181        }
182       
183        bool tryWait(long milliseconds)
184                /// Waits up to the specified interval for the result to
185                /// become available. Returns true if the result became
186                /// available, false otherwise.
187        {
188                return _event.tryWait(milliseconds);
189        }
190       
191        void wait(long milliseconds)
192                /// Waits up to the specified interval for the result to
193                /// become available. Throws a TimeoutException if the
194                /// result did not became available.
195        {
196                _event.wait(milliseconds);
197        }
198       
199        void notify()
200                /// Notifies the invoking thread that the result became available.
201        {
202                _event.set();
203        }
204       
205        bool failed() const
206                /// Returns true if the active method failed (and threw an exception).
207                /// Information about the exception can be obtained by calling error().
208        {
209                return _pExc != 0;
210        }
211       
212        std::string error() const
213                /// If the active method threw an exception, a textual representation
214                /// of the exception is returned. An empty string is returned if the
215                /// active method completed successfully.
216        {
217                if (_pExc)
218                        return _pExc->message();
219                else
220                        return std::string();
221        }
222       
223        Exception* exception() const
224                /// If the active method threw an exception, a clone of the exception
225                /// object is returned, otherwise null.
226        {
227                return _pExc;
228        }
229       
230        void error(const Exception& exc)
231                /// Sets the exception.
232        {
233                delete _pExc;
234                _pExc = exc.clone();
235        }
236       
237        void error(const std::string& msg)
238                /// Sets the exception.
239        {
240                delete _pExc;
241                _pExc = new UnhandledException(msg);
242        }
243
244protected:
245        ~ActiveResultHolder()
246        {
247                delete _pExc;
248        }
249
250private:
251        Exception*  _pExc;
252        Event       _event;
253};
254
255
256template <class RT>
257class ActiveResult
258        /// This class holds the result of an asynchronous method
259        /// invocation (see class ActiveMethod). It is used to pass the
260        /// result from the execution thread back to the invocation thread.
261{
262public:
263        typedef RT ResultType;
264        typedef ActiveResultHolder<ResultType> ActiveResultHolderType;
265
266        ActiveResult(ActiveResultHolderType* pHolder):
267                _pHolder(pHolder)
268                /// Creates the active result. For internal use only.
269        {
270                poco_check_ptr (pHolder);
271        }
272       
273        ActiveResult(const ActiveResult& result)
274                /// Copy constructor.
275        {
276                _pHolder = result._pHolder;
277                _pHolder->duplicate();
278        }
279       
280        ~ActiveResult()
281                /// Destroys the result.
282        {
283                _pHolder->release();
284        }
285       
286        ActiveResult& operator = (const ActiveResult& result)
287                /// Assignment operator.
288        {
289                ActiveResult tmp(result);
290                swap(tmp);
291                return *this;
292        }
293       
294        void swap(ActiveResult& result)
295        {
296                using std::swap;
297                swap(_pHolder, result._pHolder);
298        }
299       
300        ResultType& data() const
301                /// Returns a reference to the result data.
302        {
303                return _pHolder->data();
304        }
305       
306        void data(ResultType* pValue)
307        {
308                _pHolder->data(pValue);
309        }
310       
311        void wait()
312                /// Pauses the caller until the result becomes available.
313        {
314                _pHolder->wait();
315        }
316       
317        bool tryWait(long milliseconds)
318                /// Waits up to the specified interval for the result to
319                /// become available. Returns true if the result became
320                /// available, false otherwise.
321        {
322                return _pHolder->tryWait(milliseconds);
323        }
324       
325        void wait(long milliseconds)
326                /// Waits up to the specified interval for the result to
327                /// become available. Throws a TimeoutException if the
328                /// result did not became available.
329        {
330                _pHolder->wait(milliseconds);
331        }
332       
333        bool available() const
334                /// Returns true if a result is available.
335        {
336                return _pHolder->tryWait(0);
337        }
338       
339        bool failed() const
340                /// Returns true if the active method failed (and threw an exception).
341                /// Information about the exception can be obtained by calling error().
342        {
343                return _pHolder->failed();
344        }
345       
346        std::string error() const
347                /// If the active method threw an exception, a textual representation
348                /// of the exception is returned. An empty string is returned if the
349                /// active method completed successfully.
350        {
351                return _pHolder->error();
352        }
353
354        Exception* exception() const
355                /// If the active method threw an exception, a clone of the exception
356                /// object is returned, otherwise null.
357        {
358                return _pHolder->exception();
359        }
360
361        void notify()
362                /// Notifies the invoking thread that the result became available.
363                /// For internal use only.
364        {
365                _pHolder->notify();
366        }
367       
368        ResultType& data()
369                /// Returns a non-const reference to the result data. For internal
370                /// use only.
371        {
372                return _pHolder->data();
373        }
374       
375        void error(const std::string& msg)
376                /// Sets the failed flag and the exception message.
377        {
378                _pHolder->error(msg);
379        }
380
381        void error(const Exception& exc)
382                /// Sets the failed flag and the exception message.
383        {
384                _pHolder->error(exc);
385        }
386       
387private:
388        ActiveResult();
389
390        ActiveResultHolderType* _pHolder;
391};
392
393
394
395template <>
396class ActiveResult<void>
397        /// This class holds the result of an asynchronous method
398        /// invocation (see class ActiveMethod). It is used to pass the
399        /// result from the execution thread back to the invocation thread.
400{
401public:
402        typedef ActiveResultHolder<void> ActiveResultHolderType;
403
404        ActiveResult(ActiveResultHolderType* pHolder):
405                _pHolder(pHolder)
406                /// Creates the active result. For internal use only.
407        {
408                poco_check_ptr (pHolder);
409        }
410       
411        ActiveResult(const ActiveResult& result)
412                /// Copy constructor.
413        {
414                _pHolder = result._pHolder;
415                _pHolder->duplicate();
416        }
417       
418        ~ActiveResult()
419                /// Destroys the result.
420        {
421                _pHolder->release();
422        }
423       
424        ActiveResult& operator = (const ActiveResult& result)
425                /// Assignment operator.
426        {
427                ActiveResult tmp(result);
428                swap(tmp);
429                return *this;
430        }
431       
432        void swap(ActiveResult& result)
433        {
434                using std::swap;
435                swap(_pHolder, result._pHolder);
436        }
437       
438        void wait()
439                /// Pauses the caller until the result becomes available.
440        {
441                _pHolder->wait();
442        }
443       
444        bool tryWait(long milliseconds)
445                /// Waits up to the specified interval for the result to
446                /// become available. Returns true if the result became
447                /// available, false otherwise.
448        {
449                return _pHolder->tryWait(milliseconds);
450        }
451       
452        void wait(long milliseconds)
453                /// Waits up to the specified interval for the result to
454                /// become available. Throws a TimeoutException if the
455                /// result did not became available.
456        {
457                _pHolder->wait(milliseconds);
458        }
459       
460        bool available() const
461                /// Returns true if a result is available.
462        {
463                return _pHolder->tryWait(0);
464        }
465       
466        bool failed() const
467                /// Returns true if the active method failed (and threw an exception).
468                /// Information about the exception can be obtained by calling error().
469        {
470                return _pHolder->failed();
471        }
472       
473        std::string error() const
474                /// If the active method threw an exception, a textual representation
475                /// of the exception is returned. An empty string is returned if the
476                /// active method completed successfully.
477        {
478                return _pHolder->error();
479        }
480
481        Exception* exception() const
482                /// If the active method threw an exception, a clone of the exception
483                /// object is returned, otherwise null.
484        {
485                return _pHolder->exception();
486        }
487
488        void notify()
489                /// Notifies the invoking thread that the result became available.
490                /// For internal use only.
491        {
492                _pHolder->notify();
493        }
494       
495        void error(const std::string& msg)
496                /// Sets the failed flag and the exception message.
497        {
498                _pHolder->error(msg);
499        }
500
501        void error(const Exception& exc)
502                /// Sets the failed flag and the exception message.
503        {
504                _pHolder->error(exc);
505        }
506       
507private:
508        ActiveResult();
509
510        ActiveResultHolderType* _pHolder;
511};
512
513
514} // namespace Poco
515
516
517#endif // Foundation_ActiveResult_INCLUDED
Note: See TracBrowser for help on using the repository browser.