]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/InstanceId.h
942aa80cb6fa8627c353a46829ecd2ac891ddf0d
1 #ifndef SQUID_BASE_INSTANCE_ID_H
2 #define SQUID_BASE_INSTANCE_ID_H
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?
14 template <class Class
>
18 typedef unsigned int Value
; ///< id storage type; \todo: parameterize?
20 InstanceId(): value(++Last
? Last
: ++Last
) {}
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
;}
27 /// prints Prefix followed by ID value; \todo: use HEX for value printing?
28 std::ostream
&print(std::ostream
&os
) const;
31 static const char *Prefix
; ///< Class shorthand string for debugging
32 Value value
; ///< instance identifier
35 InstanceId(const InstanceId
& right
); ///< not implemented; IDs are unique
36 InstanceId
& operator=(const InstanceId
&right
); ///< not implemented
39 static Value Last
; ///< the last used ID value
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; \
53 template <class Class
>
55 std::ostream
&operator <<(std::ostream
&os
, const InstanceId
<Class
> &id
)
61 #endif /* SQUID_BASE_INSTANCE_ID_H */