Flow 1.0.1
Flow project: Full implementation reference.
uniq_id_holder.cpp
Go to the documentation of this file.
1/* Flow
2 * Copyright 2023 Akamai Technologies, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the
5 * "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in
12 * writing, software distributed under the License is
13 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14 * CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing
16 * permissions and limitations under the License. */
17
18/// @file
20#include <limits>
21
22namespace flow::util
23{
24
25// Static initializations.
26
27std::atomic<Unique_id_holder::id_t> Unique_id_holder::s_last_id(std::numeric_limits<id_t>::min());
28
29// Implementations.
30
32 /* Get the next available ID in the universe. Use large type for id_t to avoid overflow.
33 * Use atomic<> to ensure thread safety. Use pre-increment so that the smallest possible ID is reserved and
34 * never assigned to an actual m_id (might come in handy for debugging someday, identifying failure of this
35 * feature). */
36 m_id(++s_last_id)
37{
38 // Nothing else.
39}
40
42 Unique_id_holder() // This (unusual) behavior is explained in class doc header. Basically new obj = separate obj.
43{
44 // Nothing else.
45}
46
48{
49 // Intentionally blank. This (unusual) behavior is explained in class doc header.
50 return *this;
51}
52
54{
55 return m_id;
56}
57
59{
60 return Unique_id_holder().unique_id();
61}
62
63} // namespace flow::util
Each object of this class stores (at construction) and returns (on demand) a numeric ID unique from a...
id_t unique_id() const
Raw unique ID identifying this object as well as any object of a derived type.
static id_t create_unique_id()
Short-hand for Unique_id_holder().unique_id(); useful when all you want is the unique integer itself.
Unique_id_holder()
Thread-safely construct an ID whose value is different from any other object of this class,...
const Unique_id_holder & operator=(const Unique_id_holder &) const
This assignment operator is a const no-op.
const id_t m_id
The ID. Note its constness alone forbids a classic assignment operator.
static std::atomic< id_t > s_last_id
m_id value of the Unique_id_holder object last constructed.
uint64_t id_t
Raw integer type to uniquely identify a thing. 64-bit width should make overflow extremely hard to re...
Flow module containing miscellaneous general-use facilities that don't fit into any other Flow module...
Definition: basic_blob.hpp:29