diff --git a/include/dpp/ban.h b/include/dpp/ban.h index ddf4fa1502..3e333b383a 100644 --- a/include/dpp/ban.h +++ b/include/dpp/ban.h @@ -39,7 +39,7 @@ class DPP_EXPORT ban : public json_interface { std::string reason; /** User ID the ban applies to */ snowflake user_id; - + /** Constructor */ ban(); @@ -50,15 +50,7 @@ class DPP_EXPORT ban : public json_interface { * @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); }; /** diff --git a/include/dpp/emoji.h b/include/dpp/emoji.h index ab95736595..710d86e8bd 100644 --- a/include/dpp/emoji.h +++ b/include/dpp/emoji.h @@ -139,7 +139,7 @@ class DPP_EXPORT emoji : public managed, public json_interface { * @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 diff --git a/include/dpp/json_interface.h b/include/dpp/json_interface.h index 430a1522a4..efbe6a4075 100644 --- a/include/dpp/json_interface.h +++ b/include/dpp/json_interface.h @@ -32,30 +32,62 @@ namespace dpp { * * @tparam T Type of class that implements the interface */ - template struct DPP_EXPORT json_interface { + template + 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 + T& fill_from_json(nlohmann::json* j) { + return static_cast(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 + std::string build_json(bool with_id = false) const { + return static_cast(this)->build_json(with_id); } }; } // namespace dpp diff --git a/src/dpp/ban.cpp b/src/dpp/ban.cpp index e2a0bf127e..792fffa43c 100644 --- a/src/dpp/ban.cpp +++ b/src/dpp/ban.cpp @@ -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