InstanceId.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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
19{
20public:
21 ScopedId(): scope(nullptr), value(0) {}
22 explicit ScopedId(const char *s): scope(s), value(0) {}
23 // when the values is zero/unknown, use other constructors
24 ScopedId(const char *s, uint64_t v): scope(s), value(v) { /* assert(value) */ }
25
28 const char *scope;
29
32 uint64_t value;
33};
34
35std::ostream &operator <<(std::ostream &os, const ScopedId &id);
36
37typedef unsigned int InstanceIdDefaultValueType;
45template <class Class, class ValueType = InstanceIdDefaultValueType>
47{
48public:
49 typedef ValueType Value;
50
52
53 operator Value() const { return value; }
54 bool operator ==(const InstanceId &o) const { return value == o.value; }
55 bool operator !=(const InstanceId &o) const { return !(*this == o); }
56 void change();
57
59 std::ostream &print(std::ostream &) const;
60
61 // TODO: Refactor into static Scope().
63 const char * prefix() const;
64
66 ScopedId detach() const { return ScopedId(prefix(), value); }
67
68public:
70
71private:
74};
75
77#define InstanceIdDefinitions3(Class, pfx, ValueType, ...) \
78 template<> const char * \
79 InstanceId<Class, ValueType>::prefix() const { \
80 return pfx; \
81 } \
82 template<> std::ostream & \
83 InstanceId<Class, ValueType>::print(std::ostream &os) const { \
84 return os << pfx << value; \
85 } \
86 template<> void \
87 InstanceId<Class, ValueType>::change() { \
88 static auto Last = Value(); \
89 value = ++Last ? Last : ++Last; \
90 }
91
93#define InstanceIdDefinitions(...) InstanceIdDefinitions3(__VA_ARGS__, InstanceIdDefaultValueType)
94
96template <class Class, class ValueType>
97inline
98std::ostream &operator <<(std::ostream &os, const InstanceId<Class, ValueType> &id)
99{
100 return id.print(os);
101}
102
103#endif /* SQUID_BASE_INSTANCE_ID_H */
104
std::ostream & operator<<(std::ostream &os, const ScopedId &id)
Definition: InstanceId.cc:15
unsigned int InstanceIdDefaultValueType
Definition: InstanceId.h:37
const char * prefix() const
bool operator==(const InstanceId &o) const
Definition: InstanceId.h:54
InstanceId & operator=(const InstanceId &)
not implemented
ValueType Value
id storage type
Definition: InstanceId.h:49
void change()
bool operator!=(const InstanceId &o) const
Definition: InstanceId.h:55
InstanceId(const InstanceId &)
not implemented; IDs are unique
ScopedId detach() const
Definition: InstanceId.h:66
std::ostream & print(std::ostream &) const
writes a compact text representation of the ID
Value value
instance identifier
Definition: InstanceId.h:69
uint64_t value
Definition: InstanceId.h:32
ScopedId()
Definition: InstanceId.h:21
ScopedId(const char *s)
Definition: InstanceId.h:22
ScopedId(const char *s, uint64_t v)
Definition: InstanceId.h:24
const char * scope
Definition: InstanceId.h:28

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors