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
18#include <atomic>
19
20namespace shambase {
21
36 template<typename T, class Tint, bool thread_safe = true>
37 class WithUUID {
38
39 protected:
44
45 public:
51 inline Tint get_uuid() const { return uuid; }
52
58 inline WithUUID() {
59 if constexpr (thread_safe) {
60 // local atomic static storage for the UUID
61 static std::atomic<Tint> _uuid = 0;
62 // increment and store the UUID (atomic)
63 uuid = _uuid.fetch_add(1, std::memory_order_relaxed);
64 } else {
65 // we need to redo the static storage in this case otherwise
66 // some lock xadd would be emitted as std::atomic is thread safe.
67 static Tint _uuid = 0;
68 uuid = _uuid++;
69 }
70 }
71 };
72
73} // namespace shambase
A class that provides unique identifiers (UUID) to instances.
Definition WithUUID.hpp:37
WithUUID()
Constructor of the class.
Definition WithUUID.hpp:58
Tint get_uuid() const
Get the uuid of the class.
Definition WithUUID.hpp:51
Tint uuid
The unique identifier of the class.
Definition WithUUID.hpp:43
namespace for basic c++ utilities
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.