]>
Commit | Line | Data |
---|---|---|
bbc27441 | 1 | /* |
5b74111a | 2 | * Copyright (C) 1996-2018 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 | ||
8b110b48 AR |
9 | #ifndef SQUID_BASE_INSTANCE_ID_H |
10 | #define SQUID_BASE_INSTANCE_ID_H | |
11 | ||
8b110b48 AR |
12 | #include <iosfwd> |
13 | ||
14 | /** Identifier for class instances | |
15 | * - unique IDs for a large number of concurrent instances, but may wrap; | |
16 | * - useful for debugging and insecure request/response matching; | |
17 | * - sequential IDs within a class except when wrapping; | |
18 | * - always positive IDs. | |
19 | * \todo: add storage type parameter to support configurable Value types? | |
20 | * \todo: add creation/destruction debugging? | |
21 | */ | |
22 | template <class Class> | |
23 | class InstanceId | |
24 | { | |
25 | public: | |
26 | typedef unsigned int Value; ///< id storage type; \todo: parameterize? | |
27 | ||
827df6a4 | 28 | InstanceId() {change();} |
8b110b48 AR |
29 | |
30 | operator Value() const { return value; } | |
31 | bool operator ==(const InstanceId &o) const { return value == o.value; } | |
32 | bool operator !=(const InstanceId &o) const { return !(*this == o); } | |
f2e41480 | 33 | void change(); |
8b110b48 | 34 | |
f2e41480 | 35 | /// prints class-pecific prefix followed by ID value; \todo: use HEX for value printing? |
827df6a4 | 36 | std::ostream &print(std::ostream &) const; |
8b110b48 | 37 | |
f2e41480 | 38 | /// returns the class-pecific prefix |
827df6a4 | 39 | const char * prefix() const; |
f2e41480 | 40 | |
8b110b48 | 41 | public: |
827df6a4 | 42 | Value value = 0; ///< instance identifier |
8b110b48 AR |
43 | |
44 | private: | |
827df6a4 AJ |
45 | InstanceId(const InstanceId &); ///< not implemented; IDs are unique |
46 | InstanceId& operator=(const InstanceId &); ///< not implemented | |
8b110b48 AR |
47 | }; |
48 | ||
49 | /// convenience macro to instantiate Class-specific stuff in .cc files | |
f2e41480 | 50 | #define InstanceIdDefinitions(Class, pfx) \ |
827df6a4 | 51 | template<> const char * \ |
f2e41480 AJ |
52 | InstanceId<Class>::prefix() const { \ |
53 | return pfx; \ | |
54 | } \ | |
8b110b48 AR |
55 | template<> std::ostream & \ |
56 | InstanceId<Class>::print(std::ostream &os) const { \ | |
f2e41480 AJ |
57 | return os << pfx << value; \ |
58 | } \ | |
59 | template<> void \ | |
60 | InstanceId<Class>::change() { \ | |
61 | static InstanceId<Class>::Value Last = 0; \ | |
62 | value = ++Last ? Last : ++Last; \ | |
cd3f0ed7 | 63 | } |
8b110b48 | 64 | |
8b110b48 AR |
65 | /// print the id |
66 | template <class Class> | |
67 | inline | |
ab745b44 A |
68 | std::ostream &operator <<(std::ostream &os, const InstanceId<Class> &id) |
69 | { | |
8b110b48 AR |
70 | return id.print(os); |
71 | } | |
72 | ||
8b110b48 | 73 | #endif /* SQUID_BASE_INSTANCE_ID_H */ |
f53969cc | 74 |