2016-03-14 21:53:24 +08:00
|
|
|
// The MIT License (MIT)
|
2016-01-09 05:16:06 +08:00
|
|
|
|
2016-02-27 15:43:53 +08:00
|
|
|
// Copyright (c) 2013-2016 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"
|
2016-01-09 05:16:06 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2016-03-30 12:31:18 +08:00
|
|
|
namespace detail {
|
|
|
|
template <std::size_t n>
|
|
|
|
struct clean { lua_State* L; clean(lua_State* L) : L(L) {} ~clean() { lua_pop(L, static_cast<int>(n)); } };
|
|
|
|
struct ref_clean { lua_State* L; int& n; ref_clean(lua_State* L, int& n) : L(L), n(n) {} ~ref_clean() { lua_pop(L, static_cast<int>(n)); } };
|
|
|
|
}
|
2016-04-07 17:21:49 +08:00
|
|
|
|
|
|
|
template <bool top_level, typename base_t>
|
|
|
|
class basic_table_core : public base_t {
|
2016-01-29 08:57:02 +08:00
|
|
|
friend class state;
|
2016-01-30 17:21:33 +08:00
|
|
|
friend class state_view;
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename... Args>
|
2016-02-24 12:39:46 +08:00
|
|
|
using is_global = meta::And<meta::Bool<top_level>, meta::is_c_str<Args>...>;
|
2016-01-30 17:21:33 +08:00
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
template<typename Fx>
|
|
|
|
void for_each(std::true_type, Fx&& fx) const {
|
|
|
|
auto pp = stack::push_pop( *this );
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::push( base_t::lua_state( ), nil );
|
|
|
|
while ( lua_next( base_t::lua_state( ), -2 ) ) {
|
|
|
|
sol::object key( base_t::lua_state( ), -2 );
|
|
|
|
sol::object value( base_t::lua_state( ), -1 );
|
2016-03-12 00:34:44 +08:00
|
|
|
std::pair<sol::object&, sol::object&> keyvalue(key, value);
|
|
|
|
fx( keyvalue );
|
2016-04-07 17:21:49 +08:00
|
|
|
lua_pop( base_t::lua_state( ), 1 );
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Fx>
|
|
|
|
void for_each(std::false_type, Fx&& fx) const {
|
|
|
|
auto pp = stack::push_pop( *this );
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::push( base_t::lua_state( ), nil );
|
|
|
|
while ( lua_next( base_t::lua_state( ), -2 ) ) {
|
|
|
|
sol::object key( base_t::lua_state( ), -2 );
|
|
|
|
sol::object value( base_t::lua_state( ), -1 );
|
2016-03-12 00:34:44 +08:00
|
|
|
fx( key, value );
|
2016-04-07 17:21:49 +08:00
|
|
|
lua_pop( base_t::lua_state( ), 1 );
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
template<typename Ret0, typename Ret1, typename... Ret, std::size_t... I, typename Keys>
|
2016-03-30 12:31:18 +08:00
|
|
|
auto tuple_get( types<Ret0, Ret1, Ret...>, std::index_sequence<0, 1, I...>, Keys&& keys ) const
|
2016-03-25 03:45:44 +08:00
|
|
|
-> decltype(stack::pop<std::tuple<Ret0, Ret1, Ret...>>(nullptr)){
|
2016-03-30 12:31:18 +08:00
|
|
|
typedef decltype(stack::pop<std::tuple<Ret0, Ret1, Ret...>>(nullptr)) Tup;
|
|
|
|
return Tup(
|
2016-04-07 17:21:49 +08:00
|
|
|
traverse_get_optional<top_level, Ret0>(meta::is_specialization_of<sol::optional, meta::Unqualified<Ret0>>(), detail::forward_get<0>(keys)),
|
|
|
|
traverse_get_optional<top_level, Ret1>(meta::is_specialization_of<sol::optional, meta::Unqualified<Ret1>>(), detail::forward_get<1>(keys)),
|
|
|
|
traverse_get_optional<top_level, Ret>(meta::is_specialization_of<sol::optional, meta::Unqualified<Ret>>(), detail::forward_get<I>(keys))...
|
2016-03-30 12:31:18 +08:00
|
|
|
);
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template<typename Ret, std::size_t I, typename Keys>
|
|
|
|
decltype(auto) tuple_get( types<Ret>, std::index_sequence<I>, Keys&& keys ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
return traverse_get_optional<top_level, Ret>( meta::is_specialization_of<sol::optional, meta::Unqualified<Ret>>(), detail::forward_get<I>(keys) );
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template<typename Pairs, std::size_t... I>
|
|
|
|
void tuple_set( std::index_sequence<I...>, Pairs&& pairs ) {
|
2016-03-30 12:31:18 +08:00
|
|
|
auto pp = stack::push_pop<top_level && (is_global<decltype(detail::forward_get<I * 2>(pairs))...>::value)>(*this);
|
2016-04-07 17:21:49 +08:00
|
|
|
void(detail::swallow{ (stack::set_field<top_level>(base_t::lua_state(),
|
2016-03-30 12:31:18 +08:00
|
|
|
detail::forward_get<I * 2>(pairs),
|
2016-03-13 20:30:14 +08:00
|
|
|
detail::forward_get<I * 2 + 1>(pairs)
|
|
|
|
), 0)... });
|
2016-02-02 00:56:44 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool global, typename T, typename Key>
|
|
|
|
decltype(auto) traverse_get_deep( Key&& key ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::get_field<global>( base_t::lua_state( ), std::forward<Key>( key ) );
|
|
|
|
return stack::get<T>( base_t::lua_state( ) );
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool global, typename T, typename Key, typename... Keys>
|
|
|
|
decltype(auto) traverse_get_deep( Key&& key, Keys&&... keys ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::get_field<global>( base_t::lua_state( ), std::forward<Key>( key ) );
|
2016-02-22 08:26:58 +08:00
|
|
|
return traverse_get_deep<false, T>(std::forward<Keys>(keys)...);
|
2016-02-02 00:56:44 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
template <bool global, typename T, std::size_t I, typename Key>
|
|
|
|
decltype(auto) traverse_get_deep_optional( int& popcount, Key&& key ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
auto p = stack::probe_get_field<global>(base_t::lua_state(), std::forward<Key>(key), -1);
|
2016-04-01 04:16:07 +08:00
|
|
|
popcount += p.levels;
|
|
|
|
if (!p.success)
|
|
|
|
return T(nullopt);
|
2016-04-07 17:21:49 +08:00
|
|
|
return stack::get<T>( base_t::lua_state( ) );
|
2016-03-30 12:31:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global, typename T, std::size_t I, typename Key, typename... Keys>
|
|
|
|
decltype(auto) traverse_get_deep_optional( int& popcount, Key&& key, Keys&&... keys ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
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 ) );
|
2016-04-01 04:16:07 +08:00
|
|
|
popcount += p.levels;
|
|
|
|
if (!p.success)
|
|
|
|
return T(nullopt);
|
2016-03-30 12:31:18 +08:00
|
|
|
return traverse_get_deep_optional<false, T, I + 1>(popcount, std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global, typename T, typename... Keys>
|
|
|
|
decltype(auto) traverse_get_optional( std::false_type, Keys&&... keys ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
detail::clean<sizeof...(Keys)> c(base_t::lua_state());
|
2016-03-30 12:31:18 +08:00
|
|
|
return traverse_get_deep<top_level, T>(std::forward<Keys>(keys)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global, typename T, typename... Keys>
|
|
|
|
decltype(auto) traverse_get_optional( std::true_type, Keys&&... keys ) const {
|
2016-04-01 04:16:07 +08:00
|
|
|
int popcount = 0;
|
2016-04-07 17:21:49 +08:00
|
|
|
detail::ref_clean c(base_t::lua_state(), popcount);
|
2016-04-01 04:16:07 +08:00
|
|
|
return traverse_get_deep_optional<top_level, T, 0>(popcount, std::forward<Keys>(keys)...);
|
2016-03-30 12:31:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool global, typename Key, typename Value>
|
|
|
|
void traverse_set_deep( Key&& key, Value&& value ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::set_field<global>( base_t::lua_state( ), std::forward<Key>( key ), std::forward<Value>(value) );
|
2016-02-02 00:56:44 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool global, typename Key, typename... Keys>
|
|
|
|
void traverse_set_deep( Key&& key, Keys&&... keys ) const {
|
2016-04-07 17:21:49 +08:00
|
|
|
stack::get_field<global>( base_t::lua_state( ), std::forward<Key>( key ) );
|
2016-02-14 09:14:31 +08:00
|
|
|
traverse_set_deep<false>(std::forward<Keys>(keys)...);
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core(lua_State* L, detail::global_tag t) noexcept : reference(L, t) { }
|
2016-02-25 02:59:17 +08:00
|
|
|
|
2016-01-09 05:16:06 +08:00
|
|
|
public:
|
2016-04-07 17:21:49 +08:00
|
|
|
typedef basic_table_iterator<base_t> iterator;
|
|
|
|
typedef iterator const_iterator;
|
|
|
|
|
|
|
|
basic_table_core( ) noexcept : base_t( ) { }
|
|
|
|
basic_table_core( const table_core<true>& global ) noexcept : base_t( global ) { }
|
|
|
|
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()) {}
|
|
|
|
basic_table_core( lua_State* L, int index = -1 ) : base_t( L, index ) {
|
2016-04-04 14:28:39 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
2016-01-29 08:57:02 +08:00
|
|
|
type_assert( L, index, type::table );
|
2016-04-04 14:28:39 +08:00
|
|
|
#endif // Safety
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
iterator begin () const {
|
|
|
|
return iterator(*this);
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
iterator end() const {
|
|
|
|
return iterator();
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
const_iterator cbegin() const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return begin();
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
const_iterator cend() const {
|
2016-03-12 00:34:44 +08:00
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:57:02 +08:00
|
|
|
template<typename... Ret, typename... Keys>
|
2016-01-30 17:21:33 +08:00
|
|
|
decltype(auto) get( Keys&&... keys ) const {
|
2016-03-30 12:31:18 +08:00
|
|
|
static_assert(sizeof...(Keys) == sizeof...(Ret), "number of keys and number of return types do not match");
|
2016-04-01 04:16:07 +08:00
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
|
|
|
return tuple_get( types<Ret...>( ), std::make_index_sequence<sizeof...(Ret)>( ), std::forward_as_tuple(std::forward<Keys>(keys)...));
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:40:40 +08:00
|
|
|
template<typename T, typename Key>
|
|
|
|
decltype(auto) get_or(Key&& key, T&& otherwise) const {
|
|
|
|
typedef decltype(get<T>("")) U;
|
|
|
|
sol::optional<U> option = get<sol::optional<U>>(std::forward<Key>(key));
|
|
|
|
if (option) {
|
|
|
|
return static_cast<U>(option.value());
|
|
|
|
}
|
|
|
|
return static_cast<U>(std::forward<T>(otherwise));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename Key, typename D>
|
|
|
|
decltype(auto) get_or(Key&& key, D&& otherwise) const {
|
|
|
|
sol::optional<T> option = get<sol::optional<T>>(std::forward<Key>(key));
|
|
|
|
if (option) {
|
|
|
|
return static_cast<T>(option.value());
|
|
|
|
}
|
|
|
|
return static_cast<T>(std::forward<D>(otherwise));
|
2016-03-30 02:03:19 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename T, typename... Keys>
|
|
|
|
decltype(auto) traverse_get( Keys&&... keys ) const {
|
2016-02-26 00:07:39 +08:00
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2016-04-07 17:21:49 +08:00
|
|
|
return traverse_get_optional<top_level, T>(meta::is_specialization_of<sol::optional, meta::Unqualified<T>>(), std::forward<Keys>(keys)...);
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename... Keys>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& traverse_set( Keys&&... keys ) {
|
2016-02-26 00:07:39 +08:00
|
|
|
auto pp = stack::push_pop<is_global<Keys...>::value>(*this);
|
2016-02-22 08:26:58 +08:00
|
|
|
traverse_set_deep<top_level>(std::forward<Keys>(keys)...);
|
2016-04-07 17:21:49 +08:00
|
|
|
lua_pop(base_t::lua_state(), static_cast<int>(sizeof...(Keys)-2));
|
2016-02-22 08:26:58 +08:00
|
|
|
return *this;
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template<typename... Args>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& set( Args&&... args ) {
|
2016-02-14 09:14:31 +08:00
|
|
|
tuple_set(std::make_index_sequence<sizeof...(Args) / 2>(), std::forward_as_tuple(std::forward<Args>(args)...));
|
|
|
|
return *this;
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& set_usertype( usertype<T>& user ) {
|
2016-02-03 05:18:44 +08:00
|
|
|
return set_usertype(usertype_traits<T>::name, user);
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Key, typename T>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& set_usertype( Key&& key, usertype<T>& user ) {
|
2016-02-03 05:18:44 +08:00
|
|
|
return set(std::forward<Key>(key), user);
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
template<typename Class, typename... Args>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& new_usertype(const std::string& name, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
usertype<Class> utype(std::forward<Args>(args)...);
|
|
|
|
set_usertype(name, utype);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename CTor0, typename... CTor, typename... Args>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& new_usertype(const std::string& name, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
constructors<types<CTor0, CTor...>> ctor{};
|
|
|
|
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class, typename... CArgs, typename... Args>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
usertype<Class> utype(ctor, std::forward<Args>(args)...);
|
|
|
|
set_usertype(name, utype);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:57:02 +08:00
|
|
|
template<typename Fx>
|
|
|
|
void for_each( Fx&& fx ) const {
|
2016-03-12 00:34:44 +08:00
|
|
|
typedef meta::is_callable<Fx( std::pair<sol::object, sol::object> )> is_paired;
|
|
|
|
for_each(is_paired(), std::forward<Fx>(fx));
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t size( ) const {
|
2016-02-26 00:07:39 +08:00
|
|
|
auto pp = stack::push_pop( *this );
|
2016-04-07 17:21:49 +08:00
|
|
|
return lua_rawlen(base_t::lua_state(), -1);
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
bool empty() const {
|
|
|
|
return cbegin() == cend();
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:57:02 +08:00
|
|
|
template<typename T>
|
2016-04-07 17:21:49 +08:00
|
|
|
proxy<basic_table_core&, T> operator[]( T&& key ) & {
|
|
|
|
return proxy<basic_table_core&, T>( *this, std::forward<T>( key ) );
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-07 17:21:49 +08:00
|
|
|
proxy<const basic_table_core&, T> operator[]( T&& key ) const & {
|
|
|
|
return proxy<const basic_table_core&, T>( *this, std::forward<T>( key ) );
|
2016-01-30 17:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-07 17:21:49 +08:00
|
|
|
proxy<basic_table_core, T> operator[]( T&& key ) && {
|
|
|
|
return proxy<basic_table_core, T>( *this, std::forward<T>( key ) );
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
template<typename Sig, typename Key, typename... Args>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& set_function( Key&& key, Args&&... args ) {
|
2016-03-30 12:31:18 +08:00
|
|
|
set_fx( types<Sig>( ), std::forward<Key>( key ), std::forward<Args>( args )... );
|
2016-03-25 03:45:44 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-03-30 12:31:18 +08:00
|
|
|
template<typename Key, typename... Args>
|
2016-04-07 17:21:49 +08:00
|
|
|
basic_table_core& set_function( Key&& key, Args&&... args ) {
|
2016-03-30 12:31:18 +08:00
|
|
|
set_fx( types<>( ), std::forward<Key>( key ), std::forward<Args>( args )... );
|
2016-01-29 08:57:02 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2016-01-09 05:16:06 +08:00
|
|
|
|
|
|
|
private:
|
2016-02-05 09:16:53 +08:00
|
|
|
template<typename R, typename... Args, typename Fx, typename Key, typename = std::result_of_t<Fx( Args... )>>
|
2016-01-29 08:57:02 +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 ) );
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
template<typename Fx, typename Key, meta::EnableIf<meta::is_specialization_of<overload_set, meta::Unqualified<Fx>>> = 0>
|
2016-02-11 00:36:00 +08:00
|
|
|
void set_fx( types<>, Key&& key, Fx&& fx ) {
|
|
|
|
set(std::forward<Key>(key), std::forward<Fx>(fx));
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
template<typename Fx, typename Key, typename... Args, meta::DisableIf<meta::is_specialization_of<overload_set, meta::Unqualified<Fx>>> = 0>
|
2016-03-30 12:31:18 +08:00
|
|
|
void set_fx( types<>, Key&& key, Fx&& fx, Args&&... args ) {
|
|
|
|
set(std::forward<Key>(key), function_args(std::forward<Fx>(fx), std::forward<Args>(args)...));
|
2016-01-29 08:57:02 +08:00
|
|
|
}
|
|
|
|
|
2016-02-03 05:18:44 +08:00
|
|
|
template<typename... Sig, typename... Args, typename Key>
|
2016-01-29 08:57:02 +08:00
|
|
|
void set_resolved_function( Key&& key, Args&&... args ) {
|
2016-03-30 12:31:18 +08:00
|
|
|
set(std::forward<Key>(key), function_args<function_sig<Sig...>>(std::forward<Args>(args)...));
|
2016-02-02 00:56:44 +08:00
|
|
|
}
|
2016-02-27 15:43:53 +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;
|
|
|
|
}
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
static inline table create_with(lua_State* L, Args&&... args) {
|
|
|
|
static const int narr = static_cast<int>(meta::count_if_2_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) {
|
2016-04-07 17:21:49 +08:00
|
|
|
return create(base_t::lua_state(), narr, nrec);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key, typename Value, typename... Args>
|
|
|
|
table create(int narr, int nrec, Key&& key, Value&& value, Args&&... args) {
|
2016-04-07 17:21:49 +08:00
|
|
|
return create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name>
|
|
|
|
table create(Name&& name, int narr = 0, int nrec = 0) {
|
2016-04-07 17:21:49 +08:00
|
|
|
table x = create(base_t::lua_state(), narr, nrec);
|
2016-03-25 03:45:44 +08:00
|
|
|
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) {
|
2016-04-07 17:21:49 +08:00
|
|
|
table x = create(base_t::lua_state(), narr, nrec, std::forward<Key>(key), std::forward<Value>(value), std::forward<Args>(args)...);
|
2016-03-25 03:45:44 +08:00
|
|
|
this->set(std::forward<Name>(name), x);
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
table create_with(Args&&... args) {
|
2016-04-07 17:21:49 +08:00
|
|
|
return create_with(base_t::lua_state(), std::forward<Args>(args)...);
|
2016-03-30 12:31:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Name, typename... Args>
|
|
|
|
table create_named(Name&& name, Args&&... args) {
|
|
|
|
static const int narr = static_cast<int>(meta::count_if_2_pack<std::is_integral, Args...>::value);
|
2016-03-30 14:07:09 +08:00
|
|
|
return create(std::forward<Name>(name), narr, sizeof...(Args) / 2 - narr, std::forward<Args>(args)...);
|
2016-03-30 12:31:18 +08:00
|
|
|
}
|
2016-01-09 05:16:06 +08:00
|
|
|
};
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_TABLE_CORE_HPP
|