Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:brainboxdotcc/DPP
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 7, 2023
2 parents ac9081f + af25a53 commit daa99bb
Show file tree
Hide file tree
Showing 83 changed files with 1,982 additions and 1,723 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"coroutine": "cpp",
"numbers": "cpp",
"semaphore": "cpp",
"stop_token": "cpp"
"stop_token": "cpp",
"charconv": "cpp"
}
}
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
23 changes: 14 additions & 9 deletions include/dpp/discordevents.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

#include <dpp/export.h>
#include <dpp/json_fwd.h>
#include <dpp/json.h>
#include <dpp/json_interface.h>
#include <string_view>
#include <functional>

namespace dpp {

Expand All @@ -48,21 +49,25 @@ void DPP_EXPORT set_snowflake_not_null(const nlohmann::json* j, const char *keyn
*/
void DPP_EXPORT set_snowflake_array_not_null(const nlohmann::json* j, const char *keyname, std::vector<class snowflake> &v);

/**
* @brief Applies a function to each element of a json array.
* @param j nlohmann::json instance to retrieve value from
* @param key key name to check for the values
* @param fn function to apply to each element
*/
void DPP_EXPORT for_each_json(nlohmann::json* parent, std::string_view key, const std::function<void(nlohmann::json*)> &fn);

/** @brief Sets an array of objects from a json field value, if defined, else does nothing
* @tparam T The class of which the array consists of. Must be derived from dpp::json_interface
* @param j nlohmann::json instance to retrieve value from
* @param keyname key name to check for the values
* @param v Value to change
*/
template<class T> std::enable_if_t<std::is_base_of_v<json_interface<T>, T>, void> set_object_array_not_null(nlohmann::json* j, const char *keyname, std::vector<T> &v) {
template<class T> void set_object_array_not_null(nlohmann::json* j, std::string_view key, std::vector<T>& v) {
v.clear();
auto k = j->find(keyname);
if (k != j->end() && !k->is_null()) {
v.reserve(j->at(keyname).size());
for (auto &obj : j->at(keyname)) {
v.emplace_back(T().fill_from_json(&obj));
}
}
for_each_json(j, key, [&v](nlohmann::json* elem) {
v.push_back(T{}.fill_from_json(elem));
});
}

/** @brief Returns a string from a json field value, if defined, else returns an empty string.
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
11 changes: 3 additions & 8 deletions include/dpp/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
#include <dpp/json_fwd.h>

#define event_decl(x,wstype) /** @brief Internal event handler for wstype websocket events. Called for each websocket message of this type. @internal */ \
class x : public event { public: virtual void handle(dpp::discord_client* client, nlohmann::json &j, const std::string &raw); };

namespace dpp {

class discord_client;
class x : public event { public: virtual void handle(class dpp::discord_client* client, nlohmann::json &j, const std::string &raw); };

/**
* @brief The events namespace holds the internal event handlers for each websocket event.
* These are handled internally and also dispatched to the user code if the event is hooked.
*/
namespace events {
namespace dpp::events {

/**
* @brief An event object represents an event handled internally, passed from the websocket e.g. MESSAGE_CREATE.
Expand Down Expand Up @@ -152,5 +148,4 @@ event_decl(automod_rule_execute, AUTO_MODERATION_ACTION_EXECUTION);
/* Audit log */
event_decl(guild_audit_log_entry_create, GUILD_AUDIT_LOG_ENTRY_CREATE);

} // namespace events
} // namespace dpp
} // namespace dpp::events
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
10 changes: 8 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 Expand Up @@ -216,6 +214,10 @@ struct DPP_EXPORT error_detail {
* @brief Error reason (full message)
*/
std::string reason;
/**
* @brief Object field index
*/
int index = 0;
};

/**
Expand All @@ -235,6 +237,10 @@ struct DPP_EXPORT error_info {
* @brief Field specific error descriptions
*/
std::vector<error_detail> errors;
/**
* @brief Human readable error message constructed from the above
*/
std::string human_readable;
};

/**
Expand Down
Loading

0 comments on commit daa99bb

Please sign in to comment.