Templated return_entity and return_entity_vector.
Some checks failed
the build failed

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.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
*
* 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<typename T>
return_entity<T>::return_entity()
: entity()
{}
return_entity::return_entity(const uint8_t ec, const string &em,
const Easy::GenericEntity &ent)
: entity(ent)
template<typename T>
return_entity<T>::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<typename T>
return_entity<T>::return_entity::operator const T() const
{
return entity;
}
return_entity::operator const string() const
template<typename T>
return_entity<T>::return_entity::operator const string() const
{
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();
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()
{}
return_entity_vector::return_entity_vector(const uint8_t ec, const string &em,
const vector<Easy::GenericEntity> &vec)
template<typename T>
return_entity_vector<T>::return_entity_vector::return_entity_vector(
const uint8_t ec, const string &em, const vector<T> &vec)
: entities(vec)
{
error_code = ec;
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;
}

View File

@ -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 <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.
@ -291,14 +327,14 @@ public:
*
* @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()
*
* @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.
@ -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<Easy::Notification> 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 <typename T>
// 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<Easy::GenericEntity> entities;
// /*!
// * Return type for Easy calls, with a vector of Easy::GenericEntity.
// * @since 0.100.0
// */
// typedef struct return_entity_vector : return_base
// {
// vector<Easy::GenericEntity> entities;
return_entity_vector();
return_entity_vector(const uint8_t ec, const string &em,
const vector<Easy::GenericEntity> &vec);
// return_entity_vector();
// return_entity_vector(const uint8_t ec, const string &em,
// const vector<Easy::GenericEntity> &vec);
operator const vector<Easy::GenericEntity>() const;
} return_entity_vector;
// operator const vector<Easy::GenericEntity>() const;
// } return_entity_vector;
}
#endif // MASTODON_EASY_CPP_HPP

View File

@ -1,6 +1,6 @@
/* This file is part of mastodon-cpp.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
*
* 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<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.
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
*
*
* 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::Status> Easy::send_toot(const Status &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;
@ -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::Notification> 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<string> &answer_v = json_array_to_vector(ret.answer);
vector<GenericEntity> notifications;
vector<Notification> 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 };
}