]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Added InstanceId to uniquely identify concurrent messages, transactions, etc.
authorAlex Rousskov <rousskov@measurement-factory.com>
Mon, 4 Oct 2010 14:49:57 +0000 (08:49 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Mon, 4 Oct 2010 14:49:57 +0000 (08:49 -0600)
src/base/InstanceId.h [new file with mode: 0644]
src/base/Makefile.am

diff --git a/src/base/InstanceId.h b/src/base/InstanceId.h
new file mode 100644 (file)
index 0000000..29a68f4
--- /dev/null
@@ -0,0 +1,60 @@
+#ifndef SQUID_BASE_INSTANCE_ID_H
+#define SQUID_BASE_INSTANCE_ID_H
+
+#include "config.h"
+#include <iosfwd>
+
+/** Identifier for class instances
+ *   - unique IDs for a large number of concurrent instances, but may wrap;
+ *   - useful for debugging and insecure request/response matching;
+ *   - sequential IDs within a class except when wrapping;
+ *   - always positive IDs.
+ *  \todo: add storage type parameter to support configurable Value types?
+ *  \todo: add creation/destruction debugging?
+ */
+template <class Class>
+class InstanceId
+{
+public:
+    typedef unsigned int Value; ///< id storage type; \todo: parameterize?
+
+    InstanceId(): value(++Last ? Last : ++Last) {}
+
+    operator Value() const { return value; }
+    bool operator ==(const InstanceId &o) const { return value == o.value; }
+    bool operator !=(const InstanceId &o) const { return !(*this == o); }
+
+    /// prints Prefix followed by ID value; \todo: use HEX for value printing?
+    std::ostream &print(std::ostream &os) const;
+
+public:
+    static const char *Prefix; ///< Class shorthand string for debugging
+    const Value value; ///< instance identifier
+
+private:
+    InstanceId(const InstanceId& right); ///< not implemented; IDs are unique
+    InstanceId& operator=(const InstanceId &right); ///< not implemented
+
+private:
+    static Value Last; ///< the last used ID value
+};
+
+/// convenience macro to instantiate Class-specific stuff in .cc files
+#define InstanceIdDefinitions(Class, prefix) \
+    template<> std::ostream & \
+    InstanceId<Class>::print(std::ostream &os) const { \
+        return os << Prefix << value; \
+    } \
+    template<> const char *InstanceId<Class>::Prefix = prefix; \
+    template<> InstanceId<Class>::Value InstanceId<Class>::Last = 0
+
+
+/// print the id
+template <class Class>
+inline
+std::ostream &operator <<(std::ostream &os, const InstanceId<Class> &id) {
+    return id.print(os);
+}
+
+
+#endif /* SQUID_BASE_INSTANCE_ID_H */
index 7009fb37bee9cefc37e2fc410d5949dd5de94967..72c7f830897bf671e809a89cf8975b203d55360f 100644 (file)
@@ -13,5 +13,6 @@ libbase_la_SOURCES = \
        AsyncCallQueue.cc \
        AsyncCallQueue.h \
        CbcPointer.h \
+       InstanceId.h \
        TextException.cc \
        TextException.h