]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
b8ae064d | 2 | * Copyright (C) 1996-2023 The Squid Software Foundation and contributors |
bbc27441 AJ |
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 | ||
ff9d9458 FC |
9 | #ifndef SQUID_SRC_BASE_SUBSCRIPTION_H |
10 | #define SQUID_SRC_BASE_SUBSCRIPTION_H | |
3cb19943 AJ |
11 | |
12 | #include "base/AsyncCall.h" | |
13 | ||
14 | /** API for creating a series of AsyncCalls. | |
15 | * This is necessary because the same AsyncCall callback must not be | |
16 | * fired multiple times. | |
17 | * | |
18 | * The call producer needs to accept and store a Subscription::Pointer. | |
19 | * It also should provide some mechanism for adding/removing/changing | |
20 | * the stored Subscription::Pointer. | |
21 | * | |
22 | * The callback() method of Subscription::Pointer will spawn AsyncCall | |
23 | * to be filled out and scheduled as needed. | |
24 | */ | |
25 | class Subscription: public RefCountable | |
26 | { | |
27 | public: | |
28 | typedef RefCount<Subscription> Pointer; | |
29 | ||
30 | /** returns a call object to be used for the next call back. | |
31 | * Child implementations must ensure the Call pointer produced | |
32 | * is not NULL. | |
33 | */ | |
34 | virtual AsyncCall::Pointer callback() const = 0; | |
35 | }; | |
36 | ||
37 | /** Implements Subscription API using Call's copy constructor. | |
38 | * | |
39 | * The subscriber creates one of these using a specific callback | |
40 | * type and instance. The subscription object is then passed to a | |
41 | * producer/factory which will use this API to generate calls. | |
42 | * A subscription may be passed to multiple producers. | |
43 | * | |
44 | * Call_ must have a copy constructor. | |
2f8abb64 | 45 | * A pointer to Call_ must be convertible to AsyncCall::Pointer |
3cb19943 AJ |
46 | */ |
47 | template<class Call_> | |
48 | class CallSubscription: public Subscription | |
49 | { | |
50 | public: | |
51 | /// Must be passed an object. nil pointers are not permitted. | |
aee3523a | 52 | explicit CallSubscription(const RefCount<Call_> &aCall) : call(aCall) { assert(aCall != nullptr); } |
337b9aa4 | 53 | AsyncCall::Pointer callback() const override |
ccfbe8f4 AR |
54 | { |
55 | const AsyncCall::Pointer cb = new Call_(*call); | |
56 | if (!cb->codeContext || CodeContext::Current()) | |
57 | cb->codeContext = CodeContext::Current(); | |
58 | return cb; | |
59 | } | |
3cb19943 AJ |
60 | |
61 | private: | |
62 | const RefCount<Call_> call; ///< gets copied to create callback calls | |
63 | }; | |
64 | ||
ff9d9458 | 65 | #endif /* SQUID_SRC_BASE_SUBSCRIPTION_H */ |
f53969cc | 66 |