Taskolib  1.4.4
UniqueId.h
Go to the documentation of this file.
1 
23 // SPDX-License-Identifier: LGPL-2.1-or-later
24 
25 #ifndef TASKOLIB_UNIQUEID_H_
26 #define TASKOLIB_UNIQUEID_H_
27 
28 #include <optional>
29 #include <random>
30 #include <string_view>
31 #include <string>
32 
33 namespace task {
34 
38 class UniqueId
39 {
40 public:
41  using ValueType = std::uint64_t;
42 
44  UniqueId();
45 
47  explicit UniqueId(ValueType id);
48 
56  static std::optional<UniqueId> from_string(std::string_view str);
57 
59  friend bool operator==(UniqueId a, UniqueId b)
60  {
61  return a.id_ == b.id_;
62  }
63 
65  friend bool operator!=(UniqueId a, UniqueId b)
66  {
67  return a.id_ != b.id_;
68  }
69 
71  friend std::string to_string(UniqueId uid);
72 
73 private:
74  static thread_local std::mt19937_64 random_number_generator_;
75 
77 };
78 
79 
80 namespace literals {
81 
83 inline ::task::UniqueId operator""_uid(unsigned long long id)
84 {
85  return ::task::UniqueId{ id };
86 }
87 
88 } // namespace literals
89 
90 } // namespace task
91 
92 #endif
An unsigned 64-bit integer for use as a unique ID.
Definition: UniqueId.h:39
ValueType id_
Definition: UniqueId.h:76
UniqueId()
Default-construct a random unique ID.
Definition: UniqueId.cc:36
static std::optional< UniqueId > from_string(std::string_view str)
Create a unique ID from the given string, returning an empty optional if the string does not represen...
Definition: UniqueId.cc:48
static thread_local std::mt19937_64 random_number_generator_
Definition: UniqueId.h:74
friend bool operator==(UniqueId a, UniqueId b)
Determine if two unique IDs are equal.
Definition: UniqueId.h:59
std::uint64_t ValueType
Definition: UniqueId.h:41
friend std::string to_string(UniqueId uid)
Return a hexadecimal string representation of the given unique ID.
Definition: UniqueId.cc:64
friend bool operator!=(UniqueId a, UniqueId b)
Determine if two unique IDs are different.
Definition: UniqueId.h:65
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33