]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/InstanceId.h
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / base / InstanceId.h
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #ifndef SQUID_BASE_INSTANCE_ID_H
10 #define SQUID_BASE_INSTANCE_ID_H
11
12 #include <iosfwd>
13
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?
21 */
22 template <class Class>
23 class InstanceId
24 {
25 public:
26 typedef unsigned int Value; ///< id storage type; \todo: parameterize?
27
28 InstanceId(): value(++Last ? Last : ++Last) {}
29
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;}
34
35 /// prints Prefix followed by ID value; \todo: use HEX for value printing?
36 std::ostream &print(std::ostream &os) const;
37
38 public:
39 static const char *Prefix; ///< Class shorthand string for debugging
40 Value value; ///< instance identifier
41
42 private:
43 InstanceId(const InstanceId& right); ///< not implemented; IDs are unique
44 InstanceId& operator=(const InstanceId &right); ///< not implemented
45
46 private:
47 static Value Last; ///< the last used ID value
48 };
49
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; \
57 }
58
59 /// print the id
60 template <class Class>
61 inline
62 std::ostream &operator <<(std::ostream &os, const InstanceId<Class> &id)
63 {
64 return id.print(os);
65 }
66
67 #endif /* SQUID_BASE_INSTANCE_ID_H */