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_SRC_BASE_INSTANCEID_H
10 #define SQUID_SRC_BASE_INSTANCEID_H
11 
12 #include <iosfwd>
13 
18 class ScopedId
19 {
20 public:
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 
35 std::ostream &operator <<(std::ostream &os, const ScopedId &id);
36 
37 typedef unsigned int InstanceIdDefaultValueType;
45 template <class Class, class ValueType = InstanceIdDefaultValueType>
47 {
48 public:
49  typedef ValueType Value;
50 
52  InstanceId(InstanceId &&) = delete; // no copying/moving of any kind
53 
54  operator Value() const { return value; }
55  bool operator ==(const InstanceId &o) const { return value == o.value; }
56  bool operator !=(const InstanceId &o) const { return !(*this == o); }
57  void change();
58 
60  std::ostream &print(std::ostream &) const;
61 
62  // TODO: Refactor into static Scope().
64  const char * prefix() const;
65 
67  ScopedId detach() const { return ScopedId(prefix(), value); }
68 
69 public:
71 };
72 
74 #define InstanceIdDefinitions3(Class, pfx, ValueType, ...) \
75  template<> const char * \
76  InstanceId<Class, ValueType>::prefix() const { \
77  return pfx; \
78  } \
79  template<> std::ostream & \
80  InstanceId<Class, ValueType>::print(std::ostream &os) const { \
81  return os << pfx << value; \
82  } \
83  template<> void \
84  InstanceId<Class, ValueType>::change() { \
85  static auto Last = Value(); \
86  value = ++Last ? Last : ++Last; \
87  }
88 
90 #define InstanceIdDefinitions(...) InstanceIdDefinitions3(__VA_ARGS__, InstanceIdDefaultValueType)
91 
93 template <class Class, class ValueType>
94 inline
95 std::ostream &operator <<(std::ostream &os, const InstanceId<Class, ValueType> &id)
96 {
97  return id.print(os);
98 }
99 
100 #endif /* SQUID_SRC_BASE_INSTANCEID_H */
101 
ScopedId(const char *s)
Definition: InstanceId.h:22
Value value
instance identifier
Definition: InstanceId.h:70
ScopedId detach() const
Definition: InstanceId.h:67
ValueType Value
id storage type
Definition: InstanceId.h:49
const char * scope
Definition: InstanceId.h:28
unsigned int InstanceIdDefaultValueType
Definition: InstanceId.h:37
ScopedId()
Definition: InstanceId.h:21
const char * prefix() const
ScopedId(const char *s, uint64_t v)
Definition: InstanceId.h:24
std::ostream & print(std::ostream &) const
writes a compact text representation of the ID
uint64_t value
Definition: InstanceId.h:32
void change()
std::ostream & operator<<(std::ostream &os, const ScopedId &id)
Definition: InstanceId.cc:15
bool operator==(const InstanceId &o) const
Definition: InstanceId.h:55
bool operator!=(const InstanceId &o) const
Definition: InstanceId.h:56

 

Introduction

Documentation

Support

Miscellaneous