Skip to content

Instantly share code, notes, and snippets.

@microcai
Last active August 29, 2015 14:01
Show Gist options
  • Save microcai/9489cd9d0aa7c6d540da to your computer and use it in GitHub Desktop.
Save microcai/9489cd9d0aa7c6d540da to your computer and use it in GitHub Desktop.
cfunc
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// insert
namespace trails_detail{
template <typename... T>
struct type_array {};
template <typename T, typename ArrayT>
struct type_insert;
template <typename T, typename... TypesT>
struct type_insert < T, type_array<TypesT...> >
{
typedef type_array<T, TypesT...> type;
};
// delete
template <size_t N, typename ArrayT>
struct type_delete;
template <typename T1, typename... T>
struct type_delete < 0, type_array<T1, T...> >
{
typedef type_array<T...> type;
};
template <typename T1, typename... T, size_t N>
struct type_delete < N, type_array<T1, T...> >
{
typedef typename type_insert < T1,
typename type_delete<N - 1,
type_array<T...>
>::type > ::type type;
};
// make function's parameters from a type_array
template <typename R, typename ArrayT>
struct make_func_par;
template <typename R, typename... P>
struct make_func_par < R, type_array<P...> >
{
typedef R type(P...);
};
// remove function's parameter
template <size_t N, typename F>
struct remove_func_par;
template <size_t N, typename R, typename... P>
struct remove_func_par < N, R(P...) >
{
typedef typename make_func_par < R,
typename type_delete<N, type_array<P...>
>::type > ::type type;
};
}
template<int index, typename F>
struct cfunction;
template<int index, typename R, typename... Args>
struct cfunction < index, R(Args...) >
{
typedef typename trails_detail::remove_func_par<index, R(Args...)>::type Type_for_boost_Func;
typedef typename std::function<Type_for_boost_Func> ClosureFuncType;
// std::function<_F> _f;
static R c_func(Args... args)
{
// 找到 index 的 args
std::tuple<Args...> t_args(args...);
void * p = std::get<index>(t_args);
// 然后 push 并调用
auto _f = reinterpret_cast<ClosureFuncType*>(p);
// (*_f)(args...);
}
};
int main(void)
{
trails_detail::remove_func_par<2, void(int, long, short)>::type* pp;
std::cout << typeid(trails_detail::remove_func_par<1, void(const char * message, void* _apitag)>::type).name() << std::endl;
typedef trails_detail::remove_func_par<1, void(const char * message, void* _apitag)>::type pt;
std::function<pt> p;
cfunction<1, void(const char * message, void* _apitag)> ppp;
cfunction<1, void(const char * message, void* _apitag)>::c_func("123", 0);
std::cout << typeid(decltype(p)).name() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment