]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/Subscription.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / base / Subscription.h
1 /*
2 * Copyright (C) 1996-2019 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_BASE_SUBSCRIPTION_H
10 #define _SQUID_BASE_SUBSCRIPTION_H
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.
45 * A pointer to Call_ must be convertable to AsyncCall::Pointer
46 */
47 template<class Call_>
48 class CallSubscription: public Subscription
49 {
50 public:
51 /// Must be passed an object. nil pointers are not permitted.
52 explicit CallSubscription(const RefCount<Call_> &aCall) : call(aCall) { assert(aCall != NULL); }
53 virtual AsyncCall::Pointer callback() const { return new Call_(*call); }
54
55 private:
56 const RefCount<Call_> call; ///< gets copied to create callback calls
57 };
58
59 #endif /* _SQUID_BASE_SUBSCRIPTION_H */
60