[DEV] change some internal interface
This commit is contained in:
parent
11bbc0e7a1
commit
1c941a031b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.pyc
|
@ -37,6 +37,7 @@
|
|||||||
// If you are linking against Lua compiled in C++, define LUAW_NO_EXTERN_C
|
// If you are linking against Lua compiled in C++, define LUAW_NO_EXTERN_C
|
||||||
#include <lua/lua.h>
|
#include <lua/lua.h>
|
||||||
#include <lua/lauxlib.h>
|
#include <lua/lauxlib.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#define LUAW_POSTCTOR_KEY "__postctor"
|
#define LUAW_POSTCTOR_KEY "__postctor"
|
||||||
#define LUAW_EXTENDS_KEY "__extends"
|
#define LUAW_EXTENDS_KEY "__extends"
|
||||||
@ -59,12 +60,8 @@ inline int luaW_correctindex(lua_State* _L, int _index, int _correction) {
|
|||||||
* alternative option, you may select a different function when registering
|
* alternative option, you may select a different function when registering
|
||||||
* your class.
|
* your class.
|
||||||
*/
|
*/
|
||||||
template <typename T> T* luaW_defaultallocator(lua_State*) {
|
template <typename T> std::shared_ptr<T> luaW_defaultallocator(lua_State* _L) {
|
||||||
return new T();
|
return std::make_shared<T>();
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> void luaW_defaultdeallocator(lua_State*, T* _obj) {
|
|
||||||
delete _obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,8 +71,8 @@ template <typename T> void luaW_defaultdeallocator(lua_State*, T* _obj) {
|
|||||||
* are using shared_ptr you would need to push the address of the object the
|
* are using shared_ptr you would need to push the address of the object the
|
||||||
* shared_ptr represents, rather than the address of the shared_ptr itself.
|
* shared_ptr represents, rather than the address of the shared_ptr itself.
|
||||||
*/
|
*/
|
||||||
template <typename T> void luaW_defaultidentifier(lua_State* _L, T* _obj) {
|
template <typename T> void luaW_defaultidentifier(lua_State* _L, std::shared_ptr<T> _obj) {
|
||||||
lua_pushlightuserdata(_L, _obj);
|
lua_pushlightuserdata(_L, _obj.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,12 +84,12 @@ template <typename T> void luaW_defaultidentifier(lua_State* _L, T* _obj) {
|
|||||||
* when and object is the type I want. This is only used internally.
|
* when and object is the type I want. This is only used internally.
|
||||||
*/
|
*/
|
||||||
struct luaW_Userdata {
|
struct luaW_Userdata {
|
||||||
luaW_Userdata(void* vptr = NULL, luaW_Userdata (*udcast)(const luaW_Userdata&) = NULL) :
|
luaW_Userdata(std::shared_ptr<void> vptr = NULL, luaW_Userdata (*udcast)(const luaW_Userdata&) = NULL) :
|
||||||
data(vptr),
|
data(vptr),
|
||||||
cast(udcast) {
|
cast(udcast) {
|
||||||
// nothing to do ...
|
// nothing to do ...
|
||||||
}
|
}
|
||||||
void* data;
|
std::shared_ptr<void> data;
|
||||||
luaW_Userdata (*cast)(const luaW_Userdata&);
|
luaW_Userdata (*cast)(const luaW_Userdata&);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,18 +100,16 @@ struct luaW_Userdata {
|
|||||||
template <typename T> class LuaWrapper {
|
template <typename T> class LuaWrapper {
|
||||||
public:
|
public:
|
||||||
static const char* classname;
|
static const char* classname;
|
||||||
static void (*identifier)(lua_State*, T*);
|
static void (*identifier)(lua_State*, std::shared_ptr<T>);
|
||||||
static T* (*allocator)(lua_State*);
|
static std::shared_ptr<T> (*allocator)(lua_State*);
|
||||||
static void (*deallocator)(lua_State*, T*);
|
|
||||||
static luaW_Userdata (*cast)(const luaW_Userdata&);
|
static luaW_Userdata (*cast)(const luaW_Userdata&);
|
||||||
static void (*postconstructorrecurse)(lua_State* _L, int numargs);
|
static void (*postconstructorrecurse)(lua_State* _L, int numargs);
|
||||||
private:
|
private:
|
||||||
LuaWrapper();
|
LuaWrapper();
|
||||||
};
|
};
|
||||||
template <typename T> const char* LuaWrapper<T>::classname;
|
template <typename T> const char* LuaWrapper<T>::classname;
|
||||||
template <typename T> void (*LuaWrapper<T>::identifier)(lua_State*, T*);
|
template <typename T> void (*LuaWrapper<T>::identifier)(lua_State*, std::shared_ptr<T>);
|
||||||
template <typename T> T* (*LuaWrapper<T>::allocator)(lua_State*);
|
template <typename T> std::shared_ptr<T> (*LuaWrapper<T>::allocator)(lua_State*);
|
||||||
template <typename T> void (*LuaWrapper<T>::deallocator)(lua_State*, T*);
|
|
||||||
template <typename T> luaW_Userdata (*LuaWrapper<T>::cast)(const luaW_Userdata&);
|
template <typename T> luaW_Userdata (*LuaWrapper<T>::cast)(const luaW_Userdata&);
|
||||||
template <typename T> void (*LuaWrapper<T>::postconstructorrecurse)(lua_State* _L, int _numargs);
|
template <typename T> void (*LuaWrapper<T>::postconstructorrecurse)(lua_State* _L, int _numargs);
|
||||||
|
|
||||||
@ -124,7 +119,7 @@ template <typename T> void (*LuaWrapper<T>::postconstructorrecurse)(lua_State* _
|
|||||||
* internally.
|
* internally.
|
||||||
*/
|
*/
|
||||||
template <typename T, typename U> luaW_Userdata luaW_cast(const luaW_Userdata& _obj) {
|
template <typename T, typename U> luaW_Userdata luaW_cast(const luaW_Userdata& _obj) {
|
||||||
return luaW_Userdata(static_cast<U*>(static_cast<T*>(_obj.data)), LuaWrapper<U>::cast);
|
return luaW_Userdata(static_cast<U*>(std::static_pointer_cast<T>(_obj.data)), LuaWrapper<U>::cast);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename U> void luaW_identify(lua_State* _L, T* _obj) {
|
template <typename T, typename U> void luaW_identify(lua_State* _L, T* _obj) {
|
||||||
@ -174,7 +169,7 @@ template <typename T> bool luaW_is(lua_State *_L, int _index, bool _strict = fal
|
|||||||
* Converts the given acceptable index to a T*. That value must be of (or
|
* Converts the given acceptable index to a T*. That value must be of (or
|
||||||
* convertable to) type T; otherwise, returns NULL.
|
* convertable to) type T; otherwise, returns NULL.
|
||||||
*/
|
*/
|
||||||
template <typename T> T* luaW_to(lua_State* _L, int _index, bool _strict = false) {
|
template <typename T> std::shared_ptr<T> luaW_to(lua_State* _L, int _index, bool _strict = false) {
|
||||||
if (luaW_is<T>(_L, _index, _strict)) {
|
if (luaW_is<T>(_L, _index, _strict)) {
|
||||||
luaW_Userdata* pud = static_cast<luaW_Userdata*>(lua_touserdata(_L, _index));
|
luaW_Userdata* pud = static_cast<luaW_Userdata*>(lua_touserdata(_L, _index));
|
||||||
luaW_Userdata ud;
|
luaW_Userdata ud;
|
||||||
@ -183,7 +178,7 @@ template <typename T> T* luaW_to(lua_State* _L, int _index, bool _strict = false
|
|||||||
ud = pud->cast(*pud);
|
ud = pud->cast(*pud);
|
||||||
pud = &ud;
|
pud = &ud;
|
||||||
}
|
}
|
||||||
return static_cast<T*>(pud->data);
|
return std::static_pointer_cast<T>(pud->data);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -194,8 +189,8 @@ template <typename T> T* luaW_to(lua_State* _L, int _index, bool _strict = false
|
|||||||
* Converts the given acceptable index to a T*. That value must be of (or
|
* Converts the given acceptable index to a T*. That value must be of (or
|
||||||
* convertable to) type T; otherwise, an error is raised.
|
* convertable to) type T; otherwise, an error is raised.
|
||||||
*/
|
*/
|
||||||
template <typename T> T* luaW_check(lua_State* _L, int _index, bool _strict = false) {
|
template <typename T> std::shared_ptr<T> luaW_check(lua_State* _L, int _index, bool _strict = false) {
|
||||||
T* obj = NULL;
|
std::shared_ptr<T> obj;
|
||||||
if (luaW_is<T>(_L, _index, _strict)) {
|
if (luaW_is<T>(_L, _index, _strict)) {
|
||||||
luaW_Userdata* pud = static_cast<luaW_Userdata*>(lua_touserdata(_L, _index));
|
luaW_Userdata* pud = static_cast<luaW_Userdata*>(lua_touserdata(_L, _index));
|
||||||
luaW_Userdata ud;
|
luaW_Userdata ud;
|
||||||
@ -203,7 +198,7 @@ template <typename T> T* luaW_check(lua_State* _L, int _index, bool _strict = fa
|
|||||||
ud = pud->cast(*pud);
|
ud = pud->cast(*pud);
|
||||||
pud = &ud;
|
pud = &ud;
|
||||||
}
|
}
|
||||||
obj = (T*)pud->data;
|
obj = std::static_pointer_cast<T>(pud->data);
|
||||||
} else {
|
} else {
|
||||||
const char *msg = lua_pushfstring(_L, "%s expected, got %s", LuaWrapper<T>::classname, luaL_typename(_L, _index));
|
const char *msg = lua_pushfstring(_L, "%s expected, got %s", LuaWrapper<T>::classname, luaL_typename(_L, _index));
|
||||||
luaL_argerror(_L, _index, msg);
|
luaL_argerror(_L, _index, msg);
|
||||||
@ -211,7 +206,7 @@ template <typename T> T* luaW_check(lua_State* _L, int _index, bool _strict = fa
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> T* luaW_opt(lua_State* _L, int _index, T* _fallback = NULL, bool _strict = false) {
|
template <typename T> std::shared_ptr<T> luaW_opt(lua_State* _L, int _index, std::shared_ptr<T> _fallback = nullptr, bool _strict = false) {
|
||||||
if (lua_isnil(_L, _index)) {
|
if (lua_isnil(_L, _index)) {
|
||||||
return _fallback;
|
return _fallback;
|
||||||
} else {
|
} else {
|
||||||
@ -226,7 +221,7 @@ template <typename T> T* luaW_opt(lua_State* _L, int _index, T* _fallback = NULL
|
|||||||
* the Lua environment, it will assign the existing storage table to it.
|
* the Lua environment, it will assign the existing storage table to it.
|
||||||
* Otherwise, a new storage table will be created for it.
|
* Otherwise, a new storage table will be created for it.
|
||||||
*/
|
*/
|
||||||
template <typename T> void luaW_push(lua_State* _L, T* _obj) {
|
template <typename T> void luaW_push(lua_State* _L, std::shared_ptr<T> _obj) {
|
||||||
if (_obj) {
|
if (_obj) {
|
||||||
LuaWrapper<T>::identifier(_L, _obj); // ... id
|
LuaWrapper<T>::identifier(_L, _obj); // ... id
|
||||||
luaW_wrapperfield<T>(_L, LUAW_CACHE_KEY); // ... id cache
|
luaW_wrapperfield<T>(_L, LUAW_CACHE_KEY); // ... id cache
|
||||||
@ -262,7 +257,7 @@ template <typename T> void luaW_push(lua_State* _L, T* _obj) {
|
|||||||
* Returns true if luaW_hold took hold of the object, and false if it was
|
* Returns true if luaW_hold took hold of the object, and false if it was
|
||||||
* already held
|
* already held
|
||||||
*/
|
*/
|
||||||
template <typename T> bool luaW_hold(lua_State* _L, T* _obj) {
|
template <typename T> bool luaW_hold(lua_State* _L, std::shared_ptr<T> _obj) {
|
||||||
luaW_wrapperfield<T>(_L, LUAW_HOLDS_KEY); // ... holds
|
luaW_wrapperfield<T>(_L, LUAW_HOLDS_KEY); // ... holds
|
||||||
LuaWrapper<T>::identifier(_L, _obj); // ... holds id
|
LuaWrapper<T>::identifier(_L, _obj); // ... holds id
|
||||||
lua_pushvalue(_L, -1); // ... holds id id
|
lua_pushvalue(_L, -1); // ... holds id id
|
||||||
@ -346,7 +341,7 @@ template <typename T> void luaW_postconstructor(lua_State* _L, int _numargs) {
|
|||||||
*/
|
*/
|
||||||
template <typename T> inline int luaW_new(lua_State* _L, int _numargs) {
|
template <typename T> inline int luaW_new(lua_State* _L, int _numargs) {
|
||||||
// ... args...
|
// ... args...
|
||||||
T* obj = LuaWrapper<T>::allocator(_L);
|
std::shared_ptr<T> obj = LuaWrapper<T>::allocator(_L);
|
||||||
luaW_push<T>(_L, obj); // ... args... ud
|
luaW_push<T>(_L, obj); // ... args... ud
|
||||||
luaW_hold<T>(_L, obj);
|
luaW_hold<T>(_L, obj);
|
||||||
lua_insert(_L, -1 - _numargs); // ... ud args...
|
lua_insert(_L, -1 - _numargs); // ... ud args...
|
||||||
@ -369,7 +364,7 @@ template <typename T> int luaW_new(lua_State* _L) {
|
|||||||
*/
|
*/
|
||||||
template <typename T> int luaW_index(lua_State* _L) {
|
template <typename T> int luaW_index(lua_State* _L) {
|
||||||
// obj key
|
// obj key
|
||||||
T* obj = luaW_to<T>(_L, 1);
|
std::shared_ptr<T> obj = luaW_to<T>(_L, 1);
|
||||||
luaW_wrapperfield<T>(_L, LUAW_STORAGE_KEY); // obj key storage
|
luaW_wrapperfield<T>(_L, LUAW_STORAGE_KEY); // obj key storage
|
||||||
LuaWrapper<T>::identifier(_L, obj); // obj key storage id
|
LuaWrapper<T>::identifier(_L, obj); // obj key storage id
|
||||||
lua_gettable(_L, -2); // obj key storage store
|
lua_gettable(_L, -2); // obj key storage store
|
||||||
@ -400,7 +395,7 @@ template <typename T> int luaW_index(lua_State* _L) {
|
|||||||
*/
|
*/
|
||||||
template <typename T> int luaW_newindex(lua_State* _L) {
|
template <typename T> int luaW_newindex(lua_State* _L) {
|
||||||
// obj key value
|
// obj key value
|
||||||
T* obj = luaW_check<T>(_L, 1);
|
std::shared_ptr<T> obj = luaW_check<T>(_L, 1);
|
||||||
luaW_wrapperfield<T>(_L, LUAW_STORAGE_KEY); // obj key value storage
|
luaW_wrapperfield<T>(_L, LUAW_STORAGE_KEY); // obj key value storage
|
||||||
LuaWrapper<T>::identifier(_L, obj); // obj key value storage id
|
LuaWrapper<T>::identifier(_L, obj); // obj key value storage id
|
||||||
lua_pushvalue(_L, -1); // obj key value storage id id
|
lua_pushvalue(_L, -1); // obj key value storage id id
|
||||||
@ -428,13 +423,13 @@ template <typename T> int luaW_newindex(lua_State* _L) {
|
|||||||
*/
|
*/
|
||||||
template <typename T> int luaW_gc(lua_State* _L) {
|
template <typename T> int luaW_gc(lua_State* _L) {
|
||||||
// obj
|
// obj
|
||||||
T* obj = luaW_to<T>(_L, 1);
|
std::shared_ptr<T> obj = luaW_to<T>(_L, 1);
|
||||||
LuaWrapper<T>::identifier(_L, obj); // obj key value storage id
|
LuaWrapper<T>::identifier(_L, obj); // obj key value storage id
|
||||||
luaW_wrapperfield<T>(_L, LUAW_HOLDS_KEY); // obj id counts count holds
|
luaW_wrapperfield<T>(_L, LUAW_HOLDS_KEY); // obj id counts count holds
|
||||||
lua_pushvalue(_L, 2); // obj id counts count holds id
|
lua_pushvalue(_L, 2); // obj id counts count holds id
|
||||||
lua_gettable(_L, -2); // obj id counts count holds hold
|
lua_gettable(_L, -2); // obj id counts count holds hold
|
||||||
if (lua_toboolean(_L, -1) && LuaWrapper<T>::deallocator) {
|
if (lua_toboolean(_L, -1)) {
|
||||||
LuaWrapper<T>::deallocator(_L, obj);
|
obj.reset();
|
||||||
}
|
}
|
||||||
luaW_wrapperfield<T>(_L, LUAW_STORAGE_KEY); // obj id counts count holds hold storage
|
luaW_wrapperfield<T>(_L, LUAW_STORAGE_KEY); // obj id counts count holds hold storage
|
||||||
lua_pushvalue(_L, 2); // obj id counts count holds hold storage id
|
lua_pushvalue(_L, 2); // obj id counts count holds hold storage id
|
||||||
@ -531,14 +526,12 @@ template <typename T> void luaW_setfuncs(lua_State* _L,
|
|||||||
const char* _classname,
|
const char* _classname,
|
||||||
const luaL_Reg* _table,
|
const luaL_Reg* _table,
|
||||||
const luaL_Reg* _metatable,
|
const luaL_Reg* _metatable,
|
||||||
T* (*_allocator)(lua_State*) = luaW_defaultallocator<T>,
|
std::shared_ptr<T> (*_allocator)(lua_State*),
|
||||||
void (*_deallocator)(lua_State*, T*) = luaW_defaultdeallocator<T>,
|
void (*_identifier)(lua_State*, std::shared_ptr<T>)) {
|
||||||
void (*_identifier)(lua_State*, T*) = luaW_defaultidentifier<T>) {
|
|
||||||
luaW_initialize(_L);
|
luaW_initialize(_L);
|
||||||
LuaWrapper<T>::classname = _classname;
|
LuaWrapper<T>::classname = _classname;
|
||||||
LuaWrapper<T>::identifier = _identifier;
|
LuaWrapper<T>::identifier = _identifier;
|
||||||
LuaWrapper<T>::allocator = _allocator;
|
LuaWrapper<T>::allocator = _allocator;
|
||||||
LuaWrapper<T>::deallocator = _deallocator;
|
|
||||||
const luaL_Reg defaulttable[] = {
|
const luaL_Reg defaulttable[] = {
|
||||||
{ "new", luaW_new<T> },
|
{ "new", luaW_new<T> },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
@ -575,15 +568,46 @@ template <typename T> void luaW_setfuncs(lua_State* _L,
|
|||||||
luaW_registerfuncs(_L, defaultmetatable, _metatable); // ... T mt
|
luaW_registerfuncs(_L, defaultmetatable, _metatable); // ... T mt
|
||||||
lua_setfield(_L, -2, "metatable"); // ... T
|
lua_setfield(_L, -2, "metatable"); // ... T
|
||||||
}
|
}
|
||||||
|
template <typename T> void luaW_setfuncs(lua_State* _L,
|
||||||
|
const char* _classname,
|
||||||
|
const luaL_Reg* _table,
|
||||||
|
const luaL_Reg* _metatable,
|
||||||
|
std::shared_ptr<T> (*_allocator)(lua_State*)) {
|
||||||
|
luaW_setfuncs(_L, _classname, _table, _metatable, _allocator, luaW_defaultidentifier<T>);
|
||||||
|
}
|
||||||
|
template <typename T> void luaW_setfuncs(lua_State* _L,
|
||||||
|
const char* _classname,
|
||||||
|
const luaL_Reg* _table,
|
||||||
|
const luaL_Reg* _metatable) {
|
||||||
|
luaW_setfuncs(_L, _classname, _table, _metatable, luaW_defaultallocator<T>, luaW_defaultidentifier<T>);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T> void luaW_register(lua_State* _L,
|
template <typename T> void luaW_register(lua_State* _L,
|
||||||
const char* _classname,
|
const char* _classname,
|
||||||
const luaL_Reg* _table,
|
const luaL_Reg* _table,
|
||||||
const luaL_Reg* _metatable,
|
const luaL_Reg* _metatable,
|
||||||
T* (*_allocator)(lua_State*) = luaW_defaultallocator<T>,
|
std::shared_ptr<T> (*_allocator)(lua_State*),
|
||||||
void (*_deallocator)(lua_State*, T*) = luaW_defaultdeallocator<T>,
|
void (*_identifier)(lua_State*, std::shared_ptr<T>)) {
|
||||||
void (*_identifier)(lua_State*, T*) = luaW_defaultidentifier<T>) {
|
luaW_setfuncs(_L, _classname, _table, _metatable, _allocator, _identifier); // ... T
|
||||||
luaW_setfuncs(_L, _classname, _table, _metatable, _allocator, _deallocator, _identifier); // ... T
|
lua_pushvalue(_L, -1); // ... T T
|
||||||
|
lua_setglobal(_L, _classname); // ... T
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> void luaW_register(lua_State* _L,
|
||||||
|
const char* _classname,
|
||||||
|
const luaL_Reg* _table,
|
||||||
|
const luaL_Reg* _metatable,
|
||||||
|
std::shared_ptr<T> (*_allocator)(lua_State*)) {
|
||||||
|
luaW_setfuncs(_L, _classname, _table, _metatable, _allocator, luaW_defaultidentifier<T>);
|
||||||
|
lua_pushvalue(_L, -1); // ... T T
|
||||||
|
lua_setglobal(_L, _classname); // ... T
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> void luaW_register(lua_State* _L,
|
||||||
|
const char* _classname,
|
||||||
|
const luaL_Reg* _table,
|
||||||
|
const luaL_Reg* _metatable) {
|
||||||
|
luaW_setfuncs(_L, _classname, _table, _metatable, luaW_defaultallocator<T>, luaW_defaultidentifier<T>); // ... T
|
||||||
lua_pushvalue(_L, -1); // ... T T
|
lua_pushvalue(_L, -1); // ... T T
|
||||||
lua_setglobal(_L, _classname); // ... T
|
lua_setglobal(_L, _classname); // ... T
|
||||||
}
|
}
|
||||||
@ -641,3 +665,6 @@ template <typename T, typename U> void luaW_extend(lua_State* _L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <luaWrapper/luaWrapperUtil.hpp>
|
||||||
|
//#include <luaWrapper/luaWrapperStd.hpp>
|
||||||
|
132
luaWrapper/luaWrapperStd.cpp
Normal file
132
luaWrapper/luaWrapperStd.cpp
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
/**
|
||||||
|
* @author Edouard DUPIN
|
||||||
|
*
|
||||||
|
* @copyright 2011, Edouard DUPIN, all right reserved
|
||||||
|
*
|
||||||
|
* @license APACHE v2.0 (see license file)
|
||||||
|
*/
|
||||||
|
#ifndef LUA_WRAPPER_STD_H__
|
||||||
|
#define LUA_WRAPPER_STD_H__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <lua/lua.h>
|
||||||
|
#include <luaWrapper/luaWrapper.hpp>
|
||||||
|
#include <luaWrapper/luaWrapperUtil.hpp>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LuaWrapper knows about primitive types like ints and floats, but it doesn't
|
||||||
|
* know about things like std::strings or other more complicated types.
|
||||||
|
* Sometimes, rather than register the type with LuaWrapper, it's easier to
|
||||||
|
* be able to convert it to and from Lua's primitive types, like strings or
|
||||||
|
* tables.
|
||||||
|
*
|
||||||
|
* To do this, you must write luaU_check, luaU_to and luaU_push functions for
|
||||||
|
* your type. You don't always need all three, it depends on if you're pushing
|
||||||
|
* objects to Lua, getting objects from Lua, or both.
|
||||||
|
*
|
||||||
|
* This example uses std::string, but if you have other custom string types it
|
||||||
|
* should be easy to write versions of those functions too
|
||||||
|
*/
|
||||||
|
template<> std::string luaU_check<std::string>(lua_State* _L, int _index) {
|
||||||
|
return std::string(luaL_checkstring(_L, _index));
|
||||||
|
}
|
||||||
|
template<> std::string luaU_to<std::string>(lua_State* _L, int _index) {
|
||||||
|
return std::string(lua_tostring(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<std::string>(lua_State* _L, const std::string& _val) {
|
||||||
|
lua_pushstring(_L, _val.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> bool luaU_check<bool>(lua_State* _L, int _index) {
|
||||||
|
return lua_toboolean(_L, _index) != 0;
|
||||||
|
}
|
||||||
|
template<> bool luaU_to<bool>(lua_State* _L, int _index) {
|
||||||
|
return lua_toboolean(_L, _index) != 0;
|
||||||
|
}
|
||||||
|
template<> void luaU_push<bool>(lua_State* _L, const bool& _value) {
|
||||||
|
lua_pushboolean(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> const char* luaU_check<const char*>(lua_State* _L, int _index) {
|
||||||
|
return luaL_checkstring(_L, _index);
|
||||||
|
}
|
||||||
|
template<> const char* luaU_to<const char*>(lua_State* _L, int _index) {
|
||||||
|
return lua_tostring(_L, _index);
|
||||||
|
}
|
||||||
|
template<> void luaU_push<const char*>(lua_State* _L, const char* const& _value) {
|
||||||
|
lua_pushstring(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> const char* const luaU_check<const char* const>(lua_State* _L, int _index) {
|
||||||
|
return luaL_checkstring(_L, _index);
|
||||||
|
}
|
||||||
|
template<> const char* const luaU_to<const char* const>(lua_State* _L, int _index) {
|
||||||
|
return lua_tostring(_L, _index);
|
||||||
|
}
|
||||||
|
template<> void luaU_push<const char* const>(lua_State* _L, const char* const& _value) {
|
||||||
|
lua_pushstring(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> unsigned int luaU_check<unsigned int>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<unsigned int>(luaL_checkinteger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> unsigned int luaU_to<unsigned int>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<unsigned int>(lua_tointeger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<unsigned int>(lua_State* _L, const unsigned int& _value) {
|
||||||
|
lua_pushinteger(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> int luaU_check<int>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<int>(luaL_checkinteger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> int luaU_to<int>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<int>(lua_tointeger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<int>(lua_State* _L, const int& _value) {
|
||||||
|
lua_pushinteger(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> unsigned char luaU_check<unsigned char>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<unsigned char>(luaL_checkinteger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> unsigned char luaU_to<unsigned char>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<unsigned char>(lua_tointeger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<unsigned char>(lua_State* _L, const unsigned char& _value) {
|
||||||
|
lua_pushinteger(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> char luaU_check<char>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<char>(luaL_checkinteger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> char luaU_to<char>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<char>(lua_tointeger(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<char>(lua_State* _L, const char& _value) {
|
||||||
|
lua_pushinteger(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> float luaU_check<float>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<float>(luaL_checknumber(_L, _index));
|
||||||
|
}
|
||||||
|
template<> float luaU_to<float>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<float>(lua_tonumber(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<float>(lua_State* _L, const float& _value) {
|
||||||
|
lua_pushnumber(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<> double luaU_check<double>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<double>(luaL_checknumber(_L, _index));
|
||||||
|
}
|
||||||
|
template<> double luaU_to<double>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<double>(lua_tonumber(_L, _index));
|
||||||
|
}
|
||||||
|
template<> void luaU_push<double>(lua_State* _L, const double& _value) {
|
||||||
|
lua_pushnumber(_L, _value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
@ -41,155 +41,58 @@ template <typename T> struct luaW_remove_cr {
|
|||||||
* LuaWrapper, especially with small objects.
|
* LuaWrapper, especially with small objects.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template<typename U, typename = void> struct luaU_Impl {
|
template<typename U> U luaU_check(lua_State* _L, int _index);
|
||||||
static U luaU_check(lua_State* _L, int _index);
|
template<typename U> U luaU_to(lua_State* _L, int _index);
|
||||||
static U luaU_to(lua_State* _L, int _index);
|
template<typename U> void luaU_push(lua_State* _L, const U& _value);
|
||||||
static void luaU_push(lua_State* _L, const U& _value);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
/*
|
||||||
template<typename U> U luaU_check(lua_State* _L, int _index) {
|
template<typename U> U luaU_check(lua_State* _L, int _index) {
|
||||||
return luaU_Impl<U>::luaU_check(_L, _index);
|
return implem_luaU_check<U>(_L, _index);
|
||||||
}
|
}
|
||||||
template<typename U> U luaU_to(lua_State* _L, int _index) {
|
template<typename U> U luaU_to(lua_State* _L, int _index) {
|
||||||
return luaU_Impl<U>::luaU_to(_L, _index);
|
return implem_luaU_to<U>(_L, _index);
|
||||||
}
|
}
|
||||||
template<typename U> void luaU_push(lua_State* _L, const U& _value) {
|
template<typename U> void luaU_push(lua_State* _L, const U& _value) {
|
||||||
luaU_Impl<U>::luaU_push(_L, _value);
|
implem_luaU_push<U>(_L, _value);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/** This is slightly different than the previous three functions in that you
|
/**
|
||||||
|
* This is slightly different than the previous three functions in that you
|
||||||
* shouldn't need to write your own version of it, since it uses luaU_check
|
* shouldn't need to write your own version of it, since it uses luaU_check
|
||||||
* automatically.
|
* automatically.
|
||||||
*/
|
*/
|
||||||
template<typename U> U luaU_opt(lua_State* _L, int _index, const U& _fallback = U()) {
|
template<typename U> U luaU_opt(lua_State* _L, int _index, const U& _fallback = U()) {
|
||||||
if (lua_isnil(_L, _index))
|
if (lua_isnil(_L, _index)) {
|
||||||
return _fallback;
|
return _fallback;
|
||||||
else
|
} else {
|
||||||
return luaU_Impl<U>::luaU_check(_L, _index);
|
return luaU_check<U>(_L, _index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
template<typename T> T implem_luaU_check<T, typename LUAW_STD::enable_if<LUAW_STD::is_enum<T>::value>::type>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<T>(luaL_checkinteger(_L, _index));
|
||||||
|
}
|
||||||
|
template<typename T> T implem_luaU_to<T, typename LUAW_STD::enable_if<LUAW_STD::is_enum<T>::value>::type>(lua_State* _L, int _index) {
|
||||||
|
return static_cast<T>(lua_tointeger(_L, _index));
|
||||||
|
}
|
||||||
|
template<typename T> void implem_luaU_push<T, typename LUAW_STD::enable_if<LUAW_STD::is_enum<T>::value>::type>(lua_State* _L, const T& _value) {
|
||||||
|
lua_pushnumber(_L, static_cast<int>(_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<> struct luaU_Impl<bool> {
|
template<typename T> T* implem_luaU_check<T*, typename LUAW_STD::enable_if<LUAW_STD::is_class<T>::value>::type>(lua_State* _L, int _index) {
|
||||||
static bool luaU_check(lua_State* _L, int _index) {
|
return luaW_check<T>(_L, _index);
|
||||||
return lua_toboolean(_L, _index) != 0;
|
}
|
||||||
}
|
template<typename T> T* implem_luaU_to<T*, typename LUAW_STD::enable_if<LUAW_STD::is_class<T>::value>::type>(lua_State* _L, int _index) {
|
||||||
static bool luaU_to(lua_State* _L, int _index) {
|
return luaW_to<T>(_L, _index);
|
||||||
return lua_toboolean(_L, _index) != 0;
|
}
|
||||||
}
|
template<typename T> void implem_luaU_push<T*, typename LUAW_STD::enable_if<LUAW_STD::is_class<T>::value>::type>(lua_State* _L, T*& _value) {
|
||||||
static void luaU_push(lua_State* _L, const bool& _value) {
|
luaW_push <T>(_L, _value);
|
||||||
lua_pushboolean(_L, _value);
|
}
|
||||||
}
|
template<typename T> void implem_luaU_push<T*, typename LUAW_STD::enable_if<LUAW_STD::is_class<T>::value>::type>(lua_State* _L, T* _value) {
|
||||||
};
|
luaW_push <T>(_L, _value);
|
||||||
|
}
|
||||||
template<> struct luaU_Impl<const char*> {
|
*/
|
||||||
static const char* luaU_check(lua_State* _L, int _index) {
|
|
||||||
return luaL_checkstring(_L, _index);
|
|
||||||
}
|
|
||||||
static const char* luaU_to(lua_State* _L, int _index) {
|
|
||||||
return lua_tostring(_L, _index);
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const char* const& _value) {
|
|
||||||
lua_pushstring(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> struct luaU_Impl<unsigned int> {
|
|
||||||
static unsigned int luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<unsigned int>(luaL_checkinteger(_L, _index));
|
|
||||||
}
|
|
||||||
static unsigned int luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<unsigned int>(lua_tointeger(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const unsigned int& _value) {
|
|
||||||
lua_pushinteger(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> struct luaU_Impl<int> {
|
|
||||||
static int luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<int>(luaL_checkinteger(_L, _index));
|
|
||||||
}
|
|
||||||
static int luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<int>(lua_tointeger(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const int& _value) {
|
|
||||||
lua_pushinteger(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> struct luaU_Impl<unsigned char> {
|
|
||||||
static unsigned char luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<unsigned char>(luaL_checkinteger(_L, _index));
|
|
||||||
}
|
|
||||||
static unsigned char luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<unsigned char>(lua_tointeger(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const unsigned char& _value) {
|
|
||||||
lua_pushinteger(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> struct luaU_Impl<char> {
|
|
||||||
static char luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<char>(luaL_checkinteger(_L, _index));
|
|
||||||
}
|
|
||||||
static char luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<char>(lua_tointeger(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const char& _value) {
|
|
||||||
lua_pushinteger(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> struct luaU_Impl<float> {
|
|
||||||
static float luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<float>(luaL_checknumber(_L, _index));
|
|
||||||
}
|
|
||||||
static float luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<float>(lua_tonumber(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const float& _value) {
|
|
||||||
lua_pushnumber(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<> struct luaU_Impl<double> {
|
|
||||||
static double luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<double>(luaL_checknumber(_L, _index));
|
|
||||||
}
|
|
||||||
static double luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<double>(lua_tonumber(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const double& _value) {
|
|
||||||
lua_pushnumber(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T> struct luaU_Impl<T, typename LUAW_STD::enable_if<LUAW_STD::is_enum<T>::value>::type> {
|
|
||||||
static T luaU_check(lua_State* _L, int _index) {
|
|
||||||
return static_cast<T>(luaL_checkinteger(_L, _index));
|
|
||||||
}
|
|
||||||
static T luaU_to(lua_State* _L, int _index) {
|
|
||||||
return static_cast<T>(lua_tointeger(_L, _index));
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, const T& _value) {
|
|
||||||
lua_pushnumber(_L, static_cast<int>(_value));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T> struct luaU_Impl<T*, typename LUAW_STD::enable_if<LUAW_STD::is_class<T>::value>::type> {
|
|
||||||
static T* luaU_check(lua_State* _L, int _index) {
|
|
||||||
return luaW_check<T>(_L, _index);
|
|
||||||
}
|
|
||||||
static T* luaU_to(lua_State* _L, int _index) {
|
|
||||||
return luaW_to<T>(_L, _index);
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, T*& _value) {
|
|
||||||
luaW_push <T>(_L, _value);
|
|
||||||
}
|
|
||||||
static void luaU_push(lua_State* _L, T* _value) {
|
|
||||||
luaW_push <T>(_L, _value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* These are just some functions I've always felt should exist
|
* These are just some functions I've always felt should exist
|
||||||
@ -590,7 +493,7 @@ template<class T, class ReturnType, class... Args, ReturnType(T::*MemberFunc)(Ar
|
|||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
template<int... indices> static int callImpl(lua_State* _L, luaU_IntPack<indices...>) {
|
template<int... indices> static int callImpl(lua_State* _L, luaU_IntPack<indices...>) {
|
||||||
luaU_push<ReturnType>(_L, (luaW_check<T>(_L, 1)->*MemberFunc)(luaU_check<typename luaW_remove_cr<Args>::type>(_L, indices)...));
|
luaU_push<ReturnType>(_L, ((*luaW_check<T>(_L, 1)).*MemberFunc)(luaU_check<typename luaW_remove_cr<Args>::type>(_L, indices)...));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -602,7 +505,7 @@ template<class T, class... Args, void(T::*MemberFunc)(Args...)> struct luaU_Memb
|
|||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
template<int... indices> static int callImpl(lua_State* _L, luaU_IntPack<indices...>) {
|
template<int... indices> static int callImpl(lua_State* _L, luaU_IntPack<indices...>) {
|
||||||
(luaW_check<T>(_L, 1)->*MemberFunc)(luaU_check<typename luaW_remove_cr<Args>::type>(_L, indices)...);
|
((*luaW_check<T>(_L, 1)).*MemberFunc)(luaU_check<typename luaW_remove_cr<Args>::type>(_L, indices)...);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,13 @@ def create(target):
|
|||||||
# name of the dependency
|
# name of the dependency
|
||||||
myModule.add_module_depend('lua')
|
myModule.add_module_depend('lua')
|
||||||
|
|
||||||
|
# add extra compilation flags :
|
||||||
|
myModule.add_extra_compile_flags()
|
||||||
|
# add the file to compile:
|
||||||
|
myModule.add_src_file([
|
||||||
|
'luaWrapper/luaWrapperStd.cpp'
|
||||||
|
])
|
||||||
|
|
||||||
myModule.add_export_path(tools.get_current_path(__file__))
|
myModule.add_export_path(tools.get_current_path(__file__))
|
||||||
|
|
||||||
# add the currrent module at the
|
# add the currrent module at the
|
||||||
|
@ -132,9 +132,9 @@ static luaL_Reg Example_metatable[] = {
|
|||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
int luaopen_Example(lua_State* L)
|
int luaopen_Example(lua_State* _L)
|
||||||
{
|
{
|
||||||
luaW_register<Example>(L, "Example", NULL, Example_metatable);
|
luaW_register<Example>(_L, "Example", NULL, Example_metatable);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user