diff --git a/src/easy/return_types_easy.hpp b/src/easy/return_types_easy.hpp new file mode 100644 index 0000000..beb42f3 --- /dev/null +++ b/src/easy/return_types_easy.hpp @@ -0,0 +1,121 @@ +/* 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 . + */ + +#ifndef RETURN_TYPES_EASY_HPP +#define RETURN_TYPES_EASY_HPP + +#include +#include +#include +#include "mastodon-cpp.hpp" + +using std::string; +using std::vector; + +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 +{ + 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 +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; +}; + + +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; +} +} + +#endif // RETURN_TYPES_EASY_HPP