Skip to content

Commit

Permalink
refactor: fix json_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed Oct 10, 2023
1 parent c9562e5 commit 11c9690
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
12 changes: 2 additions & 10 deletions include/dpp/ban.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class DPP_EXPORT ban : public json_interface<ban> {
std::string reason;
/** User ID the ban applies to */
snowflake user_id;

/** Constructor */
ban();

Expand All @@ -50,15 +50,7 @@ class DPP_EXPORT ban : public json_interface<ban> {
* @param j A json object to read from
* @return A reference to self
*/
ban& fill_from_json(nlohmann::json* j);

/**
* @brief Build json representation of a ban
* @param with_id Include ID in json
*
* @return std::string stringified json
*/
std::string build_json(bool with_id = false) const;
ban& fill_from_json(nlohmann::json* j);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/emoji.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class DPP_EXPORT emoji : public managed, public json_interface<emoji> {
* @param with_id include the id in the JSON
* @return std::string json data
*/
std::string build_json(bool with_id = false) const override;
std::string build_json(bool with_id = false) const;

/**
* @brief Emoji requires colons
Expand Down
62 changes: 47 additions & 15 deletions include/dpp/json_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,62 @@ namespace dpp {
*
* @tparam T Type of class that implements the interface
*/
template<typename T> struct DPP_EXPORT json_interface {
template<typename T>
struct DPP_EXPORT json_interface {
protected:
/* Must not destruct through pointer to json_interface. */
/**
* @brief Destructor is protected - cannot delete json_interface from outside
*
* Because we declare this, we have to declare all the other special members
*/
~json_interface() = default;

public:
/**
* @brief Convert object from nlohmann::json
*
* @param j nlohmann::json object
* @return T& Reference to self for fluent calling
* @brief Default constructor
*/
T& fill_from_json([[maybe_unused]] nlohmann::json* j) {
throw dpp::logic_exception("JSON interface doesn't implement parse_from_json");
}
constexpr json_interface() noexcept = default;

/**
* @brief Copy constructor
*/
constexpr json_interface(const json_interface&) noexcept = default;

/**
* @brief Move constructor
*/
constexpr json_interface(json_interface&&) noexcept = default;

/**
* @brief Build JSON string from the object
*
* @param with_id Include the ID in the JSON
* @return std::string JSON string version of object
* @brief Copy assignment operator
*/
virtual std::string build_json([[maybe_unused]] bool with_id = false) const {
throw dpp::logic_exception("JSON interface doesn't implement build_json");
constexpr json_interface &operator=(const json_interface&) noexcept = default;

/**
* @brief Move assignment operator
*/
constexpr json_interface &operator=(json_interface&&) noexcept = default;

/**
* @brief Convert object from nlohmann::json
*
* @param j nlohmann::json object
* @return T& Reference to self for fluent calling
*/
template <typename U = T, typename = decltype(U::fill_from_json)>
T& fill_from_json(nlohmann::json* j) {
return static_cast<T*>(this)->fill_from_json(j);
}

/**
* @brief Convert object from nlohmann::json
*
* @param j nlohmann::json object
* @return T& Reference to self for fluent calling
*/
template <typename U = T, typename = decltype(U::build_json)>
std::string build_json(bool with_id = false) const {
return static_cast<const T*>(this)->build_json(with_id);
}
};
} // namespace dpp
5 changes: 0 additions & 5 deletions src/dpp/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,5 @@ ban& ban::fill_from_json(nlohmann::json* j) {
return *this;
}

std::string ban::build_json(bool with_id) const {
/* This is an unused stub, because sending a ban is simple as a user id and a reason */
return "{}";
}

} // namespace dpp

0 comments on commit 11c9690

Please sign in to comment.