mirror of
https://github.com/boostorg/uuid.git
synced 2026-01-19 04:42:16 +00:00
103 lines
3.2 KiB
Plaintext
103 lines
3.2 KiB
Plaintext
[#time_generator_v7]
|
|
== <boost/uuid/{zwsp}time_generator_v7.hpp>
|
|
|
|
:idprefix: time_generator_v7_
|
|
|
|
=== Synopsis
|
|
|
|
[source,c++]
|
|
[subs=+quotes]
|
|
----
|
|
namespace boost {
|
|
namespace uuids {
|
|
|
|
class time_generator_v7
|
|
{
|
|
private:
|
|
|
|
// exposition only
|
|
_unspecified-csprng-type_ rng_;
|
|
|
|
public:
|
|
|
|
using result_type = uuid;
|
|
|
|
time_generator_v7();
|
|
|
|
time_generator_v7( time_generator_v7 const& rhs );
|
|
time_generator_v7( time_generator_v7&& rhs ) noexcept;
|
|
|
|
time_generator_v7& operator=( time_generator_v7 const& rhs ) noexcept;
|
|
time_generator_v7& operator=( time_generator_v7&& rhs ) noexcept;
|
|
|
|
result_type operator()() noexcept;
|
|
};
|
|
|
|
}} // namespace boost::uuids
|
|
----
|
|
|
|
The class `time_generator_v7` generates time-based version 7 UUIDs, as described in https://datatracker.ietf.org/doc/rfc9562/[RFC 9562] section 5.7.
|
|
|
|
=== Constructors
|
|
|
|
```
|
|
time_generator_v7();
|
|
```
|
|
|
|
Effects: :: Initializes the internal cryptographically strong pseudorandom number generator (CSPRNG) `rng_` using entropy from `std::random_device`.
|
|
|
|
```
|
|
time_generator_v7( time_generator_v7 const& rhs );
|
|
```
|
|
|
|
Effects: :: Copies the state of `rhs` into `*this`, then reseeds `this\->rng_` using entropy from `std::random_device`.
|
|
|
|
Remarks: :: Reseeding ensures that the two generators don't produce the same random number sequence after the copy.
|
|
|
|
```
|
|
time_generator_v7( time_generator_v7&& rhs ) noexcept;
|
|
```
|
|
|
|
Effects: :: Copies the state of `rhs` into `*this`, then perturbs the state of `rhs.rng_` such that it no longer produces the same random number sequence.
|
|
|
|
=== Assignment
|
|
|
|
```
|
|
time_generator_v7& operator=( time_generator_v7 const& rhs ) noexcept;
|
|
```
|
|
|
|
Effects: :: Copies the state of `rhs` into `*this`, except for `this\->rng_`, which is left unmodified.
|
|
|
|
Returns: :: `*this`.
|
|
|
|
Remarks: :: Not modifying `this\->rng_` ensures that the two generators continue to produce different random numbers.
|
|
|
|
```
|
|
time_generator_v7& operator=( time_generator_v7&& rhs ) noexcept;
|
|
```
|
|
|
|
Effects: :: Copies the state of `rhs` into `*this`, then perturbs the state of `rhs.rng_` such that it no longer produces the same random number sequence.
|
|
|
|
Returns: :: `*this`.
|
|
|
|
=== operator()
|
|
|
|
```
|
|
result_type operator()() noexcept;
|
|
```
|
|
|
|
Effects: ::
|
|
+
|
|
. Obtains a new timestamp as if by getting the system time from `std::chrono::system_clock::now()` and converting it to a number of microseconds from the Unix epoch.
|
|
. Creates a new version 7 UUID and initializes its `unix_ts_ms` field with the number of milliseconds in the timestamp.
|
|
. Initializes the `rand_a` field with the residual number of microseconds from the timestamp.
|
|
. Initializes the 6 bits of the `rand_b` field following the variant to a conflict resolution counter, such that if more than one UUID is generated using the same timestamp, monotonicity is still ensured.
|
|
. Initializes the rest of the `rand_b` field to random values obtained from the internal CSPRNG `rng_`.
|
|
. Returns the UUID.
|
|
|
|
Remarks: :: `operator()` generates a monotonically increasing sequence of UUIDs, if the following requirements are met:
|
|
+
|
|
* The system clock never goes backwards;
|
|
* The system clock has at least millisecond resolution;
|
|
* No more than 64 UUIDs are generated per microsecond (no more than one every 16 nanoseconds.)
|