mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-30 00:50:48 +08:00
32 lines
898 B
C++
32 lines
898 B
C++
#pragma once
|
|
#include <tuple>
|
|
#if __cplusplus > 201402L
|
|
#include <string_view>
|
|
using string_view = std::string_view;
|
|
#else
|
|
#include <boost/utility/string_view.hpp>
|
|
using string_view = boost::string_view;
|
|
#endif
|
|
#include "codec.h"
|
|
|
|
namespace rest_rpc {
|
|
inline bool has_error(string_view result) {
|
|
rpc_service::msgpack_codec codec;
|
|
auto tp = codec.unpack<std::tuple<int>>(result.data(), result.size());
|
|
|
|
return std::get<0>(tp) != 0;
|
|
}
|
|
|
|
template<typename T>
|
|
inline T get_result(string_view result) {
|
|
rpc_service::msgpack_codec codec;
|
|
auto tp = codec.unpack<std::tuple<int>>(result.data(), result.size());
|
|
if (std::get<0>(tp) != 0) {
|
|
auto ret = codec.unpack<std::tuple<int, std::string>>(result.data(), result.size());
|
|
throw std::logic_error(std::get<1>(ret));
|
|
}
|
|
|
|
auto err_tp = codec.unpack<std::tuple<int, T>>(result.data(), result.size());
|
|
return std::get<1>(err_tp);
|
|
}
|
|
} |