D++ (DPP)  10.0.30
C++ Discord API Bot Library
dpp::snowflake Class Referencefinal

A container for a 64 bit unsigned value representing many things on discord. More...

#include "snowflake.h"

Collaboration diagram for dpp::snowflake:

Public Member Functions

constexpr snowflake () noexcept=default
 Construct a snowflake object. More...
 
constexpr snowflake (const snowflake &rhs) noexcept=default
 Copy a snowflake object. More...
 
template<typename T , typename = std::enable_if_t<std::is_same_v<T, std::string>>>
 snowflake (const T &string_value) noexcept
 Construct a snowflake object from an unsigned integer in a string. More...
 
constexpr snowflake (snowflake &&rhs) noexcept=default
 Move a snowflake object. More...
 
 snowflake (std::string_view string_value) noexcept
 Construct a snowflake object from an unsigned integer in a string. More...
 
template<typename T , typename = std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
constexpr snowflake (T snowflake_val) noexcept(std::is_unsigned_v< T >)
 Construct a snowflake from an integer value. More...
 
constexpr bool empty () const noexcept
 Returns true if the snowflake holds an empty value (is 0) More...
 
constexpr double get_creation_time () const noexcept
 Get the creation time of this snowflake according to Discord. More...
 
constexpr uint16_t get_increment () const noexcept
 Get the increment, which is incremented for every snowflake created over the one millisecond resolution in the timestamp. More...
 
constexpr uint8_t get_process_id () const noexcept
 Get the process id that produced this snowflake value. More...
 
constexpr uint8_t get_worker_id () const noexcept
 Get the worker id that produced this snowflake value. More...
 
 operator json () const
 For building json. More...
 
constexpr operator uint64_t () const noexcept
 For acting like an integer. More...
 
constexpr operator uint64_t & () noexcept
 For acting like an integer. More...
 
constexpr dpp::snowflakeoperator= (const dpp::snowflake &rhs) noexcept=default
 Copy value from another snowflake. More...
 
constexpr dpp::snowflakeoperator= (dpp::snowflake &&rhs) noexcept=default
 Move value from another snowflake. More...
 
template<typename T , typename = std::enable_if_t<std::is_convertible_v<T, std::string_view>>>
constexpr dpp::snowflakeoperator= (T &&snowflake_val) noexcept
 Assign value converted from a string to the snowflake. More...
 
template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr dpp::snowflakeoperator= (T snowflake_val) noexcept(std::is_unsigned_v< T >)
 Assign integer value to the snowflake. More...
 
constexpr bool operator== (dpp::snowflake snowflake_val) const noexcept
 Comparison operator with another snowflake. More...
 
bool operator== (std::string_view snowflake_val) const noexcept
 Comparison operator with a string. More...
 
template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr bool operator== (T snowflake_val) const noexcept
 Comparison operator with an integer. More...
 
std::string str () const
 Returns the stringified version of the snowflake value. More...
 

Protected Attributes

uint64_t value = 0
 The snowflake value. More...
 

Friends

constexpr friend uint64_t format_as (snowflake s) noexcept
 Helper function for libfmt so that a snowflake can be directly formatted as a uint64_t. More...
 
struct std::hash< dpp::snowflake >
 

Detailed Description

A container for a 64 bit unsigned value representing many things on discord.

This value is known in distributed computing as a snowflake value.

Snowflakes are:

  • Performant (very fast to generate at source and to compare in code)
  • Uncoordinated (allowing high availability across clusters, data centres etc)
  • Time ordered (newer snowflakes have higher IDs)
  • Directly Sortable (due to time ordering)
  • Compact (64 bit numbers, not 128 bit, or string)

An identical format of snowflake is used by Twitter, Instagram and several other platforms.

See also
https://en.wikipedia.org/wiki/Snowflake_ID
https://github.com/twitter-archive/snowflake/tree/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231

Constructor & Destructor Documentation

◆ snowflake() [1/6]

constexpr dpp::snowflake::snowflake ( )
constexprdefaultnoexcept

Construct a snowflake object.

◆ snowflake() [2/6]

constexpr dpp::snowflake::snowflake ( const snowflake rhs)
constexprdefaultnoexcept

Copy a snowflake object.

◆ snowflake() [3/6]

constexpr dpp::snowflake::snowflake ( snowflake &&  rhs)
constexprdefaultnoexcept

Move a snowflake object.

◆ snowflake() [4/6]

template<typename T , typename = std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
constexpr dpp::snowflake::snowflake ( snowflake_val)
inlineconstexprnoexcept

Construct a snowflake from an integer value.

Exceptions
dpp::logic_exceptionon assigning a negative value. The function is noexcept if the type given is unsigned
Parameters
snowflake_valsnowflake value as an integer type

we cast to the unsigned version of the type given - this maintains "possible loss of data" warnings for sizeof(T) > sizeof(value) while suppressing them for signed to unsigned conversion (for example snowflake(42) will call snowflake(int) which is a signed type)

