add pointer() method

This commit is contained in:
ThePhD 2018-06-27 11:34:40 -04:00
parent 60ee53a429
commit 254466eb4b
8 changed files with 85 additions and 4 deletions

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script. // This file was generated with a script.
// Generated 2018-06-27 11:15:40.545352 UTC // Generated 2018-06-27 15:33:51.932186 UTC
// This header was generated with sol v2.20.4 (revision 3935dc4) // This header was generated with sol v2.20.4 (revision 60ee53a)
// https://github.com/ThePhD/sol2 // https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP #ifndef SOL_SINGLE_INCLUDE_HPP
@ -5530,6 +5530,9 @@ namespace sol {
template <> template <>
struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> {}; struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> {};
template <>
struct lua_type_of<const void*> : std::integral_constant<type, type::lightuserdata> {};
template <> template <>
struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> {}; struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> {};
@ -6513,6 +6516,11 @@ namespace sol {
return index; return index;
} }
const void* pointer() const noexcept {
const void* vp = lua_topointer(lua_state(), stack_index());
return vp;
}
type get_type() const noexcept { type get_type() const noexcept {
int result = lua_type(lua_state(), index); int result = lua_type(lua_state(), index);
return static_cast<type>(result); return static_cast<type>(result);
@ -6957,6 +6965,12 @@ namespace sol {
return !(ref == LUA_NOREF || ref == LUA_REFNIL); return !(ref == LUA_NOREF || ref == LUA_REFNIL);
} }
const void* pointer() const noexcept {
int si = push();
const void* vp = lua_topointer(lua_state(), -si);
return vp;
}
explicit operator bool() const noexcept { explicit operator bool() const noexcept {
return valid(); return valid();
} }
@ -9843,6 +9857,14 @@ namespace stack {
} }
}; };
template <>
struct getter<const void*> {
static const void* get(lua_State* L, int index, record& tracking) {
tracking.use(1);
return lua_touserdata(L, index);
}
};
template <typename T> template <typename T>
struct getter<detail::as_value_tag<T>> { struct getter<detail::as_value_tag<T>> {
static T* get_no_lua_nil(lua_State* L, int index, record& tracking) { static T* get_no_lua_nil(lua_State* L, int index, record& tracking) {
@ -10673,6 +10695,14 @@ namespace stack {
} }
}; };
template <>
struct pusher<const void*> {
static int push(lua_State* L, const void* userdata) {
lua_pushlightuserdata(L, const_cast<void*>(userdata));
return 1;
}
};
template <> template <>
struct pusher<lightuserdata_value> { struct pusher<lightuserdata_value> {
static int push(lua_State* L, lightuserdata_value userdata) { static int push(lua_State* L, lightuserdata_value userdata) {

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script. // This file was generated with a script.
// Generated 2018-06-27 11:15:40.807656 UTC // Generated 2018-06-27 15:33:52.214419 UTC
// This header was generated with sol v2.20.4 (revision 3935dc4) // This header was generated with sol v2.20.4 (revision 60ee53a)
// https://github.com/ThePhD/sol2 // https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP #ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP

View File

@ -429,6 +429,12 @@ namespace sol {
return !(ref == LUA_NOREF || ref == LUA_REFNIL); return !(ref == LUA_NOREF || ref == LUA_REFNIL);
} }
const void* pointer() const noexcept {
int si = push();
const void* vp = lua_topointer(lua_state(), -si);
return vp;
}
explicit operator bool() const noexcept { explicit operator bool() const noexcept {
return valid(); return valid();
} }

View File

@ -739,6 +739,14 @@ namespace stack {
} }
}; };
template <>
struct getter<const void*> {
static const void* get(lua_State* L, int index, record& tracking) {
tracking.use(1);
return lua_touserdata(L, index);
}
};
template <typename T> template <typename T>
struct getter<detail::as_value_tag<T>> { struct getter<detail::as_value_tag<T>> {
static T* get_no_lua_nil(lua_State* L, int index, record& tracking) { static T* get_no_lua_nil(lua_State* L, int index, record& tracking) {

View File

@ -469,6 +469,14 @@ namespace stack {
} }
}; };
template <>
struct pusher<const void*> {
static int push(lua_State* L, const void* userdata) {
lua_pushlightuserdata(L, const_cast<void*>(userdata));
return 1;
}
};
template <> template <>
struct pusher<lightuserdata_value> { struct pusher<lightuserdata_value> {
static int push(lua_State* L, lightuserdata_value userdata) { static int push(lua_State* L, lightuserdata_value userdata) {

View File

@ -113,6 +113,11 @@ namespace sol {
return index; return index;
} }
const void* pointer() const noexcept {
const void* vp = lua_topointer(lua_state(), stack_index());
return vp;
}
type get_type() const noexcept { type get_type() const noexcept {
int result = lua_type(lua_state(), index); int result = lua_type(lua_state(), index);
return static_cast<type>(result); return static_cast<type>(result);

View File

@ -950,6 +950,9 @@ namespace sol {
template <> template <>
struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> {}; struct lua_type_of<void*> : std::integral_constant<type, type::lightuserdata> {};
template <>
struct lua_type_of<const void*> : std::integral_constant<type, type::lightuserdata> {};
template <> template <>
struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> {}; struct lua_type_of<lightuserdata_value> : std::integral_constant<type, type::lightuserdata> {};

View File

@ -134,6 +134,27 @@ TEST_CASE("utility/thread", "fire up lots of threads at the same time to make su
}()); }());
} }
TEST_CASE("utility/pointer", "check we can get pointer value from references") {
sol::state lua;
lua.set_function("f", [](bool aorb, sol::reference a, sol::stack_reference b) {
if (aorb) {
return a.pointer();
}
return b.pointer();
});
auto result0 = lua.safe_script("v0 = 'hi'", sol::script_pass_on_error);
REQUIRE(result0.valid());
auto result1 = lua.safe_script("v1 = f(true, v0)", sol::script_pass_on_error);
REQUIRE(result1.valid());
auto result2 = lua.safe_script("v2 = f(false, nil, v0)", sol::script_pass_on_error);
REQUIRE(result2.valid());
const void* ap = lua["v1"];
const void* bp = lua["v2"];
REQUIRE(ap != nullptr);
REQUIRE(bp != nullptr);
REQUIRE(ap == bp);
}
TEST_CASE("utility/this_state", "Ensure this_state argument can be gotten anywhere in the function.") { TEST_CASE("utility/this_state", "Ensure this_state argument can be gotten anywhere in the function.") {
struct bark { struct bark {
int with_state(sol::this_state l, int a, int b) { int with_state(sol::this_state l, int a, int b) {