Templated return_entity and return_entity_vector.
the build failed Details

This commit is contained in:
tastytea 2019-03-28 13:33:28 +01:00
parent 552c92a1ef
commit 12d986ed06
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 201 additions and 90 deletions

View File

@ -1,6 +1,6 @@
/* This file is part of mastodon-cpp. /* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3. * the Free Software Foundation, version 3.
@ -25,47 +25,100 @@
using namespace Mastodon; using namespace Mastodon;
using std::string; using std::string;
return_entity::return_entity() // return_entity::return_entity()
: 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<typename T>
return_entity<T>::return_entity()
: entity()
{} {}
return_entity::return_entity(const uint8_t ec, const string &em, template<typename T>
const Easy::GenericEntity &ent) return_entity<T>::return_entity(const uint8_t ec, const string &em,
: entity(ent) const T &ent)
: entity(ent)
{ {
error_code = ec; error_code = ec;
error_message = em; error_message = em;
} }
return_entity::operator const Easy::GenericEntity() const template<typename T>
return_entity<T>::return_entity::operator const T() const
{ {
return entity; return entity;
} }
return_entity::operator const string() const template<typename T>
return_entity<T>::return_entity::operator const string() const
{ {
return entity.to_string(); return entity.to_string();
} }
std::ostream &Mastodon::operator <<(std::ostream &out, const return_entity &ret) template<typename T>
std::ostream &operator <<(std::ostream &out, const return_entity<T> &ret)
{ {
out << ret.entity.to_string(); out << ret.entity.to_string();
return out; 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<Easy::GenericEntity> &vec)
// : entities(vec)
// {
// error_code = ec;
// error_message = em;
// }
// return_entity_vector::operator const vector<Easy::GenericEntity>() const
// {
// return entities;
// }
template<typename T>
return_entity_vector<T>::return_entity_vector::return_entity_vector()
: entities() : entities()
{} {}
return_entity_vector::return_entity_vector(const uint8_t ec, const string &em, template<typename T>
const vector<Easy::GenericEntity> &vec) return_entity_vector<T>::return_entity_vector::return_entity_vector(
const uint8_t ec, const string &em, const vector<T> &vec)
: entities(vec) : entities(vec)
{ {
error_code = ec; error_code = ec;
error_message = em; error_message = em;
} }
return_entity_vector::operator const vector<Easy::GenericEntity>() const template<typename T>
return_entity_vector<T>::return_entity_vector::operator const vector<T>() const
{ {
return entities; return entities;
} }

View File

@ -42,8 +42,44 @@ using std::chrono::system_clock;
namespace Mastodon namespace Mastodon
{ {
// Defined at the bottom // Defined at the bottom
typedef struct return_entity return_entity; // typedef struct return_entity return_entity;
typedef struct return_entity_vector return_entity_vector; // typedef struct return_entity_vector return_entity_vector;
template <typename T>
struct return_entity;
// https://stackoverflow.com/a/4661372/5965450
template <typename T>
std::ostream &operator <<(std::ostream&, const return_entity<T>&);
template <typename T>
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 <<<T>(std::ostream &out,
const return_entity<T> &ret);
};
// template <typename T>
// std::ostream &operator <<(std::ostream &out,
// const return_entity<T> &ret);
template <typename T>
struct return_entity_vector : return_base
{
vector<T> entities;
return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em,
const vector<T> &vec);
operator const vector<T>() const;
};
/*! /*!
* @brief Child of Mastodon::API with abstract methods. * @brief Child of Mastodon::API with abstract methods.
@ -291,14 +327,14 @@ public:
* *
* @since 0.18.1 * @since 0.18.1
*/ */
const return_entity send_post(const Status &status); const return_entity<Easy::Status> send_post(const Status &status);
/*! /*!
* @brief Alias for send_post() * @brief Alias for send_post()
* *
* @since 0.17.0 * @since 0.17.0
*/ */
const return_entity send_toot(const Status &status); const return_entity<Easy::Status> send_toot(const Status &status);
/*! /*!
* @brief Gets notifications. * @brief Gets notifications.
@ -312,9 +348,9 @@ public:
* *
* @since 0.21.0 * @since 0.21.0
*/ */
const return_entity_vector get_notifications(const uint16_t limit = 20, const return_entity_vector<Easy::Notification> get_notifications(
const string since_id = "", const uint16_t limit = 20, const string since_id = "",
const string max_id = ""); const string max_id = "");
/*! /*!
* @brief Base class for all entities. * @brief Base class for all entities.
@ -521,32 +557,42 @@ public:
mutable bool _was_set; mutable bool _was_set;
}; };
/*! // /*!
* @brief Class to hold generic entities. // * @brief Class to hold generic entities.
* // *
* @since 0.100.0 // * @since 0.100.0
*/ // */
class GenericEntity : public Easy::Entity // class GenericEntity : public Easy::Entity
{ // {
public: // public:
/*! // /*!
* @brief Constructs an GenericEntity object from a JSON string. // * @brief Constructs an GenericEntity object from a JSON string.
* // *
* @param json JSON string // * @param json JSON string
* // *
* @since 0.100.0 // * @since 0.100.0
*/ // */
explicit GenericEntity(const string &json); // explicit GenericEntity(const string &json);
/*! // /*!
* @brief Constructs an empty GenericEntity object. // * @brief Constructs an empty GenericEntity object.
* // *
* @since 0.100.0 // * @since 0.100.0
*/ // */
explicit GenericEntity(); // explicit GenericEntity();
virtual bool valid() const override; // virtual bool valid() const override;
}; // };
// template <typename T>
// class GenericEntity : public Easy::Entity
// {
// public:
// explicit GenericEntity(const string &json);
// explicit GenericEntity();
// virtual bool valid() const override;
// };
protected: protected:
inline static const string strtime inline static const string strtime
@ -558,34 +604,34 @@ protected:
* Return type for Easy calls, with an Easy::GenericEntity. * Return type for Easy calls, with an Easy::GenericEntity.
* @since 0.100.0 * @since 0.100.0
*/ */
typedef struct return_entity : return_base // typedef struct return_entity : return_base
{ // {
Easy::GenericEntity entity; // Easy::GenericEntity entity;
return_entity(); // return_entity();
return_entity(const uint8_t ec, const string &em, // return_entity(const uint8_t ec, const string &em,
const Easy::GenericEntity &ent); // const Easy::GenericEntity &ent);
operator const Easy::GenericEntity() const; // operator const Easy::GenericEntity() const;
operator const string() const; // operator const string() const;
friend std::ostream &operator <<(std::ostream &out, // friend std::ostream &operator <<(std::ostream &out,
const return_entity &ret); // const return_entity &ret);
} return_entity; // } return_entity;
/*! // /*!
* Return type for Easy calls, with a vector of Easy::GenericEntity. // * Return type for Easy calls, with a vector of Easy::GenericEntity.
* @since 0.100.0 // * @since 0.100.0
*/ // */
typedef struct return_entity_vector : return_base // typedef struct return_entity_vector : return_base
{ // {
vector<Easy::GenericEntity> entities; // vector<Easy::GenericEntity> entities;
return_entity_vector(); // return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em, // return_entity_vector(const uint8_t ec, const string &em,
const vector<Easy::GenericEntity> &vec); // const vector<Easy::GenericEntity> &vec);
operator const vector<Easy::GenericEntity>() const; // operator const vector<Easy::GenericEntity>() const;
} return_entity_vector; // } return_entity_vector;
} }
#endif // MASTODON_EASY_CPP_HPP #endif // MASTODON_EASY_CPP_HPP

