From 51cf8fd2df3cf7a602d05540627a5ad8af6baa58 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Wed, 9 Sep 2020 23:17:26 -0400 Subject: rename and reorder a bunch of discord stuff --- discord/snowflake.hpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 discord/snowflake.hpp (limited to 'discord/snowflake.hpp') diff --git a/discord/snowflake.hpp b/discord/snowflake.hpp new file mode 100644 index 0000000..d16e236 --- /dev/null +++ b/discord/snowflake.hpp @@ -0,0 +1,50 @@ +#pragma once +#include +#include + +struct Snowflake { + Snowflake(); + Snowflake(const Snowflake &s); + Snowflake(uint64_t n); + Snowflake(const std::string &str); + + bool IsValid() const; + + bool operator==(const Snowflake &s) const noexcept { + return m_num == s.m_num; + } + + bool operator<(const Snowflake &s) const noexcept { + return m_num < s.m_num; + } + + operator uint64_t() const noexcept { + return m_num; + } + + const static int Invalid = -1; + + friend void from_json(const nlohmann::json &j, Snowflake &s); + friend void to_json(nlohmann::json &j, const Snowflake &s); + +private: + friend struct std::hash; + friend struct std::less; + unsigned long long m_num; +}; + +namespace std { +template<> +struct hash { + std::size_t operator()(const Snowflake &k) const { + return k.m_num; + } +}; + +template<> +struct less { + bool operator()(const Snowflake &l, const Snowflake &r) const { + return l.m_num < r.m_num; + } +}; +} // namespace std -- cgit v1.2.3