aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
snowflake.hpp
1 //
2 // snowflake.hpp
3 // *************
4 //
5 // Copyright (c) 2019 Sharon W (sharon at aegis dot gg)
6 //
7 // Distributed under the MIT License. (See accompanying file LICENSE)
8 //
9 
10 #pragma once
11 
12 #include "aegis/fwd.hpp"
13 #include "aegis/config.hpp"
14 #include <nlohmann/json.hpp>
15 
16 
17 namespace aegis
18 {
19 
21 class snowflake
22 {
23 public:
24  constexpr snowflake() noexcept : _id(0) {}
25  constexpr snowflake(int64_t _snowflake) noexcept : _id(_snowflake) {}
26  constexpr snowflake(const snowflake & _snowflake) noexcept : _id(_snowflake._id) {}
27  explicit snowflake(const char * _snowflake) noexcept : _id(std::stoll(std::string(_snowflake))) {}
28  explicit snowflake(const std::string & _snowflake) noexcept : _id(std::stoll(_snowflake)) {}
29 #if defined(AEGIS_CXX17)
30  explicit snowflake(const std::string_view _snowflake) noexcept : _id(std::stoll(std::string{ _snowflake })) {}
31 #endif
32  explicit snowflake(const nlohmann::json & _snowflake) noexcept : _id(std::stoll(_snowflake.get<std::string>())) {}
33  AEGIS_DECL snowflake(const aegis::user & _user) noexcept;
34  AEGIS_DECL snowflake(const aegis::guild & _guild) noexcept;
35  AEGIS_DECL snowflake(const aegis::channel & _channel) noexcept;
36  AEGIS_DECL snowflake(const aegis::gateway::objects::role & _role) noexcept;
37  AEGIS_DECL snowflake(const aegis::gateway::objects::message & _message) noexcept;
38  AEGIS_DECL snowflake(const aegis::gateway::objects::emoji & _emoji) noexcept;
39  AEGIS_DECL snowflake(const aegis::gateway::objects::attachment & _attachment) noexcept;
40 
41  constexpr operator int64_t() const noexcept
42  {
43  return _id;
44  }
45 
47 
50  constexpr int64_t get() const noexcept
51  {
52  return _id;
53  };
54 
56 
59  std::string gets() const noexcept
60  {
61  return std::to_string(_id);
62  };
63 
65 
68  constexpr std::tuple<int64_t, int8_t, int8_t, int16_t> get_all() const noexcept
69  {
70  return std::tuple<int64_t, int8_t, int8_t, int16_t>{ get_timestamp(), get_worker(), get_process(), get_count() };
71  };
72 
74 
77  constexpr int16_t get_count() const noexcept
78  {
79  return static_cast<int16_t>(_id & _countMask);
80  };
81 
83 
86  constexpr int8_t get_process() const noexcept
87  {
88  return static_cast<int8_t>((_id & _workerMask) >> 12);
89  };
90 
92 
95  constexpr int8_t get_worker() const noexcept
96  {
97  return static_cast<int8_t>((_id & _workerMask) >> 17);
98  };
99 
101 
104  constexpr int64_t get_timestamp() const noexcept
105  {
106  return (_id & _timestampMask) >> 22;
107  };
108 
110 
113  static constexpr std::tuple<int64_t, int8_t, int8_t, int16_t> c_get_all(int64_t _snowflake) noexcept
114  {
115  return std::tuple<int64_t, int8_t, int8_t, int16_t>{ c_get_timestamp(_snowflake), c_get_worker(_snowflake), c_get_process(_snowflake), c_get_count(_snowflake) };
116  };
117 
119 
122  static constexpr int16_t c_get_count(int64_t _snowflake) noexcept
123  {
124  return static_cast<int16_t>(_snowflake & _countMask);
125  };
126 
128 
131  static constexpr int8_t c_get_process(int64_t _snowflake) noexcept
132  {
133  return static_cast<int8_t>((_snowflake & _workerMask) >> 12);
134  };
135 
137 
140  static constexpr int8_t c_get_worker(int64_t _snowflake) noexcept
141  {
142  return static_cast<int8_t>((_snowflake & _workerMask) >> 17);
143  };
144 
146 
149  static constexpr int64_t c_get_timestamp(int64_t _snowflake) noexcept
150  {
151  return (_snowflake & _timestampMask) >> 22;
152  };
153 
155 
158  constexpr int64_t get_time() const noexcept
159  {
160  return get_timestamp() + _discordEpoch;
161  };
162 
164 
167  static constexpr int64_t c_get_time(int64_t _snowflake) noexcept
168  {
169  return c_get_timestamp(_snowflake) + _discordEpoch;
170  };
171 
172 private:
173  uint64_t _id;
174  static constexpr uint64_t _countMask = 0x0000000000000FFFL;
175  static constexpr uint64_t _processMask = 0x000000000001F000L;
176  static constexpr uint64_t _workerMask = 0x00000000003E0000L;
177  static constexpr uint64_t _timestampMask = 0xFFFFFFFFFFC00000L;
178  static constexpr uint64_t _discordEpoch = 1420070400000;
179 };
180 
182 AEGIS_DECL void from_json(const nlohmann::json& j, snowflake& s);
183 AEGIS_DECL void to_json(nlohmann::json& j, const snowflake& s);
185 
186 }
187 
189 namespace std
190 {
191 
192 template <>
193 struct hash<aegis::snowflake>
194 {
195  std::size_t operator()(const aegis::snowflake& k) const
196  {
197  return hash<int64_t>()(k.get());
198  }
199 };
200 
201 }
203 
204 #if defined(AEGIS_HEADER_ONLY)
205 #include "aegis/impl/snowflake.cpp"
206 #endif
constexpr int64_t get_time() const noexcept
Definition: snowflake.hpp:158
static constexpr int16_t c_get_count(int64_t _snowflake) noexcept
Definition: snowflake.hpp:122
Stores creation time and extra data specific to Discord for entities.
Definition: snowflake.hpp:21
constexpr std::tuple< int64_t, int8_t, int8_t, int16_t > get_all() const noexcept
Obtain all the snowflake values as a tuple.
Definition: snowflake.hpp:68
constexpr int8_t get_process() const noexcept
Definition: snowflake.hpp:86
Class for performing actions pertaining to a specified guild.
Definition: guild.hpp:159
constexpr int64_t get() const noexcept
Get snowflake as int64_t.
Definition: snowflake.hpp:50
constexpr int8_t get_worker() const noexcept
Definition: snowflake.hpp:95
Definition: message.hpp:57
Class for performing actions pertaining to a specified channel.
Definition: channel.hpp:170
Stores user-specific and guild-specific attributes of users.
Definition: user.hpp:37
Definition: attachment.hpp:28
static constexpr int8_t c_get_process(int64_t _snowflake) noexcept
Definition: snowflake.hpp:131
static constexpr std::tuple< int64_t, int8_t, int8_t, int16_t > c_get_all(int64_t _snowflake) noexcept
Obtain all the snowflake values as a tuple.
Definition: snowflake.hpp:113
static constexpr int64_t c_get_time(int64_t _snowflake) noexcept
Definition: snowflake.hpp:167
std::string gets() const noexcept
Get snowflake as std::string.
Definition: snowflake.hpp:59
Discord Emoji Object.
Definition: emoji.hpp:26
Stores info pertaining to guild roles.
Definition: role.hpp:34
constexpr int64_t get_timestamp() const noexcept
Definition: snowflake.hpp:104
constexpr int16_t get_count() const noexcept
Definition: snowflake.hpp:77
static constexpr int64_t c_get_timestamp(int64_t _snowflake) noexcept
Definition: snowflake.hpp:149
static constexpr int8_t c_get_worker(int64_t _snowflake) noexcept
Definition: snowflake.hpp:140