]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | /* | |
3 | * $Id$ | |
4 | */ | |
5 | ||
6 | #ifndef SQUID_ASYNCJOBCALLS_H | |
7 | #define SQUID_ASYNCJOBCALLS_H | |
8 | ||
9 | #include "base/AsyncJob.h" | |
10 | #include "base/CbcPointer.h" | |
11 | #include "Debug.h" | |
12 | ||
13 | /** | |
14 | \ingroup AsyncJobAPI | |
15 | * This is a base class for all job call dialers. It does all the job | |
16 | * dialing logic (debugging, handling exceptions, etc.) except for calling | |
17 | * the job method. The latter requires knowing the number and type of method | |
18 | * parameters. Thus, we add a dial() virtual method that the MemFunT templates | |
19 | * below implement for us, calling the job's method with the right params. | |
20 | */ | |
21 | template <class Job> | |
22 | class JobDialer: public CallDialer | |
23 | { | |
24 | public: | |
25 | typedef Job DestClass; | |
26 | typedef CbcPointer<Job> JobPointer; | |
27 | ||
28 | JobDialer(const JobPointer &aJob); | |
29 | JobDialer(const JobDialer &d); | |
30 | ||
31 | virtual bool canDial(AsyncCall &call); | |
32 | void dial(AsyncCall &call); | |
33 | ||
34 | JobPointer job; | |
35 | ||
36 | protected: | |
37 | virtual void doDial() = 0; // actually calls the job method | |
38 | ||
39 | private: | |
40 | // not implemented and should not be needed | |
41 | JobDialer &operator =(const JobDialer &); | |
42 | }; | |
43 | ||
44 | /// schedule an async job call using a dialer; use CallJobHere macros instead | |
45 | template <class Dialer> | |
46 | bool | |
47 | CallJob(int debugSection, int debugLevel, const char *fileName, int fileLine, | |
48 | const char *callName, const Dialer &dialer) | |
49 | { | |
50 | AsyncCall::Pointer call = asyncCall(debugSection, debugLevel, callName, dialer); | |
51 | return ScheduleCall(fileName, fileLine, call); | |
52 | } | |
53 | ||
54 | #define CallJobHere(debugSection, debugLevel, job, Class, method) \ | |
55 | CallJob((debugSection), (debugLevel), __FILE__, __LINE__, \ | |
56 | (#Class "::" #method), \ | |
57 | JobMemFun<Class>((job), &Class::method)) | |
58 | ||
59 | #define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1) \ | |
60 | CallJob((debugSection), (debugLevel), __FILE__, __LINE__, \ | |
61 | (#Class "::" #method), \ | |
62 | JobMemFun((job), &Class::method, (arg1))) | |
63 | ||
64 | /// Convenience macro to create a Dialer-based job callback | |
65 | #define JobCallback(dbgSection, dbgLevel, Dialer, job, method) \ | |
66 | asyncCall((dbgSection), (dbgLevel), #method, \ | |
67 | Dialer(CbcPointer<Dialer::DestClass>(job), &method)) | |
68 | ||
69 | /* | |
70 | * *MemFunT are member function (i.e., class method) wrappers. They store | |
71 | * details of a method call in an object so that the call can be delayed | |
72 | * and executed asynchronously. Details may include the object pointer, | |
73 | * the handler method pointer, and parameters. To simplify, we require | |
74 | * all handlers to return void and not be constant. | |
75 | */ | |
76 | ||
77 | /* | |
78 | * We need one wrapper for every supported member function arity (i.e., | |
79 | * number of handler arguments). The first template parameter is the class | |
80 | * type of the handler. That class must be an AsyncJob child. | |
81 | */ | |
82 | ||
83 | // Arity names are from http://en.wikipedia.org/wiki/Arity | |
84 | ||
85 | template <class Job> | |
86 | class NullaryMemFunT: public JobDialer<Job> | |
87 | { | |
88 | public: | |
89 | typedef void (Job::*Method)(); | |
90 | explicit NullaryMemFunT(const CbcPointer<Job> &aJob, Method aMethod): | |
91 | JobDialer<Job>(aJob), method(aMethod) {} | |
92 | ||
93 | virtual void print(std::ostream &os) const { os << "()"; } | |
94 | ||
95 | public: | |
96 | Method method; | |
97 | ||
98 | protected: | |
99 | virtual void doDial() { ((&(*this->job))->*method)(); } | |
100 | }; | |
101 | ||
102 | template <class Job, class Data, class Argument1 = Data> | |
103 | class UnaryMemFunT: public JobDialer<Job> | |
104 | { | |
105 | public: | |
106 | typedef void (Job::*Method)(Argument1); | |
107 | explicit UnaryMemFunT(const CbcPointer<Job> &aJob, Method aMethod, | |
108 | const Data &anArg1): JobDialer<Job>(aJob), | |
109 | method(aMethod), arg1(anArg1) {} | |
110 | ||
111 | virtual void print(std::ostream &os) const { os << '(' << arg1 << ')'; } | |
112 | ||
113 | public: | |
114 | Method method; | |
115 | Data arg1; | |
116 | ||
117 | protected: | |
118 | virtual void doDial() { ((&(*this->job))->*method)(arg1); } | |
119 | }; | |
120 | ||
121 | // ... add more as needed | |
122 | ||
123 | // Now we add global templated functions that create the member function | |
124 | // wrappers above. These are for convenience: it is often easier to | |
125 | // call a templated function than to create a templated object. | |
126 | ||
127 | template <class C> | |
128 | NullaryMemFunT<C> | |
129 | JobMemFun(const CbcPointer<C> &job, typename NullaryMemFunT<C>::Method method) | |
130 | { | |
131 | return NullaryMemFunT<C>(job, method); | |
132 | } | |
133 | ||
134 | template <class C, class Argument1> | |
135 | UnaryMemFunT<C, Argument1> | |
136 | JobMemFun(const CbcPointer<C> &job, typename UnaryMemFunT<C, Argument1>::Method method, | |
137 | Argument1 arg1) | |
138 | { | |
139 | return UnaryMemFunT<C, Argument1>(job, method, arg1); | |
140 | } | |
141 | ||
142 | // inlined methods | |
143 | ||
144 | template<class Job> | |
145 | JobDialer<Job>::JobDialer(const JobPointer &aJob): job(aJob) | |
146 | { | |
147 | } | |
148 | ||
149 | template<class Job> | |
150 | JobDialer<Job>::JobDialer(const JobDialer<Job> &d): CallDialer(d), job(d.job) | |
151 | { | |
152 | } | |
153 | ||
154 | template<class Job> | |
155 | bool | |
156 | JobDialer<Job>::canDial(AsyncCall &call) | |
157 | { | |
158 | if (!job) | |
159 | return call.cancel("job gone"); | |
160 | ||
161 | return job->canBeCalled(call); | |
162 | } | |
163 | ||
164 | template<class Job> | |
165 | void | |
166 | JobDialer<Job>::dial(AsyncCall &call) | |
167 | { | |
168 | job->callStart(call); | |
169 | ||
170 | try { | |
171 | doDial(); | |
172 | } catch (const std::exception &e) { | |
173 | debugs(call.debugSection, 3, | |
174 | HERE << call.name << " threw exception: " << e.what()); | |
175 | job->callException(e); | |
176 | } | |
177 | ||
178 | job->callEnd(); // may delete job | |
179 | } | |
180 | ||
181 | #endif /* SQUID_ASYNCJOBCALLS_H */ |