forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneat-object-id.hh
40 lines (32 loc) · 911 Bytes
/
neat-object-id.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include <atomic>
namespace utils {
/*
* The neat_id class is purely a debugging thing -- when reading
* the logs with object IDs in it it's more handy to look at those
* consisting * of 1-3 digits, rather than 16 hex-digits of a printed
* pointer.
*
* Embed with [[no_unique_address]] tag for memory efficiency
*/
template <bool Debug>
struct neat_id {
unsigned int operator()() const noexcept { return reinterpret_cast<uintptr_t>(this); }
};
template <>
struct neat_id<true> {
unsigned int _id;
static unsigned int _next() noexcept {
static std::atomic<unsigned int> rover {1};
return rover.fetch_add(1);
}
neat_id() noexcept : _id(_next()) {}
unsigned int operator()() const noexcept { return _id; }
};
} // namespace