]> git.ipfire.org Git - thirdparty/squid.git/blob - src/helper/ReservationId.h
7e4d27975863ae6e1e5a24d7b45f86d91f9f1d68
[thirdparty/squid.git] / src / helper / ReservationId.h
1 /*
2 * Copyright (C) 1996-2020 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_SRC_HELPER_RESERVATIONID_H
10 #define _SQUID_SRC_HELPER_RESERVATIONID_H
11
12 #include <ostream>
13
14 namespace Helper
15 {
16 /// a (temporary) lock on a (stateful) helper channel
17 class ReservationId
18 {
19 public:
20 static ReservationId Next();
21
22 bool reserved() const { return id > 0; }
23
24 explicit operator bool() const { return reserved(); }
25 bool operator !() const { return !reserved(); }
26 bool operator ==(const Helper::ReservationId &other) const { return id == other.id; }
27 bool operator !=(const Helper::ReservationId &other) const { return !(*this == other); }
28
29 void clear() { id = 0; }
30 uint64_t value() const {return id;}
31
32 /// dumps the reservation info for debugging
33 std::ostream &print(std::ostream &os) const;
34
35 private:
36 uint64_t id = 0; ///< uniquely identifies this reservation
37 };
38
39 }; // namespace Helper
40
41 inline std::ostream &
42 operator <<(std::ostream &os, const Helper::ReservationId &id)
43 {
44 return id.print(os);
45 }
46
47 namespace std {
48 /// default hash functor to support std::unordered_map<HelperReservationId, *>
49 template <>
50 struct hash<Helper::ReservationId>
51 {
52 typedef Helper::ReservationId argument_type;
53 typedef std::size_t result_type;
54 result_type operator()(const argument_type &reservation) const noexcept
55 {
56 std::hash<uint64_t> aHash;
57 return aHash(reservation.value());
58 }
59 };
60 }
61
62 #endif
63