diff --git a/src/easy/easy.cpp b/src/easy/easy.cpp index d4a5347..fe7b4c1 100644 --- a/src/easy/easy.cpp +++ b/src/easy/easy.cpp @@ -1,6 +1,6 @@ /* This file is part of mastodon-cpp. * Copyright © 2018, 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. @@ -25,47 +25,100 @@ using namespace Mastodon; using std::string; -return_entity::return_entity() -: entity() +// return_entity::return_entity() +// : entity() +// {} + +// return_entity::return_entity(const uint8_t ec, const string &em, +// const Easy::GenericEntity &ent) +// : entity(ent) +// { +// error_code = ec; +// error_message = em; +// } + +// return_entity::operator const Easy::GenericEntity() const +// { +// return entity; +// } + +// return_entity::operator const string() const +// { +// return entity.to_string(); +// } + +// std::ostream &Mastodon::operator <<(std::ostream &out, const return_entity &ret) +// { +// out << ret.entity.to_string(); +// return out; +// } + +template +return_entity::return_entity() + : entity() {} -return_entity::return_entity(const uint8_t ec, const string &em, - const Easy::GenericEntity &ent) -: entity(ent) +template +return_entity::return_entity(const uint8_t ec, const string &em, + const T &ent) + : entity(ent) { error_code = ec; error_message = em; } -return_entity::operator const Easy::GenericEntity() const +template +return_entity::return_entity::operator const T() const { return entity; } -return_entity::operator const string() const +template +return_entity::return_entity::operator const string() const { return entity.to_string(); } -std::ostream &Mastodon::operator <<(std::ostream &out, const return_entity &ret) +template +std::ostream &operator <<(std::ostream &out, const return_entity &ret) { out << ret.entity.to_string(); return out; } -return_entity_vector::return_entity_vector() +// return_entity_vector::return_entity_vector() +// : entities() +// {} + +// return_entity_vector::return_entity_vector(const uint8_t ec, const string &em, +// const vector &vec) +// : entities(vec) +// { +// error_code = ec; +// error_message = em; +// } + +// return_entity_vector::operator const vector() const +// { +// return entities; +// } + +template +return_entity_vector::return_entity_vector::return_entity_vector() : entities() {} -return_entity_vector::return_entity_vector(const uint8_t ec, const string &em, - const vector &vec) +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; } -return_entity_vector::operator const vector() const +template +return_entity_vector::return_entity_vector::operator const vector() const { return entities; } diff --git a/src/easy/easy.hpp b/src/easy/easy.hpp index f314f80..4066715 100644 --- a/src/easy/easy.hpp +++ b/src/easy/easy.hpp @@ -42,8 +42,44 @@ using std::chrono::system_clock; namespace Mastodon { // Defined at the bottom -typedef struct return_entity return_entity; -typedef struct return_entity_vector return_entity_vector; +// typedef struct return_entity return_entity; +// typedef struct return_entity_vector return_entity_vector; + +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 +{ + T entity; + + return_entity(); + return_entity(const uint8_t ec, const string &em, const T &ent); + + operator const T() const; + operator const string() const; + + friend std::ostream &operator <<(std::ostream &out, + const return_entity &ret); +}; +// template +// std::ostream &operator <<(std::ostream &out, +// const return_entity &ret); + +template +struct return_entity_vector : return_base +{ + vector entities; + + return_entity_vector(); + return_entity_vector(const uint8_t ec, const string &em, + const vector &vec); + + operator const vector() const; +}; /*! * @brief Child of Mastodon::API with abstract methods. @@ -291,14 +327,14 @@ public: * * @since 0.18.1 */ - const return_entity send_post(const Status &status); + const return_entity send_post(const Status &status); /*! * @brief Alias for send_post() * * @since 0.17.0 */ - const return_entity send_toot(const Status &status); + const return_entity send_toot(const Status &status); /*! * @brief Gets notifications. @@ -312,9 +348,9 @@ public: * * @since 0.21.0 */ - const return_entity_vector get_notifications(const uint16_t limit = 20, - const string since_id = "", - const string max_id = ""); + const return_entity_vector get_notifications( + const uint16_t limit = 20, const string since_id = "", + const string max_id = ""); /*! * @brief Base class for all entities. @@ -521,32 +557,42 @@ public: mutable bool _was_set; }; - /*! - * @brief Class to hold generic entities. - * - * @since 0.100.0 - */ - class GenericEntity : public Easy::Entity - { - public: - /*! - * @brief Constructs an GenericEntity object from a JSON string. - * - * @param json JSON string - * - * @since 0.100.0 - */ - explicit GenericEntity(const string &json); + // /*! + // * @brief Class to hold generic entities. + // * + // * @since 0.100.0 + // */ + // class GenericEntity : public Easy::Entity + // { + // public: + // /*! + // * @brief Constructs an GenericEntity object from a JSON string. + // * + // * @param json JSON string + // * + // * @since 0.100.0 + // */ + // explicit GenericEntity(const string &json); - /*! - * @brief Constructs an empty GenericEntity object. - * - * @since 0.100.0 - */ - explicit GenericEntity(); + // /*! + // * @brief Constructs an empty GenericEntity object. + // * + // * @since 0.100.0 + // */ + // explicit GenericEntity(); - virtual bool valid() const override; - }; + // virtual bool valid() const override; + // }; + + // template + // class GenericEntity : public Easy::Entity + // { + // public: + // explicit GenericEntity(const string &json); + // explicit GenericEntity(); + + // virtual bool valid() const override; + // }; protected: inline static const string strtime @@ -558,34 +604,34 @@ protected: * Return type for Easy calls, with an Easy::GenericEntity. * @since 0.100.0 */ -typedef struct return_entity : return_base -{ - Easy::GenericEntity entity; +// typedef struct return_entity : return_base +// { +// Easy::GenericEntity entity; - return_entity(); - return_entity(const uint8_t ec, const string &em, - const Easy::GenericEntity &ent); +// return_entity(); +// return_entity(const uint8_t ec, const string &em, +// const Easy::GenericEntity &ent); - operator const Easy::GenericEntity() const; - operator const string() const; - friend std::ostream &operator <<(std::ostream &out, - const return_entity &ret); -} return_entity; +// operator const Easy::GenericEntity() const; +// operator const string() const; +// friend std::ostream &operator <<(std::ostream &out, +// const return_entity &ret); +// } return_entity; -/*! - * Return type for Easy calls, with a vector of Easy::GenericEntity. - * @since 0.100.0 - */ -typedef struct return_entity_vector : return_base -{ - vector entities; +// /*! +// * Return type for Easy calls, with a vector of Easy::GenericEntity. +// * @since 0.100.0 +// */ +// typedef struct return_entity_vector : return_base +// { +// vector entities; - return_entity_vector(); - return_entity_vector(const uint8_t ec, const string &em, - const vector &vec); +// return_entity_vector(); +// return_entity_vector(const uint8_t ec, const string &em, +// const vector &vec); - operator const vector() const; -} return_entity_vector; +// operator const vector() const; +// } return_entity_vector; } #endif // MASTODON_EASY_CPP_HPP diff --git a/src/easy/entity.cpp b/src/easy/entity.cpp index 9aca9f0..a338889 100644 --- a/src/easy/entity.cpp +++ b/src/easy/entity.cpp @@ -1,6 +1,6 @@ /* This file is part of mastodon-cpp. * Copyright © 2018, 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. @@ -300,15 +300,28 @@ std::uint64_t Easy::Entity::stouint64(const string &str) const } } -Easy::GenericEntity::GenericEntity(const string &json) -: Entity(json) -{} +// Easy::GenericEntity::GenericEntity(const string &json) +// : Entity(json) +// {} -Easy::GenericEntity::GenericEntity() -: Entity() -{} +// Easy::GenericEntity::GenericEntity() +// : Entity() +// {} -bool Easy::GenericEntity::valid() const -{ - return true; -} +// bool Easy::GenericEntity::valid() const +// { +// return true; +// } + +// template GenericEntity::GenericEntity(const string &json) +// : Entity(json) +// {} + +// template GenericEntity::GenericEntity() +// : Entity() +// {} + +// template bool GenericEntity::valid() const +// { +// return true; +// } diff --git a/src/easy/simple_calls.cpp b/src/easy/simple_calls.cpp index e7a203b..18589a2 100644 --- a/src/easy/simple_calls.cpp +++ b/src/easy/simple_calls.cpp @@ -1,6 +1,6 @@ /* This file is part of mastodon-cpp. * Copyright © 2018, 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. @@ -23,12 +23,12 @@ using namespace Mastodon; -const return_entity Easy::send_toot(const Status &status) +const return_entity Easy::send_toot(const Status &status) { return send_post(status); } -const return_entity Easy::send_post(const Status &status) +const return_entity Easy::send_post(const Status &status) { API::parametermap parameters; @@ -39,7 +39,7 @@ const return_entity Easy::send_post(const Status &status) else { ttdebug << "ERROR: Easy::Status::content can not be empty.\n"; - return {22, "Easy::Status::content can not be empty", GenericEntity()}; + return {22, "Easy::Status::content can not be empty", Status()}; } if (!status.in_reply_to_id().empty()) @@ -95,7 +95,7 @@ const return_entity Easy::send_post(const Status &status) { ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n"; return { 22, "Easy::Attachment::file can not be empty", - GenericEntity() }; + Status() }; } if (!att.description().empty()) { @@ -118,7 +118,7 @@ const return_entity Easy::send_post(const Status &status) { ttdebug << "ERROR: Could not upload file.\n"; return { ret.error_code, ret.error_message, - GenericEntity(ret.answer) }; + Status(ret.answer) }; } } @@ -126,12 +126,11 @@ const return_entity Easy::send_post(const Status &status) } return_call ret = post(API::v1::statuses, parameters); - return { ret.error_code, ret.error_message, GenericEntity(ret.answer) }; + return { ret.error_code, ret.error_message, Status(ret.answer) }; } -const return_entity_vector Easy::get_notifications(const uint16_t limit, - const string since_id, - const string max_id) +const return_entity_vector Easy::get_notifications( + const uint16_t limit, const string since_id, const string max_id) { API::parametermap parameters; @@ -150,13 +149,13 @@ const return_entity_vector Easy::get_notifications(const uint16_t limit, if (ret.error_code == 0) { const vector &answer_v = json_array_to_vector(ret.answer); - vector notifications; + vector notifications; notifications.resize(answer_v.size()); // Transform vector of strings to vector of Notification. std::transform(answer_v.begin(), answer_v.end(), notifications.begin(), [](const string &s) - { return GenericEntity(s); }); + { return Notification(s); }); return { ret.error_code, ret.error_message, notifications }; }