RAVENNAKIT
Loading...
Searching...
No Matches
id.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3 * Project: RAVENNAKIT (RAVENNA / AES67 / ST2110-30 SDK)
4 * Copyright (c) 2024-2025 Sound on Digital
5 *
6 * This file is part of RAVENNAKIT.
7 *
8 * RAVENNAKIT is dual-licensed:
9 * 1) Under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version (the "AGPL License"); and
12 * 2) Under a commercial license from Sound on Digital, for customers who
13 * cannot (or do not wish to) comply with the AGPL License terms.
14 *
15 * If you obtained this file under the AGPL License, you may redistribute it
16 * and/or modify it under the terms of the AGPL License. See the LICENSE
17 * file in the project root for details.
18 *
19 * For commercial licensing, support, and other inquiries, please visit:
20 *
21 * https://ravennakit.com
22 *
23 */
24
25#pragma once
26
28
29#include <cstdint>
30#include <atomic>
31
32namespace rav {
33
38class Id {
39 public:
40 class Generator {
41 public:
42 Generator() = default;
43
44 Generator(const Generator&) = delete;
45 Generator& operator=(const Generator&) = delete;
46
47 Generator(Generator&&) = delete;
49
54 [[nodiscard]] Id next() {
55 RAV_ASSERT(next_id_ != 0, "Next ID is 0, which is reserved for invalid IDs");
56 RAV_ASSERT(next_id_ != std::numeric_limits<uint64_t>::max(), "The next ID is at the maximum value");
57 return Id(next_id_.fetch_add(1));
58 }
59
65 void reset() {
66 next_id_ = 1;
67 }
68
69 private:
70 std::atomic<uint64_t> next_id_ {1};
71 };
72
73 Id() = default;
74
79 explicit Id(const uint64_t int_id) : id_(int_id) {}
80
81 Id(const Id& other) = default;
82 Id& operator=(const Id& other) = default;
83
84 Id(Id&& other) noexcept = default;
85 Id& operator=(Id&& other) noexcept = default;
86
90 [[nodiscard]] bool is_valid() const noexcept {
91 return id_ != 0;
92 }
93
97 [[nodiscard]] uint64_t value() const {
98 return id_;
99 }
100
104 [[nodiscard]] std::string to_string() const {
105 return std::to_string(id_);
106 }
107
108 bool operator==(const uint64_t value) const {
109 return id_ == value;
110 }
111
112 friend bool operator==(const Id& lhs, const Id& rhs) {
113 return lhs.id_ == rhs.id_;
114 }
115
116 friend bool operator!=(const Id& lhs, const Id& rhs) {
117 return !(lhs == rhs);
118 }
119
126 static Generator gen;
127 return gen.next();
128 }
129
130 private:
131 uint64_t id_ {};
132};
133
134static_assert(std::atomic<Id>::is_always_lock_free);
135static_assert(std::is_trivially_copyable_v<Id>);
136
137} // namespace rav
#define RAV_ASSERT(condition, message)
Asserts condition to be true, otherwise:
Definition assert.hpp:101
Generator & operator=(const Generator &)=delete
Generator & operator=(Generator &&)=delete
Generator(Generator &&)=delete
Id next()
This function is thread safe and can be safely called from any thread at any time.
Definition id.hpp:54
Generator(const Generator &)=delete
void reset()
Resets the ID generator to start at 1 again.
Definition id.hpp:65
Id(Id &&other) noexcept=default
bool operator==(const uint64_t value) const
Definition id.hpp:108
bool is_valid() const noexcept
Definition id.hpp:90
friend bool operator!=(const Id &lhs, const Id &rhs)
Definition id.hpp:116
Id & operator=(const Id &other)=default
Id(const Id &other)=default
Id & operator=(Id &&other) noexcept=default
static Id get_next_process_wide_unique_id() noexcept
Returns the next id from a process-wide, global generator.
Definition id.hpp:125
Id(const uint64_t int_id)
Constructs an id from an integer value.
Definition id.hpp:79
std::string to_string() const
Definition id.hpp:104
Id()=default
friend bool operator==(const Id &lhs, const Id &rhs)
Definition id.hpp:112
uint64_t value() const
Definition id.hpp:97