[DEV] add a namespace

This commit is contained in:
Edouard DUPIN 2018-08-13 22:26:02 +02:00
parent 831faeef69
commit f84ced7d43
16 changed files with 1518 additions and 1538 deletions

12
luaWrapper/debug.cpp Normal file
View File

@ -0,0 +1,12 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#include <luaWrapper/debug.hpp>
int32_t luaWrapper::getLogId() {
static int32_t g_val = elog::registerInstance("luaWrapper");
return g_val;
}

37
luaWrapper/debug.hpp Normal file
View File

@ -0,0 +1,37 @@
/** @file
* @author Edouard DUPIN
* @copyright 2011, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
#pragma once
#include <elog/log.hpp>
namespace luaWrapper {
int32_t getLogId();
};
#define LUAW_BASE(info,data) ELOG_BASE(luaWrapper::getLogId(),info,data)
#define LUAW_PRINT(data) LUAW_BASE(-1, data)
#define LUAW_CRITICAL(data) LUAW_BASE(1, data)
#define LUAW_ERROR(data) LUAW_BASE(2, data)
#define LUAW_WARNING(data) LUAW_BASE(3, data)
#define LUAW_INFO(data) LUAW_BASE(4, data)
#ifdef DEBUG
#define LUAW_DEBUG(data) LUAW_BASE(5, data)
#define LUAW_VERBOSE(data) LUAW_BASE(6, data)
#define LUAW_TODO(data) LUAW_BASE(4, "TODO : " << data)
#else
#define LUAW_DEBUG(data) do { } while(false)
#define LUAW_VERBOSE(data) do { } while(false)
#define LUAW_TODO(data) do { } while(false)
#endif
#define LUAW_ASSERT(cond,data) \
do { \
if (!(cond)) { \
LUAW_CRITICAL(data); \
assert(!#cond); \
} \
} while (0)

File diff suppressed because it is too large Load Diff

View File

@ -5,128 +5,129 @@
*
* @license APACHE v2.0 (see license file)
*/
#ifndef LUA_WRAPPER_STD_H__
#define LUA_WRAPPER_STD_H__
#include <etk/String.hpp>
#include <lua/lua.h>
#include <luaWrapper/luaWrapper.hpp>
#include <luaWrapper/luaWrapperUtil.hpp>
#include <luaWrapper/debug.hpp>
/**
* LuaWrapper knows about primitive types like ints and floats, but it doesn't
* know about things like etk::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 etk::String, but if you have other custom string types it
* should be easy to write versions of those functions too
*/
template<> etk::String luaU_check<etk::String>(lua_State* _L, int _index) {
return etk::String(luaL_checkstring(_L, _index));
}
template<> etk::String luaU_to<etk::String>(lua_State* _L, int _index) {
return etk::String(lua_tostring(_L, _index));
}
template<> void luaU_push<etk::String>(lua_State* _L, const etk::String& _val) {
lua_pushstring(_L, _val.c_str());
namespace luaWrapper {
namespace utils {
/**
* LuaWrapper knows about primitive types like ints and floats, but it doesn't
* know about things like etk::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 luaWrapper::utils::check, luaWrapper::utils::to and luaWrapper::utils::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 etk::String, but if you have other custom string types it
* should be easy to write versions of those functions too
*/
template<> etk::String check<etk::String>(lua_State* _L, int _index) {
return etk::String(luaL_checkstring(_L, _index));
}
template<> etk::String to<etk::String>(lua_State* _L, int _index) {
return etk::String(lua_tostring(_L, _index));
}
template<> void push<etk::String>(lua_State* _L, const etk::String& _val) {
lua_pushstring(_L, _val.c_str());
}
template<> bool check<bool>(lua_State* _L, int _index) {
return lua_toboolean(_L, _index) != 0;
}
template<> bool to<bool>(lua_State* _L, int _index) {
return lua_toboolean(_L, _index) != 0;
}
template<> void push<bool>(lua_State* _L, const bool& _value) {
lua_pushboolean(_L, _value);
}
template<> const char* check<const char*>(lua_State* _L, int _index) {
return luaL_checkstring(_L, _index);
}
template<> const char* to<const char*>(lua_State* _L, int _index) {
return lua_tostring(_L, _index);
}
template<> void push<const char*>(lua_State* _L, const char* const& _value) {
lua_pushstring(_L, _value);
}
template<> const char* const check<const char* const>(lua_State* _L, int _index) {
return luaL_checkstring(_L, _index);
}
template<> const char* const to<const char* const>(lua_State* _L, int _index) {
return lua_tostring(_L, _index);
}
template<> void push<const char* const>(lua_State* _L, const char* const& _value) {
lua_pushstring(_L, _value);
}
template<> unsigned int check<unsigned int>(lua_State* _L, int _index) {
return static_cast<unsigned int>(luaL_checkinteger(_L, _index));
}
template<> unsigned int to<unsigned int>(lua_State* _L, int _index) {
return static_cast<unsigned int>(lua_tointeger(_L, _index));
}
template<> void push<unsigned int>(lua_State* _L, const unsigned int& _value) {
lua_pushinteger(_L, _value);
}
template<> int check<int>(lua_State* _L, int _index) {
return static_cast<int>(luaL_checkinteger(_L, _index));
}
template<> int to<int>(lua_State* _L, int _index) {
return static_cast<int>(lua_tointeger(_L, _index));
}
template<> void push<int>(lua_State* _L, const int& _value) {
lua_pushinteger(_L, _value);
}
template<> unsigned char check<unsigned char>(lua_State* _L, int _index) {
return static_cast<unsigned char>(luaL_checkinteger(_L, _index));
}
template<> unsigned char to<unsigned char>(lua_State* _L, int _index) {
return static_cast<unsigned char>(lua_tointeger(_L, _index));
}
template<> void push<unsigned char>(lua_State* _L, const unsigned char& _value) {
lua_pushinteger(_L, _value);
}
template<> char check<char>(lua_State* _L, int _index) {
return static_cast<char>(luaL_checkinteger(_L, _index));
}
template<> char to<char>(lua_State* _L, int _index) {
return static_cast<char>(lua_tointeger(_L, _index));
}
template<> void push<char>(lua_State* _L, const char& _value) {
lua_pushinteger(_L, _value);
}
template<> float check<float>(lua_State* _L, int _index) {
return static_cast<float>(luaL_checknumber(_L, _index));
}
template<> float to<float>(lua_State* _L, int _index) {
return static_cast<float>(lua_tonumber(_L, _index));
}
template<> void push<float>(lua_State* _L, const float& _value) {
lua_pushnumber(_L, _value);
}
template<> double check<double>(lua_State* _L, int _index) {
return static_cast<double>(luaL_checknumber(_L, _index));
}
template<> double to<double>(lua_State* _L, int _index) {
return static_cast<double>(lua_tonumber(_L, _index));
}
template<> void push<double>(lua_State* _L, const double& _value) {
lua_pushnumber(_L, _value);
}
}
}
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

File diff suppressed because it is too large Load Diff

View File

@ -36,9 +36,11 @@ def configure(target, my_module):
'ememory'
])
my_module.add_src_file([
'luaWrapper/debug.cpp',
'luaWrapper/luaWrapperEtk.cpp',
])
my_module.add_header_file([
'luaWrapper/debug.hpp',
'luaWrapper/luaWrapper.hpp',
'luaWrapper/luaWrapperUtil.hpp',
])

