Skip to content

Commit

Permalink
fix reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Dec 4, 2023
1 parent 0f88adb commit b046480
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
8 changes: 4 additions & 4 deletions include/dpp/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace dpp {
* here. In this case, or where the code is not specific, refer to `what()`.
*/
enum exception_error_code {
err_this_is_not_an_error = 0,
err_no_code_specified = 0,
err_zlib_see_errno = -1,
err_zlib_init_stream = -2,
err_zlib_init_data = -3,
Expand Down Expand Up @@ -411,7 +411,7 @@ class exception : public std::exception
*
* @param what reason message
*/
explicit exception(const char* what) : msg(what), error_code(err_this_is_not_an_error) { }
explicit exception(const char* what) : msg(what), error_code(err_no_code_specified) { }

/**
* @brief Construct a new exception object
Expand All @@ -427,14 +427,14 @@ class exception : public std::exception
* @param what reason message
* @param len length of reason message
*/
exception(const char* what, size_t len) : msg(what, len), error_code(err_this_is_not_an_error) { }
exception(const char* what, size_t len) : msg(what, len), error_code(err_no_code_specified) { }

/**
* @brief Construct a new exception object
*
* @param what reason message
*/
explicit exception(const std::string& what) : msg(what), error_code(err_this_is_not_an_error) { }
explicit exception(const std::string& what) : msg(what), error_code(err_no_code_specified) { }

/**
* @brief Construct a new exception object
Expand Down
94 changes: 47 additions & 47 deletions include/dpp/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,55 @@

namespace dpp {

/**
* @brief Call a D++ REST function synchronously.
*
* Synchronously calling a REST function means *IT WILL BLOCK* - This is a Bad Thing™ and strongly discouraged.
* There are very few circumstances you actually need this. If you do need to use this, you'll know it.
*
* Example:
*
* ```cpp
* dpp::message m = dpp::sync<dpp::message>(&bot, &dpp::cluster::message_create, dpp::message(channel_id, "moo."));
* ```
*
* @warning As previously mentioned, this template will block. It is ill-advised to call this outside of
* a separate thread and this should never be directly used in any event such as dpp::cluster::on_interaction_create!
* @tparam T type of expected return value, should match up with the method called
* @tparam F Type of class method in dpp::cluster to call.
* @tparam Ts Function parameters in method call
* @param c A pointer to dpp::cluster object
* @param func pointer to class method in dpp::cluster to call. This can call any
* dpp::cluster member function who's last parameter is a dpp::command_completion_event_t callback type.
* @param args Zero or more arguments for the method call
* @return An instantiated object of type T
* @throw dpp::rest_exception On failure of the method call, an exception is thrown
*/
template<typename T, class F, class... Ts> T sync(class cluster* c, F func, Ts&&... args) {
std::promise<T> _p;
std::future<T> _f = _p.get_future();
/* (obj ->* func) is the obscure syntax for calling a method pointer on an object instance */
(c ->* func)(std::forward<Ts>(args)..., [&_p](const auto& cc) {
try {
if (cc.is_error()) {
const auto& error = cc.get_error();
throw dpp::rest_exception((exception_error_code)error.code, error.message);
} else {
try {
_p.set_value(std::get<T>(cc.value));
} catch (const std::exception& e) {
throw dpp::rest_exception(err_unknown, e.what());
}
/**
* @brief Call a D++ REST function synchronously.
*
* Synchronously calling a REST function means *IT WILL BLOCK* - This is a Bad Thing™ and strongly discouraged.
* There are very few circumstances you actually need this. If you do need to use this, you'll know it.
*
* Example:
*
* ```cpp
* dpp::message m = dpp::sync<dpp::message>(&bot, &dpp::cluster::message_create, dpp::message(channel_id, "moo."));
* ```
*
* @warning As previously mentioned, this template will block. It is ill-advised to call this outside of
* a separate thread and this should never be directly used in any event such as dpp::cluster::on_interaction_create!
* @tparam T type of expected return value, should match up with the method called
* @tparam F Type of class method in dpp::cluster to call.
* @tparam Ts Function parameters in method call
* @param c A pointer to dpp::cluster object
* @param func pointer to class method in dpp::cluster to call. This can call any
* dpp::cluster member function who's last parameter is a dpp::command_completion_event_t callback type.
* @param args Zero or more arguments for the method call
* @return An instantiated object of type T
* @throw dpp::rest_exception On failure of the method call, an exception is thrown
*/
template<typename T, class F, class... Ts> T sync(class cluster* c, F func, Ts&&... args) {
std::promise<T> _p;
std::future<T> _f = _p.get_future();
/* (obj ->* func) is the obscure syntax for calling a method pointer on an object instance */
(c ->* func)(std::forward<Ts>(args)..., [&_p](const auto& cc) {
try {
if (cc.is_error()) {
const auto& error = cc.get_error();
throw dpp::rest_exception((exception_error_code)error.code, error.message);
} else {
try {
_p.set_value(std::get<T>(cc.value));
} catch (const std::exception& e) {
throw dpp::rest_exception(err_unknown, e.what());
}
} catch (const dpp::rest_exception&) {
_p.set_exception(std::current_exception());
}
});
} catch (const dpp::rest_exception&) {
_p.set_exception(std::current_exception());
}
});

/* Blocking calling thread until rest request is completed.
* Exceptions encountered on the other thread are re-thrown.
*/
return _f.get();
}
/* Blocking calling thread until rest request is completed.
* Exceptions encountered on the other thread are re-thrown.
*/
return _f.get();
}

} // namespace dpp

0 comments on commit b046480

Please sign in to comment.