Flow 3.0.0
Flow project: Full implementation reference.
Public Types | Public Member Functions | Public Attributes | Related Functions | List of all members
flow::util::Thread_token Struct Reference

An integer-like, type-safe wrapper for the thread-token concept – see this_thread_unique_token(), which explains the whole situation and returns one of these; we are merely the type-safe type usable for it. More...

#include <uniq_id_holder.hpp>

Collaboration diagram for flow::util::Thread_token:
[legend]

Public Types

using id_t = Unique_id_holder::id_t
 Short-hand for the underlying integer type. More...
 

Public Member Functions

constexpr operator id_t () const
 Returns m_val; so comparisons and the like work as for an integer. More...
 

Public Attributes

id_t m_val = 0
 The underlying integer value; 0 <=> "none." Typically it is better to assign to *this rather than to m_val. More...
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &os, Thread_token val)
 Prints string representation of the given Thread_token to the given ostream: the low 32 bits of the underlying integer in fixed-width hex – 0x followed by exactly N (as of this writing N=8) zero-padded digits – a token being an identity, not a quantity. More...
 
size_t hash_value (Thread_token val)
 Hashes the given Thread_token; making it, e.g., boost.unordered-compatible as a key out of the box. More...
 

Detailed Description

An integer-like, type-safe wrapper for the thread-token concept – see this_thread_unique_token(), which explains the whole situation and returns one of these; we are merely the type-safe type usable for it.

A default-constructed Thread_token is "none": it equals no thread's token, ever – so a data member of this type needs no explicit not-yet-set sentinel value or initializer.

See also
this_thread_unique_token() that returns a unique (across all threads in this process) Thread_token.

It converts to id_t implicitly, so comparisons and the like work as for an integer; while the reverse requires a deliberate Thread_token{...} (type-safety being the whole point). ostream<< prints it in fixed-width hex (see that operator's doc header): a token is an identity, not a quantity. Thread_tokens are usable as associate keys, whether ordered or unordered.

Impl notes

Stylistically there is some inconsistency between Unique_id_holder and Thread_token; for example Unique_id_holder::create_unique_id() is a static member that returns raw values alongside a default-ctor that creates a Unique_id_holder – while Thread_token default-ctor returns a sentinel, and free function this_thread_unique_token() returns a Thread_token. The two are slightly related too. The inconsistency is neither criminal nor ideal; and the reason for the inconsistency is that Unique_id_holder was written so long ago; the author (ygoldfel) has developed their style since then. That said Unique_id_holder is fine too; it probably could be simplified a bit, but it works fine. We are not marking a to-do here, as it would create unnecessary breaking-code situations, but this all was worth writing down for posterity.

Definition at line 183 of file uniq_id_holder.hpp.

Member Typedef Documentation

◆ id_t

Short-hand for the underlying integer type.

Definition at line 188 of file uniq_id_holder.hpp.

Member Function Documentation

◆ operator id_t()

constexpr flow::util::Thread_token::operator id_t ( ) const
constexpr

Returns m_val; so comparisons and the like work as for an integer.

Returns
See above.

Definition at line 212 of file uniq_id_holder.hpp.

Friends And Related Function Documentation

◆ hash_value()

size_t hash_value ( Thread_token  val)
related

Hashes the given Thread_token; making it, e.g., boost.unordered-compatible as a key out of the box.

Parameters
valObject to hash.
Returns
The hash.

Definition at line 107 of file uniq_id_holder.cpp.

◆ operator<<()

std::ostream & operator<< ( std::ostream &  os,
Thread_token  val 
)
related

Prints string representation of the given Thread_token to the given ostream: the low 32 bits of the underlying integer in fixed-width hex – 0x followed by exactly N (as of this writing N=8) zero-padded digits – a token being an identity, not a quantity.

Why the low bits only? Tokens come from a monotonic counter, so the high bits are zeros until the 2^32nd ID is issued process-wide – i.e., in practice always – and would print as pure noise; like an abbreviated git hash, the short form identifies uniquely in all but shocking situations. (The stored value keeps all 64 bits regardless; print Thread_token::id_t(val) for the full value.)

Parameters
osStream to which to write.
valObject to serialize.
Returns
os.

Definition at line 90 of file uniq_id_holder.cpp.

Member Data Documentation

◆ m_val

id_t flow::util::Thread_token::m_val = 0

The underlying integer value; 0 <=> "none." Typically it is better to assign to *this rather than to m_val.

Definition at line 193 of file uniq_id_holder.hpp.


The documentation for this struct was generated from the following files: