mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Allow functions that return C++ sequential containers to return tables on the lua side
This commit is contained in:
parent
072a88092f
commit
5c8f661447
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user