Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
WithUUID.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
17
18#include <atomic>
19#include <limits>
20
21namespace shambase {
22
40 template<typename T, class Tint, bool thread_safe = true>
41 class WithUUID {
42
43 protected:
47 Tint uuid;
48
49 public:
52 static constexpr Tint invalid_uuid = std::numeric_limits<Tint>::max();
53
59 inline Tint get_uuid() const { return uuid; }
60
62 inline bool is_alive() const { return uuid != invalid_uuid; }
63
65 inline void invalidate() { uuid = invalid_uuid; }
66
72 inline WithUUID() {
73 if constexpr (thread_safe) {
74 // local atomic static storage for the UUID
75 static std::atomic<Tint> _uuid = 0;
76 // increment and store the UUID (atomic)
77 uuid = _uuid.fetch_add(1, std::memory_order_relaxed);
78 } else {
79 // we need to redo the static storage in this case otherwise
80 // some lock xadd would be emitted as std::atomic is thread safe.
81 static Tint _uuid = 0;
82 uuid = _uuid++;
83 }
84 }
85
86 WithUUID(const WithUUID &) = delete;
87 WithUUID &operator=(const WithUUID &) = delete;
88
90 inline WithUUID(WithUUID &&other) noexcept : uuid(other.uuid) { other.invalidate(); }
91
93 inline WithUUID &operator=(WithUUID &&other) noexcept {
94 if (this != &other) {
95 uuid = other.uuid;
96 other.invalidate();
97 }
98 return *this;
99 }
100 };
101
102} // namespace shambase
WithUUID & operator=(const WithUUID &)=delete
would duplicate the uuid
WithUUID()
Constructor of the class.
Definition WithUUID.hpp:72
void invalidate()
Marks this instance's uuid as invalid, e.g. to give up identity outside of a move.
Definition WithUUID.hpp:65
WithUUID(WithUUID &&other) noexcept
Move constructor: transfers the uuid to this and invalidates other.
Definition WithUUID.hpp:90
static constexpr Tint invalid_uuid
Definition WithUUID.hpp:52
WithUUID & operator=(WithUUID &&other) noexcept
Move assignment: transfers the uuid to this and invalidates other.
Definition WithUUID.hpp:93
bool is_alive() const
Whether this instance still holds a valid uuid (false once moved from).
Definition WithUUID.hpp:62
WithUUID(const WithUUID &)=delete
would duplicate the uuid
Tint get_uuid() const
Get the uuid of the class.
Definition WithUUID.hpp:59
Tint uuid
The unique identifier of the class.
Definition WithUUID.hpp:47
namespace for basic c++ utilities