]>
Commit | Line | Data |
---|---|---|
5faec1a1 | 1 | /* |
7c6a034c | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
5faec1a1 EB |
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_ASYNCFUNCALLS_H | |
10 | #define SQUID_BASE_ASYNCFUNCALLS_H | |
11 | ||
12 | #include "base/AsyncCall.h" | |
13 | ||
14 | #include <iostream> | |
15 | ||
16 | /// Calls a function without arguments. See also: NullaryMemFunT. | |
17 | class NullaryFunDialer: public CallDialer | |
18 | { | |
19 | public: | |
20 | using Handler = void (); | |
21 | ||
22 | explicit NullaryFunDialer(Handler * const aHandler): handler(aHandler) {} | |
23 | ||
24 | /* CallDialer API */ | |
25 | bool canDial(AsyncCall &) { return bool(handler); } | |
26 | void dial(AsyncCall &) { handler(); } | |
337b9aa4 | 27 | void print(std::ostream &os) const override { os << "()"; } |
5faec1a1 EB |
28 | |
29 | private: | |
30 | Handler *handler; ///< the function to call (or nil) | |
31 | }; | |
32 | ||
2949dd02 EB |
33 | /// CallDialer for single-parameter stand-alone functions |
34 | template <typename Argument1> | |
35 | class UnaryFunDialer: public CallDialer | |
36 | { | |
37 | public: | |
38 | /// a stand-alone function that receives the parameter given to us | |
39 | using Handler = void (Argument1); | |
40 | ||
41 | UnaryFunDialer(Handler * const aHandler, Argument1 anArg1): | |
42 | handler(aHandler), | |
43 | arg1(anArg1) | |
44 | {} | |
337b9aa4 | 45 | ~UnaryFunDialer() override = default; |
2949dd02 EB |
46 | |
47 | /* CallDialer API */ | |
48 | bool canDial(AsyncCall &) { return bool(handler); } | |
49 | void dial(AsyncCall &) { handler(std::move(arg1)); } | |
337b9aa4 | 50 | void print(std::ostream &os) const final { os << '(' << arg1 << ')'; } |
2949dd02 EB |
51 | |
52 | private: | |
53 | Handler *handler; ///< the function to call | |
54 | Argument1 arg1; ///< actual call parameter | |
55 | }; | |
56 | ||
57 | /// helper function to simplify UnaryFunDialer creation | |
58 | template <typename Argument1> | |
59 | UnaryFunDialer<Argument1> | |
60 | callDialer(void (*handler)(Argument1), Argument1 arg1) | |
61 | { | |
62 | return UnaryFunDialer<Argument1>(handler, arg1); | |
63 | } | |
64 | ||
5faec1a1 EB |
65 | #endif /* SQUID_BASE_ASYNCFUNCALLS_H */ |
66 |