]> git.ipfire.org Git - thirdparty/squid.git/blame - src/base/InstanceId.h
Added InstanceId to uniquely identify concurrent messages, transactions, etc.
[thirdparty/squid.git] / src / base / InstanceId.h
CommitLineData
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 */
15template <class Class>
16class InstanceId
17{
18public:
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
30public:
31 static const char *Prefix; ///< Class shorthand string for debugging
32 const Value value; ///< instance identifier
33
34private:
35 InstanceId(const InstanceId& right); ///< not implemented; IDs are unique
36 InstanceId& operator=(const InstanceId &right); ///< not implemented
37
38private:
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
53template <class Class>
54inline
55std::ostream &operator <<(std::ostream &os, const InstanceId<Class> &id) {
56 return id.print(os);
57}
58
59
60#endif /* SQUID_BASE_INSTANCE_ID_H */