◆ snowflake() [5/6]

dpp::snowflake::snowflake ( std::string_view  string_value)
noexcept

Construct a snowflake object from an unsigned integer in a string.

On invalid string the value will be 0

Parameters
string_valueA snowflake value

◆ snowflake() [6/6]

template<typename T , typename = std::enable_if_t<std::is_same_v<T, std::string>>>
dpp::snowflake::snowflake ( const T &  string_value)
inlinenoexcept

Construct a snowflake object from an unsigned integer in a string.

On invalid string the value will be 0

Parameters
string_valueA snowflake value

Member Function Documentation

◆ empty()

constexpr bool dpp::snowflake::empty ( ) const
inlineconstexprnoexcept

Returns true if the snowflake holds an empty value (is 0)

Returns
true if empty (zero)

Referenced by dpp::utility::channel_url(), dpp::utility::message_url(), dpp::utility::user_url(), and dpp::commandhandler::~commandhandler().

Here is the caller graph for this function:

◆ get_creation_time()

constexpr double dpp::snowflake::get_creation_time ( ) const
inlineconstexprnoexcept

Get the creation time of this snowflake according to Discord.

Returns
double creation time inferred from the snowflake ID. The minimum possible value is the first second of 2015.

◆ get_increment()

constexpr uint16_t dpp::snowflake::get_increment ( ) const
inlineconstexprnoexcept

Get the increment, which is incremented for every snowflake created over the one millisecond resolution in the timestamp.

Returns
uint64_t millisecond increment

◆ get_process_id()

constexpr uint8_t dpp::snowflake::get_process_id ( ) const
inlineconstexprnoexcept

Get the process id that produced this snowflake value.

Returns
uint8_t process id

◆ get_worker_id()

constexpr uint8_t dpp::snowflake::get_worker_id ( ) const
inlineconstexprnoexcept

Get the worker id that produced this snowflake value.

Returns
uint8_t worker id

◆ operator json()

dpp::snowflake::operator json ( ) const

For building json.

Returns
The snowflake value as a string

◆ operator uint64_t()

constexpr dpp::snowflake::operator uint64_t ( ) const
inlineconstexprnoexcept

For acting like an integer.

Returns
The snowflake value

◆ operator uint64_t &()

constexpr dpp::snowflake::operator uint64_t & ( )
inlineconstexprnoexcept

For acting like an integer.

Returns
A reference to the snowflake value

◆ operator=() [1/4]

constexpr dpp::snowflake& dpp::snowflake::operator= ( const dpp::snowflake rhs)
constexprdefaultnoexcept

Copy value from another snowflake.

Parameters
rhsThe snowflake to copy from

◆ operator=() [2/4]

constexpr dpp::snowflake& dpp::snowflake::operator= ( dpp::snowflake &&  rhs)
constexprdefaultnoexcept

Move value from another snowflake.

Parameters
rhsThe snowflake to move from

◆ operator=() [3/4]

template<typename T , typename = std::enable_if_t<std::is_convertible_v<T, std::string_view>>>
constexpr dpp::snowflake& dpp::snowflake::operator= ( T &&  snowflake_val)
inlineconstexprnoexcept

Assign value converted from a string to the snowflake.

On invalid string the value will be 0

Parameters
snowflake_valsnowflake value as a string

◆ operator=() [4/4]

template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr dpp::snowflake& dpp::snowflake::operator= ( snowflake_val)
inlineconstexprnoexcept

Assign integer value to the snowflake.

Exceptions
dpp::logic_exceptionon assigning a negative value. The function is noexcept if the type given is unsigned
Parameters
snowflake_valsnowflake value as an integer type

◆ operator==() [1/3]

constexpr bool dpp::snowflake::operator== ( dpp::snowflake  snowflake_val) const
inlineconstexprnoexcept

Comparison operator with another snowflake.

Parameters
snowflake_valsnowflake

◆ operator==() [2/3]

bool dpp::snowflake::operator== ( std::string_view  snowflake_val) const
noexcept

Comparison operator with a string.

Parameters
snowflake_valsnowflake value as a string

◆ operator==() [3/3]

template<typename T , typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr bool dpp::snowflake::operator== ( snowflake_val) const
inlineconstexprnoexcept

Comparison operator with an integer.

Parameters
snowflake_valsnowflake value as an integer type

◆ str()

std::string dpp::snowflake::str ( ) const
inline

Returns the stringified version of the snowflake value.

Returns
std::string string form of snowflake value

Friends And Related Function Documentation

◆ format_as

constexpr friend uint64_t format_as ( snowflake  s)
friend

Helper function for libfmt so that a snowflake can be directly formatted as a uint64_t.

See also
https://fmt.dev/latest/api.html#formatting-user-defined-types
Returns
uint64_t snowflake ID

◆ std::hash< dpp::snowflake >

friend struct std::hash< dpp::snowflake >
friend

Member Data Documentation

◆ value

uint64_t dpp::snowflake::value = 0
protected

The snowflake value.


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