2021-03-06 23:14:48 +08:00
|
|
|
// sol2
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-01-09 05:16:06 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
// Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
|
2016-01-09 05:16:06 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_TABLE_CORE_HPP
|
|
|
|
#define SOL_TABLE_CORE_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/table_proxy.hpp>
|
|
|
|
#include <sol/stack.hpp>
|
|
|
|
#include <sol/function_types.hpp>
|
|
|
|
#include <sol/table_iterator.hpp>
|
2021-01-21 02:55:44 +08:00
|
|
|
#include <sol/pairs_iterator.hpp>
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/types.hpp>
|
|
|
|
#include <sol/object.hpp>
|
|
|
|
#include <sol/usertype.hpp>
|
|
|
|
#include <sol/optional.hpp>
|
2016-01-09 05:16:06 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-06-20 05:59:40 +08:00
|
|
|
namespace detail {
|
|
|
|
template <std::size_t n>
|
2017-09-13 14:46:56 +08:00
|
|
|
struct clean {
|
|
|
|
lua_State* L;
|
2018-12-20 12:17:15 +08:00
|
|
|
clean(lua_State* luastate) : L(luastate) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
|
|
|
~clean() {
|
|
|
|
lua_pop(L, static_cast<int>(n));
|
|
|
|
}
|
|
|
|
};
|
2019-02-17 06:27:03 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
struct ref_clean {
|
|
|
|
lua_State* L;
|
2020-12-19 13:47:55 +08:00
|
|
|
int& pop_count;
|
|
|
|
|
2021-09-15 15:24:47 +08:00
|
|
|
ref_clean(lua_State* L_, int& pop_count_) noexcept : L(L_), pop_count(pop_count_) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
|
|
|
~ref_clean() {
|
2020-12-19 13:47:55 +08:00
|
|
|
lua_pop(L, static_cast<int>(pop_count));
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
|
|
|
};
|
2019-02-17 06:27:03 +08:00
|
|
|
|
2020-12-19 13:47:55 +08:00
|
|
|
inline int fail_on_newindex(lua_State* L_) {
|
|
|
|
return luaL_error(L_, "sol: cannot modify the elements of an enumeration table");
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2019-01-17 21:28:34 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace detail
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2019-01-17 21:28:34 +08:00
|
|
|
template <bool top_level, typename ref_t>
|
|
|
|
class basic_table_core : public basic_object<ref_t> {
|
|
|
|
private:
|
|
|
|
using base_t = basic_object<ref_t>;
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
friend class state;
|
|
|
|
friend class state_view;
|
2019-04-28 21:40:51 +08:00
|
|
|
template <typename, typename>
|
|
|
|
friend class basic_usertype;
|
|
|
|
template <typename>
|
|
|
|
friend class basic_metatable;
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2020-12-30 10:50:16 +08:00
|
|
|
template <typename T>
|
|
|
|
using is_get_direct_tableless = meta::boolean<stack::stack_detail::is_get_direct_tableless_v<T, top_level, false>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using is_raw_get_direct_tableless = std::false_type;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using is_set_direct_tableless = meta::boolean<stack::stack_detail::is_set_direct_tableless_v<T, top_level, false>>;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using is_raw_set_direct_tableless = std::false_type;
|
|
|
|
|
2019-01-17 21:28:34 +08:00
|
|
|
template <bool raw, typename... Ret, typename... Keys>
|
|
|
|
decltype(auto) tuple_get(int table_index, Keys&&... keys) const {
|
|
|
|
if constexpr (sizeof...(Ret) < 2) {
|
|
|
|
return traverse_get_single_maybe_tuple<raw, Ret...>(table_index, std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
using multi_ret = decltype(stack::pop<std::tuple<Ret...>>(nullptr));
|
|
|
|
return multi_ret(traverse_get_single_maybe_tuple<raw, Ret>(table_index, std::forward<Keys>(keys))...);
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2019-01-17 21:28:34 +08:00
|
|
|
template <bool raw, typename Ret, size_t... I, typename Key>
|
|
|
|
decltype(auto) traverse_get_single_tuple(int table_index, std::index_sequence<I...>, Key&& key) const {
|
|
|
|
return traverse_get_single<raw, Ret>(table_index, std::get<I>(std::forward<Key>(key))...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2019-01-17 21:28:34 +08:00
|
|
|
template <bool raw, typename Ret, typename Key>
|
|
|
|
decltype(auto) traverse_get_single_maybe_tuple(int table_index, Key&& key) const {
|
|
|
|
if constexpr (meta::is_tuple_v<meta::unqualified_t<Key>>) {
|
|
|
|
return traverse_get_single_tuple<raw, Ret>(
|
|
|
|
table_index, std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<Key>>>(), std::forward<Key>(key));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return traverse_get_single<raw, Ret>(table_index, std::forward<Key>(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool raw, typename Ret, typename... Keys>
|
|
|
|
decltype(auto) traverse_get_single(int table_index, Keys&&... keys) const {
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr static bool global = (meta::count_for_to_pack_v < 1, is_get_direct_tableless, meta::unqualified_t<Keys>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
if constexpr (meta::is_optional_v<meta::unqualified_t<Ret>>) {
|
|
|
|
int popcount = 0;
|
|
|
|
detail::ref_clean c(base_t::lua_state(), popcount);
|
2019-05-21 14:13:56 +08:00
|
|
|
return traverse_get_deep_optional<global, raw, detail::insert_mode::none, Ret>(popcount, table_index, std::forward<Keys>(keys)...);
|
2019-01-17 21:28:34 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-05-21 14:13:56 +08:00
|
|
|
detail::clean<sizeof...(Keys) - meta::count_for_pack_v<detail::is_insert_mode, meta::unqualified_t<Keys>...>> c(base_t::lua_state());
|
|
|
|
return traverse_get_deep<global, raw, detail::insert_mode::none, Ret>(table_index, std::forward<Keys>(keys)...);
|
2019-01-17 21:28:34 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <bool raw, typename Pairs, std::size_t... I>
|
2016-06-20 05:59:40 +08:00
|
|
|
void tuple_set(std::index_sequence<I...>, Pairs&& pairs) {
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr static bool global = (meta::count_even_for_pack_v < is_set_direct_tableless,
|
|
|
|
meta::unqualified_t<decltype(std::get<I * 2>(std::forward<Pairs>(pairs)))>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2019-04-28 21:40:51 +08:00
|
|
|
(void)table_index;
|
|
|
|
(void)L;
|
2020-07-04 15:58:24 +08:00
|
|
|
void(detail::swallow { (stack::set_field<(top_level), raw>(
|
|
|
|
L, std::get<I * 2>(std::forward<Pairs>(pairs)), std::get<I * 2 + 1>(std::forward<Pairs>(pairs)), table_index),
|
2018-12-20 12:17:15 +08:00
|
|
|
0)... });
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 14:13:56 +08:00
|
|
|
template <bool global, bool raw, detail::insert_mode mode, typename T, typename Key, typename... Keys>
|
2019-01-17 21:28:34 +08:00
|
|
|
decltype(auto) traverse_get_deep(int table_index, Key&& key, Keys&&... keys) const {
|
2019-05-21 14:13:56 +08:00
|
|
|
if constexpr (std::is_same_v<meta::unqualified_t<Key>, create_if_nil_t>) {
|
|
|
|
(void)key;
|
|
|
|
return traverse_get_deep<false, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::create_if_nil), T>(
|
|
|
|
table_index, std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
lua_State* L = base_t::lua_state();
|
|
|
|
stack::get_field<global, raw>(L, std::forward<Key>(key), table_index);
|
|
|
|
if constexpr (sizeof...(Keys) > 0) {
|
|
|
|
if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
|
|
|
|
type t = type_of(L, -1);
|
2019-05-27 01:32:28 +08:00
|
|
|
if (t == type::lua_nil || t == type::none) {
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
stack::push(L, new_table(0, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return traverse_get_deep<false, raw, mode, T>(lua_gettop(L), std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
|
|
|
|
type t = type_of(L, -1);
|
2019-05-27 01:32:28 +08:00
|
|
|
if ((t == type::lua_nil || t == type::none) && (is_table_like_v<T>)) {
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
stack::push(L, new_table(0, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack::get<T>(L);
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 14:13:56 +08:00
|
|
|
template <bool global, bool raw, detail::insert_mode mode, typename T, typename Key, typename... Keys>
|
2019-01-17 21:28:34 +08:00
|
|
|
decltype(auto) traverse_get_deep_optional(int& popcount, int table_index, Key&& key, Keys&&... keys) const {
|
2019-05-21 14:13:56 +08:00
|
|
|
if constexpr (std::is_same_v<meta::unqualified_t<Key>, create_if_nil_t>) {
|
|
|
|
constexpr detail::insert_mode new_mode = static_cast<detail::insert_mode>(mode | detail::insert_mode::create_if_nil);
|
|
|
|
(void)key;
|
|
|
|
return traverse_get_deep_optional<global, raw, new_mode, T>(popcount, table_index, std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<meta::unqualified_t<Key>, update_if_empty_t>) {
|
|
|
|
constexpr detail::insert_mode new_mode = static_cast<detail::insert_mode>(mode | detail::insert_mode::update_if_empty);
|
|
|
|
(void)key;
|
|
|
|
return traverse_get_deep_optional<global, raw, new_mode, T>(popcount, table_index, std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<meta::unqualified_t<Key>, override_value_t>) {
|
|
|
|
constexpr detail::insert_mode new_mode = static_cast<detail::insert_mode>(mode | detail::insert_mode::override_value);
|
|
|
|
(void)key;
|
|
|
|
return traverse_get_deep_optional<global, raw, new_mode, T>(popcount, table_index, std::forward<Keys>(keys)...);
|
2019-01-17 21:28:34 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-05-21 14:13:56 +08:00
|
|
|
if constexpr (sizeof...(Keys) > 0) {
|
|
|
|
lua_State* L = base_t::lua_state();
|
|
|
|
auto p = stack::probe_get_field<global, raw>(L, std::forward<Key>(key), table_index);
|
|
|
|
popcount += p.levels;
|
|
|
|
if (!p.success) {
|
|
|
|
if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
|
|
|
|
lua_pop(L, 1);
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr bool is_seq = meta::count_for_to_pack_v < 1, std::is_integral, Keys... >> 0;
|
2019-05-21 14:13:56 +08:00
|
|
|
stack::push(L, new_table(static_cast<int>(is_seq), static_cast<int>(!is_seq)));
|
|
|
|
stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return T(nullopt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return traverse_get_deep_optional<false, raw, mode, T>(popcount, lua_gettop(L), std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
using R = decltype(stack::get<T>(nullptr));
|
|
|
|
using value_type = typename meta::unqualified_t<R>::value_type;
|
|
|
|
lua_State* L = base_t::lua_state();
|
|
|
|
auto p = stack::probe_get_field<global, raw, value_type>(L, key, table_index);
|
|
|
|
popcount += p.levels;
|
|
|
|
if (!p.success) {
|
|
|
|
if constexpr ((mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
stack::push(L, new_table(0, 0));
|
|
|
|
stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
|
2021-01-04 18:52:30 +08:00
|
|
|
if (stack::check<value_type>(L, lua_gettop(L), &no_panic)) {
|
2019-05-21 14:13:56 +08:00
|
|
|
return stack::get<T>(L);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R(nullopt);
|
|
|
|
}
|
|
|
|
return stack::get<T>(L);
|
|
|
|
}
|
2019-01-17 21:28:34 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2019-02-17 06:27:03 +08:00
|
|
|
template <bool global, bool raw, detail::insert_mode mode, typename Key, typename... Keys>
|
2019-01-17 21:28:34 +08:00
|
|
|
void traverse_set_deep(int table_index, Key&& key, Keys&&... keys) const {
|
2019-02-15 17:01:03 +08:00
|
|
|
using KeyU = meta::unqualified_t<Key>;
|
2019-02-17 06:27:03 +08:00
|
|
|
if constexpr (std::is_same_v<KeyU, update_if_empty_t>) {
|
2019-02-15 17:01:03 +08:00
|
|
|
(void)key;
|
2020-07-04 15:58:24 +08:00
|
|
|
traverse_set_deep<global, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::update_if_empty)>(
|
|
|
|
table_index, std::forward<Keys>(keys)...);
|
2019-05-21 14:13:56 +08:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<KeyU, create_if_nil_t>) {
|
|
|
|
(void)key;
|
|
|
|
traverse_set_deep<global, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::create_if_nil)>(
|
|
|
|
table_index, std::forward<Keys>(keys)...);
|
2019-02-17 06:27:03 +08:00
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<KeyU, override_value_t>) {
|
|
|
|
(void)key;
|
2019-05-21 14:13:56 +08:00
|
|
|
traverse_set_deep<global, raw, static_cast<detail::insert_mode>(mode | detail::insert_mode::override_value)>(
|
|
|
|
table_index, std::forward<Keys>(keys)...);
|
2019-01-17 21:28:34 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-02-17 06:27:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2019-02-15 17:01:03 +08:00
|
|
|
if constexpr (sizeof...(Keys) == 1) {
|
2019-02-17 06:27:03 +08:00
|
|
|
if constexpr ((mode & detail::insert_mode::update_if_empty) == detail::insert_mode::update_if_empty) {
|
2019-05-21 14:13:56 +08:00
|
|
|
auto p = stack::probe_get_field<global, raw>(L, key, table_index);
|
|
|
|
lua_pop(L, p.levels);
|
|
|
|
if (!p.success) {
|
2019-02-17 06:27:03 +08:00
|
|
|
stack::set_field<global, raw>(L, std::forward<Key>(key), std::forward<Keys>(keys)..., table_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stack::set_field<global, raw>(L, std::forward<Key>(key), std::forward<Keys>(keys)..., table_index);
|
|
|
|
}
|
2019-02-15 17:01:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2019-05-21 14:13:56 +08:00
|
|
|
if constexpr (mode != detail::insert_mode::none) {
|
2019-02-17 06:27:03 +08:00
|
|
|
stack::get_field<global, raw>(L, key, table_index);
|
|
|
|
type vt = type_of(L, -1);
|
2019-05-21 14:13:56 +08:00
|
|
|
if constexpr ((mode & detail::insert_mode::update_if_empty) == detail::insert_mode::update_if_empty
|
|
|
|
|| (mode & detail::insert_mode::create_if_nil) == detail::insert_mode::create_if_nil) {
|
|
|
|
if (vt == type::lua_nil || vt == type::none) {
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr bool is_seq = meta::count_for_to_pack_v < 1, std::is_integral, Keys... >> 0;
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
stack::push(L, new_table(static_cast<int>(is_seq), static_cast<int>(!is_seq)));
|
|
|
|
stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (vt != type::table) {
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr bool is_seq = meta::count_for_to_pack_v < 1, std::is_integral, Keys... >> 0;
|
2019-05-21 14:13:56 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
stack::push(L, new_table(static_cast<int>(is_seq), static_cast<int>(!is_seq)));
|
|
|
|
stack::set_field<global, raw>(L, std::forward<Key>(key), stack_reference(L, -1), table_index);
|
|
|
|
}
|
2019-02-17 06:27:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 17:01:03 +08:00
|
|
|
else {
|
|
|
|
stack::get_field<global, raw>(L, std::forward<Key>(key), table_index);
|
|
|
|
}
|
2019-02-17 06:27:03 +08:00
|
|
|
traverse_set_deep<false, raw, mode>(lua_gettop(L), std::forward<Keys>(keys)...);
|
2019-02-15 17:01:03 +08:00
|
|
|
}
|
2019-01-17 21:28:34 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-04-20 01:00:47 +08:00
|
|
|
protected:
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, lua_nil_t n) : base_t(n) {
|
2018-01-29 11:21:13 +08:00
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, lua_State* L, int index) : base_t(L, index) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, lua_State* L, ref_index index) : base_t(L, index) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T,
|
2019-01-17 21:28:34 +08:00
|
|
|
meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<ref_t, stack_reference>>,
|
2018-12-20 12:17:15 +08:00
|
|
|
meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
|
|
|
basic_table_core(detail::no_safety_tag, T&& r) noexcept : base_t(std::forward<T>(r)) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2017-09-14 20:45:44 +08:00
|
|
|
template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, lua_State* L, T&& r) noexcept : base_t(L, std::forward<T>(r)) {
|
2017-09-14 20:45:44 +08:00
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
public:
|
2019-01-17 21:28:34 +08:00
|
|
|
using iterator = basic_table_iterator<ref_t>;
|
|
|
|
using const_iterator = iterator;
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2017-09-14 20:45:44 +08:00
|
|
|
using base_t::lua_state;
|
|
|
|
|
2017-04-03 04:10:00 +08:00
|
|
|
basic_table_core() noexcept = default;
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core(const basic_table_core&) = default;
|
|
|
|
basic_table_core(basic_table_core&&) = default;
|
|
|
|
basic_table_core& operator=(const basic_table_core&) = default;
|
|
|
|
basic_table_core& operator=(basic_table_core&&) = default;
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(const stack_reference& r) : basic_table_core(r.lua_state(), r.stack_index()) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(stack_reference&& r) : basic_table_core(r.lua_state(), r.stack_index()) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2017-09-14 12:35:40 +08:00
|
|
|
template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(lua_State* L, T&& r) : base_t(L, std::forward<T>(r)) {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_REFERENCES)
|
2017-09-14 20:45:44 +08:00
|
|
|
auto pp = stack::push_pop(*this);
|
2019-09-09 07:54:17 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2020-07-04 15:58:24 +08:00
|
|
|
constructor_handler handler {};
|
2019-09-09 07:54:17 +08:00
|
|
|
stack::check<basic_table_core>(lua_state(), table_index, handler);
|
2017-09-14 20:45:44 +08:00
|
|
|
#endif // Safety
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(lua_State* L, const new_table& nt) : base_t(L, -stack::push(L, nt)) {
|
2019-01-17 21:28:34 +08:00
|
|
|
if (!is_stack_based<meta::unqualified_t<ref_t>>::value) {
|
2017-04-03 04:10:00 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(lua_State* L, int index = -1) : basic_table_core(detail::no_safety, L, index) {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_REFERENCES)
|
2020-07-04 15:58:24 +08:00
|
|
|
constructor_handler handler {};
|
2017-09-01 08:47:09 +08:00
|
|
|
stack::check<basic_table_core>(L, index, handler);
|
2016-04-04 14:28:39 +08:00
|
|
|
#endif // Safety
|
2016-06-08 08:32:10 +08:00
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(lua_State* L, ref_index index) : basic_table_core(detail::no_safety, L, index) {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_REFERENCES)
|
2016-12-10 13:25:25 +08:00
|
|
|
auto pp = stack::push_pop(*this);
|
2019-09-09 07:54:17 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2020-07-04 15:58:24 +08:00
|
|
|
constructor_handler handler {};
|
2019-09-09 07:54:17 +08:00
|
|
|
stack::check<basic_table_core>(lua_state(), table_index, handler);
|
2016-12-10 13:25:25 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename T,
|
2019-01-17 21:28:34 +08:00
|
|
|
meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<ref_t, stack_reference>>,
|
2018-12-20 12:17:15 +08:00
|
|
|
meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
|
|
|
basic_table_core(T&& r) noexcept : basic_table_core(detail::no_safety, std::forward<T>(r)) {
|
2022-06-23 04:32:13 +08:00
|
|
|
#if SOL_IS_ON(SOL_SAFE_REFERENCES)
|
2017-04-03 04:10:00 +08:00
|
|
|
if (!is_table<meta::unqualified_t<T>>::value) {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2019-09-09 07:54:17 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2020-07-04 15:58:24 +08:00
|
|
|
constructor_handler handler {};
|
2019-09-09 07:54:17 +08:00
|
|
|
stack::check<basic_table_core>(lua_state(), table_index, handler);
|
2017-04-03 04:10:00 +08:00
|
|
|
}
|
|
|
|
#endif // Safety
|
|
|
|
}
|
2021-01-21 02:55:44 +08:00
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
basic_table_core(lua_nil_t r) noexcept : basic_table_core(detail::no_safety, r) {
|
2018-01-29 11:21:13 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
basic_table_core(lua_State* L, global_tag_t t) noexcept : base_t(L, t) {
|
|
|
|
}
|
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
iterator begin() const {
|
2020-03-31 12:22:46 +08:00
|
|
|
if (this->get_type() == type::table) {
|
|
|
|
return iterator(*this);
|
|
|
|
}
|
|
|
|
return iterator();
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() const {
|
|
|
|
return iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator cbegin() const {
|
|
|
|
return begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator cend() const {
|
|
|
|
return end();
|
2016-06-08 08:32:10 +08:00
|
|
|
}
|
|
|
|
|
2021-01-21 02:55:44 +08:00
|
|
|
basic_pairs_range<basic_table_core> pairs() noexcept {
|
|
|
|
return basic_pairs_range<basic_table_core>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
basic_pairs_range<const basic_table_core> pairs() const noexcept {
|
|
|
|
return basic_pairs_range<const basic_table_core>(*this);
|
|
|
|
}
|
|
|
|
|
2020-07-04 15:58:24 +08:00
|
|
|
void clear() {
|
2019-03-23 04:28:17 +08:00
|
|
|
auto pp = stack::push_pop<false>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
|
|
|
stack::clear(lua_state(), table_index);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Ret, typename... Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) get(Keys&&... keys) const {
|
|
|
|
static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
|
2020-12-30 10:50:16 +08:00
|
|
|
constexpr static bool global = meta::all<meta::boolean<top_level>, is_get_direct_tableless<meta::unqualified_t<Keys>>...>::value;
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
|
|
|
return tuple_get<false, Ret...>(table_index, std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Key>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) get_or(Key&& key, T&& otherwise) const {
|
|
|
|
typedef decltype(get<T>("")) U;
|
2017-05-15 22:41:50 +08:00
|
|
|
optional<U> option = get<optional<U>>(std::forward<Key>(key));
|
2016-06-20 05:59:40 +08:00
|
|
|
if (option) {
|
|
|
|
return static_cast<U>(option.value());
|
|
|
|
}
|
|
|
|
return static_cast<U>(std::forward<T>(otherwise));
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Key, typename D>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) get_or(Key&& key, D&& otherwise) const {
|
2017-05-15 22:41:50 +08:00
|
|
|
optional<T> option = get<optional<T>>(std::forward<Key>(key));
|
2016-06-20 05:59:40 +08:00
|
|
|
if (option) {
|
|
|
|
return static_cast<T>(option.value());
|
|
|
|
}
|
|
|
|
return static_cast<T>(std::forward<D>(otherwise));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Keys>
|
|
|
|
decltype(auto) traverse_get(Keys&&... keys) const {
|
2019-05-21 14:13:56 +08:00
|
|
|
static_assert(sizeof...(Keys) > 0, "must pass at least 1 key to get");
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr static bool global = (meta::count_for_to_pack_v < 1, is_get_direct_tableless, meta::unqualified_t<Keys>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
|
|
|
return traverse_get_single<false, T>(table_index, std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Keys>
|
|
|
|
basic_table_core& traverse_set(Keys&&... keys) {
|
2019-05-21 14:13:56 +08:00
|
|
|
static_assert(sizeof...(Keys) > 1, "must pass at least 1 key and 1 value to set");
|
2020-07-04 15:58:24 +08:00
|
|
|
constexpr static bool global
|
2021-01-21 02:55:44 +08:00
|
|
|
= (meta::count_when_for_to_pack_v < detail::is_not_insert_mode, 1, is_set_direct_tableless, meta::unqualified_t<Keys>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2019-05-21 14:13:56 +08:00
|
|
|
auto pn = stack::pop_n(L, static_cast<int>(sizeof...(Keys) - 2 - meta::count_for_pack_v<detail::is_insert_mode, meta::unqualified_t<Keys>...>));
|
2019-02-17 06:27:03 +08:00
|
|
|
traverse_set_deep<top_level, false, detail::insert_mode::none>(table_index, std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& set(Args&&... args) {
|
2020-07-04 15:58:24 +08:00
|
|
|
if constexpr (sizeof...(Args) == 2) {
|
2019-01-17 21:28:34 +08:00
|
|
|
traverse_set(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tuple_set<false>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
|
|
|
|
}
|
2017-09-07 03:09:51 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Ret, typename... Keys>
|
2017-09-07 03:09:51 +08:00
|
|
|
decltype(auto) raw_get(Keys&&... keys) const {
|
|
|
|
static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr static bool global = (meta::count_for_to_pack_v < 1, is_raw_get_direct_tableless, meta::unqualified_t<Keys>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
|
|
|
return tuple_get<true, Ret...>(table_index, std::forward<Keys>(keys)...);
|
2017-09-07 03:09:51 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Key>
|
2017-09-07 03:09:51 +08:00
|
|
|
decltype(auto) raw_get_or(Key&& key, T&& otherwise) const {
|
|
|
|
typedef decltype(raw_get<T>("")) U;
|
|
|
|
optional<U> option = raw_get<optional<U>>(std::forward<Key>(key));
|
|
|
|
if (option) {
|
|
|
|
return static_cast<U>(option.value());
|
|
|
|
}
|
|
|
|
return static_cast<U>(std::forward<T>(otherwise));
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, typename Key, typename D>
|
2017-09-07 03:09:51 +08:00
|
|
|
decltype(auto) raw_get_or(Key&& key, D&& otherwise) const {
|
|
|
|
optional<T> option = raw_get<optional<T>>(std::forward<Key>(key));
|
|
|
|
if (option) {
|
|
|
|
return static_cast<T>(option.value());
|
|
|
|
}
|
|
|
|
return static_cast<T>(std::forward<D>(otherwise));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Keys>
|
|
|
|
decltype(auto) traverse_raw_get(Keys&&... keys) const {
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr static bool global = (meta::count_for_to_pack_v < 1, is_raw_get_direct_tableless, meta::unqualified_t<Keys>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
|
|
|
int table_index = pp.index_of(*this);
|
|
|
|
return traverse_get_single<true, T>(table_index, std::forward<Keys>(keys)...);
|
2017-09-07 03:09:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Keys>
|
|
|
|
basic_table_core& traverse_raw_set(Keys&&... keys) {
|
2021-01-21 02:55:44 +08:00
|
|
|
constexpr static bool global = (meta::count_for_to_pack_v < 1, is_raw_set_direct_tableless, meta::unqualified_t<Keys>... >> 0);
|
2019-01-17 21:28:34 +08:00
|
|
|
auto pp = stack::push_pop<global>(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2019-05-21 15:57:10 +08:00
|
|
|
auto pn = stack::pop_n(L, static_cast<int>(sizeof...(Keys) - 2 - meta::count_for_pack_v<detail::is_insert_mode, meta::unqualified_t<Keys>...>));
|
2019-02-15 17:01:03 +08:00
|
|
|
traverse_set_deep<top_level, true, false>(std::forward<Keys>(keys)...);
|
2017-09-07 03:09:51 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Args>
|
2017-09-07 03:09:51 +08:00
|
|
|
basic_table_core& raw_set(Args&&... args) {
|
|
|
|
tuple_set<true>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
|
2016-06-20 05:59:40 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-09-28 13:27:38 +08:00
|
|
|
template <typename Class, typename Key>
|
2018-11-19 02:06:01 +08:00
|
|
|
usertype<Class> new_usertype(Key&& key);
|
|
|
|
|
2021-01-21 16:26:42 +08:00
|
|
|
template <typename Class, typename Key, automagic_flags enrollment_flags>
|
|
|
|
usertype<Class> new_usertype(Key&& key, constant_automagic_enrollments<enrollment_flags> enrollment);
|
|
|
|
|
2018-11-19 02:06:01 +08:00
|
|
|
template <typename Class, typename Key>
|
|
|
|
usertype<Class> new_usertype(Key&& key, automagic_enrollments enrollment);
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename Class, typename Key, typename Arg, typename... Args,
|
2021-01-21 16:26:42 +08:00
|
|
|
typename = std::enable_if_t<!std::is_base_of_v<automagic_enrollments, meta::unqualified_t<Arg>>>>
|
2018-11-19 02:06:01 +08:00
|
|
|
usertype<Class> new_usertype(Key&& key, Arg&& arg, Args&&... args);
|
2016-09-17 23:22:19 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <bool read_only = true, typename... Args>
|
2017-09-07 03:09:51 +08:00
|
|
|
table new_enum(const string_view& name, Args&&... args) {
|
|
|
|
table target = create_with(std::forward<Args>(args)...);
|
2021-10-23 04:17:49 +08:00
|
|
|
if constexpr (read_only) {
|
|
|
|
// Need to create a special iterator to handle this
|
|
|
|
table x
|
|
|
|
= create_with(meta_function::new_index, detail::fail_on_newindex, meta_function::index, target, meta_function::pairs, stack::stack_detail::readonly_pairs);
|
2017-09-07 03:09:51 +08:00
|
|
|
table shim = create_named(name, metatable_key, x);
|
|
|
|
return shim;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
else {
|
2017-09-07 03:09:51 +08:00
|
|
|
set(name, target);
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T, bool read_only = true>
|
2017-09-07 03:09:51 +08:00
|
|
|
table new_enum(const string_view& name, std::initializer_list<std::pair<string_view, T>> items) {
|
2018-01-31 10:05:08 +08:00
|
|
|
table target = create(static_cast<int>(items.size()), static_cast<int>(0));
|
2017-09-07 03:09:51 +08:00
|
|
|
for (const auto& kvp : items) {
|
|
|
|
target.set(kvp.first, kvp.second);
|
|
|
|
}
|
2019-01-05 18:48:51 +08:00
|
|
|
if constexpr (read_only) {
|
2018-12-20 12:17:15 +08:00
|
|
|
table x = create_with(meta_function::new_index, detail::fail_on_newindex, meta_function::index, target);
|
2017-09-07 03:09:51 +08:00
|
|
|
table shim = create_named(name, metatable_key, x);
|
|
|
|
return shim;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set(name, target);
|
|
|
|
return target;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 18:48:51 +08:00
|
|
|
template <typename Key = object, typename Value = object, typename Fx>
|
2016-06-20 05:59:40 +08:00
|
|
|
void for_each(Fx&& fx) const {
|
2019-02-15 17:01:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2019-01-05 18:48:51 +08:00
|
|
|
if constexpr (std::is_invocable_v<Fx, Key, Value>) {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2019-09-09 07:54:17 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
stack::push(L, lua_nil);
|
2019-09-09 07:54:17 +08:00
|
|
|
while (lua_next(L, table_index)) {
|
2019-02-15 17:01:03 +08:00
|
|
|
Key key(L, -2);
|
|
|
|
Value value(L, -1);
|
|
|
|
auto pn = stack::pop_n(L, 1);
|
2019-01-05 18:48:51 +08:00
|
|
|
fx(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2019-09-09 07:54:17 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
stack::push(L, lua_nil);
|
2019-09-09 07:54:17 +08:00
|
|
|
while (lua_next(L, table_index)) {
|
2019-02-15 17:01:03 +08:00
|
|
|
Key key(L, -2);
|
|
|
|
Value value(L, -1);
|
|
|
|
auto pn = stack::pop_n(L, 1);
|
2019-01-05 18:48:51 +08:00
|
|
|
std::pair<Key&, Value&> keyvalue(key, value);
|
|
|
|
fx(keyvalue);
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2019-09-09 07:54:17 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2019-09-09 07:54:17 +08:00
|
|
|
lua_len(L, table_index);
|
2019-02-15 17:01:03 +08:00
|
|
|
return stack::pop<size_t>(L);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return cbegin() == cend();
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-03-15 04:15:43 +08:00
|
|
|
auto operator[](T&& key) & {
|
2019-09-15 12:43:44 +08:00
|
|
|
return table_proxy<basic_table_core&, detail::proxy_key_t<T>>(*this, std::forward<T>(key));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-03-15 04:15:43 +08:00
|
|
|
auto operator[](T&& key) const& {
|
2019-09-15 12:43:44 +08:00
|
|
|
return table_proxy<const basic_table_core&, detail::proxy_key_t<T>>(*this, std::forward<T>(key));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2019-03-15 04:15:43 +08:00
|
|
|
auto operator[](T&& key) && {
|
2019-09-15 12:43:44 +08:00
|
|
|
return table_proxy<basic_table_core, detail::proxy_key_t<T>>(std::move(*this), std::forward<T>(key));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Sig, typename Key, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& set_function(Key&& key, Args&&... args) {
|
|
|
|
set_fx(types<Sig>(), std::forward<Key>(key), std::forward<Args>(args)...);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Key, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& set_function(Key&& key, Args&&... args) {
|
|
|
|
set_fx(types<>(), std::forward<Key>(key), std::forward<Args>(args)...);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
basic_table_core& add(Args&&... args) {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2019-01-17 21:28:34 +08:00
|
|
|
int table_index = pp.index_of(*this);
|
2019-02-15 17:01:03 +08:00
|
|
|
lua_State* L = base_t::lua_state();
|
2022-06-25 15:42:51 +08:00
|
|
|
(void)detail::swallow { 0, (stack::stack_detail::raw_table_set(L, std::forward<Args>(args), table_index), 0)... };
|
2016-06-20 05:59:40 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-04 15:58:24 +08:00
|
|
|
template <typename R, typename... Args, typename Fx, typename Key, typename = std::invoke_result_t<Fx, Args...>>
|
2016-06-20 05:59:40 +08:00
|
|
|
void set_fx(types<R(Args...)>, Key&& key, Fx&& fx) {
|
|
|
|
set_resolved_function<R(Args...)>(std::forward<Key>(key), std::forward<Fx>(fx));
|
|
|
|
}
|
|
|
|
|
2018-03-02 11:08:27 +08:00
|
|
|
template <typename Fx, typename Key, meta::enable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
|
2016-06-20 05:59:40 +08:00
|
|
|
void set_fx(types<>, Key&& key, Fx&& fx) {
|
|
|
|
set(std::forward<Key>(key), std::forward<Fx>(fx));
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:17:15 +08:00
|
|
|
template <typename Fx, typename Key, typename... Args,
|
|
|
|
meta::disable<meta::is_specialization_of<meta::unqualified_t<Fx>, overload_set>> = meta::enabler>
|
2016-06-20 05:59:40 +08:00
|
|
|
void set_fx(types<>, Key&& key, Fx&& fx, Args&&... args) {
|
2016-09-14 00:37:08 +08:00
|
|
|
set(std::forward<Key>(key), as_function_reference(std::forward<Fx>(fx), std::forward<Args>(args)...));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename... Sig, typename... Args, typename Key>
|
2016-06-20 05:59:40 +08:00
|
|
|
void set_resolved_function(Key&& key, Args&&... args) {
|
2016-09-14 00:37:08 +08:00
|
|
|
set(std::forward<Key>(key), as_function_reference<function_sig<Sig...>>(std::forward<Args>(args)...));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static inline table create(lua_State* L, int narr = 0, int nrec = 0) {
|
|
|
|
lua_createtable(L, narr, nrec);
|
|
|
|
table result(L);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key, typename Value, typename... Args>
|
|
|
|
static inline table create(lua_State* L, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
|
|
|
lua_createtable(L, narr, nrec);
|
|
|
|
table result(L);
|
|
|
|
result.set(std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
static inline table create_with(lua_State* L, Args&&... args) {
|
2016-10-28 16:36:04 +08:00
|
|
|
static_assert(sizeof...(Args) % 2 == 0, "You must have an even number of arguments for a key, value ... list.");
|
2019-05-21 14:13:56 +08:00
|
|
|
constexpr int narr = static_cast<int>(meta::count_odd_for_pack_v<std::is_integral, Args...>);
|
2016-06-20 05:59:40 +08:00
|
|
|
return create(L, narr, static_cast<int>((sizeof...(Args) / 2) - narr), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
table create(int narr = 0, int nrec = 0) {
|
|
|
|
return create(base_t::lua_state(), narr, nrec);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key, typename Value, typename... Args>
|
|
|
|
table create(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
|
|
|
return create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name>
|
|
|
|
table create(Name&& name, int narr = 0, int nrec = 0) {
|
|
|
|
table x = create(base_t::lua_state(), narr, nrec);
|
|
|
|
this->set(std::forward<Name>(name), x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name, typename Key, typename Value, typename... Args>
|
|
|
|
table create(Name&& name, int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
|
|
|
table x = create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
|
|
|
this->set(std::forward<Name>(name), x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
table create_with(Args&&... args) {
|
|
|
|
return create_with(base_t::lua_state(), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name, typename... Args>
|
|
|
|
table create_named(Name&& name, Args&&... args) {
|
2019-05-21 14:13:56 +08:00
|
|
|
static const int narr = static_cast<int>(meta::count_even_for_pack_v<std::is_integral, Args...>);
|
2017-09-07 03:09:51 +08:00
|
|
|
return create(std::forward<Name>(name), narr, (sizeof...(Args) / 2) - narr, std::forward<Args>(args)...);
|
2016-11-26 09:47:15 +08:00
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-01-09 05:16:06 +08:00
|
|
|
|
|
|
|
#endif // SOL_TABLE_CORE_HPP
|