Skip to content

Commit

Permalink
improv: freshen up dpp::snowflake and dpp::managed (#912)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 authored Oct 7, 2023
1 parent aac3f0b commit af25a53
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 187 deletions.
2 changes: 0 additions & 2 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@

namespace dpp {

using json = nlohmann::json;

/**
* @brief Types of startup for cluster::start()
*/
Expand Down
2 changes: 0 additions & 2 deletions include/dpp/discordclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
#define API_PATH "/api/v" DISCORD_API_VERSION
namespace dpp {

using json = nlohmann::json;

// Forward declarations
class cluster;

Expand Down
2 changes: 0 additions & 2 deletions include/dpp/discordvoiceclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class audio_mixer;

inline constexpr size_t send_audio_raw_max_length = 11520;

using json = nlohmann::json;

/*
* @brief For holding a moving average of the number of current voice users, for applying a smooth gain ramp.
*/
Expand Down
2 changes: 0 additions & 2 deletions include/dpp/event_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#include <dpp/exception.h>
#include <dpp/coro/job.h>

using json = nlohmann::json;

namespace dpp {

#ifdef DPP_CORO
Expand Down
8 changes: 7 additions & 1 deletion include/dpp/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
#include <nlohmann/json.hpp>
#else
#include <dpp/nlohmann/json.hpp>
#endif
#endif

namespace dpp {

using json = nlohmann::json;

}
8 changes: 7 additions & 1 deletion include/dpp/json_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
#include <nlohmann/json_fwd.hpp>
#else
#include <dpp/nlohmann/json_fwd.hpp>
#endif
#endif

namespace dpp {

using json = nlohmann::json;

}
51 changes: 45 additions & 6 deletions include/dpp/managed.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,59 @@ namespace dpp {
* This value contains a timestamp, worker ID, internal server ID, and an incrementing value.
* Only the timestamp is relevant to us as useful metadata.
*/
snowflake id;
snowflake id = {};

/**
* @brief Constructor, initialises id to 0.
*/
constexpr managed() noexcept = default;

/**
* @brief Constructor, initialises ID
* @param nid ID to set
*/
managed(const snowflake nid = 0);
constexpr managed(const snowflake nid) noexcept : id{nid} {}

/**
* @brief Copy constructor
* @param rhs Object to copy
*/
constexpr managed(const managed &rhs) noexcept = default;

/**
* @brief Move constructor
*
* Effectively equivalent to copy constructor
* @param rhs Object to move from
*/
constexpr managed(managed &&rhs) noexcept = default;

/**
* @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) noexcept = default;

/**
* @brief Move assignment operator
* @param rhs Object to move from
*/
constexpr managed &operator=(managed&& rhs) noexcept = default;

/**
* @brief Get the creation time of this object according to Discord.
*
* @return double creation time inferred from the snowflake ID.
* The minimum possible value is the first second of 2015.
*/
double get_creation_time() const;
constexpr double get_creation_time() const noexcept {
return id.get_creation_time();
};

/**
* @brief Comparison operator for comparing two managed objects by id
Expand All @@ -62,7 +97,9 @@ namespace dpp {
* @return true objects are the same id
* @return false objects are not the same id
*/
bool operator==(const managed& other) const noexcept;
constexpr bool operator==(const managed& other) const noexcept {
return id == other.id;
}

/**
* @brief Comparison operator for comparing two managed objects by id
Expand All @@ -71,7 +108,9 @@ namespace dpp {
* @return true objects are not the same id
* @return false objects are the same id
*/
bool operator!=(const managed& other) const noexcept;
constexpr bool operator!=(const managed& other) const noexcept {
return id != other.id;
}
};

} // namespace dpp
2 changes: 0 additions & 2 deletions include/dpp/restresults.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
#include <shared_mutex>
#include <cstring>

using json = nlohmann::json;

namespace dpp {

#ifdef _WIN32
Expand Down
Loading

0 comments on commit af25a53

Please sign in to comment.