]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/InstanceId.h
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_BASE_INSTANCE_ID_H
10 #define SQUID_BASE_INSTANCE_ID_H
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?
22 template <class Class
>
26 typedef unsigned int Value
; ///< id storage type; \todo: parameterize?
28 InstanceId(): value(++Last
? Last
: ++Last
) {}
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
); }
33 void change() {value
= ++Last
? Last
: ++Last
;}
35 /// prints Prefix followed by ID value; \todo: use HEX for value printing?
36 std::ostream
&print(std::ostream
&os
) const;
39 static const char *Prefix
; ///< Class shorthand string for debugging
40 Value value
; ///< instance identifier
43 InstanceId(const InstanceId
& right
); ///< not implemented; IDs are unique
44 InstanceId
& operator=(const InstanceId
&right
); ///< not implemented
47 static Value Last
; ///< the last used ID value
50 /// convenience macro to instantiate Class-specific stuff in .cc files
51 #define InstanceIdDefinitions(Class, prefix) \
52 template<> const char *InstanceId<Class>::Prefix = prefix; \
53 template<> InstanceId<Class>::Value InstanceId<Class>::Last = 0; \
54 template<> std::ostream & \
55 InstanceId<Class>::print(std::ostream &os) const { \
56 return os << Prefix << value; \
60 template <class Class
>
62 std::ostream
&operator <<(std::ostream
&os
, const InstanceId
<Class
> &id
)
67 #endif /* SQUID_BASE_INSTANCE_ID_H */