summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2012-03-29 08:44:48 -0700
committerPixel <pixel@nobis-crew.org>2012-03-29 08:44:48 -0700
commit01e69854c5445ea1ae73cfc0f386d1a7c7b687f3 (patch)
tree5d8b7a6e500de6e8c9fa055001d7754b08207b61
parent11478487e7b3eb24dccb829aa201c3358cfc7e68 (diff)
Working / cleaning a bit the Lua class.
-rw-r--r--includes/BLua.h36
-rw-r--r--src/BLua.cc47
2 files changed, 52 insertions, 31 deletions
diff --git a/includes/BLua.h b/includes/BLua.h
index fe84db9..be78660 100644
--- a/includes/BLua.h
+++ b/includes/BLua.h
@@ -1,7 +1,5 @@
#pragma once
-#include <typeinfo>
-
extern "C" {
#include <lua.h>
#include <lauxlib.h>
@@ -14,16 +12,16 @@ namespace Balau {
class Lua;
-class LuaExport {
+class LuaObject {
public:
- virtual ~LuaExport() { }
+ virtual ~LuaObject() { }
};
-class LuaObject {
+class LuaObjectFactory {
public:
- LuaObject() : m_wantsDestruct(false), m_pushed(false) { }
- virtual void push(Lua & L) throw (GeneralException);
- void pushDestruct(Lua & L) throw (GeneralException);
+ LuaObjectFactory() : m_wantsDestruct(false), m_pushed(false) { }
+ virtual void push(Lua & L);
+ void pushDestruct(Lua & L);
template<class T>
static T * getMe(Lua & L, int idx = 1);
protected:
@@ -35,6 +33,8 @@ class LuaObject {
friend class Lua;
private:
bool m_wantsDestruct, m_pushed;
+ LuaObjectFactory & operator=(const LuaObjectFactory &) = delete;
+ LuaObjectFactory(const LuaObjectFactory &) = delete;
};
typedef int (*openlualib_t)(lua_State * L);
@@ -43,13 +43,16 @@ class Lua {
public:
Lua();
Lua(lua_State * __L) : L(__L) { }
- Lua(const Lua &) throw (GeneralException) { throw GeneralException("Error: can't duplicate a Lua object."); }
+ Lua(Lua && oL) : L(oL.L) { oL.L = NULL; }
+
+ Lua & operator=(Lua && oL);
typedef int (*lua_CallWrapper)(lua_State *, lua_CFunction);
int ref(int t = -2) { return luaL_ref(L, t); }
void unref(int ref, int t = -1) { luaL_unref(L, t, ref); }
+ void close();
void open_base();
void open_table();
void open_string();
@@ -132,17 +135,17 @@ class Lua {
template<class T>
T * recast(int n = 1) {
- LuaExport * b;
+ LuaObject * b;
T * r;
- b = (LuaExport *) LuaObject::getMeInternal(*this, n);
+ b = (LuaObject *) LuaObjectFactory::getMeInternal(*this, n);
if (!b)
- error("LuaExport base object required; got null.");
+ error("LuaObject base object required; got null.");
r = dynamic_cast<T *>(b);
if (!r)
- error(String("Object not compatible; expecting ") + typeid(r).name() + " but got *" + typeid(*b).name() + " instead.");
+ error(String("Object not compatible; expecting ") + ClassName(r).c_str() + " but got *" + ClassName(b).c_str() + " instead.");
return r;
}
@@ -156,6 +159,9 @@ class Lua {
lua_State * L;
friend class LuaStatics;
+
+ Lua & operator=(const Lua &) = delete;
+ Lua(const Lua &) = delete;
};
class LuaException : public GeneralException {
@@ -249,7 +255,7 @@ struct lua_functypes_t {
}
template <class T>
-T * LuaObject::getMe(Lua & L, int idx) { return L.recast<T>(idx); }
+T * LuaObjectFactory::getMe(Lua & L, int idx) { return L.recast<T>(idx); }
template <class T>
class LuaHelpers {
@@ -263,7 +269,7 @@ class LuaHelpers {
bool invalid = false, arg_valid;
if (method)
- obj = LuaObject::getMe<T>(L);
+ obj = LuaObjectFactory::getMe<T>(L);
if ((n < tab[caller].minargs) || (n > tab[caller].maxargs)) {
invalid = true;
diff --git a/src/BLua.cc b/src/BLua.cc
index 2f0a5c4..4c52c7c 100644
--- a/src/BLua.cc
+++ b/src/BLua.cc
@@ -232,7 +232,7 @@ int Balau::LuaStatics::collector(lua_State * __L) {
Lua L(__L);
ObjData * u = (ObjData *) L.touserdata();
if (u->isObj) {
- LuaExport * obj = (LuaExport *) u->ptr;
+ LuaObjectFactory * obj = (LuaObjectFactory *) u->ptr;
delete obj;
} else {
free(u->ptr);
@@ -247,7 +247,7 @@ int Balau::LuaStatics::destructor(lua_State * __L) {
L.gettable(-2, true);
ObjData * u = (ObjData *) L.touserdata();
if (u->isObj) {
- LuaExport * obj = (LuaExport *) u->ptr;
+ LuaObjectFactory * obj = (LuaObjectFactory *) u->ptr;
delete obj;
} else {
free(u->ptr);
@@ -273,6 +273,25 @@ Balau::Lua::Lua() : L(lua_open()) {
settable(LUA_REGISTRYINDEX);
}
+Balau::Lua & Balau::Lua::operator=(Lua && oL) {
+ if (this == &oL)
+ return *this;
+
+ AAssert(!L, "Can't assign a Lua VM to another one.");
+
+ L = oL.L;
+ oL.L = NULL;
+
+ return *this;
+}
+
+void Balau::Lua::close() {
+ AAssert(L, "Can't close an already closed VM");
+
+ lua_close(L);
+ L = NULL;
+}
+
#define IntPoint(p) ((unsigned int)(lu_mem)(p))
typedef size_t lu_mem;
@@ -676,15 +695,13 @@ Balau::Lua Balau::Lua::thread(bool saveit) {
}
Balau::Lua Balau::Lua::thread(const String & code, int nargs, bool saveit) {
- Lua L1;
- L1 = thread(saveit);
+ Lua L1 = thread(saveit);
L1.resume(code, nargs);
return L1;
}
Balau::Lua Balau::Lua::thread(IO<Handle> h, int nargs, bool saveit) {
- Lua L1;
- L1 = thread(saveit);
+ Lua L1 = thread(saveit);
L1.resume(h, nargs);
return L1;
}
@@ -783,15 +800,14 @@ void Balau::Lua::showerror() {
showstack(M_ERROR);
}
-void Balau::LuaObject::push(Lua & L) throw (GeneralException) {
- if (m_pushed && m_wantsDestruct)
- throw GeneralException("Error: object is owned by the LUA script and can not be pushed.");
+void Balau::LuaObjectFactory::push(Lua & L) {
+ AAssert(!(m_pushed && m_wantsDestruct), "Error: object is owned by the LUA script and can not be pushed.");
L.newtable();
pushMembers(L);
m_pushed = true;
}
-void Balau::LuaObject::pushMe(Lua & L, void * o, const char * objname, bool obj) {
+void Balau::LuaObjectFactory::pushMe(Lua & L, void * o, const char * objname, bool obj) {
ObjData * u;
L.push("__obj");
u = (ObjData *) L.newuser(sizeof(ObjData));
@@ -805,7 +821,7 @@ void Balau::LuaObject::pushMe(Lua & L, void * o, const char * objname, bool obj)
}
}
-void * Balau::LuaObject::getMeInternal(Lua & L, int i) {
+void * Balau::LuaObjectFactory::getMeInternal(Lua & L, int i) {
ObjData * u = NULL;
if (L.istable(i)) {
@@ -825,13 +841,13 @@ void * Balau::LuaObject::getMeInternal(Lua & L, int i) {
return u ? u->ptr : NULL;
}
-void Balau::LuaObject::pushIt(Lua & L, const char * s, lua_CFunction f) {
+void Balau::LuaObjectFactory::pushIt(Lua & L, const char * s, lua_CFunction f) {
L.push(s);
L.push(f);
L.settable(-3, true);
}
-void Balau::LuaObject::pushMeta(Lua & L, const char * s, lua_CFunction f) {
+void Balau::LuaObjectFactory::pushMeta(Lua & L, const char * s, lua_CFunction f) {
if (!L.getmetatable())
L.newtable();
L.push(s);
@@ -840,9 +856,8 @@ void Balau::LuaObject::pushMeta(Lua & L, const char * s, lua_CFunction f) {
L.setmetatable();
}
-void Balau::LuaObject::pushDestruct(Lua & L) throw (GeneralException) {
- if (m_pushed)
- throw GeneralException("Error: can't push destructor, object already pushed");
+void Balau::LuaObjectFactory::pushDestruct(Lua & L) {
+ AAssert(!m_pushed, "Error: can't push destructor, object already pushed");
push(L);
L.push("__obj");
L.gettable(-2, true);