Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
UniqueID.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_UniqueID_h_
5#define _Stroika_Foundation_Common_UniqueID_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <atomic>
10#include <compare>
11#include <cstdint>
12
13#include "Stroika/Foundation/Common/Common.h"
14
15/**
16 * \file
17 *
18 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
19 */
20
22 class String;
23}
24
26
27 namespace Private_ {
28 inline atomic<uint64_t> sUniqueIDLast_{0};
29 }
30
31 /**
32 * \brief An opaque ID - each New () one never given before, in this process - of a type of its own for each TAG, so one
33 * kind cannot be passed for another. UniqueID{} names nothing.
34 *
35 * For naming what was added - a callback, a timer, a task - so it can be removed, or asked about, later, without comparing
36 * the things themselves (@see Execution::CallbackRegistry for why). One made up (UniqueID{}, or another's) names nothing
37 * added, so New () need not be guarded.
38 *
39 * \par Example Usage
40 * \code
41 * class Thing {
42 * public:
43 * using ID = Common::UniqueID<Thing>;
44 * ID Add (...); // ID id = ID::New (); ...; return id;
45 * void Remove (ID id);
46 * };
47 * \endcode
48 */
49 template <typename TAG>
50 class UniqueID {
51 public:
52 constexpr UniqueID () = default;
53
54 public:
55 /**
56 * \brief One never given before in this process - by New () of any TAG.
57 */
58 static UniqueID New ();
59
60 public:
61 constexpr bool operator== (const UniqueID& rhs) const = default;
62 constexpr strong_ordering operator<=> (const UniqueID& rhs) const = default;
63
64 public:
65 /**
66 * @see Characters::ToString ()
67 */
68 nonvirtual Characters::String ToString () const;
69
70 private:
71 constexpr explicit UniqueID (uint64_t id);
72
73 private:
74 uint64_t fID_{0}; // 0: none
75 };
76
77}
78
79/*
80 ********************************************************************************
81 ***************************** Implementation Details ***************************
82 ********************************************************************************
83 */
84#include "UniqueID.inl"
85
86#endif /*_Stroika_Foundation_Common_UniqueID_h_*/
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
An opaque ID - each New () one never given before, in this process - of a type of its own for each TA...
Definition UniqueID.h:50
static UniqueID New()
One never given before in this process - by New () of any TAG.
Definition UniqueID.inl:19
nonvirtual Characters::String ToString() const
Definition UniqueID.inl:24