Style fixes and fixes for templates not usually used by MSVC (broken two-phase lookup killing me here. :c)

Member functions now work as well for set_function. If performance of `new`ing a type ever becomes too large, we can create a custom allocator for the std::shared_ptr's of the types. We can also up-front allocate for the unordered_map as well.
This commit is contained in:
ThePhD 2013-12-02 18:11:25 -05:00
parent be98a4fdd4
commit 29f171e124
5 changed files with 135 additions and 106 deletions

View File

@ -30,70 +30,70 @@ namespace detail {
template<typename TFuncSignature> template<typename TFuncSignature>
struct function_traits; struct function_traits;
template<typename T, typename R, typename... Tn> template<typename T, typename R, typename... Args>
struct function_traits<R(T::*)(Tn...)> { struct function_traits<R(T::*)(Args...)> {
static const std::size_t arity = sizeof...(Tn); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = true; static const bool is_member_function = true;
typedef std::tuple<Tn...> arg_tuple_t; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Tn...> args_t; typedef types<Args...> args_type;
typedef R(T::* func_t)(Tn...); typedef R(T::* func_pointer_type)(Args...);
typedef R(T::* func_pointer_t)(Tn...); typedef typename std::remove_pointer<func_pointer_type>::type func_type;
typedef R return_t; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg_n = std::tuple_element<i, arg_tuple_t>; using arg_n = std::tuple_element<i, arg_tuple_type>;
}; };
template<typename T, typename R, typename... Tn> template<typename T, typename R, typename... Args>
struct function_traits<R(T::*)(Tn...) const> { struct function_traits<R(T::*)(Args...) const> {
static const std::size_t arity = sizeof...(Tn); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = true; static const bool is_member_function = true;
typedef std::tuple<Tn...> arg_tuple_t; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Tn...> args_t; typedef types<Args...> args_type;
typedef R(T::* func_t)(Tn...); typedef R(T::* func_type)(Args...);
typedef R(T::* func_pointer_t)(Tn...); typedef R(T::* func_pointer_type)(Args...);
typedef R return_t; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg_n = std::tuple_element<i, arg_tuple_t>; using arg_n = std::tuple_element<i, arg_tuple_type>;
}; };
template<typename R, typename... Tn> template<typename R, typename... Args>
struct function_traits<R(Tn...)> { struct function_traits<R(Args...)> {
static const std::size_t arity = sizeof...(Tn); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = false; static const bool is_member_function = false;
typedef std::tuple<Tn...> arg_tuple_t; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Tn...> args_t; typedef types<Args...> args_type;
typedef R(func_t)(Tn...); typedef R(func_type)(Args...);
typedef R(*func_pointer_t)(Tn...); typedef R(*func_pointer_type)(Args...);
typedef R return_t; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg_n = std::tuple_element<i, arg_tuple_t>; using arg_n = std::tuple_element<i, arg_tuple_type>;
}; };
template<typename R, typename... Tn> template<typename R, typename... Args>
struct function_traits<R(*)(Tn...)> { struct function_traits<R(*)(Args...)> {
static const std::size_t arity = sizeof...(Tn); static const std::size_t arity = sizeof...(Args);
static const bool is_member_function = false; static const bool is_member_function = false;
typedef std::tuple<Tn...> arg_tuple_t; typedef std::tuple<Args...> arg_tuple_type;
typedef types<Tn...> args_t; typedef types<Args...> args_type;
typedef R(func_t)(Tn...); typedef R(func_type)(Args...);
typedef R(*func_pointer_t)(Tn...); typedef R(*func_pointer_type)(Args...);
typedef R return_t; typedef R return_type;
template<std::size_t i> template<std::size_t i>
using arg_n = std::tuple_element<i, arg_tuple_t>; using arg_n = std::tuple_element<i, arg_tuple_type>;
}; };
using std::get; using std::get;
template<typename Function, typename Tuple, size_t... Indices> template<typename Function, typename Tuple, size_t... Indices>
inline auto call(Function f, const Tuple& t, indices<Indices...>) -> decltype(f(get<Indices>(t)...)) { inline auto call(Function&& f, const Tuple& t, indices<Indices...>) -> decltype(f(get<Indices>(t)...)) {
return f(get<Indices>(t)...); return f(get<Indices>(t)...);
} }
} // detail } // detail
template<typename Function, typename... Args> template<typename Function, typename... Args>
inline auto call(Function f, const std::tuple<Args...>& t) -> decltype(detail::call(f, t, detail::build_indices<sizeof...(Args)>{})) { inline auto call(Function&& f, const std::tuple<Args...>& t) -> decltype(detail::call(std::forward<Function>(f), t, build_indices<sizeof...(Args)>{})) {
return call(f, t, detail::build_indices<sizeof...(Args)>{}); return call(std::forward<Function>(f), t, build_indices<sizeof...(Args)>{});
} }
} // sol } // sol

View File