View File

@ -1,15 +0,0 @@
LuaWrapperExample
Copyright (c) 2010-2013 Alexander Ames
Alexander.Ames@gmail.com
This repository is a very simple example of how you can use LuaWrapper to
create typesafe bindings between Lua and C++. This is not a complete tutorial,
but rather just a quick project to illustrate how the library is used.
Specifically, it does not cover the hold, release or clean functions.
See the README functions in ./example1/ and ./example2/ for more information.
The main repository for LuaWrapper can be found at
https://bitbucket.org/alexames/luawrapper/

View File

@ -15,7 +15,7 @@ ETK_DECLARE_TYPE(BankAccount);
* Allocator
*
* Types that do not have a default contructor require you to write an allocator function.
* This function is passed to luaW_register.
* This function is passed to luaWrapper::register.
*/
ememory::SharedPtr<BankAccount> BankAccount_new(lua_State *_luaState) {
const char* owner = luaL_checkstring(_luaState, 1);
@ -43,27 +43,27 @@ int BankAccount_checkTotalMoneyInBank(lua_State *_luaState) {
*/
int BankAccount_getOwnerName(lua_State *_luaState) {
auto account = luaW_check<BankAccount>(_luaState, 1);
auto account = luaWrapper::check<BankAccount>(_luaState, 1);
lua_pushstring(_luaState, account->getOwnerName());
return 1;
}
int BankAccount_deposit(lua_State* _luaState) {
auto account = luaW_check<BankAccount>(_luaState, 1);
auto account = luaWrapper::check<BankAccount>(_luaState, 1);
float amount = luaL_checknumber(_luaState, 2);
account->deposit(amount);
return 0;
}
int BankAccount_withdraw(lua_State* _luaState) {
auto account = luaW_check<BankAccount>(_luaState, 1);
auto account = luaWrapper::check<BankAccount>(_luaState, 1);
float amount = luaL_checknumber(_luaState, 2);
account->withdraw(amount);
return 0;
}
int BankAccount_checkBalance(lua_State* _luaState) {
auto account = luaW_check<BankAccount>(_luaState, 1);
auto account = luaWrapper::check<BankAccount>(_luaState, 1);
lua_pushnumber(_luaState, account->checkBalance());
return 1;
}
@ -82,7 +82,7 @@ static luaL_Reg BankAccount_metatable[] = {
};
int luaopen_BankAccount(lua_State* _luaState) {
luaW_register<BankAccount>(_luaState,
luaWrapper::registerElement<BankAccount>(_luaState,
"BankAccount",
BankAccount_table,
BankAccount_metatable,

View File

@ -1,3 +0,0 @@
all:
g++ -std=c++0x -L../lua-5.1/src -I../lua-5.1/src -I../luawrapper BankAccount.cpp LuaBankAccount.cpp main.cpp -llua -ldl -o example1

View File

@ -1,22 +0,0 @@
This folder includes a simple project to demonstrate how LuaWrapper may be used
to bind your C++ class to Lua.
main.cpp contains a simple main function that will call luaopen_BankAccount
then run the script given.
BankAccount.cpp and BankAccount.hpp contain a simple BankAccount class which you
can use to deposit and withdraw money from, and check the current balance.
LuaBankAccount.cpp contains the function wrappers. This is where the interesting
part is.
A visual studio project and makefile are provided, or you can use your own
tool chain to compile the example. Once compiled, run
example1.exe example1.lua
or
./example1 example1.lua
to see the code in action.

View File

@ -1,9 +1,6 @@
#include <iostream>
#include <lua/lua.h>
#include <lua/lualib.h>
#include <lua/lauxlib.h>
#include <test-debug/debug.hpp>
#include <etk/etk.hpp>
#include <luaWrapper/luaWrapper.hpp>
#include "LuaBankAccount.hpp"
@ -32,13 +29,11 @@ int main(int _argc, const char *_argv[]) {
TEST_ERROR("missing file...");
usage();
}
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaopen_BankAccount(L);
if (luaL_dofile(L, inputFileName.c_str())) {
TEST_PRINT(lua_tostring(L, -1));
{
luaWrapper::Lua lua;
luaopen_BankAccount(lua.getState());
lua.executeFile(inputFileName);
}
lua_close(L);
TEST_PRINT("END SAMPLE 1");
return 0;
}

View File

@ -13,21 +13,21 @@
* table holding the x and y values
*/
template<>
Vector2D luaU_check<Vector2D>(lua_State* _L, int _index) {
return Vector2D(luaU_getfield<float>(_L, _index, "x"),
luaU_getfield<float>(_L, _index, "y"));
Vector2D luaWrapper::utils::check<Vector2D>(lua_State* _L, int _index) {
return Vector2D(luaWrapper::utils::getfield<float>(_L, _index, "x"),
luaWrapper::utils::getfield<float>(_L, _index, "y"));
}
template<>
Vector2D luaU_to<Vector2D>(lua_State* _L, int _index ) {
return Vector2D(luaU_getfield<float>(_L, _index, "x"),
luaU_getfield<float>(_L, _index, "y"));
Vector2D luaWrapper::utils::to<Vector2D>(lua_State* _L, int _index ) {
return Vector2D(luaWrapper::utils::getfield<float>(_L, _index, "x"),
luaWrapper::utils::getfield<float>(_L, _index, "y"));
}
template<>
void luaU_push<Vector2D>(lua_State* _L, const Vector2D& _val) {
void luaWrapper::utils::push<Vector2D>(lua_State* _L, const Vector2D& _val) {
lua_newtable(_L);
luaU_setfield<float>(_L, -1, "x", _val.x);
luaU_setfield<float>(_L, -1, "y", _val.y);
luaWrapper::utils::setfield<float>(_L, -1, "x", _val.x);
luaWrapper::utils::setfield<float>(_L, -1, "y", _val.y);
}

View File

@ -10,7 +10,7 @@
#include <test-debug/debug.hpp>
static int Example_PrintMe(lua_State* L) {
ememory::SharedPtr<Example> ex = luaW_check<Example>(L, 1);
ememory::SharedPtr<Example> ex = luaWrapper::check<Example>(L, 1);
TEST_PRINT( "Example=");
TEST_PRINT( " m_boolean=" << ex->m_boolean);
TEST_PRINT( " m_integer=" << ex->m_integer);
@ -36,41 +36,41 @@ static luaL_Reg Example_metatable[] = {
// them directly with these templates.
//
// Class | data type | class member
{ "getBoolean", luaU_get<Example, bool, &Example::m_boolean> },
{ "setBoolean", luaU_set<Example, bool, &Example::m_boolean> },
{ "m_boolean", luaU_getset<Example, bool, &Example::m_boolean> },
{ "getBoolean", luaWrapper::utils::get<Example, bool, &Example::m_boolean> },
{ "setBoolean", luaWrapper::utils::set<Example, bool, &Example::m_boolean> },
{ "m_boolean", luaWrapper::utils::getset<Example, bool, &Example::m_boolean> },
{ "getInteger", luaU_get<Example, int, &Example::m_integer> },
{ "setInteger", luaU_set<Example, int, &Example::m_integer> },
{ "m_integer", luaU_getset<Example, int, &Example::m_integer> },
{ "getInteger", luaWrapper::utils::get<Example, int, &Example::m_integer> },
{ "setInteger", luaWrapper::utils::set<Example, int, &Example::m_integer> },
{ "m_integer", luaWrapper::utils::getset<Example, int, &Example::m_integer> },
{ "getUInteger", luaU_get<Example, unsigned int, &Example::m_uinteger> },
{ "setUInteger", luaU_set<Example, unsigned int, &Example::m_uinteger> },
{ "m_uinteger", luaU_getset<Example, unsigned int, &Example::m_uinteger> },
{ "getUInteger", luaWrapper::utils::get<Example, unsigned int, &Example::m_uinteger> },
{ "setUInteger", luaWrapper::utils::set<Example, unsigned int, &Example::m_uinteger> },
{ "m_uinteger", luaWrapper::utils::getset<Example, unsigned int, &Example::m_uinteger> },
{ "getCString", luaU_get<Example, const char*, &Example::m_cstring> },
{ "setCString", luaU_set<Example, const char*, &Example::m_cstring> },
{ "m_string", luaU_getset<Example, const char*, &Example::m_cstring> },
{ "getCString", luaWrapper::utils::get<Example, const char*, &Example::m_cstring> },
{ "setCString", luaWrapper::utils::set<Example, const char*, &Example::m_cstring> },
{ "m_string", luaWrapper::utils::getset<Example, const char*, &Example::m_cstring> },
{ "getCPPString", luaU_get<Example, etk::String, &Example::m_cppstring> },
{ "setCPPString", luaU_set<Example, etk::String, &Example::m_cppstring> },
{ "m_cppstring", luaU_getset<Example, etk::String, &Example::m_cppstring> },
{ "getCPPString", luaWrapper::utils::get<Example, etk::String, &Example::m_cppstring> },
{ "setCPPString", luaWrapper::utils::set<Example, etk::String, &Example::m_cppstring> },
{ "m_cppstring", luaWrapper::utils::getset<Example, etk::String, &Example::m_cppstring> },
{ "getVec", luaU_get<Example, Vector2D, &Example::m_vec> },
{ "setVec", luaU_set<Example, Vector2D, &Example::m_vec> },
{ "m_vec", luaU_getset<Example, Vector2D, &Example::m_vec> },
{ "getVec", luaWrapper::utils::get<Example, Vector2D, &Example::m_vec> },
{ "setVec", luaWrapper::utils::set<Example, Vector2D, &Example::m_vec> },
{ "m_vec", luaWrapper::utils::getset<Example, Vector2D, &Example::m_vec> },
{ "getNumber", luaU_get<Example, double, &Example::m_number> },
{ "setNumber", luaU_set<Example, double, &Example::m_number> },
{ "m_number", luaU_getset<Example, double, &Example::m_number> },
{ "getNumber", luaWrapper::utils::get<Example, double, &Example::m_number> },
{ "setNumber", luaWrapper::utils::set<Example, double, &Example::m_number> },
{ "m_number", luaWrapper::utils::getset<Example, double, &Example::m_number> },
{ "getFloatNumber", luaU_get<Example, float, &Example::m_floatnumber> },
{ "setFloatNumber", luaU_set<Example, float, &Example::m_floatnumber> },
{ "m_floatnumber", luaU_getset<Example, float, &Example::m_floatnumber> },
{ "getFloatNumber", luaWrapper::utils::get<Example, float, &Example::m_floatnumber> },
{ "setFloatNumber", luaWrapper::utils::set<Example, float, &Example::m_floatnumber> },
{ "m_floatnumber", luaWrapper::utils::getset<Example, float, &Example::m_floatnumber> },
{ "getPtr", luaU_get<Example, Example, &Example::m_ptr> },
{ "setPtr", luaU_set<Example, Example, &Example::m_ptr> },
{ "Ptr", luaU_getset<Example, Example, &Example::m_ptr> },
{ "getPtr", luaWrapper::utils::get<Example, Example, &Example::m_ptr> },
{ "setPtr", luaWrapper::utils::set<Example, Example, &Example::m_ptr> },
{ "Ptr", luaWrapper::utils::getset<Example, Example, &Example::m_ptr> },
// The getters and setters above work on member variables directly, but
// sometimes all you have are getter and setter functions instead of
@ -85,55 +85,55 @@ static luaL_Reg Example_metatable[] = {
// Class | data type | getter
// Class | data type | setter
// Class | data type | getter | setter
{ "getBooleanFunc", luaU_get<Example, bool, &Example::GetBoolean> },
{ "setBooleanFunc", luaU_set<Example, bool, &Example::SetBoolean> },
{ "BooleanFunc", luaU_getset<Example, bool, &Example::GetBoolean, &Example::SetBoolean> },
{ "getBooleanFunc", luaWrapper::utils::get<Example, bool, &Example::GetBoolean> },
{ "setBooleanFunc", luaWrapper::utils::set<Example, bool, &Example::SetBoolean> },
{ "BooleanFunc", luaWrapper::utils::getset<Example, bool, &Example::GetBoolean, &Example::SetBoolean> },
{ "getIntegerFunc", luaU_get<Example, int, &Example::GetInteger> },
{ "setIntegerFunc", luaU_set<Example, int, &Example::SetInteger> },
{ "IntegerFunc", luaU_getset<Example, int, &Example::GetInteger, &Example::SetInteger> },
{ "getIntegerFunc", luaWrapper::utils::get<Example, int, &Example::GetInteger> },
{ "setIntegerFunc", luaWrapper::utils::set<Example, int, &Example::SetInteger> },
{ "IntegerFunc", luaWrapper::utils::getset<Example, int, &Example::GetInteger, &Example::SetInteger> },
{ "getUIntegerFunc", luaU_get<Example, unsigned int, &Example::GetUInteger> },
{ "setUIntegerFunc", luaU_set<Example, unsigned int, &Example::SetUInteger> },
{ "UIntegerFunc", luaU_getset<Example, unsigned int, &Example::GetUInteger, &Example::SetUInteger> },
{ "getUIntegerFunc", luaWrapper::utils::get<Example, unsigned int, &Example::GetUInteger> },
{ "setUIntegerFunc", luaWrapper::utils::set<Example, unsigned int, &Example::SetUInteger> },
{ "UIntegerFunc", luaWrapper::utils::getset<Example, unsigned int, &Example::GetUInteger, &Example::SetUInteger> },
{ "getCStringFunc", luaU_get<Example, const char*, &Example::GetCString> },
{ "setCStringFunc", luaU_set<Example, const char*, &Example::SetCString> },
{ "CStringFunc", luaU_getset<Example, const char*, &Example::GetCString, &Example::SetCString> },
{ "getCStringFunc", luaWrapper::utils::get<Example, const char*, &Example::GetCString> },
{ "setCStringFunc", luaWrapper::utils::set<Example, const char*, &Example::SetCString> },
{ "CStringFunc", luaWrapper::utils::getset<Example, const char*, &Example::GetCString, &Example::SetCString> },
{ "getNumberFunc", luaU_get<Example, double, &Example::GetNumber> },
{ "setNumberFunc", luaU_set<Example, double, &Example::SetNumber> },
{ "NumberFunc", luaU_getset<Example, double, &Example::GetNumber, &Example::SetNumber> },
{ "getNumberFunc", luaWrapper::utils::get<Example, double, &Example::GetNumber> },
{ "setNumberFunc", luaWrapper::utils::set<Example, double, &Example::SetNumber> },
{ "NumberFunc", luaWrapper::utils::getset<Example, double, &Example::GetNumber, &Example::SetNumber> },
{ "getFloatNumberFunc", luaU_get<Example, float, &Example::GetFloatNumber> },
{ "setFloatNumberFunc", luaU_set<Example, float, &Example::SetFloatNumber> },
{ "FloatNumberFunc", luaU_getset<Example, float, &Example::GetFloatNumber, &Example::SetFloatNumber> },
{ "getFloatNumberFunc", luaWrapper::utils::get<Example, float, &Example::GetFloatNumber> },
{ "setFloatNumberFunc", luaWrapper::utils::set<Example, float, &Example::SetFloatNumber> },
{ "FloatNumberFunc", luaWrapper::utils::getset<Example, float, &Example::GetFloatNumber, &Example::SetFloatNumber> },
{ "getPtrFunc", luaU_get<Example, Example, &Example::GetPtr> },
{ "setPtrFunc", luaU_set<Example, Example, &Example::SetPtr> },
{ "PtrFunc", luaU_getset<Example, Example, &Example::GetPtr, &Example::SetPtr> },
{ "getPtrFunc", luaWrapper::utils::get<Example, Example, &Example::GetPtr> },
{ "setPtrFunc", luaWrapper::utils::set<Example, Example, &Example::SetPtr> },
{ "PtrFunc", luaWrapper::utils::getset<Example, Example, &Example::GetPtr, &Example::SetPtr> },
// In order to use luaU_get and luaU_set on non-primitive types, you must define luaU_to
// and luaU_check for that type.
// In order to use luaWrapper::utils::get and luaWrapper::utils::set on non-primitive types, you must define luaWrapper::utils::to
// and luaWrapper::utils::check for that type.
// See LuaCustomTypes.hpp for an example involving etk::String and Vector2D
{ "getCPPStringFunc", luaU_get<Example, etk::String, &Example::GetCPPString> },
{ "setCPPStringFunc", luaU_set<Example, etk::String, &Example::SetCPPString> },
{ "CPPStringFunc", luaU_getset<Example, etk::String, &Example::GetCPPString, &Example::SetCPPString> },
{ "getCPPStringFunc", luaWrapper::utils::get<Example, etk::String, &Example::GetCPPString> },
{ "setCPPStringFunc", luaWrapper::utils::set<Example, etk::String, &Example::SetCPPString> },
{ "CPPStringFunc", luaWrapper::utils::getset<Example, etk::String, &Example::GetCPPString, &Example::SetCPPString> },
{ "getVecFunc", luaU_get<Example, Vector2D, &Example::GetVec> },
{ "setSetFunc", luaU_set<Example, Vector2D, &Example::SetVec> },
{ "VecFunc", luaU_getset<Example, Vector2D, &Example::GetVec, &Example::SetVec> },
{ "getVecFunc", luaWrapper::utils::get<Example, Vector2D, &Example::GetVec> },
{ "setSetFunc", luaWrapper::utils::set<Example, Vector2D, &Example::SetVec> },
{ "VecFunc", luaWrapper::utils::getset<Example, Vector2D, &Example::GetVec, &Example::SetVec> },
{ "DoSomething", luaU_func(&Example::DoSomething) },
{ "DoSomething2", luaU_func(&Example::DoSomething2) },
{ "DoSomething", luaWrapperUtils_func(&Example::DoSomething) },
{ "DoSomething2", luaWrapperUtils_func(&Example::DoSomething2) },
//{ "DoSomethingElse1", luaU_funcsig(int, Example, DoSomethingElse, int, int) },
//{ "DoSomethingElse2", luaU_funcsig(int, Example, DoSomethingElse, float) },
//{ "DoSomethingElse1", luaWrapper::utils::funcsig(int, Example, DoSomethingElse, int, int) },
//{ "DoSomethingElse2", luaWrapper::utils::funcsig(int, Example, DoSomethingElse, float) },
{ NULL, NULL }
};
int luaopen_Example(lua_State* _L) {
luaW_register<Example>(_L, "Example", NULL, Example_metatable);
luaWrapper::registerElement<Example>(_L, "Example", NULL, Example_metatable);
return 1;
}

View File

@ -1,66 +0,0 @@
This folder includes a project that demonstrates some of the more sophisticated
and complex features of LuaWrapper. Make sure you fully understand the code in
example 1 before reading the code here. Example 2 introduces some of the
features contained in the luawrapperutil.hpp file which are prefixed with luaU_
instead of luaW_
# Automatic Getter and Setter Generation
In example 1, all of the wrapper functions were written manually. It is very
common need to write get or set function wrappers - so common in fact that this
patter was extracted out into a templated function. In example 1, BankAccount
had the following wrapper:
int BankAccount_checkBalance(lua_State *L)
{
BankAccount* account = luaW_check<BankAccount>(L, 1);
lua_pushnumber(L, account->checkBalance());
return 1;
}
static luaL_Reg BankAccount_metatable[] =
{
...
{ "checkBalance", BankAccount_checkBalance },
...
};
Rather than write code like that every time you need to create a get function,
you can instead just do this:
{ "checkBalance", luaU_get<BankAccount, float, &BankAccount::checkBalance },
This code will automatically instantiate a function wrapper that returns a
float, the result of a call to checkBalance.
# Generic push, to and check functions
Normally, when you need to push a value to Lua or get a value from the stack,
you use a function like lua_pushnumber or lua_tonumber. luawrapperutil.hpp
contains a generic version of these functions as well as specializations for
the primitive types in C++. That means you can do things like this:
int i = luaU_check<int>(L, 1);
luaU_push<double>(L, 1.234);
Additionally, it is possible to extend this functionality to non-primitive
types. A common use is to create a wrapper for std::strings so that you may do
this:
std::string str1 = luaU_check<std::string>(L, 1);
std::string str2 = "Lua is awesome!"
luaU_push<std::string>(L, str2);
Examples of how to create your own luaU_push, luaU_to, and luaU_check can be
found in LuaCustomTypes.hpp
A visual studio project and makefile are provided, or you can use your own
tool chain to compile the example. Once compiled, run
example2.exe example2.lua
or
./example2 example2.lua
to see the code in action.

View File

@ -6,6 +6,8 @@
#include <test-debug/debug.hpp>
#include <etk/etk.hpp>
#include <luaWrapper/luaWrapper.hpp>
#include "LuaExample.hpp"
static void usage() {
@ -32,13 +34,11 @@ int main(int _argc, const char *_argv[]) {
TEST_ERROR("missing file...");
usage();
}
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaopen_Example(L);
if (luaL_dofile(L, inputFileName.c_str())) {
TEST_PRINT(lua_tostring(L, -1));
{
luaWrapper::Lua lua;
luaopen_Example(lua.getState());
lua.executeFile(inputFileName);
}
lua_close(L);
TEST_PRINT("END SAMPLE 2");
return 0;
}