Skip to content

Commit

Permalink
luigi
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Oct 5, 2023
1 parent 237c7d6 commit 111f3b4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
8 changes: 4 additions & 4 deletions include/dpp/managed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 17 additions & 14 deletions include/dpp/snowflake.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <dpp/json_fwd.h>
#include <dpp/exception.h>
#include <cstdint>
#include <charconv>
#include <type_traits>

/**
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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 <typename T, typename = std::enable_if_t<std::is_same_v<T, std::string>>>
snowflake(const T &string_value) noexcept : snowflake(std::string_view{string_value}) {}
/* ^ this exists to preserve `message_cache.find(std::get<std::string>(event.get_parameter("message_id")));`*/

/**
* @brief Copy value from another snowflake
*
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/dpp/snowflake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 111f3b4

Please sign in to comment.