2017-12-20 17:58:32 +08:00
|
|
|
// sol2
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-01-09 05:16:06 +08:00
|
|
|
|
2017-05-15 22:41:50 +08:00
|
|
|
// Copyright (c) 2013-2017 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
|
|
|
|
|
|
|
|
#include "proxy.hpp"
|
|
|
|
#include "stack.hpp"
|
|
|
|
#include "function_types.hpp"
|
|
|
|
#include "usertype.hpp"
|
2016-03-12 00:34:44 +08:00
|
|
|
#include "table_iterator.hpp"
|
2017-07-10 00:00:57 +08:00
|
|
|
#include "types.hpp"
|
|
|
|
#include "object_base.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;
|
|
|
|
clean(lua_State* luastate)
|
|
|
|
: L(luastate) {
|
|
|
|
}
|
|
|
|
~clean() {
|
|
|
|
lua_pop(L, static_cast<int>(n));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct ref_clean {
|
|
|
|
lua_State* L;
|
|
|
|
int& n;
|
|
|
|
ref_clean(lua_State* luastate, int& n)
|
|
|
|
: L(luastate), n(n) {
|
|
|
|
}
|
|
|
|
~ref_clean() {
|
|
|
|
lua_pop(L, static_cast<int>(n));
|
|
|
|
}
|
|
|
|
};
|
2016-06-20 05:59:40 +08:00
|
|
|
inline int fail_on_newindex(lua_State* L) {
|
|
|
|
return luaL_error(L, "sol: cannot modify the elements of an enumeration table");
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace detail
|
2016-06-20 05:59:40 +08:00
|
|
|
|
2017-04-03 04:10:00 +08:00
|
|
|
const new_table create = new_table{};
|
|
|
|
|
|
|
|
template <bool top_level, typename base_type>
|
|
|
|
class basic_table_core : public basic_object_base<base_type> {
|
|
|
|
typedef basic_object_base<base_type> base_t;
|
2016-06-20 05:59:40 +08:00
|
|
|
friend class state;
|
|
|
|
friend class state_view;
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
using is_global = meta::all<meta::boolean<top_level>, meta::is_c_str<Args>...>;
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Fx>
|
2016-06-20 05:59:40 +08:00
|
|
|
void for_each(std::true_type, Fx&& fx) const {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2016-12-03 20:33:18 +08:00
|
|
|
stack::push(base_t::lua_state(), lua_nil);
|
2016-06-20 05:59:40 +08:00
|
|
|
while (lua_next(base_t::lua_state(), -2)) {
|
2017-09-14 12:35:40 +08:00
|
|
|
object key(base_t::lua_state(), -2);
|
|
|
|
object value(base_t::lua_state(), -1);
|
|
|
|
std::pair<object&, object&> keyvalue(key, value);
|
2016-07-29 01:33:08 +08:00
|
|
|
auto pn = stack::pop_n(base_t::lua_state(), 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
fx(keyvalue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Fx>
|
2016-06-20 05:59:40 +08:00
|
|
|
void for_each(std::false_type, Fx&& fx) const {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2016-12-03 20:33:18 +08:00
|
|
|
stack::push(base_t::lua_state(), lua_nil);
|
2016-06-20 05:59:40 +08:00
|
|
|
while (lua_next(base_t::lua_state(), -2)) {
|
2017-09-14 12:35:40 +08:00
|
|
|
object key(base_t::lua_state(), -2);
|
|
|
|
object value(base_t::lua_state(), -1);
|
2016-07-29 01:33:08 +08:00
|
|
|
auto pn = stack::pop_n(base_t::lua_state(), 1);
|
2016-06-20 05:59:40 +08:00
|
|
|
fx(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <bool raw, typename Ret0, typename Ret1, typename... Ret, std::size_t... I, typename Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
auto tuple_get(types<Ret0, Ret1, Ret...>, std::index_sequence<0, 1, I...>, Keys&& keys) const
|
|
|
|
-> decltype(stack::pop<std::tuple<Ret0, Ret1, Ret...>>(nullptr)) {
|
|
|
|
typedef decltype(stack::pop<std::tuple<Ret0, Ret1, Ret...>>(nullptr)) Tup;
|
|
|
|
return Tup(
|
2017-09-07 03:09:51 +08:00
|
|
|
traverse_get_optional<top_level, raw, Ret0>(meta::is_optional<meta::unqualified_t<Ret0>>(), detail::forward_get<0>(keys)),
|
|
|
|
traverse_get_optional<top_level, raw, Ret1>(meta::is_optional<meta::unqualified_t<Ret1>>(), detail::forward_get<1>(keys)),
|
2017-09-13 14:46:56 +08:00
|
|
|
traverse_get_optional<top_level, raw, Ret>(meta::is_optional<meta::unqualified_t<Ret>>(), detail::forward_get<I>(keys))...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <bool raw, typename Ret, std::size_t I, typename Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) tuple_get(types<Ret>, std::index_sequence<I>, Keys&& keys) const {
|
2017-09-07 03:09:51 +08:00
|
|
|
return traverse_get_optional<top_level, raw, Ret>(meta::is_optional<meta::unqualified_t<Ret>>(), detail::forward_get<I>(keys));
|
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) {
|
2017-09-13 14:46:56 +08:00
|
|
|
auto pp = stack::push_pop < top_level && (is_global<decltype(detail::forward_get<I * 2>(pairs))...>::value) > (*this);
|
2017-09-14 20:45:44 +08:00
|
|
|
void(detail::swallow{ (stack::set_field<top_level, raw>(base_t::lua_state(),
|
|
|
|
detail::forward_get<I * 2>(pairs),
|
|
|
|
detail::forward_get<I * 2 + 1>(pairs),
|
|
|
|
lua_gettop(base_t::lua_state())),
|
|
|
|
0)... });
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename T, typename Key>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) traverse_get_deep(Key&& key) const {
|
2017-09-07 03:09:51 +08:00
|
|
|
stack::get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key));
|
2016-06-20 05:59:40 +08:00
|
|
|
return stack::get<T>(base_t::lua_state());
|
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename T, typename Key, typename... Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) traverse_get_deep(Key&& key, Keys&&... keys) const {
|
2017-09-07 03:09:51 +08:00
|
|
|
stack::get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key));
|
|
|
|
return traverse_get_deep<false, raw, T>(std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename T, std::size_t I, typename Key>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key) const {
|
|
|
|
typedef decltype(stack::get<T>(base_t::lua_state())) R;
|
2017-09-07 03:09:51 +08:00
|
|
|
auto p = stack::probe_get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
|
2016-06-20 05:59:40 +08:00
|
|
|
popcount += p.levels;
|
|
|
|
if (!p.success)
|
|
|
|
return R(nullopt);
|
|
|
|
return stack::get<T>(base_t::lua_state());
|
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename T, std::size_t I, typename Key, typename... Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key, Keys&&... keys) const {
|
|
|
|
auto p = I > 0 ? stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), -1) : stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
|
|
|
|
popcount += p.levels;
|
|
|
|
if (!p.success)
|
|
|
|
return T(nullopt);
|
2017-09-07 03:09:51 +08:00
|
|
|
return traverse_get_deep_optional<false, raw, T, I + 1>(popcount, std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename T, typename... Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) traverse_get_optional(std::false_type, Keys&&... keys) const {
|
|
|
|
detail::clean<sizeof...(Keys)> c(base_t::lua_state());
|
2017-09-07 03:09:51 +08:00
|
|
|
return traverse_get_deep<global, raw, T>(std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename T, typename... Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
decltype(auto) traverse_get_optional(std::true_type, Keys&&... keys) const {
|
|
|
|
int popcount = 0;
|
|
|
|
detail::ref_clean c(base_t::lua_state(), popcount);
|
2017-09-07 03:09:51 +08:00
|
|
|
return traverse_get_deep_optional<global, raw, T, 0>(popcount, std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename Key, typename Value>
|
2016-06-20 05:59:40 +08:00
|
|
|
void traverse_set_deep(Key&& key, Value&& value) const {
|
2017-09-07 03:09:51 +08:00
|
|
|
stack::set_field<global, raw>(base_t::lua_state(), std::forward<Key>(key), std::forward<Value>(value));
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-07 03:09:51 +08:00
|
|
|
template <bool global, bool raw, typename Key, typename... Keys>
|
2016-06-20 05:59:40 +08:00
|
|
|
void traverse_set_deep(Key&& key, Keys&&... keys) const {
|
2017-09-07 03:09:51 +08:00
|
|
|
stack::get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key));
|
|
|
|
traverse_set_deep<false, raw>(std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(lua_State* L, detail::global_tag t) noexcept
|
|
|
|
: base_t(L, t) {
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:00:47 +08:00
|
|
|
protected:
|
2018-01-29 11:21:13 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, lua_nil_t n)
|
|
|
|
: base_t(n) {
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, lua_State* L, int index)
|
|
|
|
: base_t(L, index) {
|
|
|
|
}
|
|
|
|
basic_table_core(detail::no_safety_tag, lua_State* L, ref_index index)
|
|
|
|
: base_t(L, index) {
|
|
|
|
}
|
2018-01-29 11:21:13 +08:00
|
|
|
template <typename T, meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<base_type, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(detail::no_safety_tag, T&& r) noexcept
|
|
|
|
: base_t(std::forward<T>(r)) {
|
|
|
|
}
|
2017-09-14 20:45:44 +08:00
|
|
|
template <typename T, meta::enable<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
|
|
|
basic_table_core(detail::no_safety_tag, lua_State*L, T&& r) noexcept
|
|
|
|
: base_t(L, std::forward<T>(r)) {
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
|
2016-06-20 05:59:40 +08:00
|
|
|
public:
|
2017-04-03 04:10:00 +08:00
|
|
|
typedef basic_table_iterator<base_type> iterator;
|
2016-06-20 05:59:40 +08:00
|
|
|
typedef iterator const_iterator;
|
|
|
|
|
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;
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(const stack_reference& r)
|
|
|
|
: basic_table_core(r.lua_state(), r.stack_index()) {
|
|
|
|
}
|
|
|
|
basic_table_core(stack_reference&& r)
|
|
|
|
: basic_table_core(r.lua_state(), r.stack_index()) {
|
|
|
|
}
|
2017-09-14 12:35:40 +08:00
|
|
|
template <typename T, meta::enable_any<is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(lua_State* L, T&& r)
|
2017-09-14 20:45:44 +08:00
|
|
|
: base_t(L, std::forward<T>(r)) {
|
2017-12-08 10:46:43 +08:00
|
|
|
#ifdef SOL_SAFE_REFERENCES
|
2017-09-14 20:45:44 +08:00
|
|
|
auto pp = stack::push_pop(*this);
|
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_table_core>(lua_state(), -1, handler);
|
|
|
|
#endif // Safety
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2017-12-19 21:39:39 +08:00
|
|
|
basic_table_core(lua_State* L, const new_table& nt)
|
|
|
|
: base_t(L, -stack::push(L, nt)) {
|
2017-09-14 12:35:40 +08:00
|
|
|
if (!is_stack_based<meta::unqualified_t<base_type>>::value) {
|
2017-04-03 04:10:00 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(lua_State* L, int index = -1)
|
|
|
|
: basic_table_core(detail::no_safety, L, index) {
|
2017-12-08 10:46:43 +08:00
|
|
|
#ifdef SOL_SAFE_REFERENCES
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
|
|
|
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
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(lua_State* L, ref_index index)
|
|
|
|
: basic_table_core(detail::no_safety, L, index) {
|
2017-12-08 10:46:43 +08:00
|
|
|
#ifdef SOL_SAFE_REFERENCES
|
2016-12-10 13:25:25 +08:00
|
|
|
auto pp = stack::push_pop(*this);
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
2017-09-14 20:45:44 +08:00
|
|
|
stack::check<basic_table_core>(lua_state(), -1, handler);
|
2016-12-10 13:25:25 +08:00
|
|
|
#endif // Safety
|
|
|
|
}
|
2018-01-29 11:21:13 +08:00
|
|
|
template <typename T, meta::enable<meta::neg<meta::any_same<meta::unqualified_t<T>, basic_table_core>>, meta::neg<std::is_same<base_type, stack_reference>>, meta::neg<std::is_same<lua_nil_t, meta::unqualified_t<T>>>, is_lua_reference<meta::unqualified_t<T>>> = meta::enabler>
|
2017-09-13 14:46:56 +08:00
|
|
|
basic_table_core(T&& r) noexcept
|
|
|
|
: basic_table_core(detail::no_safety, std::forward<T>(r)) {
|
2017-12-08 10:46:43 +08:00
|
|
|
#ifdef 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);
|
2017-09-01 08:47:09 +08:00
|
|
|
constructor_handler handler{};
|
|
|
|
stack::check<basic_table_core>(base_t::lua_state(), -1, handler);
|
2017-04-03 04:10:00 +08:00
|
|
|
}
|
|
|
|
#endif // Safety
|
|
|
|
}
|
2018-01-29 11:21:13 +08:00
|
|
|
basic_table_core(lua_nil_t r) noexcept
|
|
|
|
: basic_table_core(detail::no_safety, r) {
|
|
|
|
}
|
2016-06-20 05:59:40 +08:00
|
|
|
|
|
|
|
iterator begin() const {
|
|
|
|
return iterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2017-09-07 03:09:51 +08:00
|
|
|
return tuple_get<false>(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), std::forward_as_tuple(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 {
|
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2017-09-07 12:51:35 +08:00
|
|
|
return traverse_get_optional<top_level, false, T>(meta::is_optional<meta::unqualified_t<T>>(), std::forward<Keys>(keys)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Keys>
|
|
|
|
basic_table_core& traverse_set(Keys&&... keys) {
|
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2017-09-13 14:46:56 +08:00
|
|
|
auto pn = stack::pop_n(base_t::lua_state(), static_cast<int>(sizeof...(Keys) - 2));
|
2017-09-07 12:51:35 +08:00
|
|
|
traverse_set_deep<top_level, false>(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) {
|
2017-09-07 03:09:51 +08:00
|
|
|
tuple_set<false>(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
|
|
|
|
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");
|
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2017-09-07 12:51:35 +08:00
|
|
|
return tuple_get<true>(types<Ret...>(), std::make_index_sequence<sizeof...(Ret)>(), std::forward_as_tuple(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 {
|
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2017-09-07 12:51:35 +08:00
|
|
|
return traverse_get_optional<top_level, true, T>(meta::is_optional<meta::unqualified_t<T>>(), std::forward<Keys>(keys)...);
|
2017-09-07 03:09:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Keys>
|
|
|
|
basic_table_core& traverse_raw_set(Keys&&... keys) {
|
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2017-09-13 14:46:56 +08:00
|
|
|
auto pn = stack::pop_n(base_t::lua_state(), static_cast<int>(sizeof...(Keys) - 2));
|
2017-09-07 12:51:35 +08:00
|
|
|
traverse_set_deep<top_level, true>(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;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& set_usertype(usertype<T>& user) {
|
2016-10-22 05:32:27 +08:00
|
|
|
return set_usertype(usertype_traits<T>::name(), user);
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Key, typename T>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& set_usertype(Key&& key, usertype<T>& user) {
|
|
|
|
return set(std::forward<Key>(key), user);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& new_usertype(const std::string& name, Args&&... args) {
|
|
|
|
usertype<Class> utype(std::forward<Args>(args)...);
|
2017-03-14 05:45:54 +08:00
|
|
|
set_usertype(name, utype);
|
2016-06-20 05:59:40 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename CTor0, typename... CTor, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& new_usertype(const std::string& name, Args&&... args) {
|
|
|
|
constructors<types<CTor0, CTor...>> ctor{};
|
|
|
|
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename... CArgs, typename... Args>
|
2016-06-20 05:59:40 +08:00
|
|
|
basic_table_core& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
|
|
|
|
usertype<Class> utype(ctor, std::forward<Args>(args)...);
|
2017-03-14 05:45:54 +08:00
|
|
|
set_usertype(name, utype);
|
2016-06-20 05:59:40 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename... Args>
|
2016-07-08 04:52:39 +08:00
|
|
|
basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) {
|
2016-09-17 23:22:19 +08:00
|
|
|
simple_usertype<Class> utype(base_t::lua_state(), std::forward<Args>(args)...);
|
2016-07-08 04:52:39 +08:00
|
|
|
set_usertype(name, utype);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename CTor0, typename... CTor, typename... Args>
|
2016-07-08 04:52:39 +08:00
|
|
|
basic_table_core& new_simple_usertype(const std::string& name, Args&&... args) {
|
|
|
|
constructors<types<CTor0, CTor...>> ctor{};
|
|
|
|
return new_simple_usertype<Class>(name, ctor, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename... CArgs, typename... Args>
|
2016-07-08 04:52:39 +08:00
|
|
|
basic_table_core& new_simple_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
|
2016-09-17 23:22:19 +08:00
|
|
|
simple_usertype<Class> utype(base_t::lua_state(), ctor, std::forward<Args>(args)...);
|
2016-07-08 04:52:39 +08:00
|
|
|
set_usertype(name, utype);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename... Args>
|
2016-09-17 23:22:19 +08:00
|
|
|
simple_usertype<Class> create_simple_usertype(Args&&... args) {
|
|
|
|
simple_usertype<Class> utype(base_t::lua_state(), std::forward<Args>(args)...);
|
|
|
|
return utype;
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename CTor0, typename... CTor, typename... Args>
|
2016-09-17 23:22:19 +08:00
|
|
|
simple_usertype<Class> create_simple_usertype(Args&&... args) {
|
|
|
|
constructors<types<CTor0, CTor...>> ctor{};
|
|
|
|
return create_simple_usertype<Class>(ctor, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Class, typename... CArgs, typename... Args>
|
2016-09-17 23:22:19 +08:00
|
|
|
simple_usertype<Class> create_simple_usertype(constructors<CArgs...> ctor, Args&&... args) {
|
|
|
|
simple_usertype<Class> utype(base_t::lua_state(), ctor, std::forward<Args>(args)...);
|
|
|
|
return utype;
|
|
|
|
}
|
|
|
|
|
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)...);
|
2016-06-20 05:59:40 +08:00
|
|
|
if (read_only) {
|
|
|
|
table x = create_with(
|
|
|
|
meta_function::new_index, detail::fail_on_newindex,
|
2017-09-13 14:46:56 +08:00
|
|
|
meta_function::index, target);
|
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) {
|
|
|
|
table target = create(items.size(), 0);
|
|
|
|
for (const auto& kvp : items) {
|
|
|
|
target.set(kvp.first, kvp.second);
|
|
|
|
}
|
|
|
|
if (read_only) {
|
|
|
|
table x = create_with(
|
|
|
|
meta_function::new_index, detail::fail_on_newindex,
|
2017-09-13 14:46:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Fx>
|
2016-06-20 05:59:40 +08:00
|
|
|
void for_each(Fx&& fx) const {
|
2017-09-14 12:35:40 +08:00
|
|
|
typedef meta::is_invokable<Fx(std::pair<object, object>)> is_paired;
|
2016-06-20 05:59:40 +08:00
|
|
|
for_each(is_paired(), std::forward<Fx>(fx));
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const {
|
|
|
|
auto pp = stack::push_pop(*this);
|
2016-08-24 09:42:27 +08:00
|
|
|
lua_len(base_t::lua_state(), -1);
|
|
|
|
return stack::pop<size_t>(base_t::lua_state());
|
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>
|
2016-06-20 05:59:40 +08:00
|
|
|
proxy<basic_table_core&, T> operator[](T&& key) & {
|
|
|
|
return proxy<basic_table_core&, T>(*this, std::forward<T>(key));
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
|
|
|
proxy<const basic_table_core&, T> operator[](T&& key) const& {
|
2016-06-20 05:59:40 +08:00
|
|
|
return proxy<const basic_table_core&, T>(*this, std::forward<T>(key));
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename T>
|
2016-06-20 05:59:40 +08:00
|
|
|
proxy<basic_table_core, T> operator[](T&& key) && {
|
|
|
|
return proxy<basic_table_core, T>(*this, std::forward<T>(key));
|
|
|
|
}
|
|
|
|
|
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);
|
2017-09-14 20:45:44 +08:00
|
|
|
(void)detail::swallow{ 0,
|
|
|
|
(stack::set_ref(base_t::lua_state(), std::forward<Args>(args)), 0)... };
|
2016-06-20 05:59:40 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename R, typename... Args, typename Fx, typename Key, typename = std::result_of_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));
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Fx, typename Key, meta::enable<meta::is_specialization_of<overload_set, meta::unqualified_t<Fx>>> = 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));
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
template <typename Fx, typename Key, typename... Args, meta::disable<meta::is_specialization_of<overload_set, meta::unqualified_t<Fx>>> = 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.");
|
2016-06-20 05:59:40 +08:00
|
|
|
static const int narr = static_cast<int>(meta::count_2_for_pack<std::is_integral, Args...>::value);
|
|
|
|
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) {
|
|
|
|
static const int narr = static_cast<int>(meta::count_2_for_pack<std::is_integral, Args...>::value);
|
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
|