]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/InstanceId.h
942aa80cb6fa8627c353a46829ecd2ac891ddf0d
[thirdparty/squid.git] / src / base / InstanceId.h
1 #ifndef SQUID_BASE_INSTANCE_ID_H
2 #define SQUID_BASE_INSTANCE_ID_H
3
4 #include <iosfwd>
5
6 /** Identifier for class instances
7 * - unique IDs for a large number of concurrent instances, but may wrap;
8 * - useful for debugging and insecure request/response matching;
9 * - sequential IDs within a class except when wrapping;
10 * - always positive IDs.
11 * \todo: add storage type parameter to support configurable Value types?
12 * \todo: add creation/destruction debugging?
13 */
14 template <class Class>
15 class InstanceId
16 {
17 public:
18 typedef unsigned int Value; ///< id storage type; \todo: parameterize?
19
20 InstanceId(): value(++Last ? Last : ++Last) {}
21
22 operator Value() const { return value; }
23 bool operator ==(const InstanceId &o) const { return value == o.value; }
24 bool operator !=(const InstanceId &o) const { return !(*this == o); }
25 void change() {value = ++Last ? Last : ++Last;}
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 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<> const char *InstanceId<Class>::Prefix = prefix; \
45 template<> InstanceId<Class>::Value InstanceId<Class>::Last = 0; \
46 template<> std::ostream & \
47 InstanceId<Class>::print(std::ostream &os) const { \
48 return os << Prefix << value; \
49 }
50
51
52 /// print the id
53 template <class Class>
54 inline
55 std::ostream &operator <<(std::ostream &os, const InstanceId<Class> &id)
56 {
57 return id.print(os);
58 }
59
60
61 #endif /* SQUID_BASE_INSTANCE_ID_H */