Moved return_base and return_call to their own header.

This commit is contained in:
tastytea 2019-04-02 10:37:27 +02:00
parent c35b7f56fa
commit 38e8809b92
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 189 additions and 136 deletions

View File

@ -27,46 +27,6 @@
using namespace Mastodon;
return_base::operator bool()
{
if (error_code == 0)
{
return true;
}
else
{
return false;
}
}
return_base::operator uint8_t()
{
return error_code;
}
return_call::operator const string() const
{
return answer;
}
std::ostream &Mastodon::operator <<(std::ostream &out, const return_call &ret)
{
out << ret.answer;
return out;
}
return_call::return_call()
{}
return_call::return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a)
: http_error_code(hec)
, answer(a)
{
error_code = ec;
error_message = em;
}
API::API(const string &instance, const string &access_token)
: _instance(instance)
, _access_token(access_token)
@ -310,7 +270,7 @@ const string API::unescape_html(const string &html)
// Matches numbered entities between 1 and 8 digits, decimal or hexadecimal
std::regex re_entity("&#(x)?(\\d{1,8});");
std::smatch match;
while (std::regex_search(buffer, match, re_entity))
{
char32_t codepoint = 0;

View File

@ -19,7 +19,6 @@
#include <string>
#include <vector>
#include <cstdint>
#include <map>
#include <memory>
#include <array>
@ -28,8 +27,13 @@
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
using std::uint8_t;
using std::uint16_t;
// If we are compiling mastodon-cpp, use another include path
#ifdef MASTODON_CPP
#include "return_types.hpp"
#else
#include <mastodon-cpp/return_types.hpp>
#endif
using std::string;
/*!
@ -38,98 +42,6 @@ using std::string;
namespace Mastodon
{
/*!
* @brief Basis for return types.
*
* @since 0.100.0
*/
typedef struct return_base
{
/*!
* @brief @ref error "Error code".
*
* @since 0.100.0
*/
uint8_t error_code = 0;
/*!
* @brief The error message, or "".
*
* @since 0.100.0
*/
string error_message;
/*!
* @brief true if return_base::error_code is 0, otherwise false.
*
* @since 0.100.0
*/
operator bool();
/*!
* @brief Same as return_base::error_code.
*
* @since 0.100.0
*/
operator uint8_t();
} return_base;
/*!
* @brief Return type for API calls.
*
* Example:
* @code
* Mastodon::return_call ret = masto.get(Mastodon::API::v1::instance);
* if (!ret) // Or ret.error_code != 0
* {
* cout << "Error " << std::to_string(ret.error_code);
* cout << " (HTTP " << std::to_string(ret.http_error_code) << "): ";
* cout << ret.error_message << endl
* }
* else
* {
* cout << ret << endl; // Or ret.answer
* }
* @endcode
*
* @since 0.100.0
*/
typedef struct return_call : return_base
{
/*!
* @brief HTTP error code.
*
* @since 0.100.0
*/
uint16_t http_error_code = 0;
/*!
* @brief The response from the server.
*
* @since 0.100.0
*/
string answer;
return_call();
return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a);
/*!
* @brief Same es return_call::answer.
*
* @since 0.100.0
*/
operator const string() const;
/*!
* @brief Same es return_call::answer.
*
* @since 0.100.0
*/
friend std::ostream &operator <<(std::ostream &out,
const return_call &ret);
} return_call;
/*!
* @brief Interface to the Mastodon API.
*

59
src/return_types.cpp Normal file
View File

@ -0,0 +1,59 @@
/* This file is part of mastodon-cpp.
* Copyright © 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.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "return_types.hpp"
using namespace Mastodon;
return_base::operator bool()
{
if (error_code == 0)
{
return true;
}
else
{
return false;
}
}
return_base::operator uint8_t()
{
return error_code;
}
return_call::operator const string() const
{
return answer;
}
std::ostream &Mastodon::operator <<(std::ostream &out, const return_call &ret)
{
out << ret.answer;
return out;
}
return_call::return_call()
{}
return_call::return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a)
: http_error_code(hec)
, answer(a)
{
error_code = ec;
error_message = em;
}

122
src/return_types.hpp Normal file
View File

@ -0,0 +1,122 @@
/* This file is part of mastodon-cpp.
* Copyright © 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.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef MASTODON_CPP_RETURN_TYPES_HPP
#define MASTODON_CPP_RETURN_TYPES_HPP
#include <cstdint>
#include <string>
using std::uint8_t;
using std::uint16_t;
using std::string;
namespace Mastodon
{
/*!
* @brief Basis for return types.
*
* @since 0.100.0
*/
typedef struct return_base
{
/*!
* @brief @ref error "Error code".
*
* @since 0.100.0
*/
uint8_t error_code = 0;
/*!
* @brief The error message, or "".
*
* @since 0.100.0
*/
string error_message;
/*!
* @brief true if return_base::error_code is 0, otherwise false.
*
* @since 0.100.0
*/
operator bool();
/*!
* @brief Same as return_base::error_code.
*
* @since 0.100.0
*/
operator uint8_t();
} return_base;
/*!
* @brief Return type for API calls.
*
* Example:
* @code
* Mastodon::return_call ret = masto.get(Mastodon::API::v1::instance);
* if (!ret) // Or ret.error_code != 0
* {
* cout << "Error " << std::to_string(ret.error_code);
* cout << " (HTTP " << std::to_string(ret.http_error_code) << "): ";
* cout << ret.error_message << endl
* }
* else
* {
* cout << ret << endl; // Or ret.answer
* }
* @endcode
*
* @since 0.100.0
*/
typedef struct return_call : return_base
{
/*!
* @brief HTTP error code.
*
* @since 0.100.0
*/
uint16_t http_error_code = 0;
/*!
* @brief The response from the server.
*
* @since 0.100.0
*/
string answer;
return_call();
return_call(const uint8_t ec, const string &em,
const uint16_t hec, const string &a);
/*!
* @brief Same es return_call::answer.
*
* @since 0.100.0
*/
operator const string() const;
/*!
* @brief Same es return_call::answer.
*
* @since 0.100.0
*/
friend std::ostream &operator <<(std::ostream &out,
const return_call &ret);
} return_call;
}
#endif // MASTODON_CPP_RETURN_TYPES_HPP