]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/AsyncJobCalls.h
6 #ifndef SQUID_ASYNCJOBCALLS_H
7 #define SQUID_ASYNCJOBCALLS_H
9 #include "base/AsyncJob.h"
10 #include "base/CbcPointer.h"
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.
22 class JobDialer
: public CallDialer
25 typedef Job DestClass
;
26 typedef CbcPointer
<Job
> JobPointer
;
28 JobDialer(const JobPointer
&aJob
);
29 JobDialer(const JobDialer
&d
);
31 virtual bool canDial(AsyncCall
&call
);
32 void dial(AsyncCall
&call
);
37 virtual void doDial() = 0; // actually calls the job method
40 // not implemented and should not be needed
41 JobDialer
&operator =(const JobDialer
&);
44 /// schedule an async job call using a dialer; use CallJobHere macros instead
45 template <class Dialer
>
47 CallJob(int debugSection
, int debugLevel
, const char *fileName
, int fileLine
,
48 const char *callName
, const Dialer
&dialer
)
50 AsyncCall::Pointer call
= asyncCall(debugSection
, debugLevel
, callName
, dialer
);
51 return ScheduleCall(fileName
, fileLine
, call
);
55 #define CallJobHere(debugSection, debugLevel, job, Class, method) \
56 CallJob((debugSection), (debugLevel), __FILE__, __LINE__, \
57 (#Class "::" #method), \
58 JobMemFun<Class>((job), &Class::method))
60 #define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1) \
61 CallJob((debugSection), (debugLevel), __FILE__, __LINE__, \
62 (#Class "::" #method), \
63 JobMemFun((job), &Class::method, (arg1)))
66 /// Convenience macro to create a Dialer-based job callback
67 #define JobCallback(dbgSection, dbgLevel, Dialer, job, method) \
68 asyncCall((dbgSection), (dbgLevel), #method, \
69 Dialer(CbcPointer<Dialer::DestClass>(job), &method))
73 * *MemFunT are member function (i.e., class method) wrappers. They store
74 * details of a method call in an object so that the call can be delayed
75 * and executed asynchronously. Details may include the object pointer,
76 * the handler method pointer, and parameters. To simplify, we require
77 * all handlers to return void and not be constant.
81 * We need one wrapper for every supported member function arity (i.e.,
82 * number of handler arguments). The first template parameter is the class
83 * type of the handler. That class must be an AsyncJob child.
86 // Arity names are from http://en.wikipedia.org/wiki/Arity
89 class NullaryMemFunT
: public JobDialer
<Job
>
92 typedef void (Job::*Method
)();
93 explicit NullaryMemFunT(const CbcPointer
<Job
> &aJob
, Method aMethod
):
94 JobDialer
<Job
>(aJob
), method(aMethod
) {}
96 virtual void print(std::ostream
&os
) const { os
<< "()"; }
102 virtual void doDial() { ((&(*this->job
))->*method
)(); }
105 template <class Job
, class Data
, class Argument1
= Data
>
106 class UnaryMemFunT
: public JobDialer
<Job
>
109 typedef void (Job::*Method
)(Argument1
);
110 explicit UnaryMemFunT(const CbcPointer
<Job
> &aJob
, Method aMethod
,
111 const Data
&anArg1
): JobDialer
<Job
>(aJob
),
112 method(aMethod
), arg1(anArg1
) {}
114 virtual void print(std::ostream
&os
) const { os
<< '(' << arg1
<< ')'; }
121 virtual void doDial() { ((&(*this->job
))->*method
)(arg1
); }
124 // ... add more as needed
127 // Now we add global templated functions that create the member function
128 // wrappers above. These are for convenience: it is often easier to
129 // call a templated function than to create a templated object.
133 JobMemFun(const CbcPointer
<C
> &job
, typename NullaryMemFunT
<C
>::Method method
)
135 return NullaryMemFunT
<C
>(job
, method
);
138 template <class C
, class Argument1
>
139 UnaryMemFunT
<C
, Argument1
>
140 JobMemFun(const CbcPointer
<C
> &job
, typename UnaryMemFunT
<C
, Argument1
>::Method method
,
143 return UnaryMemFunT
<C
, Argument1
>(job
, method
, arg1
);
150 JobDialer
<Job
>::JobDialer(const JobPointer
&aJob
): job(aJob
)
155 JobDialer
<Job
>::JobDialer(const JobDialer
<Job
> &d
): CallDialer(d
), job(d
.job
)
161 JobDialer
<Job
>::canDial(AsyncCall
&call
)
164 return call
.cancel("job gone");
166 return job
->canBeCalled(call
);
171 JobDialer
<Job
>::dial(AsyncCall
&call
)
173 job
->callStart(call
);
177 } catch (const std::exception
&e
) {
178 debugs(call
.debugSection
, 3,
179 HERE
<< call
.name
<< " threw exception: " << e
.what());
180 job
->callException(e
);
183 job
->callEnd(); // may delete job
186 #endif /* SQUID_ASYNCJOBCALLS_H */