From 111f3b490edd0b89ff844fe6c11e8dd5f28fe088 Mon Sep 17 00:00:00 2001 From: Amber Ehrlich Date: Thu, 5 Oct 2023 11:54:37 -0400 Subject: [PATCH] luigi --- include/dpp/managed.h | 8 ++++---- include/dpp/snowflake.h | 31 +++++++++++++++++-------------- src/dpp/snowflake.cpp | 12 ++++++++++-- src/unittest/test.cpp | 5 ++++- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/include/dpp/managed.h b/include/dpp/managed.h index 191910b639..4d89a5d84b 100644 --- a/include/dpp/managed.h +++ b/include/dpp/managed.h @@ -66,19 +66,19 @@ namespace dpp { /** * @brief Destroy the managed object */ - virtual ~managed() = default; + virtual ~managed() noexcept = default; /** * @brief Copy assignment operator * @param rhs Object to copy */ - constexpr managed &operator=(const managed& rhs) = default; + constexpr managed &operator=(const managed& rhs) noexcept = default; /** * @brief Move assignment operator - * @param rhs Object to copy + * @param rhs Object to move from */ - constexpr managed &operator=(managed&& rhs) = default; + constexpr managed &operator=(managed&& rhs) noexcept = default; /** * @brief Get the creation time of this object according to Discord. diff --git a/include/dpp/snowflake.h b/include/dpp/snowflake.h index 226a8d88a9..1f947ed73d 100644 --- a/include/dpp/snowflake.h +++ b/include/dpp/snowflake.h @@ -24,7 +24,6 @@ #include #include #include -#include #include /** @@ -58,18 +57,18 @@ class DPP_EXPORT snowflake final { public: /** - * @brief Construct a snowflake object - */ + * @brief Construct a snowflake object + */ constexpr snowflake() noexcept = default; /** - * @brief Copy a snowflake object - */ + * @brief Copy a snowflake object + */ constexpr snowflake(const snowflake &rhs) noexcept = default; /** - * @brief Move a snowflake object - */ + * @brief Move a snowflake object + */ constexpr snowflake(snowflake &&rhs) noexcept = default; /** @@ -101,6 +100,16 @@ class DPP_EXPORT snowflake final { */ snowflake(std::string_view string_value) noexcept; + /** + * @brief Construct a snowflake object from an unsigned integer in a string + * + * On invalid string the value will be 0 + * @param string_value A snowflake value + */ + template >> + snowflake(const T &string_value) noexcept : snowflake(std::string_view{string_value}) {} + /* ^ this exists to preserve `message_cache.find(std::get(event.get_parameter("message_id")));`*/ + /** * @brief Copy value from another snowflake * @@ -166,13 +175,7 @@ class DPP_EXPORT snowflake final { * * @param snowflake_val snowflake value as a string */ - inline bool operator==(std::string_view snowflake_val) const noexcept { - uint64_t v; - auto [end, err] = std::from_chars(snowflake_val.data(), snowflake_val.data() + snowflake_val.size(), v); - if (end != snowflake_val.data() + snowflake_val.size()) // parse error - return false; - return *this == v; - } + bool operator==(std::string_view snowflake_val) const noexcept; /** * @brief Comparison operator with an integer diff --git a/src/dpp/snowflake.cpp b/src/dpp/snowflake.cpp index 5f7ad6e844..4b2d8a3c46 100644 --- a/src/dpp/snowflake.cpp +++ b/src/dpp/snowflake.cpp @@ -32,12 +32,20 @@ snowflake::snowflake(std::string_view string_value) noexcept { } snowflake& snowflake::operator=(std::string_view string_value) noexcept { - auto [end, err] = std::from_chars(string_value.data(), string_value.data() + string_value.size(), value); - if (end != string_value.data() + string_value.size()) + auto [_, err] = std::from_chars(string_value.data(), string_value.data() + string_value.size(), value); + if (err != std::errc{}) value = 0; return *this; } +bool snowflake::operator==(std::string_view snowflake_val) const noexcept { + uint64_t v; + auto [end, err] = std::from_chars(snowflake_val.data(), snowflake_val.data() + snowflake_val.size(), v); + if (err != std::errc{}) + return false; + return *this == v; +} + snowflake::operator json() const { /* Discord transfers snowflakes as strings for compatibility with javascript */ return std::to_string(value); diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 2764dd54ff..6c73a38267 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -206,7 +206,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b success = success && (dpp::snowflake{69} < dpp::snowflake{420} && (dpp::snowflake{69} < 420)); s = "69420"; success = success && s == 69420; - s = dpp::snowflake{"1337"}; + auto conversion_test = [](dpp::snowflake sl) { + return sl; + }; + s = conversion_test(std::string{"1337"}); success = success && s == 1337; success = success && dpp::snowflake{0} == 0; set_test(SNOWFLAKE, success);