matrix_thing/include/matrix/netreturn.hpp

91 lines
2.7 KiB
C++

/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MATRIX_NETRETURN_HPP
#define MATRIX_NETRETURN_HPP
#include "raii/rjp_string.hpp"
#include <utility> //std::forward
namespace matrix{
class netreturn_base
{
protected:
raii::rjp_string m_error = {};
raii::rjp_string m_errorcode = {};
int m_http_error = 200;
public:
constexpr netreturn_base(void) = default;
netreturn_base(const raii::rjp_string& error, const raii::rjp_string& errorcode, int httpstatus);
netreturn_base(const raii::rjp_string& error, raii::rjp_string&& errorcode, int httpstatus);
netreturn_base(raii::rjp_string&& error, const raii::rjp_string& errorcode, int httpstatus);
netreturn_base(raii::rjp_string&& error, raii::rjp_string&& errorcode, int httpstatus);
bool ok(void)const;
const raii::rjp_string& mxerror(void)const;
const raii::rjp_string& mxerrorcode(void)const;
raii::rjp_string& mxerror(void);
raii::rjp_string& mxerrorcode(void);
int httpstatus(void)const;
bool has_httperror(void)const;
};
template<class Retval>
class netreturn : public netreturn_base
{
public:
using value_type = Retval;
private:
Retval m_ret = {};
public:
constexpr netreturn(void) = default;
template<class Str1, class Str2, class Val = Retval>
netreturn(Str1&& error, Str2&& errorcode, int httpcode, Val&& val = Val()):
netreturn_base(std::forward<Str1>(error), std::forward<Str2>(errorcode), httpcode),
m_ret(std::forward<Val>(val)){}
netreturn(netreturn_base&& n):
netreturn_base(std::move(n)){}
netreturn(const netreturn_base& n):
netreturn_base(n){}
Retval& value(void){
return m_ret;
}
const Retval& value(void)const{
return m_ret;
}
};
template<>
class netreturn<void> : public netreturn_base
{
public:
using value_type = void;
public:
template<class Str1, class Str2>
netreturn(Str1&& error, Str2&& errorcode, int httpcode):
netreturn_base(std::forward<Str1>(error), std::forward<Str2>(errorcode), httpcode){}
netreturn(netreturn_base&& n):
netreturn_base(std::move(n)){}
netreturn(const netreturn_base& n):
netreturn_base(n){}
};
}
#endif