AsyncJobCalls.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_ASYNCJOBCALLS_H
10 #define SQUID_ASYNCJOBCALLS_H
11 
12 #include "base/AsyncJob.h"
13 #include "base/CbcPointer.h"
14 #include "Debug.h"
15 
24 template <class Job>
25 class JobDialer: public CallDialer
26 {
27 public:
28  typedef Job DestClass;
30 
31  JobDialer(const JobPointer &aJob);
32  JobDialer(const JobDialer &d);
33 
34  virtual bool canDial(AsyncCall &call);
35  void dial(AsyncCall &call);
36 
38 
39 protected:
40  virtual void doDial() = 0; // actually calls the job method
41 
42 private:
43  // not implemented and should not be needed
44  JobDialer &operator =(const JobDialer &);
45 };
46 
48 template <class Dialer>
49 bool
50 CallJob(int debugSection, int debugLevel, const char *fileName, int fileLine,
51  const char *callName, const Dialer &dialer)
52 {
53  AsyncCall::Pointer call = asyncCall(debugSection, debugLevel, callName, dialer);
54  return ScheduleCall(fileName, fileLine, call);
55 }
56 
57 #define CallJobHere(debugSection, debugLevel, job, Class, method) \
58  CallJob((debugSection), (debugLevel), __FILE__, __LINE__, \
59  (#Class "::" #method), \
60  JobMemFun<Class>((job), &Class::method))
61 
62 #define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1) \
63  CallJob((debugSection), (debugLevel), __FILE__, __LINE__, \
64  (#Class "::" #method), \
65  JobMemFun((job), &Class::method, (arg1)))
66 
68 #define JobCallback(dbgSection, dbgLevel, Dialer, job, method) \
69  asyncCall((dbgSection), (dbgLevel), #method, \
70  Dialer(CbcPointer<Dialer::DestClass>(job), &method))
71 
72 /*
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.
78  */
79 
80 /*
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.
84  */
85 
86 // Arity names are from http://en.wikipedia.org/wiki/Arity
87 
88 template <class Job>
89 class NullaryMemFunT: public JobDialer<Job>
90 {
91 public:
92  typedef void (Job::*Method)();
93  explicit NullaryMemFunT(const CbcPointer<Job> &aJob, Method aMethod):
94  JobDialer<Job>(aJob), method(aMethod) {}
95 
96  virtual void print(std::ostream &os) const { os << "()"; }
97 
98 public:
100 
101 protected:
102  virtual void doDial() { ((&(*this->job))->*method)(); }
103 };
104 
105 template <class Job, class Data, class Argument1 = Data>
106 class UnaryMemFunT: public JobDialer<Job>
107 {
108 public:
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) {}
113 
114  virtual void print(std::ostream &os) const { os << '(' << arg1 << ')'; }
115 
116 public:
118  Data arg1;
119 
120 protected:
121  virtual void doDial() { ((&(*this->job))->*method)(arg1); }
122 };
123 
124 // ... add more as needed
125 
126 // Now we add global templated functions that create the member function
127 // wrappers above. These are for convenience: it is often easier to
128 // call a templated function than to create a templated object.
129 
130 template <class C>
133 {
134  return NullaryMemFunT<C>(job, method);
135 }
136 
137 template <class C, class Argument1>
140  Argument1 arg1)
141 {
142  return UnaryMemFunT<C, Argument1>(job, method, arg1);
143 }
144 
145 // inlined methods
146 
147 template<class Job>
148 JobDialer<Job>::JobDialer(const JobPointer &aJob): job(aJob)
149 {
150 }
151 
152 template<class Job>
154 {
155 }
156 
157 template<class Job>
158 bool
160 {
161  if (!job)
162  return call.cancel("job gone");
163 
164  return job->canBeCalled(call);
165 }
166 
167 template<class Job>
168 void
170 {
171  job->callStart(call);
172 
173  try {
174  doDial();
175  } catch (const std::exception &e) {
176  debugs(call.debugSection, 3,
177  HERE << call.name << " threw exception: " << e.what());
178  job->callException(e);
179  }
180 
181  job->callEnd(); // may delete job
182 }
183 
184 #endif /* SQUID_ASYNCJOBCALLS_H */
185 
const char *const name
Definition: AsyncCall.h:76
virtual void print(std::ostream &os) const
NullaryMemFunT< C > JobMemFun(const CbcPointer< C > &job, typename NullaryMemFunT< C >::Method method)
virtual void print(std::ostream &os) const
Definition: AsyncJobCalls.h:96
const int debugSection
Definition: AsyncCall.h:77
bool CallJob(int debugSection, int debugLevel, const char *fileName, int fileLine, const char *callName, const Dialer &dialer)
schedule an async job call using a dialer; use CallJobHere macros instead
Definition: AsyncJobCalls.h:50
void(Job::* Method)()
Definition: AsyncJobCalls.h:92
JobDialer & operator=(const JobDialer &)
bool cancel(const char *reason)
Definition: AsyncCall.cc:52
JobDialer(const JobPointer &aJob)
virtual void doDial()
UnaryMemFunT(const CbcPointer< Job > &aJob, Method aMethod, const Data &anArg1)
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
void dial(AsyncCall &call)
bool ScheduleCall(const char *fileName, int fileLine, AsyncCall::Pointer &call)
Definition: AsyncCall.cc:90
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
NullaryMemFunT(const CbcPointer< Job > &aJob, Method aMethod)
Definition: AsyncJobCalls.h:93
virtual void doDial()=0
virtual bool canDial(AsyncCall &call)
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:147
virtual void doDial()
size_t HttpReply *STUB StoreEntry const KeyScope scope const HttpRequestMethod & method
Definition: stub_store.cc:126
CbcPointer< Job > JobPointer
Definition: AsyncJobCalls.h:29
JobPointer job
Definition: AsyncJobCalls.h:37
void(Job::* Method)(Argument1)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors