From a65f795b793f709ef868e19b3a182f31a6270867 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 21 Oct 2016 07:05:58 -0400 Subject: [PATCH] ipairs and pairs improved support, folding the registration table down to avoid duplicated code --- sol/container_usertype_metatable.hpp | 47 +++++++++++++--------------- sol/types.hpp | 9 +++++- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/sol/container_usertype_metatable.hpp b/sol/container_usertype_metatable.hpp index a07b85be..3f9e98da 100644 --- a/sol/container_usertype_metatable.hpp +++ b/sol/container_usertype_metatable.hpp @@ -298,11 +298,12 @@ namespace sol { iter& i = stack::get>(L, 1); auto& source = i.source; auto& it = i.it; - std::advance(it, 1); if (it == end(source)) { return 0; } - return stack::multi_push_reference(L, it->first, it->second); + int p = stack::multi_push_reference(L, it->first, it->second); + std::advance(it, 1); + return p; } static int real_pairs_call(lua_State* L) { @@ -341,6 +342,22 @@ namespace sol { }; namespace stack { + namespace stack_detail { + template + inline const auto& container_metatable() { + typedef container_usertype_metatable> cumt; + static const luaL_Reg reg[] = { + { "__index", &cumt::index_call }, + { "__newindex", &cumt::new_index_call }, + { "__pairs", &cumt::pairs_call }, + { "__ipairs", &cumt::pairs_call }, + { "__len", &cumt::length_call }, + { std::is_pointer::value ? nullptr : "__gc", std::is_pointer::value ? nullptr : &detail::usertype_alloc_destroy }, + { nullptr, nullptr } + }; + return reg; + } + } template struct pusher, meta::neg, std::is_base_of>>>::value>> { @@ -349,14 +366,7 @@ namespace sol { auto fx = [&L]() { const char* metakey = &usertype_traits::metatable[0]; if (luaL_newmetatable(L, metakey) == 1) { - luaL_Reg reg[] = { - { "__index", &cumt::index_call }, - { "__newindex", &cumt::new_index_call }, - { "__pairs", &cumt::pairs_call }, - { "__len", &cumt::length_call }, - { "__gc", &detail::usertype_alloc_destroy }, - { nullptr, nullptr } - }; + const auto& reg = stack_detail::container_metatable(); luaL_setfuncs(L, reg, 0); } lua_setmetatable(L, -2); @@ -368,14 +378,7 @@ namespace sol { auto fx = [&L]() { const char* metakey = &usertype_traits::metatable[0]; if (luaL_newmetatable(L, metakey) == 1) { - luaL_Reg reg[] = { - { "__index", &cumt::index_call }, - { "__newindex", &cumt::new_index_call }, - { "__pairs", &cumt::pairs_call }, - { "__len", &cumt::length_call }, - { "__gc", &detail::usertype_alloc_destroy }, - { nullptr, nullptr } - }; + const auto& reg = stack_detail::container_metatable(); luaL_setfuncs(L, reg, 0); } lua_setmetatable(L, -2); @@ -391,13 +394,7 @@ namespace sol { auto fx = [&L]() { const char* metakey = &usertype_traits*>::metatable[0]; if (luaL_newmetatable(L, metakey) == 1) { - luaL_Reg reg[] = { - { "__index", &cumt::index_call }, - { "__newindex", &cumt::new_index_call }, - { "__pairs", &cumt::pairs_call }, - { "__len", &cumt::length_call }, - { nullptr, nullptr } - }; + const auto& reg = stack_detail::container_metatable(); luaL_setfuncs(L, reg, 0); } lua_setmetatable(L, -2); diff --git a/sol/types.hpp b/sol/types.hpp index e84bb345..4c605929 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -361,6 +361,7 @@ namespace sol { new_index, mode, call, + call_function = call, metatable, to_string, length, @@ -377,7 +378,13 @@ namespace sol { less_than, less_than_or_equal_to, garbage_collect, - call_function = call, + floor_division, + bitwise_left_shift, + bitwise_right_shift, + bitwise_not, + bitwise_and, + bitwise_or, + bitwise_xor, }; typedef meta_function meta_method;