From dff3b11c001bced1e8a028871b60aa269b88ef9f Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 28 Mar 2019 21:23:24 +0100 Subject: [PATCH] Cleaned up, fixed formatting and so on. --- src/easy/all.hpp | 2 +- src/easy/easy.cpp | 11 +- src/easy/easy.hpp | 211 ++++++++++---------- src/easy/entity.hpp | 354 ++++++++++++++++----------------- src/easy/return_types_easy.cpp | 88 ++++++++ src/easy/return_types_easy.hpp | 114 ++++------- src/easy/simple_calls.cpp | 8 +- src/mastodon-cpp.hpp | 2 +- 8 files changed, 419 insertions(+), 371 deletions(-) create mode 100644 src/easy/return_types_easy.cpp diff --git a/src/easy/all.hpp b/src/easy/all.hpp index f66f904..4084026 100644 --- a/src/easy/all.hpp +++ b/src/easy/all.hpp @@ -1,6 +1,6 @@ /* This file is part of mastodon-cpp. * Copyright © 2018 tastytea - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index b472722..8664ac1 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -23,7 +23,6 @@ #include "debug.hpp" using namespace Mastodon; -using namespace Mastodon::Easy; using std::string; Easy::API::API(const string &instance, const string &access_token) @@ -51,8 +50,8 @@ const std::vector Easy::json_array_to_vector(const string &json) return {}; } -const std::vector - Easy::parse_stream(const std::string &streamdata) +const vector Easy::parse_stream( + const std::string &streamdata) { string stream = streamdata; std::regex reevent("event: (update|notification|delete)\ndata: (.*)\n"); @@ -85,19 +84,19 @@ const Easy::Link Easy::API::get_link() const } const string Easy::API::strtime_utc(const system_clock::time_point &timepoint, - const string &format) + const string &format) { return strtime(timepoint, format, true); } const string Easy::API::strtime_local(const system_clock::time_point &timepoint, - const string &format) + const string &format) { return strtime(timepoint, format, false); } const string Easy::API::strtime(const system_clock::time_point &timepoint, - const string &format, const bool &utc) + const string &format, const bool &utc) { constexpr std::uint16_t bufsize = 1024; std::time_t time = system_clock::to_time_t(timepoint); diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index 3e169cd..455a001 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -44,14 +44,14 @@ namespace Mastodon { /*! * @brief Child of Mastodon::API with abstract methods. - * + * * @since before 0.11.0 */ namespace Easy { /*! * @brief Describes the event type - * + * * @since before 0.11.0 */ enum class event_type @@ -64,7 +64,7 @@ namespace Easy /*! * @brief Describes visibility of toots. - * + * * @since before 0.11.0 */ enum class visibility_type @@ -78,7 +78,7 @@ namespace Easy /*! * @brief Describes the attachment type - * + * * @since before 0.11.0 */ enum class attachment_type @@ -92,7 +92,7 @@ namespace Easy /*! * @brief Describes the card type - * + * * @since before 0.11.0 */ enum class card_type @@ -106,7 +106,7 @@ namespace Easy /*! * @brief Describes the notification type - * + * * @since before 0.11.0 */ enum class notification_type @@ -120,7 +120,7 @@ namespace Easy /*! * @brief Used for stream events. - * + * * @since before 0.11.0 */ typedef std::pair stream_event; @@ -129,11 +129,12 @@ namespace Easy * @brief Map of 'notification type' and 'push is requested or not' * * Used in PushSubscription::alerts(). - * + * * @since 0.13.3 */ typedef std::map alertmap; + // TODO: Get rid of forward declarations. class Account; class Application; class Attachment; @@ -153,9 +154,9 @@ namespace Easy /*! * @brief Class to hold the `Link`-header. - * + * * Extracts max_id and since_id from the `Link`-header - * + * * @since before 0.11.0 */ // TODO: Convert to struct? @@ -164,35 +165,35 @@ namespace Easy public: /*! * @param link_header The content of the `Link` header - * + * * @since before 0.11.0 */ explicit Link(const string &link_header); /*! * @brief Returns max_id - * + * * @since before 0.11.0 */ const string next() const; /*! * @brief Returns max_id - * + * * @since before 0.11.0 */ const string max_id() const; /*! * @brief Returns since_id - * + * * @since before 0.11.0 */ const string prev() const; /*! * @brief Returns since_id - * + * * @since before 0.11.0 */ const string since_id() const; @@ -208,7 +209,7 @@ namespace Easy * @param json JSON string holding the array * * @return vector of strings or an empty vector on error - * + * * @since before 0.11.0 */ const std::vector json_array_to_vector(const string &json); @@ -219,106 +220,112 @@ namespace Easy * @param streamdata Data from get_stream() * * @return vector of stream events - * + * * @since before 0.11.0 */ - const std::vector - parse_stream(const std::string &streamdata); + const std::vector parse_stream(const std::string &streamdata); + /*! + * @brief Interface to the Mastodon API, easy version. + * + * Provides convenient functions to deal with the responses you get. + */ class API : public Mastodon::API { public: - /*! - * @brief Constructs a new Easy object. - * - * To register your application, leave access_token blank and call - * register_app1() and register_app2(). - * - * @param instance The hostname of your instance - * @param access_token The access token - * - * @since before 0.11.0 - */ - explicit API(const string &instance, const string &access_token); + /*! + * @brief Constructs a new Easy object. + * + * To register your application, leave access_token blank and + * call register_app1() and register_app2(). + * + * @param instance The hostname of your instance + * @param access_token The access token + * + * @since before 0.11.0 + */ + explicit API(const string &instance, const string &access_token); - /*! - * @brief Gets the links from the last answer - * - * @since before 0.11.0 - */ - const Link get_link() const; + /*! + * @brief Gets the links from the last answer + * + * @since before 0.11.0 + */ + const Link get_link() const; - /*! - * @brief Converts a time_point to a string - * - * The return value can not exceed 1023 chars. - * - * @param timepoint The timepoint - * @param format The format of the string, same as with `strftime`. - * - * Example: - * @code - * auto timepoint = status.created_at(); - * cout << Easy::strtime_utc(timepoint, "%F, %T") << '\n'; - * @endcode - * - * @return The UTC time as string - * - * @since 0.11.0 - */ + /*! + * @brief Converts a time_point to a string + * + * The return value can not exceed 1023 chars. + * + * @param timepoint The timepoint + * @param format The format of the string, same as with + * `strftime`. + * + * Example: + * @code + * auto timepoint = status.created_at(); + * cout << Easy::strtime_utc(timepoint, "%F, %T") << '\n'; + * @endcode + * + * @return The UTC time as string + * + * @since 0.11.0 + */ // TODO: Time type, convertible to time_point, str_utc and str_local. - static const string strtime_utc(const system_clock::time_point &timepoint, - const string &format); + static const string strtime_utc(const system_clock::time_point &timepoint, + const string &format); - /*! - * @brief See strtime_utc - * - * @return The local time as string - * - * @since 0.11.0 - */ - static const string strtime_local(const system_clock::time_point &timepoint, - const string &format); + /*! + * @brief See strtime_utc + * + * @return The local time as string + * + * @since 0.11.0 + */ + static const string strtime_local(const system_clock::time_point &timepoint, + const string &format); - // #### simple calls #### - - /*! - * @brief Sends a toot. - * - * @param status The status to send - * @param error @ref error "Error code" - * - * @return The new Easy::Status - * - * @since 0.18.1 - */ - const return_entity send_post(const Status &status); + // #### simple calls #### + // TODO: Own file. - /*! - * @brief Alias for send_post() - * - * @since 0.17.0 - */ - const return_entity send_toot(const Status &status); + /*! + * @brief Sends a post. + * + * @param status The status to send + * @param error @ref error "Error code" + * + * @return The new Easy::Status + * + * @since 0.18.1 + */ + const return_entity send_post(const Status &status); - /*! - * @brief Gets notifications. - * - * @param error @ref error "Error code" - * @param limit Maximum number of notifications - * @param since_id Return notifications newer than ID - * @param max_id Return notifications older than ID - * - * @return vector of Easy::Notification. - * - * @since 0.21.0 - */ - const return_entity_vector get_notifications( - const uint16_t limit = 20, const string since_id = "", - const string max_id = ""); + /*! + * @brief Alias for send_post() + * + * @since 0.17.0 + */ + const return_entity send_toot(const Status &status); -protected: - inline static const string strtime + /*! + * @brief Gets notifications. + * + * @param error @ref error "Error code" + * @param limit Maximum number of notifications + * @param since_id Return notifications newer than ID + * @param max_id Return notifications older than ID + * + * @return vector of Easy::Notification. + * + * @since 0.21.0 + */ + const return_entity_vector get_notifications( + const uint16_t limit = 20, const string since_id = "", + const string max_id = ""); + + protected: + inline static const string strtime (const system_clock::time_point &timepoint, const string &format, const bool &utc); }; diff --git a/src/easy/entity.hpp b/src/easy/entity.hpp index 4fce78f..b376352 100644 --- a/src/easy/entity.hpp +++ b/src/easy/entity.hpp @@ -28,209 +28,209 @@ namespace Mastodon { namespace Easy { -/*! - * @brief Base class for all entities. - * - * @since before 0.11.0 - */ -class Entity -{ -public: /*! - * @brief Constructs an Entity object from a JSON string. + * @brief Base class for all entities. * - * @param json JSON string - * * @since before 0.11.0 */ - explicit Entity(const string &json); + class Entity + { + public: + /*! + * @brief Constructs an Entity object from a JSON string. + * + * @param json JSON string + * + * @since before 0.11.0 + */ + explicit Entity(const string &json); - /*! - * @brief Constructs an Entity object from a JSON object. - * - * @param object JSON object - * - * @since 0.100.0 - */ - explicit Entity(const Json::Value &object); + /*! + * @brief Constructs an Entity object from a JSON object. + * + * @param object JSON object + * + * @since 0.100.0 + */ + explicit Entity(const Json::Value &object); - /*! - * @brief Constructs an empty Entity object. - * - * @since before 0.11.0 - */ - Entity(); + /*! + * @brief Constructs an empty Entity object. + * + * @since before 0.11.0 + */ + Entity(); - /*! - * @brief Destroys the object. - * - * @since 0.100.0 - */ - virtual ~Entity(); + /*! + * @brief Destroys the object. + * + * @since 0.100.0 + */ + virtual ~Entity(); - /*! - * Returns the JSON object of the Entity - * - * @since 0.100.0 - */ - operator const Json::Value() const; + /*! + * Returns the JSON object of the Entity + * + * @since 0.100.0 + */ + operator const Json::Value() const; - /*! - * @brief Replaces the Entity with a new one from a JSON string. - * - * @param json JSON string - * - * @since before 0.11.0 - */ - void from_string(const string &json); + /*! + * @brief Replaces the Entity with a new one from a JSON string. + * + * @param json JSON string + * + * @since before 0.11.0 + */ + void from_string(const string &json); - /*! - * @brief Returns the JSON object of the Entity - * - * @return JSON object - * - * @since before 0.11.0 - */ - const string to_string() const; + /*! + * @brief Returns the JSON object of the Entity + * + * @return JSON object + * + * @since before 0.11.0 + */ + const string to_string() const; - /*! - * @brief Replaces the Entity with a new one from a JSON object. - * - * @param object JSON object - * - * @since 0.100.0 - */ - void from_object(const Json::Value &object); + /*! + * @brief Replaces the Entity with a new one from a JSON object. + * + * @param object JSON object + * + * @since 0.100.0 + */ + void from_object(const Json::Value &object); - /*! - * @brief Returns the JSON object of the Entity - * - * @return JSON object - * - * @since before 0.11.0 - */ - const Json::Value to_object() const; + /*! + * @brief Returns the JSON object of the Entity + * + * @return JSON object + * + * @since before 0.11.0 + */ + const Json::Value to_object() const; - /*! - * @brief Returns true if the Entity holds valid data - * - * @since before 0.11.0 (virtual since 0.18.2) - */ - virtual bool valid() const = 0; + /*! + * @brief Returns true if the Entity holds valid data + * + * @since before 0.11.0 (virtual since 0.18.2) + */ + virtual bool valid() const = 0; - /*! - * @brief Returns error string sent by the server - * - * @since before 0.11.0 - */ - const string error() const; + /*! + * @brief Returns error string sent by the server + * + * @since before 0.11.0 + */ + const string error() const; - /*! - * @brief Returns true if the last requested value was set, false if - * it was unset. - * - * Members of Easy::Entity-derived classes return a default - * value depending on its type when the requested value is not - * found in the JSON. "" for strings, false for bools and so - * on. Most of the time this is no problem, but sometimes you - * need to know for sure. - * - * Example: - * @code - * Easy::Account a(jsonstring); - * if (a.note().empty()) - * { - * if (a.was_set()) - * { - * cout << "Account has an empty description.\n"; - * } - * else - * { - * cout << "Account has no description.\n"; - * } - * } - * @endcode - * - * @since before 0.11.0 - */ - bool was_set() const; + /*! + * @brief Returns true if the last requested value was set, false if + * it was unset. + * + * Members of Easy::Entity-derived classes return a default + * value depending on its type when the requested value is not + * found in the JSON. "" for strings, false for bools and so + * on. Most of the time this is no problem, but sometimes you + * need to know for sure. + * + * Example: + * @code + * Easy::Account a(jsonstring); + * if (a.note().empty()) + * { + * if (a.was_set()) + * { + * cout << "Account has an empty description.\n"; + * } + * else + * { + * cout << "Account has no description.\n"; + * } + * } + * @endcode + * + * @since before 0.11.0 + */ + bool was_set() const; -protected: - /*! - * @brief Returns the value of key as Json::Value - * - * Returns an empty object if the value does not exist or is - * null. - */ - const Json::Value get(const string &key) const; + protected: + /*! + * @brief Returns the value of key as Json::Value + * + * Returns an empty object if the value does not exist or is + * null. + */ + const Json::Value get(const string &key) const; - /*! - * @brief Returns the value of key as std::string - * - * returns "" if the value does not exist or is null. - */ - const string get_string(const string &key) const; + /*! + * @brief Returns the value of key as std::string + * + * returns "" if the value does not exist or is null. + */ + const string get_string(const string &key) const; - /*! - * @brief Returns the value of key as std::uint64_t - * - * Returns 0 if the value does not exist or is null. - */ - uint64_t get_uint64(const string &key) const; + /*! + * @brief Returns the value of key as std::uint64_t + * + * Returns 0 if the value does not exist or is null. + */ + uint64_t get_uint64(const string &key) const; - /*! - * @brief Returns the value of key as double - * - * Returns 0.0 if the value does not exist or is null. - */ - double get_double(const string &key) const; + /*! + * @brief Returns the value of key as double + * + * Returns 0.0 if the value does not exist or is null. + */ + double get_double(const string &key) const; - // TODO: Maybe an enum would be better? - /*! - * @brief Returns the value of key as bool - * - * Returns false if the value does not exist or is null. - */ - bool get_bool(const string &key) const; + // TODO: Maybe an enum would be better? + /*! + * @brief Returns the value of key as bool + * + * Returns false if the value does not exist or is null. + */ + bool get_bool(const string &key) const; - /*! - * @brief Returns the value of key as time_point - * - * Returns clocks epoch if the value does not exist or is null. - */ - const system_clock::time_point get_time_point(const string &key) const; + /*! + * @brief Returns the value of key as time_point + * + * Returns clocks epoch if the value does not exist or is null. + */ + const system_clock::time_point get_time_point(const string &key) const; - /*! - * @brief Returns the value of key as vector - * - * Returns an empty vector if the value does not exist or is - * null. - */ - const std::vector get_vector(const string &key) const; + /*! + * @brief Returns the value of key as vector + * + * Returns an empty vector if the value does not exist or is + * null. + */ + const std::vector get_vector(const string &key) const; - /*! - * @brief Sets the value of key - * - * @since 0.17.0 - */ - void set(const string &key, const Json::Value &value); + /*! + * @brief Sets the value of key + * + * @since 0.17.0 + */ + void set(const string &key, const Json::Value &value); - std::uint64_t stouint64(const string &str) const; + std::uint64_t stouint64(const string &str) const; - /*! - * @brief Checks if an Entity is valid - * - * @param attributes The attributes to check - * - * @return true if all attributes are set - * - * @since 0.18.2 - */ - bool check_valid(const std::vector &attributes) const; + /*! + * @brief Checks if an Entity is valid + * + * @param attributes The attributes to check + * + * @return true if all attributes are set + * + * @since 0.18.2 + */ + bool check_valid(const std::vector &attributes) const; -private: - Json::Value _tree; - mutable bool _was_set; + private: + Json::Value _tree; + mutable bool _was_set; }; } } diff --git a/src/easy/return_types_easy.cpp b/src/easy/return_types_easy.cpp new file mode 100644 index 0000000..de01c44 --- /dev/null +++ b/src/easy/return_types_easy.cpp @@ -0,0 +1,88 @@ +/* This file is part of mastodon-cpp. + * Copyright © 2019 tastytea + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "return_types_easy.hpp" +#include "easy/entities/status.hpp" +#include "easy/entities/notification.hpp" + +using namespace Mastodon; + +template +Easy::return_entity::return_entity() + : entity() +{} + +template +Easy::return_entity::return_entity(const uint8_t ec, const string &em, + const T &ent) + : entity(ent) +{ + error_code = ec; + error_message = em; +} + +template +Easy::return_entity::return_entity::operator const T() const +{ + return entity; +} + +template +Easy::return_entity::return_entity::operator const string() const +{ + return entity.to_string(); +} + +template +std::ostream &Easy::operator <<(std::ostream &out, + const Easy::return_entity &ret) +{ + out << ret.entity.to_string(); + return out; +} + +// Explicit instantiations, so it can be used from outside. +// TODO: Complete this. +template struct Easy::return_entity; +template struct Easy::return_entity; + + +template +Easy::return_entity_vector:: +return_entity_vector::return_entity_vector() + : entities() +{} + +template +Easy::return_entity_vector::return_entity_vector::return_entity_vector( + const uint8_t ec, const string &em, const vector &vec) + : entities(vec) +{ + error_code = ec; + error_message = em; +} + +template +Easy::return_entity_vector::return_entity_vector::operator const vector() + const +{ + return entities; +} + +// Explicit instantiations, so it can be used from outside. +// TODO: Complete this. +template struct Easy::return_entity_vector; +template struct Easy::return_entity_vector; diff --git a/src/easy/return_types_easy.hpp b/src/easy/return_types_easy.hpp index beb42f3..4d728e4 100644 --- a/src/easy/return_types_easy.hpp +++ b/src/easy/return_types_easy.hpp @@ -20,101 +20,55 @@ #include #include #include +#include #include "mastodon-cpp.hpp" using std::string; using std::vector; +using std::uint8_t; namespace Mastodon { -template -struct return_entity; -// https://stackoverflow.com/a/4661372/5965450 -template -std::ostream &operator <<(std::ostream&, const return_entity&); - -template -struct return_entity : return_base +namespace Easy { - T entity; + template + struct return_entity; + template // https://stackoverflow.com/a/4661372/5965450 + std::ostream &operator <<(std::ostream&, const return_entity&); - return_entity(); - return_entity(const uint8_t ec, const string &em, const T &ent); + /*! + * @brief Return types for calls that return a single `Easy::Entity`. + */ + template + struct return_entity : return_base + { + T entity; - operator const T() const; - operator const string() const; + return_entity(); + return_entity(const uint8_t ec, const string &em, const T &ent); - friend std::ostream &operator <<(std::ostream &out, - const return_entity &ret); -}; + operator const T() const; + operator const string() const; -template -struct return_entity_vector : return_base -{ - vector entities; + // FIXME: Can't get it to work, don't know why. + friend std::ostream &operator <<(std::ostream &out, + const return_entity &ret); + }; - return_entity_vector(); - return_entity_vector(const uint8_t ec, const string &em, - const vector &vec); + /*! + * @brief Return types for calls that return multiple `Easy::Entity`s. + */ + template + struct return_entity_vector : return_base + { + vector entities; - operator const vector() const; -}; + return_entity_vector(); + return_entity_vector(const uint8_t ec, const string &em, + const vector &vec); - -template -return_entity::return_entity() - : entity() -{} - -template -return_entity::return_entity(const uint8_t ec, const string &em, - const T &ent) - : entity(ent) -{ - error_code = ec; - error_message = em; -} - -template -return_entity::return_entity::operator const T() const -{ - return entity; -} - -template -return_entity::return_entity::operator const string() const -{ - return entity.to_string(); -} - -template -std::ostream &operator <<(std::ostream &out, const return_entity &ret) -{ - out << ret.entity.to_string(); - return out; -} - -// Explicit instantiation -// template struct Mastodon::return_entity; - -template -return_entity_vector::return_entity_vector::return_entity_vector() -: entities() -{} - -template -return_entity_vector::return_entity_vector::return_entity_vector( - const uint8_t ec, const string &em, const vector &vec) -: entities(vec) -{ - error_code = ec; - error_message = em; -} - -template -return_entity_vector::return_entity_vector::operator const vector() const -{ - return entities; + operator const vector() const; + }; } } diff --git a/src/easy/simple_calls.cpp b/src/easy/simple_calls.cpp index f70b414..646a6fd 100644 --- a/src/easy/simple_calls.cpp +++ b/src/easy/simple_calls.cpp @@ -21,14 +21,14 @@ #include "easy/entities/attachment.hpp" #include "easy/entities/notification.hpp" -using namespace Mastodon; +using namespace Mastodon::Easy; -const return_entity Easy::API::send_toot(const Status &status) +const return_entity API::send_toot(const Status &status) { return send_post(status); } -const return_entity Easy::API::send_post(const Status &status) +const return_entity API::send_post(const Status &status) { API::parametermap parameters; @@ -129,7 +129,7 @@ const return_entity Easy::API::send_post(const Status &status) return { ret.error_code, ret.error_message, Status(ret.answer) }; } -const return_entity_vector Easy::API::get_notifications( +const return_entity_vector API::get_notifications( const uint16_t limit, const string since_id, const string max_id) { API::parametermap parameters; diff --git a/src/mastodon-cpp.hpp b/src/mastodon-cpp.hpp index 58404fc..7af8c44 100644 --- a/src/mastodon-cpp.hpp +++ b/src/mastodon-cpp.hpp @@ -70,7 +70,7 @@ namespace Mastodon } return_call; /*! - * @brief Class for the Mastodon API. + * @brief Interface to the Mastodon API. * * All input is expected to be UTF-8. Binary data must be * base64-encoded or a filename.