Allow functions that return C++ sequential containers to return tables on the lua side

This commit is contained in:
Rapptz 2014-05-31 20:02:54 -04:00
parent 072a88092f
commit 5c8f661447
2 changed files with 31 additions and 2 deletions

View File

@ -167,11 +167,27 @@ auto pop(lua_State* L) -> decltype(get<T>(L)) {
return r;
}
template<typename T>
inline EnableIf<std::is_arithmetic<T>> push(lua_State* L, T arithmetic) {
template<typename T, bool B = std::is_arithmetic<T>::value>
inline typename std::enable_if<B, void>::type push(lua_State* L, T arithmetic) {
detail::push_arithmetic(std::is_integral<T>{}, L, arithmetic);
}
template<typename T, bool B = !std::is_arithmetic<T>::value &&
!std::is_same<Unqualified<T>, std::string>::value &&
has_begin_end<T>::value>
inline typename std::enable_if<B, void>::type push(lua_State* L, const T& cont) {
lua_createtable(L, cont.size(), 0);
unsigned index = 1;
for(auto&& i : cont) {
// push the index
push(L, index++);
// push the value
push(L, i);
// set the table
lua_settable(L, -3);
}
}
inline void push(lua_State*, reference& ref) {
ref.push();
}

View File

@ -157,6 +157,19 @@ struct function_traits<R(*)(Args...)> {
template<std::size_t i>
using arg = typename std::tuple_element<i, arg_tuple_type>::type;
};
struct has_begin_end_impl {
template<typename T, typename U = Unqualified<T>,
typename B = decltype(std::declval<U&>().begin()),
typename E = decltype(std::declval<U&>().end())>
static std::true_type test(int);
template<typename...>
static std::false_type test(...);
};
template<typename T>
struct has_begin_end : decltype(has_begin_end_impl::test<T>(0)) {};
} // sol
#endif // SOL_TRAITS_HPP