variadic set - introduction of state_view type to allow for all of the benefits of state without destructing lua instance.

This commit is contained in:
ThePhD 2016-01-30 04:21:33 -05:00
parent f21f9c9959
commit 27f09fbb35
6 changed files with 381 additions and 280 deletions

View File

@ -30,13 +30,13 @@ namespace sol {
template<typename Table, typename Key>
struct proxy {
private:
Table& tbl;
Table tbl;
If<std::is_array<Unqualified<Key>>, Key&, Unqualified<Key>> key;
public:
template<typename T>
proxy(Table& table, T&& key) : tbl(table), key(std::forward<T>(key)) {}
proxy(Table table, T&& key) : tbl(table), key(std::forward<T>(key)) {}
template<typename T>
proxy& set(T&& item) {

View File

@ -25,6 +25,15 @@
#include "types.hpp"
namespace sol {
namespace detail {
template <typename T>
struct push_pop {
T t;
push_pop (T x) : t(x) { t.push(); }
~push_pop() { t.pop(); }
};
} // detail
class reference {
private:
lua_State* L = nullptr; // non-owning

View File

@ -133,6 +133,29 @@ true;
false;
#endif
inline void lua_getglobali(lua_State* L, lua_Integer n) {
#if SOL_LUA_VERSION >= 503
lua_geti(L, LUA_RIDX_GLOBALS, n);
#else
lua_pushglobaltable(L);
lua_pushinteger(L, n);
lua_gettable(L, -2);
lua_remove(L, -2); // remove the global table, leave final value on the stack
#endif
}
inline void lua_setglobali(lua_State* L, lua_Integer n) {
#if SOL_LUA_VERSION >= 503
lua_seti(L, LUA_RIDX_GLOBALS, n);
#else
lua_pushglobaltable(L);
lua_pushinteger(L, n);
lua_pushvalue(L, -3)
lua_settable(L, -3);
lua_pop(L, 2); // remove table, and the copy of the value
#endif
}
template <typename T, typename Key, typename... Args>
inline int push_confirmed_userdata(lua_State* L, Key&& metatablekey, Args&&... args) {
T* pdatum = static_cast<T*>(lua_newuserdata(L, sizeof(T)));

View File

@ -1,6 +1,6 @@
// The MIT License (MIT)
// Copyright (c) 2013-2015 Rapptz and contributors
// Copyright (c) 2013-2016 Rapptz and contributors
// 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
@ -22,256 +22,19 @@
#ifndef SOL_STATE_HPP
#define SOL_STATE_HPP
#include "error.hpp"
#include "table.hpp"
#include <memory>
#include "state_view.hpp"
namespace sol {
namespace detail {
inline int atpanic(lua_State* L) {
const char* message = lua_tostring(L, -1);
std::string err = message ? message : "An unexpected error occurred and forced the lua state to call atpanic";
throw error(err);
}
} // detail
enum class lib : char {
base,
package,
coroutine,
string,
os,
math,
table,
debug,
bit32,
io,
count
};
class state {
class state : private std::unique_ptr<lua_State, void(*)(lua_State*)>, public state_view {
private:
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
table reg;
global_table globals;
typedef std::unique_ptr<lua_State, void(*)(lua_State*)> unique_base;
public:
using state_view::get;
state(lua_CFunction panic = detail::atpanic):
L(luaL_newstate(), lua_close),
reg(L.get(), LUA_REGISTRYINDEX),
globals(detail::global_overload, reg) {
unique_base(luaL_newstate(), lua_close),
state_view(unique_base::get()) {
set_panic(panic);
}
lua_State* lua_state() const {
return L.get();
}
template<typename... Args>
void open_libraries(Args&&... args) {
static_assert(are_same<lib, Args...>::value, "all types must be libraries");
if(sizeof...(args) == 0) {
luaL_openlibs(L.get());
return;
}
lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
for(auto&& library : libraries) {
switch(library) {
#if SOL_LUA_VERSION <= 501 && defined(SOL_LUAJIT)
case lib::coroutine:
#endif // luajit opens coroutine base stuff
case lib::base:
luaL_requiref(L.get(), "base", luaopen_base, 1);
lua_pop(L.get(), 1);
break;
case lib::package:
luaL_requiref(L.get(), "package", luaopen_package, 1);
lua_pop(L.get(), 1);
break;
#if SOL_LUA_VERSION > 501
case lib::coroutine:
luaL_requiref(L.get(), "coroutine", luaopen_coroutine, 1);
lua_pop(L.get(), 1);
break;
#endif // Lua 5.2+ only
case lib::string:
luaL_requiref(L.get(), "string", luaopen_string, 1);
lua_pop(L.get(), 1);
break;
case lib::table:
luaL_requiref(L.get(), "table", luaopen_table, 1);
lua_pop(L.get(), 1);
break;
case lib::math:
luaL_requiref(L.get(), "math", luaopen_math, 1);
lua_pop(L.get(), 1);
break;
case lib::bit32:
#if SOL_LUA_VERSION > 510
luaL_requiref(L.get(), "bit32", luaopen_bit32, 1);
lua_pop(L.get(), 1);
#else
#endif // Lua 5.2+ only
break;
case lib::io:
luaL_requiref(L.get(), "io", luaopen_io, 1);
lua_pop(L.get(), 1);
break;
case lib::os:
luaL_requiref(L.get(), "os", luaopen_os, 1);
lua_pop(L.get(), 1);
break;
case lib::debug:
luaL_requiref(L.get(), "debug", luaopen_debug, 1);
lua_pop(L.get(), 1);
break;
case lib::count:
break;
}
}
}
void script(const std::string& code) {
if(luaL_dostring(L.get(), code.c_str())) {
lua_error(L.get());
}
}
void open_file(const std::string& filename) {
if(luaL_dofile(L.get(), filename.c_str())) {
lua_error(L.get());
}
}
template<typename... Args, typename... Keys>
auto get(Keys&&... keys) const
-> decltype(globals.get<Args...>(std::forward<Keys>(keys)...)) {
return globals.get<Args...>(std::forward<Keys>(keys)...);
}
template<typename T, typename U>
state& set(T&& key, U&& value) {
globals.set(std::forward<T>(key), std::forward<U>(value));
return *this;
}
template<typename T>
SOL_DEPRECATED table& set_userdata(usertype<T>& user) {
return set_usertype(user);
}
template<typename Key, typename T>
SOL_DEPRECATED table& set_userdata(Key&& key, usertype<T>& user) {
return set_usertype(std::forward<Key>(key), user);
}
template<typename Class, typename... CTor, typename... Args>
SOL_DEPRECATED state& new_userdata(const std::string& name, Args&&... args) {
return new_usertype<Class>(name, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
SOL_DEPRECATED state& new_userdata(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
return new_usertype(name, std::move(ctor), std::forward<Args>(args)...);
}
template<typename T>
state& set_usertype(usertype<T>& user) {
return set_usertype(usertype_traits<T>::name, user);
}
template<typename Key, typename T>
state& set_usertype(Key&& key, usertype<T>& user) {
globals.set_usertype(std::forward<Key>(key), user);
return *this;
}
template<typename Class, typename... CTor, typename... Args>
state& new_usertype(const std::string& name, Args&&... args) {
constructors<types<CTor...>> ctor{};
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
state& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
usertype<Class> utype(ctor, std::forward<Args>(args)...);
set_usertype(name, utype);
return *this;
}
template <typename Fx>
void for_each(Fx&& fx) {
globals.for_each(std::forward<Fx>(fx));
}
template<typename T>
table create_table(T&& key, int narr = 0, int nrec = 0) {
lua_createtable(L.get(), narr, nrec);
table result(L.get());
lua_pop(L.get(), 1);
globals.set(std::forward<T>(key), result);
return result;
}
table create_table(int narr = 0, int nrec = 0) {
lua_createtable(L.get(), narr, nrec);
table result(L.get());
lua_pop(L.get(), 1);
return result;
}
global_table global() const {
return globals;
}
table registry() const {
return reg;
}
void set_panic(lua_CFunction panic){
lua_atpanic(L.get(), panic);
}
template<typename T>
proxy<global_table, T> operator[](T&& key) {
return globals[std::forward<T>(key)];
}
template<typename T>
proxy<const global_table, T> operator[](T&& key) const {
return globals[std::forward<T>(key)];
}
template<typename... Args, typename R, typename Key>
state& set_function(Key&& key, R fun_ptr(Args...)){
globals.set_function(std::forward<Key>(key), fun_ptr);
return *this;
}
template<typename Sig, typename Key>
state& set_function(Key&& key, Sig* fun_ptr){
globals.set_function(std::forward<Key>(key), fun_ptr);
return *this;
}
template<typename... Args, typename R, typename C, typename T, typename Key>
state& set_function(Key&& key, R (C::*mem_ptr)(Args...), T&& obj) {
globals.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
return *this;
}
template<typename Sig, typename C, typename T, typename Key>
state& set_function(Key&& key, Sig C::* mem_ptr, T&& obj) {
globals.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
return *this;
}
template<typename... Sig, typename Fx, typename Key>
state& set_function(Key&& key, Fx&& fx) {
globals.set_function<Sig...>(std::forward<Key>(key), std::forward<Fx>(fx));
return *this;
}
};
} // sol

280
sol/state_view.hpp Normal file
View File

@ -0,0 +1,280 @@
// The MIT License (MIT)
// Copyright (c) 2013-2016 Rapptz and contributors
// 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_STATE_VIEW_HPP
#define SOL_STATE_VIEW_HPP
#include "error.hpp"
#include "table.hpp"
#include <memory>
namespace sol {
namespace detail {
inline int atpanic(lua_State* L) {
const char* message = lua_tostring(L, -1);
std::string err = message ? message : "An unexpected error occurred and forced the lua state to call atpanic";
throw error(err);
}
} // detail
enum class lib : char {
base,
package,
coroutine,
string,
os,
math,
table,
debug,
bit32,
io,
count
};
class state_view {
private:
lua_State* L;
table reg;
global_table globals;
public:
state_view(lua_State* L):
L(L),
reg(L, LUA_REGISTRYINDEX),
globals(detail::global_overload, reg) {
}
lua_State* lua_state() const {
return L;
}
template<typename... Args>
void open_libraries(Args&&... args) {
static_assert(are_same<lib, Args...>::value, "all types must be libraries");
if(sizeof...(args) == 0) {
luaL_openlibs(L);
return;
}
lib libraries[1 + sizeof...(args)] = { lib::count, std::forward<Args>(args)... };
for(auto&& library : libraries) {
switch(library) {
#if SOL_LUA_VERSION <= 501 && defined(SOL_LUAJIT)
case lib::coroutine:
#endif // luajit opens coroutine base stuff
case lib::base:
luaL_requiref(L, "base", luaopen_base, 1);
lua_pop(L, 1);
break;
case lib::package:
luaL_requiref(L, "package", luaopen_package, 1);
lua_pop(L, 1);
break;
#if SOL_LUA_VERSION > 501
case lib::coroutine:
luaL_requiref(L, "coroutine", luaopen_coroutine, 1);
lua_pop(L, 1);
break;
#endif // Lua 5.2+ only
case lib::string:
luaL_requiref(L, "string", luaopen_string, 1);
lua_pop(L, 1);
break;
case lib::table:
luaL_requiref(L, "table", luaopen_table, 1);
lua_pop(L, 1);
break;
case lib::math:
luaL_requiref(L, "math", luaopen_math, 1);
lua_pop(L, 1);
break;
case lib::bit32:
#if SOL_LUA_VERSION > 510
luaL_requiref(L, "bit32", luaopen_bit32, 1);
lua_pop(L, 1);
#else
#endif // Lua 5.2+ only
break;
case lib::io:
luaL_requiref(L, "io", luaopen_io, 1);
lua_pop(L, 1);
break;
case lib::os:
luaL_requiref(L, "os", luaopen_os, 1);
lua_pop(L, 1);
break;
case lib::debug:
luaL_requiref(L, "debug", luaopen_debug, 1);
lua_pop(L, 1);
break;
case lib::count:
break;
}
}
}
void script(const std::string& code) {
if(luaL_dostring(L, code.c_str())) {
lua_error(L);
}
}
void open_file(const std::string& filename) {
if(luaL_dofile(L, filename.c_str())) {
lua_error(L);
}
}
template<typename... Args, typename... Keys>
auto get(Keys&&... keys) const
-> decltype(globals.get<Args...>(std::forward<Keys>(keys)...)) {
return globals.get<Args...>(std::forward<Keys>(keys)...);
}
template<typename T, typename U>
state_view& set(T&& key, U&& value) {
globals.set(std::forward<T>(key), std::forward<U>(value));
return *this;
}
template<typename T>
SOL_DEPRECATED table& set_userdata(usertype<T>& user) {
return set_usertype(user);
}
template<typename Key, typename T>
SOL_DEPRECATED table& set_userdata(Key&& key, usertype<T>& user) {
return set_usertype(std::forward<Key>(key), user);
}
template<typename Class, typename... CTor, typename... Args>
SOL_DEPRECATED state_view& new_userdata(const std::string& name, Args&&... args) {
return new_usertype<Class>(name, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
SOL_DEPRECATED state_view& new_userdata(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
return new_usertype(name, std::move(ctor), std::forward<Args>(args)...);
}
template<typename T>
state_view& set_usertype(usertype<T>& user) {
return set_usertype(usertype_traits<T>::name, user);
}
template<typename Key, typename T>
state_view& set_usertype(Key&& key, usertype<T>& user) {
globals.set_usertype(std::forward<Key>(key), user);
return *this;
}
template<typename Class, typename... CTor, typename... Args>
state_view& new_usertype(const std::string& name, Args&&... args) {
constructors<types<CTor...>> ctor{};
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
}
template<typename Class, typename... CArgs, typename... Args>
state_view& new_usertype(const std::string& name, constructors<CArgs...> ctor, Args&&... args) {
usertype<Class> utype(ctor, std::forward<Args>(args)...);
set_usertype(name, utype);
return *this;
}
template <typename Fx>
void for_each(Fx&& fx) {
globals.for_each(std::forward<Fx>(fx));
}
template<typename T>
table create_table(T&& key, int narr = 0, int nrec = 0) {
lua_createtable(L, narr, nrec);
table result(L);
lua_pop(L, 1);
globals.set(std::forward<T>(key), result);
return result;
}
template <typename... Tn>
table create_table(int narr = 0, int nrec = sizeof...(Tn), Tn&&... argn) {
lua_createtable(L, narr, nrec);
table result(L);
result.set(std::forward<Tn>(argn)...);
lua_pop(L, 1);
return result;
}
global_table global() const {
return globals;
}
table registry() const {
return reg;
}
void set_panic(lua_CFunction panic){
lua_atpanic(L, panic);
}
template<typename T>
proxy<global_table&, T> operator[](T&& key) {
return globals[std::forward<T>(key)];
}
template<typename T>
proxy<const global_table, T> operator[](T&& key) const {
return globals[std::forward<T>(key)];
}
template<typename... Args, typename R, typename Key>
state_view& set_function(Key&& key, R fun_ptr(Args...)){
globals.set_function(std::forward<Key>(key), fun_ptr);
return *this;
}
template<typename Sig, typename Key>
state_view& set_function(Key&& key, Sig* fun_ptr){
globals.set_function(std::forward<Key>(key), fun_ptr);
return *this;
}
template<typename... Args, typename R, typename C, typename T, typename Key>
state_view& set_function(Key&& key, R (C::*mem_ptr)(Args...), T&& obj) {
globals.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
return *this;
}
template<typename Sig, typename C, typename T, typename Key>
state_view& set_function(Key&& key, Sig C::* mem_ptr, T&& obj) {
globals.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
return *this;
}
template<typename... Sig, typename Fx, typename Key>
state_view& set_function(Key&& key, Fx&& fx) {
globals.set_function<Sig...>(std::forward<Key>(key), std::forward<Fx>(fx));
return *this;
}
};
} // sol
#endif // SOL_STATE_VIEW_HPP

View File

@ -1,6 +1,6 @@
// The MIT License (MIT)
// Copyright (c) 2013-2015 Danny Y., Rapptz
// Copyright (c) 2013-2015 Rapptz and contributors
// 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
@ -29,27 +29,31 @@
namespace sol {
namespace detail {
struct global_overload_tag { } const global_overload;
struct global_overload_tag { } const global_overload;
} // detail
template <bool top_level>
class table_core : public reference {
friend class state;
template<typename T, typename Key, EnableIf<Not<std::is_arithmetic<Unqualified<Key>>>, Bool<top_level>> = 0>
stack::get_return<T> single_get( Key&& key ) const {
lua_getglobal( lua_state( ), &key[ 0 ] );
stack::get_return<T> result = stack::pop<T>( lua_state( ) );
return result;
friend class state_view;
template<typename T, typename Key, EnableIf<Not<std::is_integral<Unqualified<Key>>>, Bool<top_level>> = 0>
decltype(auto) single_get( Key&& key ) const {
lua_getglobal(lua_state( ), &key[0]);
return stack::pop<T>(lua_state());
}
template<typename T, typename Key, DisableIf<Not<std::is_arithmetic<Unqualified<Key>>>, Bool<top_level>> = 0>
stack::get_return<T> single_get( Key&& key ) const {
push( );
template<typename T, typename Key, EnableIf<std::is_integral<Unqualified<Key>>, Bool<top_level>> = 0>
decltype(auto) single_get(Key&& key) const {
stack::detail::lua_getglobali(lua_state(), &key[0]);
return stack::pop<T>(lua_state());
}
template<typename T, typename Key, DisableIf<Not<std::is_void<Key>>, Bool<top_level>> = 0>
decltype(auto) single_get( Key&& key ) const {
stack::push( lua_state( ), std::forward<Key>( key ) );
lua_gettable( lua_state( ), -2 );
stack::get_return<T> result = stack::pop<T>( lua_state( ) );
pop( );
return result;
return stack::pop<T>( lua_state( ) );
}
template<typename Keys, typename... Ret, std::size_t... I>
@ -58,10 +62,35 @@ class table_core : public reference {
}
template<typename Keys, typename Ret, std::size_t I>
stack::get_return<Ret> tuple_get( types<Ret>, indices<I>, Keys&& keys ) const {
decltype(auto) tuple_get( types<Ret>, indices<I>, Keys&& keys ) const {
return single_get<Ret>( std::get<I>( keys ) );
}
template<typename Key, typename U, EnableIf<std::is_integral<Unqualified<Key>>, Bool<top_level>> = 0>
void single_set( Key&& key, U&& value ) {
stack::push( lua_state( ), std::forward<Value>( value ) );
stack::detail::lua_setglobali( lua_state( ), std::forward<Key>(key) );
}
template<typename Key, typename Value, EnableIf<Not<std::is_integral<Unqualified<Key>>>, Bool<top_level>> = 0>
void single_set( Key&& key, Value&& value ) {
stack::push( lua_state( ), std::forward<Value>( value ) );
lua_setglobal( lua_state( ), &key[0] );
}
template<typename Key, typename Value, DisableIf<Not<std::is_void<Key>>, Bool<top_level>> = 0>
void single_set(Key&& key, Value&& value) {
stack::push(lua_state(), std::forward<Key>(key));
stack::push(lua_state(), std::forward<Value>(value));
lua_settable(lua_state(), -3);
}
template<typename Pairs, std::size_t... I>
void tuple_set( indices<I...>, Pairs&& pairs ) {
using swallow = int[];
swallow{ 0, ( single_set(std::get<I * 2>(pairs), std::get<I * 2 + 1>(pairs)) , 0)..., 0 };
}
#if SOL_LUA_VERSION < 502
table_core( detail::global_overload_tag, const table_core<false>& reg ) noexcept : reference( reg.lua_state(), LUA_GLOBALSINDEX ) { }
#else
@ -75,23 +104,15 @@ public:
}
template<typename... Ret, typename... Keys>
stack::get_return<Ret...> get( Keys&&... keys ) const {
return tuple_get( types<Ret...>( ), build_indices<sizeof...( Ret )>( ), std::tie( keys... ) );
decltype(auto) get( Keys&&... keys ) const {
auto pp = detail::push_pop<const table_core&>(*this);
return tuple_get( types<Ret...>( ), build_indices<sizeof...( Ret )>( ), std::forward_as_tuple(std::forward<Keys>(keys)...));
}
template<typename T, typename U>
table_core& set( T&& key, U&& value ) {
if ( top_level ) {
stack::push( lua_state( ), std::forward<U>( value ) );
lua_setglobal( lua_state( ), &key[0] );
}
else {
push( );
stack::push( lua_state( ), std::forward<T>( key ) );
stack::push( lua_state( ), std::forward<U>( value ) );
lua_settable( lua_state( ), -3 );
pop( );
}
template<typename... Tn>
table_core& set( Tn&&... argn ) {
auto pp = detail::push_pop<const table_core&>(*this);
tuple_set(build_indices<sizeof...(Tn) / 2>(), std::forward_as_tuple(std::forward<Tn>(argn)...));
return *this;
}
@ -148,13 +169,18 @@ public:
}
template<typename T>
proxy<table_core, T> operator[]( T&& key ) {
return proxy<table_core, T>( *this, std::forward<T>( key ) );
proxy<table_core&, T> operator[]( T&& key ) & {
return proxy<table_core&, T>( *this, std::forward<T>( key ) );
}
template<typename T>
proxy<const table_core, T> operator[]( T&& key ) const {
return proxy<const table_core, T>( *this, std::forward<T>( key ) );
proxy<const table_core&, T> operator[]( T&& key ) const & {
return proxy<const table_core&, T>( *this, std::forward<T>( key ) );
}
template<typename T>
proxy<table_core, T> operator[]( T&& key ) && {
return proxy<table_core, T>( *this, std::forward<T>( key ) );
}
void pop( int n = 1 ) const noexcept {