source: XMLIO_V2/external/include/Poco/ActiveMethod.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: 7.1 KB
Line 
1//
2// ActiveMethod.h
3//
4// $Id: //poco/1.3/Foundation/include/Poco/ActiveMethod.h#3 $
5//
6// Library: Foundation
7// Package: Threading
8// Module:  ActiveObjects
9//
10// Definition of the ActiveMethod 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_ActiveMethod_INCLUDED
40#define Foundation_ActiveMethod_INCLUDED
41
42
43#include "Poco/Foundation.h"
44#include "Poco/ActiveResult.h"
45#include "Poco/ActiveRunnable.h"
46#include "Poco/ActiveStarter.h"
47#include "Poco/AutoPtr.h"
48
49
50namespace Poco {
51
52
53template <class ResultType, class ArgType, class OwnerType, class StarterType = ActiveStarter<OwnerType> >
54class ActiveMethod
55        /// An active method is a method that, when called, executes
56        /// in its own thread. ActiveMethod's take exactly one
57        /// argument and can return a value. To pass more than one
58        /// argument to the method, use a struct.
59        /// The following example shows how to add an ActiveMethod
60        /// to a class:
61        ///
62        ///     class ActiveObject
63        ///     {
64        ///     public:
65        ///         ActiveObject():
66        ///             exampleActiveMethod(this, &ActiveObject::exampleActiveMethodImpl)
67        ///         {
68        ///         }
69        ///
70        ///         ActiveMethod<std::string, std::string, ActiveObject> exampleActiveMethod;
71        ///
72        ///     protected:
73        ///         std::string exampleActiveMethodImpl(const std::string& arg)
74        ///         {
75        ///             ...
76        ///         }
77        ///     };
78        ///
79        /// And following is an example that shows how to invoke an ActiveMethod.
80        ///
81        ///     ActiveObject myActiveObject;
82        ///     ActiveResult<std::string> result = myActiveObject.exampleActiveMethod("foo");
83        ///     ...
84        ///     result.wait();
85        ///     std::cout << result.data() << std::endl;
86        ///
87        /// The way an ActiveMethod is started can be changed by passing a StarterType
88        /// template argument with a corresponding class. The default ActiveStarter
89        /// starts the method in its own thread, obtained from a thread pool.
90        ///
91        /// For an alternative implementation of StarterType, see ActiveDispatcher.
92        ///
93        /// For methods that do not require an argument or a return value, the Void
94        /// class can be used.
95{
96public:
97        typedef ResultType (OwnerType::*Callback)(const ArgType&);
98        typedef ActiveResult<ResultType> ActiveResultType;
99        typedef ActiveRunnable<ResultType, ArgType, OwnerType> ActiveRunnableType;
100
101        ActiveMethod(OwnerType* pOwner, Callback method):
102                _pOwner(pOwner),
103                _method(method)
104                /// Creates an ActiveMethod object.
105        {
106                poco_check_ptr (pOwner);
107        }
108       
109        ActiveResultType operator () (const ArgType& arg)
110                /// Invokes the ActiveMethod.
111        {
112                ActiveResultType result(new ActiveResultHolder<ResultType>());
113                ActiveRunnableBase::Ptr pRunnable(new ActiveRunnableType(_pOwner, _method, arg, result));
114                StarterType::start(_pOwner, pRunnable);
115                return result;
116        }
117               
118        ActiveMethod(const ActiveMethod& other):
119                _pOwner(other._pOwner),
120                _method(other._method)
121        {
122        }
123
124        ActiveMethod& operator = (const ActiveMethod& other)
125        {
126                ActiveMethod tmp(other);
127                swap(tmp);
128                return *this;
129        }
130
131        void swap(ActiveMethod& other)
132        {
133                std::swap(_pOwner, other._pOwner);
134                std::swap(_method, other._method);
135        }
136
137private:
138        ActiveMethod();
139
140        OwnerType* _pOwner;
141        Callback   _method;
142};
143
144
145
146template <class ResultType, class OwnerType, class StarterType>
147class ActiveMethod <ResultType, void, OwnerType, StarterType>
148        /// An active method is a method that, when called, executes
149        /// in its own thread. ActiveMethod's take exactly one
150        /// argument and can return a value. To pass more than one
151        /// argument to the method, use a struct.
152        /// The following example shows how to add an ActiveMethod
153        /// to a class:
154        ///
155        ///     class ActiveObject
156        ///     {
157        ///     public:
158        ///         ActiveObject():
159        ///             exampleActiveMethod(this, &ActiveObject::exampleActiveMethodImpl)
160        ///         {
161        ///         }
162        ///
163        ///         ActiveMethod<std::string, std::string, ActiveObject> exampleActiveMethod;
164        ///
165        ///     protected:
166        ///         std::string exampleActiveMethodImpl(const std::string& arg)
167        ///         {
168        ///             ...
169        ///         }
170        ///     };
171        ///
172        /// And following is an example that shows how to invoke an ActiveMethod.
173        ///
174        ///     ActiveObject myActiveObject;
175        ///     ActiveResult<std::string> result = myActiveObject.exampleActiveMethod("foo");
176        ///     ...
177        ///     result.wait();
178        ///     std::cout << result.data() << std::endl;
179        ///
180        /// The way an ActiveMethod is started can be changed by passing a StarterType
181        /// template argument with a corresponding class. The default ActiveStarter
182        /// starts the method in its own thread, obtained from a thread pool.
183        ///
184        /// For an alternative implementation of StarterType, see ActiveDispatcher.
185        ///
186        /// For methods that do not require an argument or a return value, simply use void.
187{
188public:
189        typedef ResultType (OwnerType::*Callback)(void);
190        typedef ActiveResult<ResultType> ActiveResultType;
191        typedef ActiveRunnable<ResultType, void, OwnerType> ActiveRunnableType;
192
193        ActiveMethod(OwnerType* pOwner, Callback method):
194                _pOwner(pOwner),
195                _method(method)
196                /// Creates an ActiveMethod object.
197        {
198                poco_check_ptr (pOwner);
199        }
200       
201        ActiveResultType operator () (void)
202                /// Invokes the ActiveMethod.
203        {
204                ActiveResultType result(new ActiveResultHolder<ResultType>());
205                ActiveRunnableBase::Ptr pRunnable(new ActiveRunnableType(_pOwner, _method, result));
206                StarterType::start(_pOwner, pRunnable);
207                return result;
208        }
209               
210        ActiveMethod(const ActiveMethod& other):
211                _pOwner(other._pOwner),
212                _method(other._method)
213        {
214        }
215
216        ActiveMethod& operator = (const ActiveMethod& other)
217        {
218                ActiveMethod tmp(other);
219                swap(tmp);
220                return *this;
221        }
222
223        void swap(ActiveMethod& other)
224        {
225                std::swap(_pOwner, other._pOwner);
226                std::swap(_method, other._method);
227        }
228
229private:
230        ActiveMethod();
231
232        OwnerType* _pOwner;
233        Callback   _method;
234};
235
236
237} // namespace Poco
238
239
240#endif // Foundation_ActiveMethod_INCLUDED
Note: See TracBrowser for help on using the repository browser.