我有以下代码(或类似的东西)来生成绑定表达式。我使用这些表达式以及boost.function_types& luabind.tag_function将我们框架中的一些函数暴露给lua。
#define BOOST_BIND_ARG(J, I, D) BOOST_PP_CAT(_, BOOST_PP_INC(I))
#define MAKE_BIND_EXPR_GEN(_, N, __) \
template < > \
struct MakeBindExpr < N > \
{ \
template < typename FP > \
struct result \
{ \
typedef BOOST_TYPEOF_TPL(boost::bind( \
FP(0), BOOST_PP_ENUM(N, BOOST_BIND_ARG, BOOST_PP_EMPTY) \
)) type; \
}; \
\
template < typename FP > \
static typename result < FP >::type get(FP const & f) \
{ \
return boost::bind(f, BOOST_PP_ENUM(N, BOOST_BIND_ARG, BOOST_PP_EMPTY)); \
} \
}; \
BOOST_PP_REPEAT_FROM_TO(0, 9, MAKE_BIND_EXPR_GEN, _)
这很有效。但是,实现MakeBindExpr是否有一种不那么丑陋的方式?
以下是我现在能做的......如果你有兴趣知道......
// N is arity
// F is function type
// FP is function pointer type
template < typename N, typename FP >
typename MakeBindExpr < N >::template result < FP >::type makeBindExpr(FP const & fp);
// FP is a function pointer type
template < typename FP >
void bind2Lua(char const * const fname, FP const & f)
{
namespace ft = boost::function_types;
using ft::function_type;
using ft::function_arity;
typedef function_type < FP >::type F;
typedef function_arity < F >::type Arity;
luabind::tag_function < F >(makeBindExpr < Arity::value >::template get(f));
}
deque < string >(string const &, string const &);
bind2Lua("names", getNames);