[DEV] better sample

This commit is contained in:
Edouard DUPIN 2018-08-12 14:57:02 +02:00
parent e228ec0040
commit d1180282ac
8 changed files with 74 additions and 38 deletions

View File

@ -38,6 +38,7 @@
#include <lua/lauxlib.h>
#include <ememory/memory.hpp>
#include <etk/typeInfo.hpp>
#include <etk/Allocator.hpp>
#define LUAW_POSTCTOR_KEY "__postctor"
#define LUAW_EXTENDS_KEY "__extends"
@ -246,7 +247,7 @@ void luaW_push(lua_State* _luaState,
lua_pop(_luaState, 1); // ... id cache
lua_insert(_luaState, -2); // ... cache id
// placement new creation (need to initilaize the sructure:
luaW_Userdata* ud = new (lua_newuserdata(_luaState, sizeof(luaW_Userdata))) luaW_Userdata(_obj, ETK_GET_TYPE_ID(LUAW_TYPE)); // ... cache id obj
luaW_Userdata* ud = new ((char*)lua_newuserdata(_luaState, sizeof(luaW_Userdata))) luaW_Userdata(_obj, ETK_GET_TYPE_ID(LUAW_TYPE)); // ... cache id obj
lua_pushvalue(_luaState, -1); // ... cache id obj obj
lua_insert(_luaState, -4); // ... obj cache id obj
lua_settable(_luaState, -3); // ... obj cache

View File

@ -1,5 +1,4 @@
#ifndef EXAMPLE_HPP_
#define EXAMPLE_HPP_
#pragma once
#include <etk/String.hpp>
#include <iostream>
@ -19,4 +18,3 @@ class BankAccount {
static float s_totalMoneyInBank;
};
#endif // EXAMPLE_HPP_

View File

@ -2,21 +2,43 @@
#include <lua/lua.h>
#include <lua/lualib.h>
#include <lua/lauxlib.h>
#include <test-debug/debug.hpp>
#include <etk/etk.hpp>
#include "LuaBankAccount.hpp"
using namespace std;
static void usage() {
TEST_PRINT("Help:");
TEST_PRINT(" ./xxx [OPTIONS] ---");
TEST_PRINT(" --file=XXX File to execute");
exit(0);
}
int main(int _argc, const char *_argv[]) {
if (_argc == 2) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaopen_BankAccount(L);
if (luaL_dofile(L, _argv[1])) {
std::cout << lua_tostring(L, -1) << std::endl;
}
lua_close(L);
}
return 0;
TEST_PRINT("START SAMPLE 1");
etk::init(_argc, _argv);
etk::String inputFileName;
for (int32_t iii=0; iii<_argc ; ++iii) {
etk::String data = _argv[iii];
if ( data == "-h"
|| data == "--help") {
usage();
} else if (data.startWith("--file=") == true) {
inputFileName = etk::String(&_argv[iii][7]);
}
}
if (inputFileName.empty() == true) {
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));
}
lua_close(L);
TEST_PRINT("END SAMPLE 1");
return 0;
}

View File

@ -1,5 +1,4 @@
#ifndef EXAMPLE_HPP_
#define EXAMPLE_HPP_
#pragma once
#include <etk/String.hpp>
@ -57,4 +56,3 @@ class Example {
int DoSomethingElse(float _f);
};
#endif

View File

@ -1,5 +1,4 @@
#ifndef LUA_CUSTOM_TYPES_H__
#define LUA_CUSTOM_TYPES_H__
#pragma once
#include <etk/String.hpp>
@ -57,4 +56,3 @@ template<> struct luaU_Impl<Vector2D> {
}
};
#endif

View File

@ -1,7 +1,5 @@
#ifndef LUAEXAMPLE_HPP_
#define LUAEXAMPLE_HPP_
#pragma once
struct lua_State;
int luaopen_Example(lua_State* _L);
#endif

View File

@ -1,5 +1,4 @@
#ifndef VECTOR2D_H_
#define VECTOR2D_H_
#pragma once
struct Vector2D {
Vector2D(float x_ = 0.f, float y_ = 0.f) :
@ -11,4 +10,3 @@ struct Vector2D {
float y;
};
#endif

View File

@ -3,18 +3,41 @@
#include <lua/lua.h>
#include <lua/lualib.h>
#include <lua/lauxlib.h>
#include <test-debug/debug.hpp>
#include <etk/etk.hpp>
#include "LuaExample.hpp"
int main(int argc, const char *argv[]) {
if (argc == 2) {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luaopen_Example(L);
if (luaL_dofile(L, argv[1])) {
std::cout << lua_tostring(L, -1) << std::endl;
}
lua_close(L);
}
return 0;
static void usage() {
TEST_PRINT("Help:");
TEST_PRINT(" ./xxx [OPTIONS] ---");
TEST_PRINT(" --file=XXX File to execute");
exit(0);
}
int main(int _argc, const char *_argv[]) {
TEST_PRINT("START SAMPLE 1");
etk::init(_argc, _argv);
etk::String inputFileName;
for (int32_t iii=0; iii<_argc ; ++iii) {
etk::String data = _argv[iii];
if ( data == "-h"
|| data == "--help") {
usage();
} else if (data.startWith("--file=") == true) {
inputFileName = etk::String(&_argv[iii][7]);
}
}
if (inputFileName.empty() == true) {
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));
}
lua_close(L);
return 0;
}