]>
Commit | Line | Data |
---|---|---|
8b110b48 AR |
1 | #ifndef SQUID_BASE_INSTANCE_ID_H |
2 | #define SQUID_BASE_INSTANCE_ID_H | |
3 | ||
4 | #include "config.h" | |
5 | #include <iosfwd> | |
6 | ||
7 | /** Identifier for class instances | |
8 | * - unique IDs for a large number of concurrent instances, but may wrap; | |
9 | * - useful for debugging and insecure request/response matching; | |
10 | * - sequential IDs within a class except when wrapping; | |
11 | * - always positive IDs. | |
12 | * \todo: add storage type parameter to support configurable Value types? | |
13 | * \todo: add creation/destruction debugging? | |
14 | */ | |
15 | template <class Class> | |
16 | class InstanceId | |
17 | { | |
18 | public: | |
19 | typedef unsigned int Value; ///< id storage type; \todo: parameterize? | |
20 | ||
21 | InstanceId(): value(++Last ? Last : ++Last) {} | |
22 | ||
23 | operator Value() const { return value; } | |
24 | bool operator ==(const InstanceId &o) const { return value == o.value; } | |
25 | bool operator !=(const InstanceId &o) const { return !(*this == o); } | |
26 | ||
27 | /// prints Prefix followed by ID value; \todo: use HEX for value printing? | |
28 | std::ostream &print(std::ostream &os) const; | |
29 | ||
30 | public: | |
31 | static const char *Prefix; ///< Class shorthand string for debugging | |
32 | const Value value; ///< instance identifier | |
33 | ||
34 | private: | |
35 | InstanceId(const InstanceId& right); ///< not implemented; IDs are unique | |
36 | InstanceId& operator=(const InstanceId &right); ///< not implemented | |
37 | ||
38 | private: | |
39 | static Value Last; ///< the last used ID value | |
40 | }; | |
41 | ||
42 | /// convenience macro to instantiate Class-specific stuff in .cc files | |
43 | #define InstanceIdDefinitions(Class, prefix) \ | |
44 | template<> std::ostream & \ | |
45 | InstanceId<Class>::print(std::ostream &os) const { \ | |
46 | return os << Prefix << value; \ | |
47 | } \ | |
48 | template<> const char *InstanceId<Class>::Prefix = prefix; \ | |
49 | template<> InstanceId<Class>::Value InstanceId<Class>::Last = 0 | |
50 | ||
51 | ||
52 | /// print the id | |
53 | template <class Class> | |
54 | inline | |
55 | std::ostream &operator <<(std::ostream &os, const InstanceId<Class> &id) { | |
56 | return id.print(os); | |
57 | } | |
58 | ||
59 | ||
60 | #endif /* SQUID_BASE_INSTANCE_ID_H */ |