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

A container for a 64 bit unsigned value representing many things on discord. This value is known in distributed computing as a snowflak value. More...

#include <snowflake.h>

+ Collaboration diagram for dpp::snowflake:

Public Member Functions

 snowflake (const uint64_t &value)
 Construct a snowflake object. More...
 
 snowflake (const std::string &string_value)
 Construct a snowflake object. More...
 
 snowflake ()
 Construct a snowflake object. More...
 
 ~snowflake ()=default
 Destroy the snowflake object. More...
 
 operator uint64_t () const
 For acting like an integer. More...
 
bool empty () const
 Returns true if the snowflake holds an empty value (is 0) More...
 
snowflakeoperator= (const std::string &snowflake_val)
 Assign from std::string. More...
 
snowflakeoperator= (const uint64_t &snowflake_val)
 Assign from std::string. More...
 
bool operator== (const snowflake &other) const
 Check if one snowflake value is equal to another. More...
 
bool operator== (const uint64_t &other) const
 Check if one snowflake value is equal to a uint64_t. More...
 
 operator uint64_t & ()
 For acting like an integer. More...
 
double get_creation_time () const
 Get the creation time of this snowflake according to Discord. More...
 
uint8_t get_worker_id () const
 Get the worker id that produced this snowflake value. More...
 
uint8_t get_process_id () const
 Get the process id that produced this snowflake value. More...
 
uint16_t get_increment () const
 Get the increment, which is incremented for every snowflake created over the one millisecond resolution in the timestamp. More...
 

Protected Attributes

uint64_t value
 The snowflake value. More...
 

Friends

struct std::hash< dpp::snowflake >
 
bool operator< (const snowflake &lhs, const snowflake &rhs)
 Operator less than, used for maps/unordered maps when the snowflake is a key value. More...
 

Detailed Description

A container for a 64 bit unsigned value representing many things on discord. This value is known in distributed computing as a snowflak 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/3]

dpp::snowflake::snowflake ( const uint64_t &  value)

Construct a snowflake object.

Parameters
valueA snowflake value

◆ snowflake() [2/3]

dpp::snowflake::snowflake ( const std::string &  string_value)

Construct a snowflake object.

Parameters
valueA snowflake value

◆ snowflake() [3/3]

dpp::snowflake::snowflake ( )

Construct a snowflake object.

◆ ~snowflake()

dpp::snowflake::~snowflake ( )
default

Destroy the snowflake object.

Member Function Documentation

◆ empty()

bool dpp::snowflake::empty ( ) const
inline

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

Returns
true if empty (zero)

◆ get_creation_time()

double dpp::snowflake::get_creation_time ( ) const

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()

uint16_t dpp::snowflake::get_increment ( ) const

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()

uint8_t dpp::snowflake::get_process_id ( ) const

Get the process id that produced this snowflake value.

Returns
uint8_t process id

◆ get_worker_id()

uint8_t dpp::snowflake::get_worker_id ( ) const

Get the worker id that produced this snowflake value.

Returns
uint8_t worker id

◆ operator uint64_t()

dpp::snowflake::operator uint64_t ( ) const

For acting like an integer.

Returns
The snowflake value

◆ operator uint64_t &()

dpp::snowflake::operator uint64_t & ( )

For acting like an integer.

Returns
A reference to the snowflake value

◆ operator=() [1/2]

snowflake & dpp::snowflake::operator= ( const std::string &  snowflake_val)

Assign from std::string.

Parameters
snowflake_valstring to assign from.

◆ operator=() [2/2]

snowflake & dpp::snowflake::operator= ( const uint64_t &  snowflake_val)

Assign from std::string.

Parameters
snowflake_valvalue to assign from.

◆ operator==() [1/2]

bool dpp::snowflake::operator== ( const snowflake other) const

Check if one snowflake value is equal to another.

Parameters
otherother snowflake to compare
Returns
True if the snowflake objects match

◆ operator==() [2/2]

bool dpp::snowflake::operator== ( const uint64_t &  other) const

Check if one snowflake value is equal to a uint64_t.

Parameters
otherother snowflake to compare
Returns
True if the snowflake objects match

Friends And Related Function Documentation

◆ operator<

bool operator< ( const snowflake lhs,
const snowflake rhs 
)
friend

Operator less than, used for maps/unordered maps when the snowflake is a key value.

Parameters
lhsfist snowflake to compare
rhssecond snowflake to compare
Returns
true if lhs is less than rhs

◆ std::hash< dpp::snowflake >

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

Member Data Documentation

◆ value

uint64_t dpp::snowflake::value
protected

The snowflake value.

D++ Library version 10.0.34D++ Library version 10.0.33D++ Library version 10.0.32D++ Library version 10.0.31D++ Library version 10.0.30D++ Library version 10.0.29D++ Library version 10.0.28D++ Library version 10.0.27D++ Library version 10.0.26D++ Library version 10.0.25D++ Library version 10.0.24D++ Library version 10.0.23D++ Library version 10.0.22D++ Library version 10.0.21D++ Library version 10.0.20D++ Library version 10.0.19D++ Library version 10.0.18D++ Library version 10.0.17D++ Library version 10.0.16D++ Library version 10.0.15D++ Library version 10.0.14D++ Library version 10.0.13D++ Library version 10.0.12D++ Library version 10.0.11D++ Library version 10.0.10D++ Library version 10.0.9D++ Library version 10.0.8D++ Library version 10.0.7D++ Library version 10.0.6D++ Library version 10.0.5D++ Library version 10.0.4D++ Library version 10.0.3D++ Library version 10.0.2D++ Library version 10.0.1D++ Library version 10.0.0D++ Library version 9.0.19D++ Library version 9.0.18D++ Library version 9.0.17D++ Library version 9.0.16D++ Library version 9.0.15D++ Library version 9.0.14D++ Library version 9.0.13D++ Library version 9.0.12D++ Library version 9.0.11D++ Library version 9.0.10D++ Library version 9.0.9D++ Library version 9.0.8D++ Library version 9.0.7D++ Library version 9.0.6D++ Library version 9.0.5D++ Library version 9.0.4D++ Library version 9.0.3D++ Library version 9.0.2D++ Library version 9.0.1D++ Library version 9.0.0D++ Library version 1.0.2D++ Library version 1.0.1D++ Library version 1.0.0