View File

@ -1,6 +1,6 @@
/* This file is part of mastodon-cpp. /* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3. * 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) // Easy::GenericEntity::GenericEntity(const string &json)
: Entity(json) // : Entity(json)
{} // {}
Easy::GenericEntity::GenericEntity() // Easy::GenericEntity::GenericEntity()
: Entity() // : Entity()
{} // {}
bool Easy::GenericEntity::valid() const // bool Easy::GenericEntity::valid() const
{ // {
return true; // return true;
} // }
// template<typename T> GenericEntity<T>::GenericEntity(const string &json)
// : Entity(json)
// {}
// template<typename T> GenericEntity<T>::GenericEntity()
// : Entity()
// {}
// template<typename T> bool GenericEntity<T>::valid() const
// {
// return true;
// }

View File

@ -1,6 +1,6 @@
/* This file is part of mastodon-cpp. /* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de> * Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3. * the Free Software Foundation, version 3.
@ -23,12 +23,12 @@
using namespace Mastodon; using namespace Mastodon;
const return_entity Easy::send_toot(const Status &status) const return_entity<Easy::Status> Easy::send_toot(const Status &status)
{ {
return send_post(status); return send_post(status);
} }
const return_entity Easy::send_post(const Status &status) const return_entity<Easy::Status> Easy::send_post(const Status &status)
{ {
API::parametermap parameters; API::parametermap parameters;
@ -39,7 +39,7 @@ const return_entity Easy::send_post(const Status &status)
else else
{ {
ttdebug << "ERROR: Easy::Status::content can not be empty.\n"; 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()) 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"; ttdebug << "ERROR: Easy::Attachment::file can not be empty.\n";
return { 22, "Easy::Attachment::file can not be empty", return { 22, "Easy::Attachment::file can not be empty",
GenericEntity() }; Status() };
} }
if (!att.description().empty()) if (!att.description().empty())
{ {
@ -118,7 +118,7 @@ const return_entity Easy::send_post(const Status &status)
{ {
ttdebug << "ERROR: Could not upload file.\n"; ttdebug << "ERROR: Could not upload file.\n";
return { ret.error_code, ret.error_message, 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_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 return_entity_vector<Easy::Notification> Easy::get_notifications(
const string since_id, const uint16_t limit, const string since_id, const string max_id)
const string max_id)
{ {
API::parametermap parameters; API::parametermap parameters;
@ -150,13 +149,13 @@ const return_entity_vector Easy::get_notifications(const uint16_t limit,
if (ret.error_code == 0) if (ret.error_code == 0)
{ {
const vector<string> &answer_v = json_array_to_vector(ret.answer); const vector<string> &answer_v = json_array_to_vector(ret.answer);
vector<GenericEntity> notifications; vector<Notification> notifications;
notifications.resize(answer_v.size()); notifications.resize(answer_v.size());
// Transform vector of strings to vector of Notification. // Transform vector of strings to vector of Notification.
std::transform(answer_v.begin(), answer_v.end(), notifications.begin(), std::transform(answer_v.begin(), answer_v.end(), notifications.begin(),
[](const string &s) [](const string &s)
{ return GenericEntity(s); }); { return Notification(s); });
return { ret.error_code, ret.error_message, notifications }; return { ret.error_code, ret.error_message, notifications };
} }