]>
Commit | Line | Data |
---|---|---|
3cb19943 AJ |
1 | #ifndef _SQUID_BASE_SUBSCRIPTION_H |
2 | #define _SQUID_BASE_SUBSCRIPTION_H | |
3 | ||
4 | #include "base/AsyncCall.h" | |
5 | ||
6 | /** API for creating a series of AsyncCalls. | |
7 | * This is necessary because the same AsyncCall callback must not be | |
8 | * fired multiple times. | |
9 | * | |
10 | * The call producer needs to accept and store a Subscription::Pointer. | |
11 | * It also should provide some mechanism for adding/removing/changing | |
12 | * the stored Subscription::Pointer. | |
13 | * | |
14 | * The callback() method of Subscription::Pointer will spawn AsyncCall | |
15 | * to be filled out and scheduled as needed. | |
16 | */ | |
17 | class Subscription: public RefCountable | |
18 | { | |
19 | public: | |
20 | typedef RefCount<Subscription> Pointer; | |
21 | ||
22 | /** returns a call object to be used for the next call back. | |
23 | * Child implementations must ensure the Call pointer produced | |
24 | * is not NULL. | |
25 | */ | |
26 | virtual AsyncCall::Pointer callback() const = 0; | |
27 | }; | |
28 | ||
29 | /** Implements Subscription API using Call's copy constructor. | |
30 | * | |
31 | * The subscriber creates one of these using a specific callback | |
32 | * type and instance. The subscription object is then passed to a | |
33 | * producer/factory which will use this API to generate calls. | |
34 | * A subscription may be passed to multiple producers. | |
35 | * | |
36 | * Call_ must have a copy constructor. | |
37 | * A pointer to Call_ must be convertable to AsyncCall::Pointer | |
38 | */ | |
39 | template<class Call_> | |
40 | class CallSubscription: public Subscription | |
41 | { | |
42 | public: | |
43 | /// Must be passed an object. nil pointers are not permitted. | |
44 | explicit CallSubscription(const RefCount<Call_> &aCall) : call(aCall) { assert(aCall != NULL); } | |
45 | virtual AsyncCall::Pointer callback() const { return new Call_(call); } | |
46 | ||
47 | private: | |
48 | const RefCount<Call_> call; ///< gets copied to create callback calls | |
49 | }; | |
50 | ||
51 | #endif /* _SQUID_BASE_SUBSCRIPTION_H */ |