]> git.ipfire.org Git - thirdparty/squid.git/blame - src/base/Subscription.h
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / base / Subscription.h
CommitLineData
bbc27441 1/*
f70aedc4 2 * Copyright (C) 1996-2021 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
3cb19943
AJ
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 */
25class Subscription: public RefCountable
26{
27public:
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 */
47template<class Call_>
48class CallSubscription: public Subscription
49{
50public:
51 /// Must be passed an object. nil pointers are not permitted.
52 explicit CallSubscription(const RefCount<Call_> &aCall) : call(aCall) { assert(aCall != NULL); }
ccfbe8f4
AR
53 virtual AsyncCall::Pointer callback() const
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
61private:
62 const RefCount<Call_> call; ///< gets copied to create callback calls
63};
64
65#endif /* _SQUID_BASE_SUBSCRIPTION_H */
f53969cc 66