@ -42,23 +42,23 @@ struct lambda_lua_func : public lua_func {
typedef function_traits<fx_t> fx_traits; typedef function_traits<fx_t> fx_traits;
TFx fx; TFx fx;
template<typename... TFxn> template<typename... FxArgs>
lambda_lua_func(TFxn&&... fxn) : fx(std::forward<TFxn>(fxn)...) { lambda_lua_func(FxArgs&&... fxargs) : fx(std::forward<FxArgs>(fxargs)...) {
} }
virtual int operator () (lua_State* L) override { virtual int operator () (lua_State* L) override {
return (*this)(tuple_types<fx_traits::return_t>(), fx_traits::args_t(), L); return (*this)(tuple_types<fx_traits::return_type>(), fx_traits::args_type(), L);
} }
template<typename... Tn> template<typename... Args>
int operator()(types<void>, types<Tn...> t, lua_State* L) { int operator()(types<void>, types<Args...> t, lua_State* L) {
stack::pop_call(L, fx, t); stack::pop_call(L, fx, t);
return 0; return 0;
} }
template<typename... TRn, typename... Tn> template<typename... TRn, typename... Args>
int operator()(types<TRn...>, types<Tn...> t, lua_State* L) { int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
auto r = stack::pop_call(L, fx, t); auto r = stack::pop_call(L, fx, t);
stack::push(L, r); stack::push(L, r);
return sizeof...(TRn); return sizeof...(TRn);
@ -71,23 +71,23 @@ struct explicit_lua_func : public lua_func {
typedef function_traits<fx_t> fx_traits; typedef function_traits<fx_t> fx_traits;
TFx fx; TFx fx;
template<typename... TFxn> template<typename... FxArgs>
explicit_lua_func(TFxn&&... fxn) : fx(std::forward<TFxn>(fxn)...) { explicit_lua_func(FxArgs&&... fxargs) : fx(std::forward<FxArgs>(fxargs)...) {
} }
virtual int operator () (lua_State* L) override { virtual int operator () (lua_State* L) override {
return (*this)(tuple_types<fx_traits::return_t>(), fx_traits::args_t(), L); return (*this)(tuple_types<fx_traits::return_type>(), fx_traits::args_type(), L);
} }
template<typename... Tn> template<typename... Args>
int operator () (types<void>, types<Tn...> t, lua_State* L) { int operator () (types<void>, types<Args...> t, lua_State* L) {
stack::pop_call(L, fx, t); stack::pop_call(L, fx, t);
return 0; return 0;
} }
template<typename... TRn, typename... Tn> template<typename... TRn, typename... Args>
int operator () (types<TRn...>, types<Tn...> t, lua_State* L) { int operator () (types<TRn...>, types<Args...> t, lua_State* L) {
auto r = stack::pop_call(L, fx, t); auto r = stack::pop_call(L, fx, t);
stack::push(L, r); stack::push(L, r);
return sizeof...(TRn); return sizeof...(TRn);
@ -98,26 +98,43 @@ template<typename TFx, typename T>
struct explicit_lua_func<TFx, T, true> : public lua_func { struct explicit_lua_func<TFx, T, true> : public lua_func {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t; typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
typedef function_traits<fx_t> fx_traits; typedef function_traits<fx_t> fx_traits;
T* member; struct lambda {
TFx fx; T* member;
TFx invocation;
template<typename... TFxn>
explicit_lua_func(T* m, TFxn&&... fxn) : member(m), fx(std::forward<TFxn>(fxn)...) { template<typename... FxArgs>
lambda(T* m, FxArgs&&... fxargs) : member(m), invocation(std::forward<FxArgs>(fxargs)...) {
}
template<typename... Args>
typename fx_traits::return_type operator () (Args&&... args) {
return ((*member).*invocation)(std::forward<Args>(args)...);
}
} fx;
template<typename... FxArgs>
explicit_lua_func(T* m, FxArgs&&... fxargs) : fx(m, std::forward<FxArgs>(fxargs)...) {
}
template<typename... FxArgs>
explicit_lua_func(T& m, FxArgs&&... fxargs) : fx(std::addressof(m), std::forward<FxArgs>(fxargs)...) {
} }
virtual int operator () (lua_State* L) override { virtual int operator () (lua_State* L) override {
return (*this)(tuple_types<fx_traits::return_t>(), fx_traits::args_t(), L); return (*this)(tuple_types<fx_traits::return_type>(), fx_traits::args_type(), L);
} }
template<typename... Tn> template<typename... Args>
int operator () (types<void>, types<Tn...>, lua_State* L) { int operator () (types<void>, types<Args...>, lua_State* L) {
stack::pop_call(L, fx, t); stack::pop_call(L, fx, t);
return 0; return 0;
} }
template<typename... TRn, typename... Tn> template<typename... TRn, typename... Args>
int operator () (types<TRn...>, types<Tn...> t, lua_State* L) { int operator () (types<TRn...>, types<Args...> t, lua_State* L) {
auto r = stack::pop_call(L, fx, t); auto r = stack::pop_call(L, fx, t);
stack::push(L, r); stack::push(L, r);
return sizeof...(TRn); return sizeof...(TRn);

View File

@ -45,8 +45,8 @@ auto ltr_pop(T&& extra, F f, types<>, Vs&&... vs)
// take head, produce value from it, pass after other values // take head, produce value from it, pass after other values
template<class F, class Head, class... Tail, class... Vs> template<class F, class Head, class... Tail, class... Vs>
auto ltr_pop(lua_State* L, F f, types<Head, Tail...>, Vs&&... vs) auto ltr_pop(lua_State* L, F f, types<Head, Tail...>, Vs&&... vs)
-> decltype(ltr_pop(L, f, types<Tail...>{}, std::forward<Vs>(vs)..., stack::pop<Head>(L))) { -> decltype(ltr_pop(L, f, types<Tail...>{}, std::forward<Vs>(vs)..., pop<Head>(L))) {
return ltr_pop(L, f, types<Tail...>{}, std::forward<Vs>(vs)..., stack::pop<Head>(L)); return ltr_pop(L, f, types<Tail...>{}, std::forward<Vs>(vs)..., pop<Head>(L));
} }
template<typename T> template<typename T>

View File

@ -149,8 +149,14 @@ public:
template<typename T, typename TFx> template<typename T, typename TFx>
state& set_function(T&& key, TFx&& fx) { state& set_function(T&& key, TFx&& fx) {
global.set_function(std::forward<T>(key), std::forward<TFx>(fx)); global.set_function(std::forward<T>(key), std::forward<TFx>(fx));
return *this; return *this;
}
template<typename T, typename TFx, typename TM>
state& set_function(T&& key, TFx&& fx, TM& mem) {
global.set_function(std::forward<T>(key), std::forward<TFx>(fx), mem);
return *this;
} }
template<typename T> template<typename T>

View File

@ -60,53 +60,59 @@ public:
template<typename T, typename TFx> template<typename T, typename TFx>
table& set_function(T&& key, TFx&& fx) { table& set_function(T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx; typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
const static bool isfunction = std::is_function<clean_fx>::value; const static bool isfunction = std::is_function<clean_fx>::value;
return set_function(std::integral_constant<bool, !isfunction>(), return set_fx(std::integral_constant<bool, !isfunction>(),
std::forward<T>(key), std::forward<TFx>(fx)); std::forward<T>(key), std::forward<TFx>(fx));
} }
template<typename T, typename TFx> template<typename T, typename TFx, typename TM>
table& set_function(std::true_type, T&& key, TFx&& fx) { table& set_function(T&& key, TFx&& fx, TM& mem) {
typedef typename std::decay<TFx>::type clean_fx; typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::string fkey(key); std::unique_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<clean_fx, TM>(mem, std::forward<TFx>(fx)));
lua_CFunction freefunc = &detail::lua_cfun; return set_fx(std::forward<T>(key), std::move(sptr));
auto hint = funcs.find(fkey);
detail::lua_func* target = nullptr;
if (hint == funcs.end()) {
std::shared_ptr<detail::lua_func> sptr(new detail::lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
}
target = hint->second.get();
lua_pushlightuserdata(state(), static_cast<void*>(target));
lua_pushcclosure(state(), freefunc, 1);
lua_setglobal(state(), fkey.c_str());
return *this;
}
template<typename T, typename TFx>
table& set_function(std::false_type, T&& key, TFx&& fx) {
typedef typename std::decay<TFx>::type clean_fx;
std::string fkey(key);
lua_CFunction freefunc = &detail::lua_cfun;
auto hint = funcs.find(fkey);
detail::lua_func* target = nullptr;
if (hint == funcs.end()) {
std::shared_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<TFx>(std::forward<TFx>(fx)));
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
}
target = hint->second.get();
lua_pushlightuserdata(state(), static_cast<void*>(target));
lua_pushcclosure(state(), freefunc, 1);
lua_setglobal(state(), fkey.c_str());
return *this;
} }
size_t size() const { size_t size() const {
push(); push();
return lua_rawlen(state(), -1); return lua_rawlen(state(), -1);
} }
private:
template<typename T, typename TFx>
table& set_fx(std::true_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::lambda_lua_func<clean_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T, typename TFx>
table& set_fx(std::false_type, T&& key, TFx&& fx) {
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type clean_fx;
typedef typename std::decay<TFx>::type ptr_fx;
std::unique_ptr<detail::lua_func> sptr(new detail::explicit_lua_func<ptr_fx>(std::forward<TFx>(fx)));
return set_fx(std::forward<T>(key), std::move(sptr));
}
template<typename T>
table& set_fx(T&& key, std::unique_ptr<detail::lua_func> funcptr) {
std::string fkey(key);
auto hint = funcs.find(fkey);
if (hint == funcs.end()) {
std::shared_ptr<detail::lua_func> sptr(funcptr.release());
hint = funcs.emplace_hint(hint, fkey, std::move(sptr));
}
else {
hint->second.reset(funcptr.release());
}
detail::lua_func* target = target = hint->second.get();
lua_CFunction freefunc = &detail::lua_cfun;
lua_pushlightuserdata(state(), static_cast<void*>(target));
lua_pushcclosure(state(), freefunc, 1);
lua_setglobal(state(), fkey.c_str());
return *this;
}
}; };
} // sol } // sol