| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
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 snowflake value. More...

`#include <snowflake.h>`

Collaboration diagram for dpp::snowflake:
[legend]

## Public Member Functions

constexpr | snowflake () noexcept=default
| Construct a snowflake object.
constexpr | snowflake (const snowflake &rhs) noexcept=default
| Copy a snowflake object.
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.
constexpr | snowflake (snowflake &&rhs) noexcept=default
| Move a snowflake object.
| snowflake (std::string_view string_value) noexcept
| Construct a snowflake object from an unsigned integer in a string.
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.
constexpr bool | empty () const noexcept
| Returns true if the snowflake holds an empty value (is 0).
constexpr double | get_creation_time () const noexcept
| Get the creation time of this snowflake according to Discord.
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.
constexpr uint8_t | get_process_id () const noexcept
| Get the process id that produced this snowflake value.
constexpr uint8_t | get_worker_id () const noexcept
| Get the worker id that produced this snowflake value.
| operator json () const
| For building json.
constexpr | operator uint64_t () const noexcept
| For acting like an integer.
constexpr | operator uint64_t & () noexcept
| For acting like an integer.
constexpr dpp::snowflake & | operator= (const dpp::snowflake &rhs) noexcept=default
| Copy value from another snowflake.
constexpr dpp::snowflake & | operator= (dpp::snowflake &&rhs) noexcept=default
| Move value from another snowflake.
template<typename T, typename = std::enable_if_t<std::is_convertible_v<T, std::string_view>>>
constexpr dpp::snowflake & | operator= (T &&snowflake_val) noexcept
| Assign value converted from a string to the snowflake.
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
constexpr dpp::snowflake & | operator= (T snowflake_val) noexcept(std::is_unsigned_v< T >)
| Assign integer value to the snowflake.
constexpr bool | operator== (dpp::snowflake snowflake_val) const noexcept
| Comparison operator with another snowflake.
bool | operator== (std::string_view snowflake_val) const noexcept
| Comparison operator with a string.
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.
std::string | str () const
| Returns the stringified version of the snowflake value.

## Protected Attributes

uint64_t | value = 0
| The snowflake value.

## Friends

constexpr uint64_t | format_as (snowflake s) noexcept
| Helper function for libfmt so that a snowflake can be directly formatted as a uint64_t.
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]

dpp::snowflake::snowflake | ( |  | ) |

constexpr default noexcept

Construct a snowflake object.

## ◆ snowflake() [2/6]

dpp::snowflake::snowflake | ( | const snowflake & | rhs | ) |

constexpr default noexcept

Copy a snowflake object.

## ◆ snowflake() [3/6]

dpp::snowflake::snowflake | ( | snowflake && | rhs | ) |

constexpr default noexcept

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

dpp::snowflake::snowflake | ( | T | snowflake_val | ) |

inline constexpr noexcept

Construct a snowflake from an integer value.

Exceptions

  dpp::logic_exception | on assigning a negative value. The function is noexcept if the type given is unsigned

Parameters

  snowflake_val | snowflake 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_value | A 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 | ) |

inline noexcept

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

On invalid string the value will be 0

Parameters

  string_value | A snowflake value

## Member Function Documentation

## ◆ empty()

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

inline constexpr noexcept

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

inline constexpr noexcept

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

inline constexpr noexcept

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

inline constexpr noexcept

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

inline constexpr noexcept

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

dpp::snowflake::operator uint64_t | ( |  | ) | const

inline constexpr noexcept

For acting like an integer.

Returns
  The snowflake value

## ◆ operator uint64_t &()

dpp::snowflake::operator uint64_t & | ( |  | ) |

inline constexpr noexcept

For acting like an integer.

Returns
  A reference to the snowflake value

## ◆ operator=() [1/4]

dpp::snowflake & dpp::snowflake::operator= | ( | const dpp::snowflake & | rhs | ) |

constexpr default noexcept

Copy value from another snowflake.

Parameters

  rhs | The snowflake to copy from

## ◆ operator=() [2/4]

dpp::snowflake & dpp::snowflake::operator= | ( | dpp::snowflake && | rhs | ) |

constexpr default noexcept

Move value from another snowflake.

Parameters

  rhs | The snowflake to move from

## ◆ operator=() [3/4]

template<typename T, typename = std::enable_if_t<std::is_convertible_v<T, std::string_view>>>

dpp::snowflake & dpp::snowflake::operator= | ( | T && | snowflake_val | ) |

inline constexpr noexcept

Assign value converted from a string to the snowflake.

On invalid string the value will be 0

Parameters

  snowflake_val | snowflake value as a string

## ◆ operator=() [4/4]

template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>

dpp::snowflake & dpp::snowflake::operator= | ( | T | snowflake_val | ) |

inline constexpr noexcept

Assign integer value to the snowflake.

Exceptions

  dpp::logic_exception | on assigning a negative value. The function is noexcept if the type given is unsigned

Parameters

  snowflake_val | snowflake value as an integer type

## ◆ operator==() [1/3]

bool dpp::snowflake::operator== | ( | dpp::snowflake | snowflake_val | ) | const

inline constexpr noexcept

Comparison operator with another snowflake.

Parameters

  snowflake_val | snowflake

## ◆ operator==() [2/3]

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

noexcept

Comparison operator with a string.

Parameters

  snowflake_val | snowflake value as a string

## ◆ operator==() [3/3]

template<typename T, typename = std::enable_if_t<std::is_integral_v<T>>>

bool dpp::snowflake::operator== | ( | T | snowflake_val | ) | const

inline constexpr noexcept

Comparison operator with an integer.

Parameters

  snowflake_val | snowflake 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 Symbol Documentation

## ◆ format_as

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.

D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0