Taskolib  1.3.3
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 <random>
29 #include <string>
30 
31 #include <gul14/optional.h>
32 #include <gul14/string_view.h>
33 
34 namespace task {
35 
39 class UniqueId
40 {
41 public:
42  using ValueType = std::uint64_t;
43 
45  UniqueId();
46 
48  explicit UniqueId(ValueType id);
49 
57  static gul14::optional<UniqueId> from_string(gul14::string_view str);
58 
60  friend bool operator==(UniqueId a, UniqueId b)
61  {
62  return a.id_ == b.id_;
63  }
64 
66  friend bool operator!=(UniqueId a, UniqueId b)
67  {
68  return a.id_ != b.id_;
69  }
70 
72  friend std::string to_string(UniqueId uid);
73 
74 private:
75  static thread_local std::mt19937_64 random_number_generator_;
76 
78 };
79 
80 
81 namespace literals {
82 
84 inline ::task::UniqueId operator""_uid(unsigned long long id)
85 {
86  return ::task::UniqueId{ id };
87 }
88 
89 } // namespace literals
90 
91 } // namespace task
92 
93 #endif
An unsigned 64-bit integer for use as a unique ID.
Definition: UniqueId.h:40
ValueType id_
Definition: UniqueId.h:77
static gul14::optional< UniqueId > from_string(gul14::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
UniqueId()
Default-construct a random unique ID.
Definition: UniqueId.cc:36
static thread_local std::mt19937_64 random_number_generator_
Definition: UniqueId.h:75
friend bool operator==(UniqueId a, UniqueId b)
Determine if two unique IDs are equal.
Definition: UniqueId.h:60
std::uint64_t ValueType
Definition: UniqueId.h:42
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:66
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33