mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Heavily improved benchmark and first set of optimizations to sol/table.hpp
, providing much lower access times for calling a function.
This commit is contained in:
parent
f4b31c951c
commit
9ff968a09b
433
Results - Debug.html
Normal file
433
Results - Debug.html
Normal file
File diff suppressed because one or more lines are too long
433
Results - Release.html
Normal file
433
Results - Release.html
Normal file
File diff suppressed because one or more lines are too long
58
bench.cpp
58
bench.cpp
|
@ -1,6 +1,60 @@
|
||||||
#define NONIUS_RUNNER
|
#define NONIUS_RUNNER
|
||||||
#include "nonius/nonius.h++"
|
#include "nonius/nonius.h++"
|
||||||
|
#include "sol.hpp"
|
||||||
|
|
||||||
NONIUS_BENCHMARK( "Function Calls", []() {
|
struct sol_function_result_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua;
|
||||||
|
lua.script( R"(
|
||||||
|
function r ()
|
||||||
|
return 1 + 1
|
||||||
|
end
|
||||||
|
)" );
|
||||||
|
sol::function r = lua[ "r" ];
|
||||||
|
meter.measure( [ & ] ( int run_index ) {
|
||||||
|
// Measure cost of lua calling and returning a function
|
||||||
|
return r( );
|
||||||
} );
|
} );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sol_direct_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua;
|
||||||
|
lua.script( R"(
|
||||||
|
function r ()
|
||||||
|
return 1 + 1
|
||||||
|
end
|
||||||
|
)" );
|
||||||
|
auto fun = [ & ] ( int run_index ) {
|
||||||
|
// Measure cost of lua calling and returning a function
|
||||||
|
sol::function r = lua[ "r" ];
|
||||||
|
return r.call<int>( );
|
||||||
|
};
|
||||||
|
meter.measure( fun );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct c_direct_bench {
|
||||||
|
void operator () ( nonius::chronometer meter ) const {
|
||||||
|
sol::state lua;
|
||||||
|
lua.script( R"(
|
||||||
|
function r ()
|
||||||
|
return 1 + 1
|
||||||
|
end
|
||||||
|
)" );
|
||||||
|
|
||||||
|
lua_State* L = lua.lua_state( );
|
||||||
|
meter.measure( [ & ] ( int run_index ) {
|
||||||
|
lua_getglobal( L, "r" );
|
||||||
|
lua_call( L, 0, 1 );
|
||||||
|
int lua_out = (int)lua_tonumber( L, -1 );
|
||||||
|
lua_pop( L, 1 );
|
||||||
|
return lua_out;
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
NONIUS_BENCHMARK( "sol - function_result", sol_function_result_bench() );
|
||||||
|
NONIUS_BENCHMARK( "sol - int", sol_direct_bench() );
|
||||||
|
NONIUS_BENCHMARK( "C - int", c_direct_bench( ) );
|
||||||
|
|
|
@ -128,40 +128,40 @@ private:
|
||||||
int stack;
|
int stack;
|
||||||
handler(const reference& target) : target(target), stack(0) {
|
handler(const reference& target) : target(target), stack(0) {
|
||||||
if (target.valid()) {
|
if (target.valid()) {
|
||||||
stack = lua_gettop(target.state()) + 1;
|
stack = lua_gettop(target.lua_state()) + 1;
|
||||||
target.push();
|
target.push();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~handler() {
|
~handler() {
|
||||||
if (stack > 0) {
|
if (stack > 0) {
|
||||||
lua_remove(target.state(), stack);
|
lua_remove(target.lua_state(), stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int luacodecall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const {
|
int luacodecall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const {
|
||||||
return lua_pcallk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stack, 0, nullptr);
|
return lua_pcallk(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount), h.stack, 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const {
|
void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, handler& h) const {
|
||||||
lua_callk(state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, nullptr);
|
lua_callk(lua_state(), static_cast<int>(argcount), static_cast<int>(resultcount), 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t... I, typename... Ret>
|
template<std::size_t... I, typename... Ret>
|
||||||
std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::ptrdiff_t n, handler& h) const {
|
std::tuple<Ret...> invoke(indices<I...>, types<Ret...>, std::ptrdiff_t n, handler& h) const {
|
||||||
luacall(n, sizeof...(Ret), h);
|
luacall(n, sizeof...(Ret), h);
|
||||||
const int nreturns = static_cast<int>(sizeof...(Ret));
|
const int nreturns = static_cast<int>(sizeof...(Ret));
|
||||||
const int stacksize = lua_gettop(state());
|
const int stacksize = lua_gettop(lua_state());
|
||||||
const int firstreturn = std::max(0, stacksize - nreturns) + 1;
|
const int firstreturn = std::max(0, stacksize - nreturns) + 1;
|
||||||
auto r = std::make_tuple(stack::get<Ret>(state(), firstreturn + I)...);
|
auto r = std::make_tuple(stack::get<Ret>(lua_state(), firstreturn + I)...);
|
||||||
lua_pop(state(), nreturns);
|
lua_pop(lua_state(), nreturns);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<std::size_t I, typename Ret>
|
template<std::size_t I, typename Ret>
|
||||||
Ret invoke(indices<I>, types<Ret>, std::ptrdiff_t n, handler& h) const {
|
Ret invoke(indices<I>, types<Ret>, std::ptrdiff_t n, handler& h) const {
|
||||||
luacall(n, 1, h);
|
luacall(n, 1, h);
|
||||||
return stack::pop<Ret>(state());
|
return stack::pop<Ret>(lua_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t I>
|
template <std::size_t I>
|
||||||
|
@ -171,7 +171,7 @@ private:
|
||||||
|
|
||||||
function_result invoke(indices<>, types<>, std::ptrdiff_t n, handler& h) const {
|
function_result invoke(indices<>, types<>, std::ptrdiff_t n, handler& h) const {
|
||||||
const bool handlerpushed = error_handler.valid();
|
const bool handlerpushed = error_handler.valid();
|
||||||
const int stacksize = lua_gettop(state());
|
const int stacksize = lua_gettop(lua_state());
|
||||||
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
|
const int firstreturn = std::max(0, stacksize - static_cast<int>(n) - 1);
|
||||||
int code = LUA_OK;
|
int code = LUA_OK;
|
||||||
try {
|
try {
|
||||||
|
@ -181,27 +181,27 @@ private:
|
||||||
catch (const std::exception& error) {
|
catch (const std::exception& error) {
|
||||||
code = LUA_ERRRUN;
|
code = LUA_ERRRUN;
|
||||||
h.stack = 0;
|
h.stack = 0;
|
||||||
stack::push(state(), error.what());
|
stack::push(lua_state(), error.what());
|
||||||
}
|
}
|
||||||
// TODO: handle idiots?
|
// TODO: handle idiots?
|
||||||
/*catch (const char* error) {
|
/*catch (const char* error) {
|
||||||
code = LUA_ERRRUN;
|
code = LUA_ERRRUN;
|
||||||
stack::push(state(), error);
|
stack::push(lua_state(), error);
|
||||||
}
|
}
|
||||||
catch (const std::string& error) {
|
catch (const std::string& error) {
|
||||||
code = LUA_ERRRUN;
|
code = LUA_ERRRUN;
|
||||||
stack::push(state(), error);
|
stack::push(lua_state(), error);
|
||||||
}
|
}
|
||||||
catch (...) {
|
catch (...) {
|
||||||
code = LUA_ERRRUN;
|
code = LUA_ERRRUN;
|
||||||
stack::push( state(), "[sol] an unknownable runtime exception occurred" );
|
stack::push( lua_state(), "[sol] an unknownable runtime exception occurred" );
|
||||||
}*/
|
}*/
|
||||||
catch (...) {
|
catch (...) {
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
const int poststacksize = lua_gettop(state());
|
const int poststacksize = lua_gettop(lua_state());
|
||||||
const int returncount = poststacksize - firstreturn;
|
const int returncount = poststacksize - firstreturn;
|
||||||
return function_result(state(), firstreturn + ( handlerpushed ? 0 : 1 ), returncount, static_cast<call_error>(code));
|
return function_result(lua_state(), firstreturn + ( handlerpushed ? 0 : 1 ), returncount, static_cast<call_error>(code));
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -232,7 +232,7 @@ public:
|
||||||
-> decltype(invoke(types<Ret...>(), types<Ret...>(), 0, std::declval<handler&>())) {
|
-> decltype(invoke(types<Ret...>(), types<Ret...>(), 0, std::declval<handler&>())) {
|
||||||
handler h(error_handler);
|
handler h(error_handler);
|
||||||
push();
|
push();
|
||||||
int pushcount = stack::push_args(state(), std::forward<Args>(args)...);
|
int pushcount = stack::push_args(lua_state(), std::forward<Args>(args)...);
|
||||||
auto tr = types<Ret...>();
|
auto tr = types<Ret...>();
|
||||||
return invoke(tr, tr, pushcount, h);
|
return invoke(tr, tr, pushcount, h);
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,14 +33,14 @@ public:
|
||||||
object() = default;
|
object() = default;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto as() const -> decltype(stack::get<T>(state())) const {
|
auto as() const -> decltype(stack::get<T>(lua_state())) const {
|
||||||
push();
|
push();
|
||||||
type actual = stack::get<type>(state());
|
type actual = stack::get<type>(lua_state());
|
||||||
// This code is actually present
|
// This code is actually present
|
||||||
// in almost all of the type-getters,
|
// in almost all of the type-getters,
|
||||||
// and it thus insanely redundant
|
// and it thus insanely redundant
|
||||||
// type_assert(state(), -1, type_of<T>(), actual);
|
// type_assert(lua_state(), -1, type_of<T>(), actual);
|
||||||
return stack::pop<T>(state());
|
return stack::pop<T>(lua_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
@ -97,7 +97,7 @@ public:
|
||||||
return static_cast<type>(result);
|
return static_cast<type>(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
lua_State* state() const noexcept {
|
lua_State* lua_state() const noexcept {
|
||||||
return L;
|
return L;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
inline int atpanic(lua_State* L) {
|
inline int atpanic(lua_State* L) {
|
||||||
const char* message = lua_tostring(L, -1);
|
const char* message = lua_tostring(L, -1);
|
||||||
std::string err = message ? message : "An unexpected error occured and forced the lua state to call atpanic";
|
std::string err = message ? message : "An unexpected error occurred and forced the lua state to call atpanic";
|
||||||
throw error(err);
|
throw error(err);
|
||||||
}
|
}
|
||||||
} // detail
|
} // detail
|
||||||
|
@ -53,18 +53,12 @@ class state {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
|
std::unique_ptr<lua_State, void(*)(lua_State*)> L;
|
||||||
table reg;
|
table reg;
|
||||||
table global;
|
global_table globals;
|
||||||
public:
|
public:
|
||||||
state(lua_CFunction panic = detail::atpanic):
|
state(lua_CFunction panic = detail::atpanic):
|
||||||
L(luaL_newstate(), lua_close),
|
L(luaL_newstate(), lua_close),
|
||||||
reg(L.get(), LUA_REGISTRYINDEX),
|
reg(L.get(), LUA_REGISTRYINDEX),
|
||||||
#if SOL_LUA_VERSION < 502
|
globals(detail::global_overload, reg) {
|
||||||
// Global table is just a special index
|
|
||||||
global(L.get(), LUA_GLOBALSINDEX) {
|
|
||||||
#else
|
|
||||||
// Global tables are stored in the environment/registry table
|
|
||||||
global(reg.get<table>(LUA_RIDX_GLOBALS)) {
|
|
||||||
#endif // Lua 5.2
|
|
||||||
set_panic(panic);
|
set_panic(panic);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,13 +146,13 @@ public:
|
||||||
|
|
||||||
template<typename... Args, typename... Keys>
|
template<typename... Args, typename... Keys>
|
||||||
auto get(Keys&&... keys) const
|
auto get(Keys&&... keys) const
|
||||||
-> decltype(global.get<Args...>(std::forward<Keys>(keys)...)) {
|
-> decltype(globals.get<Args...>(std::forward<Keys>(keys)...)) {
|
||||||
return global.get<Args...>(std::forward<Keys>(keys)...);
|
return globals.get<Args...>(std::forward<Keys>(keys)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename U>
|
template<typename T, typename U>
|
||||||
state& set(T&& key, U&& value) {
|
state& set(T&& key, U&& value) {
|
||||||
global.set(std::forward<T>(key), std::forward<U>(value));
|
globals.set(std::forward<T>(key), std::forward<U>(value));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,7 +183,7 @@ public:
|
||||||
|
|
||||||
template<typename Key, typename T>
|
template<typename Key, typename T>
|
||||||
state& set_usertype(Key&& key, usertype<T>& user) {
|
state& set_usertype(Key&& key, usertype<T>& user) {
|
||||||
global.set_usertype(std::forward<Key>(key), user);
|
globals.set_usertype(std::forward<Key>(key), user);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,7 +202,7 @@ public:
|
||||||
|
|
||||||
template <typename Fx>
|
template <typename Fx>
|
||||||
void for_each(Fx&& fx) {
|
void for_each(Fx&& fx) {
|
||||||
global.for_each(std::forward<Fx>(fx));
|
globals.for_each(std::forward<Fx>(fx));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -216,7 +210,7 @@ public:
|
||||||
lua_createtable(L.get(), narr, nrec);
|
lua_createtable(L.get(), narr, nrec);
|
||||||
table result(L.get());
|
table result(L.get());
|
||||||
lua_pop(L.get(), 1);
|
lua_pop(L.get(), 1);
|
||||||
global.set(std::forward<T>(key), result);
|
globals.set(std::forward<T>(key), result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,8 +221,8 @@ public:
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
table global_table() const {
|
global_table global() const {
|
||||||
return global;
|
return globals;
|
||||||
}
|
}
|
||||||
|
|
||||||
table registry() const {
|
table registry() const {
|
||||||
|
@ -240,42 +234,42 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy<table, T> operator[](T&& key) {
|
proxy<global_table, T> operator[](T&& key) {
|
||||||
return global[std::forward<T>(key)];
|
return globals[std::forward<T>(key)];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
proxy<const table, T> operator[](T&& key) const {
|
proxy<const global_table, T> operator[](T&& key) const {
|
||||||
return global[std::forward<T>(key)];
|
return globals[std::forward<T>(key)];
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args, typename R, typename Key>
|
template<typename... Args, typename R, typename Key>
|
||||||
state& set_function(Key&& key, R fun_ptr(Args...)){
|
state& set_function(Key&& key, R fun_ptr(Args...)){
|
||||||
global.set_function(std::forward<Key>(key), fun_ptr);
|
globals.set_function(std::forward<Key>(key), fun_ptr);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Sig, typename Key>
|
template<typename Sig, typename Key>
|
||||||
state& set_function(Key&& key, Sig* fun_ptr){
|
state& set_function(Key&& key, Sig* fun_ptr){
|
||||||
global.set_function(std::forward<Key>(key), fun_ptr);
|
globals.set_function(std::forward<Key>(key), fun_ptr);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args, typename R, typename C, typename T, typename Key>
|
template<typename... Args, typename R, typename C, typename T, typename Key>
|
||||||
state& set_function(Key&& key, R (C::*mem_ptr)(Args...), T&& obj) {
|
state& set_function(Key&& key, R (C::*mem_ptr)(Args...), T&& obj) {
|
||||||
global.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
globals.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Sig, typename C, typename T, typename Key>
|
template<typename Sig, typename C, typename T, typename Key>
|
||||||
state& set_function(Key&& key, Sig C::* mem_ptr, T&& obj) {
|
state& set_function(Key&& key, Sig C::* mem_ptr, T&& obj) {
|
||||||
global.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
globals.set_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Sig, typename Fx, typename Key>
|
template<typename... Sig, typename Fx, typename Key>
|
||||||
state& set_function(Key&& key, Fx&& fx) {
|
state& set_function(Key&& key, Fx&& fx) {
|
||||||
global.set_function<Sig...>(std::forward<Key>(key), std::forward<Fx>(fx));
|
globals.set_function<Sig...>(std::forward<Key>(key), std::forward<Fx>(fx));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
161
sol/table.hpp
161
sol/table.hpp
|
@ -22,167 +22,10 @@
|
||||||
#ifndef SOL_TABLE_HPP
|
#ifndef SOL_TABLE_HPP
|
||||||
#define SOL_TABLE_HPP
|
#define SOL_TABLE_HPP
|
||||||
|
|
||||||
#include "proxy.hpp"
|
#include "table_core.hpp"
|
||||||
#include "stack.hpp"
|
|
||||||
#include "function_types.hpp"
|
|
||||||
#include "usertype.hpp"
|
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
class table : public reference {
|
typedef table_core<false> table;
|
||||||
friend class state;
|
|
||||||
template<typename T, typename Key>
|
|
||||||
stack::get_return<T> single_get(Key&& key) const {
|
|
||||||
push();
|
|
||||||
stack::push(state(), std::forward<Key>(key));
|
|
||||||
lua_gettable(state(), -2);
|
|
||||||
stack::get_return<T> result = stack::pop<T>(state());
|
|
||||||
pop();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Keys, typename... Ret, std::size_t... I>
|
|
||||||
stack::get_return<Ret...> tuple_get(types<Ret...>, indices<I...>, Keys&& keys) const {
|
|
||||||
return stack::get_return<Ret...>(single_get<Ret>(std::get<I>(keys))...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Keys, typename Ret, std::size_t I>
|
|
||||||
stack::get_return<Ret> tuple_get(types<Ret>, indices<I>, Keys&& keys) const {
|
|
||||||
return single_get<Ret>(std::get<I>(keys));
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
table() noexcept : reference() {}
|
|
||||||
table(lua_State* L, int index = -1) : reference(L, index) {
|
|
||||||
type_assert(L, index, type::table);
|
|
||||||
}
|
|
||||||
|
|
||||||
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...));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename U>
|
|
||||||
table& set(T&& key, U&& value) {
|
|
||||||
push();
|
|
||||||
stack::push(state(), std::forward<T>(key));
|
|
||||||
stack::push(state(), std::forward<U>(value));
|
|
||||||
lua_settable(state(), -3);
|
|
||||||
lua_pop(state(), 1);
|
|
||||||
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 T>
|
|
||||||
table& set_usertype(usertype<T>& user) {
|
|
||||||
return set_usertype(usertype_traits<T>::name, user);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Key, typename T>
|
|
||||||
table& set_usertype(Key&& key, usertype<T>& user) {
|
|
||||||
push();
|
|
||||||
stack::push(state(), std::forward<Key>(key));
|
|
||||||
stack::push(state(), user);
|
|
||||||
lua_settable(state(), -3);
|
|
||||||
lua_pop(state(), 1);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Fx>
|
|
||||||
void for_each(Fx&& fx) const {
|
|
||||||
push();
|
|
||||||
stack::push(state(), nil);
|
|
||||||
while (lua_next(this->state(), -2)) {
|
|
||||||
sol::object key(state(), -2);
|
|
||||||
sol::object value(state(), -1);
|
|
||||||
fx(key, value);
|
|
||||||
lua_pop(state(), 1);
|
|
||||||
}
|
|
||||||
pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t size() const {
|
|
||||||
push();
|
|
||||||
size_t result = lua_rawlen(state(), -1);
|
|
||||||
pop();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
proxy<table, T> operator[]( T&& key ) {
|
|
||||||
return proxy<table, T>( *this, std::forward<T>( key ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
proxy<const table, T> operator[]( T&& key ) const {
|
|
||||||
return proxy<const table, T>( *this, std::forward<T>( key ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
void pop(int n = 1) const noexcept {
|
|
||||||
lua_pop(state(), n);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args, typename R, typename Key>
|
|
||||||
table& set_function(Key&& key, R fun_ptr(Args...)){
|
|
||||||
set_resolved_function(std::forward<Key>(key), fun_ptr);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Sig, typename Key>
|
|
||||||
table& set_function(Key&& key, Sig* fun_ptr){
|
|
||||||
set_resolved_function(std::forward<Key>(key), fun_ptr);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args, typename R, typename C, typename T, typename Key>
|
|
||||||
table& set_function(Key&& key, R (C::*mem_ptr)(Args...), T&& obj) {
|
|
||||||
set_resolved_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Sig, typename C, typename T, typename Key>
|
|
||||||
table& set_function(Key&& key, Sig C::* mem_ptr, T&& obj) {
|
|
||||||
set_resolved_function(std::forward<Key>(key), mem_ptr, std::forward<T>(obj));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Sig, typename Fx, typename Key>
|
|
||||||
table& set_function(Key&& key, Fx&& fx) {
|
|
||||||
set_fx(types<Sig...>(), std::forward<Key>(key), std::forward<Fx>(fx));
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
template<typename R, typename... Args, typename Fx, typename Key, typename = typename std::result_of<Fx(Args...)>::type>
|
|
||||||
void set_fx(types<R(Args...)>, Key&& key, Fx&& fx) {
|
|
||||||
set_resolved_function<R(Args...)>(std::forward<Key>(key), std::forward<Fx>(fx));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Fx, typename Key>
|
|
||||||
void set_fx(types<>, Key&& key, Fx&& fx) {
|
|
||||||
typedef Unqualified<Unwrap<Fx>> fx_t;
|
|
||||||
typedef decltype(&fx_t::operator()) Sig;
|
|
||||||
set_fx(types<function_signature_t<Sig>>(), std::forward<Key>(key), std::forward<Fx>(fx));
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Sig, typename... Args, typename Key>
|
|
||||||
void set_resolved_function(Key&& key, Args&&... args) {
|
|
||||||
std::string fkey(std::forward<Key>(key));
|
|
||||||
push();
|
|
||||||
int tabletarget = lua_gettop(state());
|
|
||||||
stack::push<function_sig_t<Sig...>>(state(), std::forward<Args>(args)...);
|
|
||||||
lua_setfield(state(), tabletarget, fkey.c_str());
|
|
||||||
pop();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
} // sol
|
} // sol
|
||||||
|
|
||||||
#endif // SOL_TABLE_HPP
|
#endif // SOL_TABLE_HPP
|
||||||
|
|
224
sol/table_core.hpp
Normal file
224
sol/table_core.hpp
Normal file
|
@ -0,0 +1,224 @@
|
||||||
|
// The MIT License (MIT)
|
||||||
|
|
||||||
|
// Copyright (c) 2013-2015 Danny Y., Rapptz
|
||||||
|
|
||||||
|
// 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"
|
||||||
|
|
||||||
|
namespace sol {
|
||||||
|
namespace detail {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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( );
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Keys, typename... Ret, std::size_t... I>
|
||||||
|
stack::get_return<Ret...> tuple_get( types<Ret...>, indices<I...>, Keys&& keys ) const {
|
||||||
|
return stack::get_return<Ret...>( single_get<Ret>( std::get<I>( keys ) )... );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Keys, typename Ret, std::size_t I>
|
||||||
|
stack::get_return<Ret> tuple_get( types<Ret>, indices<I>, Keys&& keys ) const {
|
||||||
|
return single_get<Ret>( std::get<I>( keys ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SOL_LUA_VERSION < 502
|
||||||
|
table_core( detail::global_overload_tag, const table_core<false>& reg ) noexcept : reference( reg.lua_state(), LUA_GLOBALSINDEX ) { }
|
||||||
|
#else
|
||||||
|
table_core( detail::global_overload_tag, const table& reg ) noexcept : reference( reg.get<table>( LUA_RIDX_GLOBALS ) ) { }
|
||||||
|
#endif
|
||||||
|
public:
|
||||||
|
table_core( ) noexcept : reference( ) { }
|
||||||
|
table_core( const table_core<true>& global ) : reference( global ) { }
|
||||||
|
table_core( lua_State* L, int index = -1 ) : reference( L, index ) {
|
||||||
|
type_assert( L, index, type::table );
|
||||||
|
}
|
||||||
|
|
||||||
|
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... ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
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( );
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
SOL_DEPRECATED table_core& set_userdata( usertype<T>& user ) {
|
||||||
|
return set_usertype( user );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T>
|
||||||
|
SOL_DEPRECATED table_core& set_userdata( Key&& key, usertype<T>& user ) {
|
||||||
|
return set_usertype( std::forward<Key>( key ), user );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
table_core& set_usertype( usertype<T>& user ) {
|
||||||
|
return set_usertype( usertype_traits<T>::name, user );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Key, typename T>
|
||||||
|
table_core& set_usertype( Key&& key, usertype<T>& user ) {
|
||||||
|
if ( top_level ) {
|
||||||
|
stack::push( lua_state( ), user );
|
||||||
|
lua_setglobal( lua_state( ), &key[ 0 ] );
|
||||||
|
pop( );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
push( );
|
||||||
|
stack::push( lua_state( ), std::forward<Key>( key ) );
|
||||||
|
stack::push( lua_state( ), user );
|
||||||
|
lua_settable( lua_state( ), -3 );
|
||||||
|
pop( );
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Fx>
|
||||||
|
void for_each( Fx&& fx ) const {
|
||||||
|
push( );
|
||||||
|
stack::push( lua_state( ), nil );
|
||||||
|
while ( lua_next( this->lua_state( ), -2 ) ) {
|
||||||
|
sol::object key( lua_state( ), -2 );
|
||||||
|
sol::object value( lua_state( ), -1 );
|
||||||
|
fx( key, value );
|
||||||
|
lua_pop( lua_state( ), 1 );
|
||||||
|
}
|
||||||
|
pop( );
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size( ) const {
|
||||||
|
push( );
|
||||||
|
size_t result = lua_rawlen( lua_state( ), -1 );
|
||||||
|
pop( );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
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 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop( int n = 1 ) const noexcept {
|
||||||
|
lua_pop( lua_state( ), n );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args, typename R, typename Key>
|
||||||
|
table_core& set_function( Key&& key, R fun_ptr( Args... ) ) {
|
||||||
|
set_resolved_function( std::forward<Key>( key ), fun_ptr );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Sig, typename Key>
|
||||||
|
table_core& set_function( Key&& key, Sig* fun_ptr ) {
|
||||||
|
set_resolved_function( std::forward<Key>( key ), fun_ptr );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args, typename R, typename C, typename T, typename Key>
|
||||||
|
table_core& set_function( Key&& key, R( C::*mem_ptr )( Args... ), T&& obj ) {
|
||||||
|
set_resolved_function( std::forward<Key>( key ), mem_ptr, std::forward<T>( obj ) );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Sig, typename C, typename T, typename Key>
|
||||||
|
table_core& set_function( Key&& key, Sig C::* mem_ptr, T&& obj ) {
|
||||||
|
set_resolved_function( std::forward<Key>( key ), mem_ptr, std::forward<T>( obj ) );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Sig, typename Fx, typename Key>
|
||||||
|
table_core& set_function( Key&& key, Fx&& fx ) {
|
||||||
|
set_fx( types<Sig...>( ), std::forward<Key>( key ), std::forward<Fx>( fx ) );
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template<typename R, typename... Args, typename Fx, typename Key, typename = typename std::result_of<Fx( Args... )>::type>
|
||||||
|
void set_fx( types<R( Args... )>, Key&& key, Fx&& fx ) {
|
||||||
|
set_resolved_function<R( Args... )>( std::forward<Key>( key ), std::forward<Fx>( fx ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Fx, typename Key>
|
||||||
|
void set_fx( types<>, Key&& key, Fx&& fx ) {
|
||||||
|
typedef Unqualified<Unwrap<Fx>> fx_t;
|
||||||
|
typedef decltype( &fx_t::operator() ) Sig;
|
||||||
|
set_fx( types<function_signature_t<Sig>>( ), std::forward<Key>( key ), std::forward<Fx>( fx ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Sig, typename... Args, typename Key, EnableIf<Not<std::is_arithmetic<Unqualified<Key>>>, Bool<top_level>> = 0>
|
||||||
|
void set_resolved_function( Key&& key, Args&&... args ) {
|
||||||
|
stack::push<function_sig_t<Sig...>>( lua_state( ), std::forward<Args>( args )... );
|
||||||
|
lua_setglobal( lua_state( ), &key[ 0 ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Sig, typename... Args, typename Key, DisableIf<Not<std::is_arithmetic<Unqualified<Key>>>, Bool<top_level>> = 0>
|
||||||
|
void set_resolved_function( Key&& key, Args&&... args ) {
|
||||||
|
push( );
|
||||||
|
int tabletarget = lua_gettop( lua_state( ) );
|
||||||
|
stack::push<function_sig_t<Sig...>>( lua_state( ), std::forward<Args>( args )... );
|
||||||
|
lua_setfield( lua_state( ), tabletarget, &key[ 0 ] );
|
||||||
|
pop( );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // sol
|
||||||
|
|
||||||
|
#endif // SOL_TABLE_CORE_HPP
|
|
@ -121,7 +121,10 @@ inline std::string type_name(lua_State*L, type t) {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class usertype;
|
class usertype;
|
||||||
class table;
|
template <bool>
|
||||||
|
class table_core;
|
||||||
|
typedef table_core<false> table;
|
||||||
|
typedef table_core<true> global_table;
|
||||||
class function;
|
class function;
|
||||||
class object;
|
class object;
|
||||||
|
|
||||||
|
|
|
@ -597,7 +597,7 @@ TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works
|
||||||
std::cout << a << ',' << b << '\n';
|
std::cout << a << ',' << b << '\n';
|
||||||
};
|
};
|
||||||
|
|
||||||
REQUIRE_NOTHROW(assert1(lua.global_table()));
|
REQUIRE_NOTHROW(assert1(lua.global()));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("tables/usertype", "Show that we can create classes from usertype and use them") {
|
TEST_CASE("tables/usertype", "Show that we can create classes from usertype and use them") {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user