mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
remove some
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#include "error_code.h"
|
||||
#include "io_context_pool.hpp"
|
||||
#include "logger.hpp"
|
||||
#include "meta_util.hpp"
|
||||
// #include "meta_util.hpp"
|
||||
#include "rest_rpc_protocol.hpp"
|
||||
#include "string_resize.hpp"
|
||||
#include "traits.h"
|
||||
@@ -14,6 +14,8 @@
|
||||
#include <asio/steady_timer.hpp>
|
||||
using namespace asio::experimental::awaitable_operators;
|
||||
|
||||
using namespace rest_rpc::util;
|
||||
|
||||
namespace rest_rpc {
|
||||
template <typename R> struct call_result {
|
||||
rpc_errc ec;
|
||||
@@ -100,7 +102,7 @@ public:
|
||||
asio::awaitable<
|
||||
call_result<typename function_traits<decltype(func)>::return_type>>
|
||||
call_for(auto duration, Args &&...args) {
|
||||
using args_tuple = typename function_traits<decltype(func)>::tuple_type;
|
||||
using args_tuple = typename function_traits<decltype(func)>::parameters_type;
|
||||
static_assert(std::is_constructible_v<args_tuple, Args...>,
|
||||
"called rpc function and arguments are not match");
|
||||
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
#ifndef REST_RPC_CPLUSPLUS_14_H_
|
||||
#define REST_RPC_CPLUSPLUS_14_H_
|
||||
|
||||
#include <memory>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
|
||||
namespace nonstd {
|
||||
template <class T> struct unique_if {
|
||||
typedef std::unique_ptr<T> single_object;
|
||||
};
|
||||
|
||||
template <class T> struct unique_if<T[]> {
|
||||
typedef std::unique_ptr<T[]> unknown_bound;
|
||||
};
|
||||
|
||||
template <class T, size_t N> struct unique_if<T[N]> {
|
||||
typedef void known_bound;
|
||||
};
|
||||
|
||||
template <class T, class... Args>
|
||||
typename unique_if<T>::single_object make_unique(Args &&...args) {
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template <class T> typename unique_if<T>::unknown_bound make_unique(size_t n) {
|
||||
typedef typename std::remove_extent<T>::type U;
|
||||
return std::unique_ptr<T>(new U[n]());
|
||||
}
|
||||
|
||||
template <class T, class... Args>
|
||||
typename unique_if<T>::known_bound make_unique(Args &&...) = delete;
|
||||
|
||||
template <size_t... Ints> struct index_sequence {
|
||||
using type = index_sequence;
|
||||
using value_type = size_t;
|
||||
static constexpr std::size_t size() noexcept { return sizeof...(Ints); }
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------
|
||||
|
||||
template <class Sequence1, class Sequence2> struct _merge_and_renumber;
|
||||
|
||||
template <size_t... I1, size_t... I2>
|
||||
struct _merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>>
|
||||
: index_sequence<I1..., (sizeof...(I1) + I2)...> {};
|
||||
|
||||
// --------------------------------------------------------------
|
||||
|
||||
template <size_t N>
|
||||
struct make_index_sequence
|
||||
: _merge_and_renumber<typename make_index_sequence<N / 2>::type,
|
||||
typename make_index_sequence<N - N / 2>::type> {};
|
||||
|
||||
template <> struct make_index_sequence<0> : index_sequence<> {};
|
||||
template <> struct make_index_sequence<1> : index_sequence<0> {};
|
||||
|
||||
template <typename... T>
|
||||
using index_sequence_for = make_index_sequence<sizeof...(T)>;
|
||||
|
||||
template <bool B, class T = void>
|
||||
using enable_if_t = typename std::enable_if<B, T>::type;
|
||||
|
||||
template <typename T>
|
||||
using remove_const_t = typename std::remove_const<T>::type;
|
||||
|
||||
template <typename T>
|
||||
using remove_reference_t = typename std::remove_reference<T>::type;
|
||||
|
||||
template <int I, typename T>
|
||||
using tuple_element_t = typename std::tuple_element<I, T>::type;
|
||||
|
||||
template <typename T> using decay_t = typename std::decay<T>::type;
|
||||
|
||||
template <typename F, typename Tuple, size_t... Idx>
|
||||
auto apply_helper(F &&f, Tuple &&tp, nonstd::index_sequence<Idx...>)
|
||||
-> decltype(std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(tp))...)) {
|
||||
return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(tp))...);
|
||||
}
|
||||
|
||||
template <typename F, typename Tuple>
|
||||
auto apply(F &&f, Tuple &&tp)
|
||||
-> decltype(apply_helper(
|
||||
std::forward<F>(f), std::forward<Tuple>(tp),
|
||||
make_index_sequence<std::tuple_size<decay_t<Tuple>>::value>{})) {
|
||||
return apply_helper(
|
||||
std::forward<F>(f), std::forward<Tuple>(tp),
|
||||
make_index_sequence<std::tuple_size<decay_t<Tuple>>::value>{});
|
||||
}
|
||||
|
||||
template <typename F, typename... Args>
|
||||
auto invoke(F &&f, Args &&...args)
|
||||
-> decltype(std::forward<F>(f)(std::forward<Args>(args)...)) {
|
||||
return std::forward<F>(f)(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // namespace nonstd
|
||||
|
||||
#endif // REST_RPC_CPLUSPLUS_14_H_
|
||||
@@ -1,138 +0,0 @@
|
||||
#ifndef REST_RPC_META_UTIL_HPP
|
||||
#define REST_RPC_META_UTIL_HPP
|
||||
|
||||
#include "cplusplus_14.h"
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rest_rpc {
|
||||
|
||||
template <typename... Args, typename Func, std::size_t... Idx>
|
||||
void for_each(const std::tuple<Args...> &t, Func &&f,
|
||||
nonstd::index_sequence<Idx...>) {
|
||||
(void)std::initializer_list<int>{(f(std::get<Idx>(t)), void(), 0)...};
|
||||
}
|
||||
|
||||
template <typename... Args, typename Func, std::size_t... Idx>
|
||||
void for_each_i(const std::tuple<Args...> &t, Func &&f,
|
||||
nonstd::index_sequence<Idx...>) {
|
||||
(void)std::initializer_list<int>{
|
||||
(f(std::get<Idx>(t), std::integral_constant<size_t, Idx>{}), void(),
|
||||
0)...};
|
||||
}
|
||||
|
||||
template <typename T> struct function_traits;
|
||||
|
||||
template <typename Ret, typename Arg, typename... Args>
|
||||
struct function_traits<Ret(Arg, Args...)> {
|
||||
public:
|
||||
enum { arity = sizeof...(Args) + 1 };
|
||||
typedef Ret function_type(Arg, Args...);
|
||||
typedef std::remove_cvref_t<Ret> return_type;
|
||||
using stl_function_type = std::function<function_type>;
|
||||
typedef Ret (*pointer)(Arg, Args...);
|
||||
|
||||
typedef std::tuple<std::remove_const_t<std::remove_reference_t<Arg>>,
|
||||
std::remove_const_t<std::remove_reference_t<Args>>...>
|
||||
tuple_type;
|
||||
typedef std::tuple<
|
||||
nonstd::remove_const_t<nonstd::remove_reference_t<Args>>...>
|
||||
bare_tuple_type;
|
||||
using args_tuple =
|
||||
std::tuple<std::string,
|
||||
nonstd::remove_const_t<nonstd::remove_reference_t<Arg>>,
|
||||
nonstd::remove_const_t<nonstd::remove_reference_t<Args>>...>;
|
||||
using args_tuple_2nd =
|
||||
std::tuple<std::string,
|
||||
nonstd::remove_const_t<nonstd::remove_reference_t<Args>>...>;
|
||||
};
|
||||
|
||||
template <typename Ret> struct function_traits<Ret()> {
|
||||
public:
|
||||
enum { arity = 0 };
|
||||
typedef Ret function_type();
|
||||
typedef std::remove_cvref_t<Ret> return_type;
|
||||
using stl_function_type = std::function<function_type>;
|
||||
typedef Ret (*pointer)();
|
||||
|
||||
typedef std::tuple<> tuple_type;
|
||||
typedef std::tuple<> bare_tuple_type;
|
||||
using args_tuple = std::tuple<std::string>;
|
||||
using args_tuple_2nd = std::tuple<std::string>;
|
||||
};
|
||||
|
||||
template <typename Ret, typename... Args>
|
||||
struct function_traits<Ret (*)(Args...)> : function_traits<Ret(Args...)> {};
|
||||
|
||||
template <typename Ret, typename... Args>
|
||||
struct function_traits<std::function<Ret(Args...)>>
|
||||
: function_traits<Ret(Args...)> {};
|
||||
|
||||
template <typename ReturnType, typename ClassType, typename... Args>
|
||||
struct function_traits<ReturnType (ClassType::*)(Args...)>
|
||||
: function_traits<ReturnType(Args...)> {
|
||||
using class_type = ClassType;
|
||||
};
|
||||
|
||||
template <typename ReturnType, typename ClassType, typename... Args>
|
||||
struct function_traits<ReturnType (ClassType::*)(Args...) const>
|
||||
: function_traits<ReturnType(Args...)> {
|
||||
using class_type = ClassType;
|
||||
};
|
||||
|
||||
template <typename Callable>
|
||||
struct function_traits : function_traits<decltype(&Callable::operator())> {};
|
||||
|
||||
template <typename T>
|
||||
using remove_const_reference_t =
|
||||
nonstd::remove_const_t<nonstd::remove_reference_t<T>>;
|
||||
|
||||
template <size_t... Is>
|
||||
auto make_tuple_from_sequence(nonstd::index_sequence<Is...>)
|
||||
-> decltype(std::make_tuple(Is...)) {
|
||||
std::make_tuple(Is...);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
constexpr auto make_tuple_from_sequence()
|
||||
-> decltype(make_tuple_from_sequence(nonstd::make_index_sequence<N>{})) {
|
||||
return make_tuple_from_sequence(nonstd::make_index_sequence<N>{});
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template <class Tuple, class F, std::size_t... Is>
|
||||
void tuple_switch(const std::size_t i, Tuple &&t, F &&f,
|
||||
nonstd::index_sequence<Is...>) {
|
||||
(void)std::initializer_list<int>{
|
||||
(i == Is &&
|
||||
((void)std::forward<F>(f)(std::integral_constant<size_t, Is>{}), 0))...};
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template <class Tuple, class F>
|
||||
inline void tuple_switch(const std::size_t i, Tuple &&t, F &&f) {
|
||||
constexpr auto N = std::tuple_size<nonstd::remove_reference_t<Tuple>>::value;
|
||||
|
||||
detail::tuple_switch(i, std::forward<Tuple>(t), std::forward<F>(f),
|
||||
nonstd::make_index_sequence<N>{});
|
||||
}
|
||||
|
||||
template <int N, typename... Args>
|
||||
using nth_type_of = nonstd::tuple_element_t<N, std::tuple<Args...>>;
|
||||
|
||||
template <typename... Args>
|
||||
using last_type_of = nth_type_of<sizeof...(Args) - 1, Args...>;
|
||||
|
||||
template <typename T> struct remove_first {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template <class First, class... Second>
|
||||
struct remove_first<std::tuple<First, Second...>> {
|
||||
using type = std::tuple<Second...>;
|
||||
};
|
||||
|
||||
template <typename T> using remove_first_t = typename remove_first<T>::type;
|
||||
} // namespace rest_rpc
|
||||
|
||||
#endif // REST_RPC_META_UTIL_HPP
|
||||
@@ -178,7 +178,7 @@ private:
|
||||
// zero or one arguments
|
||||
template <auto func, typename... Args>
|
||||
asio::awaitable<std::error_code> rpc_context::response(Args &&...args) {
|
||||
using args_tuple = typename function_traits<decltype(func)>::return_type;
|
||||
using args_tuple = typename util::function_traits<decltype(func)>::return_type;
|
||||
static_assert(
|
||||
std::is_constructible_v<args_tuple, Args...>,
|
||||
"rpc function return type and response arguments are not match");
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include "codec.h"
|
||||
#include "error_code.h"
|
||||
|
||||
#include "meta_util.hpp"
|
||||
#include "util.hpp"
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
@@ -118,7 +117,7 @@ private:
|
||||
this->map_invokers_[key] =
|
||||
[this, f, self](std::string_view str,
|
||||
rpc_result &ret) -> asio::awaitable<void> {
|
||||
using args_tuple = typename function_traits<Function>::tuple_type;
|
||||
using args_tuple = typename util::function_traits<Function>::parameters_type;
|
||||
using R = typename util::function_traits<Function>::return_type;
|
||||
try {
|
||||
if constexpr (std::tuple_size_v<args_tuple> == 0) {
|
||||
|
||||
+10
-10
@@ -58,59 +58,59 @@ struct function_traits<Return (This::*)(Arguments...) const noexcept> {
|
||||
};
|
||||
|
||||
template <typename Return> struct function_traits<Return (*)()> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
};
|
||||
|
||||
template <typename Return> struct function_traits<Return (*)() noexcept> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
};
|
||||
|
||||
template <typename Return> struct function_traits<Return (&)()> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
};
|
||||
|
||||
template <typename Return> struct function_traits<Return (&)() noexcept> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
};
|
||||
|
||||
template <typename Return> struct function_traits<Return()> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
};
|
||||
|
||||
template <typename Return> struct function_traits<Return() noexcept> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
};
|
||||
|
||||
template <typename This, typename Return>
|
||||
struct function_traits<Return (This::*)()> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
using class_type = This;
|
||||
};
|
||||
|
||||
template <typename This, typename Return>
|
||||
struct function_traits<Return (This::*)() noexcept> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
using class_type = This;
|
||||
};
|
||||
|
||||
template <typename This, typename Return>
|
||||
struct function_traits<Return (This::*)() const> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
using class_type = This;
|
||||
};
|
||||
|
||||
template <typename This, typename Return>
|
||||
struct function_traits<Return (This::*)() const noexcept> {
|
||||
using parameters_type = void;
|
||||
using parameters_type = std::tuple<>;
|
||||
using return_type = Return;
|
||||
using class_type = This;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user