mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
More documentation, fix for signed/unsigned conversion issues.
This commit is contained in:
parent
3688cc3752
commit
83ba698aba
|
@ -94,6 +94,15 @@ This function returns the size of a table. It is only well-defined in the case o
|
||||||
|
|
||||||
This class of functions creates a new :doc:`usertype<usertype>` with the specified arguments, providing a few extra details for constructors. After creating a usertype with the specified argument, it passes it to :ref:`set_usertype<set_usertype>`.
|
This class of functions creates a new :doc:`usertype<usertype>` with the specified arguments, providing a few extra details for constructors. After creating a usertype with the specified argument, it passes it to :ref:`set_usertype<set_usertype>`.
|
||||||
|
|
||||||
|
.. code-block:: cpp
|
||||||
|
:caption: function: creating an enum
|
||||||
|
:name: new-enum
|
||||||
|
|
||||||
|
template<bool read_only = true, typename... Args>
|
||||||
|
basic_table_core& new_enum(const std::string& name, Args&&... args);
|
||||||
|
|
||||||
|
Use this function to create an enumeration type in Lua. By default, the enum will be made read-only, which creates a tiny performance hit to make the values stored in this table behave exactly like a read-only enumeration in C++. If you plan on changing the enum values in Lua, set the ``read_only`` template parameter in your ``new_enum`` call to false. The arguments are expected to come in ``key, value, key, value, ...`` list.
|
||||||
|
|
||||||
.. _set_usertype:
|
.. _set_usertype:
|
||||||
|
|
||||||
.. code-block:: cpp
|
.. code-block:: cpp
|
||||||
|
|
|
@ -299,7 +299,7 @@ namespace sol {
|
||||||
typedef sol::constructor_list<Args...> F;
|
typedef sol::constructor_list<Args...> F;
|
||||||
|
|
||||||
static int call(lua_State* L, F&) {
|
static int call(lua_State* L, F&) {
|
||||||
static const auto& metakey = usertype_traits<T>::metatable;
|
const auto& metakey = usertype_traits<T>::metatable;
|
||||||
int argcount = lua_gettop(L);
|
int argcount = lua_gettop(L);
|
||||||
call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, metakey, 1) : call_syntax::dot;
|
call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, metakey, 1) : call_syntax::dot;
|
||||||
argcount -= static_cast<int>(syntax);
|
argcount -= static_cast<int>(syntax);
|
||||||
|
@ -331,6 +331,7 @@ namespace sol {
|
||||||
struct onmatch {
|
struct onmatch {
|
||||||
template <typename Fx, std::size_t I, typename... R, typename... Args>
|
template <typename Fx, std::size_t I, typename... R, typename... Args>
|
||||||
int operator()(types<Fx>, index_value<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start, F& f) {
|
int operator()(types<Fx>, index_value<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start, F& f) {
|
||||||
|
const auto& metakey = usertype_traits<T>::metatable;
|
||||||
T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
||||||
reference userdataref(L, -1);
|
reference userdataref(L, -1);
|
||||||
T*& referencepointer = *pointerpointer;
|
T*& referencepointer = *pointerpointer;
|
||||||
|
@ -341,7 +342,7 @@ namespace sol {
|
||||||
stack::call_into_lua<1, false>(r, a, L, start, func, detail::implicit_wrapper<T>(obj));
|
stack::call_into_lua<1, false>(r, a, L, start, func, detail::implicit_wrapper<T>(obj));
|
||||||
|
|
||||||
userdataref.push();
|
userdataref.push();
|
||||||
luaL_getmetatable(L, &usertype_traits<T>::metatable[0]);
|
luaL_getmetatable(L, &metakey[0]);
|
||||||
if (type_of(L, -1) == type::nil) {
|
if (type_of(L, -1) == type::nil) {
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
std::string err = "sol: unable to get usertype metatable for ";
|
std::string err = "sol: unable to get usertype metatable for ";
|
||||||
|
|
|
@ -34,7 +34,8 @@ namespace sol {
|
||||||
namespace stack_detail {
|
namespace stack_detail {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool check_metatable(lua_State* L, int index = -2) {
|
inline bool check_metatable(lua_State* L, int index = -2) {
|
||||||
luaL_getmetatable(L, &usertype_traits<T>::metatable[0]);
|
const auto& metakey = usertype_traits<T>::metatable;
|
||||||
|
luaL_getmetatable(L, &metakey[0]);
|
||||||
const type expectedmetatabletype = static_cast<type>(lua_type(L, -1));
|
const type expectedmetatabletype = static_cast<type>(lua_type(L, -1));
|
||||||
if (expectedmetatabletype != type::nil) {
|
if (expectedmetatabletype != type::nil) {
|
||||||
if (lua_rawequal(L, -1, index) == 1) {
|
if (lua_rawequal(L, -1, index) == 1) {
|
||||||
|
@ -272,7 +273,7 @@ namespace sol {
|
||||||
return true;
|
return true;
|
||||||
if (stack_detail::check_metatable<detail::unique_usertype<U>>(L))
|
if (stack_detail::check_metatable<detail::unique_usertype<U>>(L))
|
||||||
return true;
|
return true;
|
||||||
bool success = true;
|
bool success = false;
|
||||||
#ifndef SOL_NO_EXCEPTIONS
|
#ifndef SOL_NO_EXCEPTIONS
|
||||||
lua_getfield(L, -1, &detail::base_class_check_key()[0]);
|
lua_getfield(L, -1, &detail::base_class_check_key()[0]);
|
||||||
if (type_of(L, -1) != type::nil) {
|
if (type_of(L, -1) != type::nil) {
|
||||||
|
|
|
@ -150,8 +150,8 @@ namespace sol {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct pusher<T, std::enable_if_t<meta::all<std::is_integral<T>, std::is_unsigned<T>>::value>> {
|
struct pusher<T, std::enable_if_t<meta::all<std::is_integral<T>, std::is_unsigned<T>>::value>> {
|
||||||
static int push(lua_State* L, const T& value) {
|
static int push(lua_State* L, const T& value) {
|
||||||
typedef std::make_signed_t<T> signed_int;
|
lua_pushinteger(L, static_cast<lua_Integer>(value));
|
||||||
return stack::push(L, static_cast<signed_int>(value));
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user