diff options
-rw-r--r-- | include/BLua.h | 39 | ||||
-rw-r--r-- | lib/BLua.cc | 2 | ||||
-rw-r--r-- | lib/LuaConfigFile.cc | 2 | ||||
-rw-r--r-- | lib/LuaHandle.cc | 50 | ||||
-rw-r--r-- | lib/LuaHttp.cc | 8 | ||||
-rw-r--r-- | lib/LuaXML.cc | 2 |
6 files changed, 51 insertions, 52 deletions
diff --git a/include/BLua.h b/include/BLua.h index fc8adb2..fe0b33b 100644 --- a/include/BLua.h +++ b/include/BLua.h @@ -139,6 +139,24 @@ class Lua : public Base { void SetPrinter(LuaPrinter *); void puts(const char * msg); + template<class T> + T * recast(int n = 1) { + Base * b; + T * r; + + b = (Base *) LuaObject::getme(this, n); + if (!b) { + error("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."); + } + + return r; +} protected: virtual Lua * spawn_from_thread(lua_State *); Lua(lua_State *); @@ -336,26 +354,7 @@ class LuaHelpers : public Base { } }; -template<class T> -T * lua_recast(Lua * L, int n = 1) { - Base * b; - T * r; - - b = (Base *) LuaObject::getme(L, n); - if (!b) { - L->error("Base object required; got null."); - } - - r = dynamic_cast<T *>(b); - - if (!r) { - L->error(String("Object not compatible; expecting ") + typeid(r).name() + " but got *" + typeid(*b).name() + " instead."); - } - - return r; -} - - +template<class T> T * lua_recast(Lua * L, int n = 1) { return L->lua_recast(n); } /*******************************\ |** Let's have a sample of use **| diff --git a/lib/BLua.cc b/lib/BLua.cc index 270e952..2451998 100644 --- a/lib/BLua.cc +++ b/lib/BLua.cc @@ -276,7 +276,7 @@ int LuaStatics::dumpvars(lua_State * _L) { if (L->isstring(2)) L->getglobal(L->tostring(2)); - h = (Handle *) LuaObject::getme(L); + h = recast<Handle>(); L->dumpvars(h, prefix); diff --git a/lib/LuaConfigFile.cc b/lib/LuaConfigFile.cc index 5c7cde9..dc0dc5f 100644 --- a/lib/LuaConfigFile.cc +++ b/lib/LuaConfigFile.cc @@ -101,7 +101,7 @@ int sLua_ConfigFile::ConfigFile_proceed_statics(Lua * L, int n, int caller) { switch (caller) { case CONFIGFILE_NEWCONFIGFILE: - h = (Handle *) LuaObject::getme(L, 1); + h = L->recast<Handle>(1); { LuaConfigFile c(new ConfigFile(h)); c.pushdestruct(L); diff --git a/lib/LuaHandle.cc b/lib/LuaHandle.cc index 29af5d7..472f3d6 100644 --- a/lib/LuaHandle.cc +++ b/lib/LuaHandle.cc @@ -172,7 +172,7 @@ int sLuaHandle::read(lua_State * __L) { t = L->tonumber(); b = (Byte *) malloc(t); - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); L->newtable(); @@ -201,7 +201,7 @@ int sLuaHandle::readstring(lua_State * __L) { L->error("Incorrect arguments to method `Headle::readstring'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); (*h) >> r; @@ -248,7 +248,7 @@ int sLuaHandle::write(lua_State * __L) { t = L->tonumber(); b = (Byte *) malloc(t); - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); for (i = 0; i < t; i++) { L->push((lua_Number) i); @@ -276,7 +276,7 @@ int sLuaHandle::writestring(lua_State * __L) { L->error("Incorrect arguments to method `Headle::writestring'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); r = L->tostring(); (*h) << r; @@ -314,7 +314,7 @@ int sLuaHandle::read(lua_State * __L, int t) { L->error("Incorrect arguments to method `Headle::readUx'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); switch (t) { case U8: r = h->readU8(); break; @@ -339,7 +339,7 @@ int sLuaHandle::write(lua_State * __L, int t) { L->error("Incorrect arguments to method `Headle::writeUx'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); r = L->tonumber(); switch (t) { @@ -371,8 +371,8 @@ int sLuaHandle::copy(lua_State * __L, int dir) { L->error("Incorrect arguments to function `handlecopy'"); } - s = (Handle *) LuaObject::getme(L, 1); - d = (Handle *) LuaObject::getme(L, 2); + s = L->recast<Handle>(1); + d = L->recast<Handle>(2); if (n == 3) { r = L->tonumber(); @@ -423,7 +423,7 @@ int sLuaHandle::getcaps(lua_State * __L, int cap) { L->error("Incorrect arguments to method `Handle::getcaps'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); switch (cap) { case capisclosed: r = h->IsClosed(); break; @@ -477,7 +477,7 @@ int sLuaHandle::action(lua_State * __L, int act) { L->error("Incorrect arguments to method `Handle::action'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); switch (act) { case Asetnonblock: r = 0; h->SetNonBlock(); break; @@ -502,7 +502,7 @@ int sLuaHandle::setz(lua_State * __L) { L->error("Incorrect arguments to method `Handle::setz'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); if (n == 2) { z = L->tonumber(2); @@ -555,8 +555,8 @@ int sLuaHandle::zlib_inflate(lua_State * __L) { L->error("Incorrect arguments to method `Handle::zlib_inflate'"); } - s = (Handle *) LuaObject::getme(L, 1); - d = (Handle *) LuaObject::getme(L, 2); + s = L->recast<Handle>(1); + d = L->recast<Handle>(2); r = Handle::zlib_inflate(s, d); @@ -575,8 +575,8 @@ int sLuaHandle::zlib_deflate(lua_State * __L) { L->error("Incorrect arguments to method `Handle::zlib_deflate'"); } - s = (Handle *) LuaObject::getme(L, 1); - d = (Handle *) LuaObject::getme(L, 2); + s = L->recast<Handle>(1); + d = L->recast<Handle>(2); r = Handle::zlib_deflate(s, d); @@ -596,8 +596,8 @@ int sLuaHandle::ucl_compress(lua_State * __L) { L->error("Incorrect arguments to method `Handle::ucl_compress'"); } - s = (Handle *) LuaObject::getme(L, 1); - d = (Handle *) LuaObject::getme(L, 2); + s = L->recast<Handle>(1); + d = L->recast<Handle>(2); r = Handle::ucl_compress(s, d); @@ -616,8 +616,8 @@ int sLuaHandle::ucl_decompress(lua_State * __L) { L->error("Incorrect arguments to method `Handle::ucl_decompress'"); } - s = (Handle *) LuaObject::getme(L, 1); - d = (Handle *) LuaObject::getme(L, 2); + s = L->recast<Handle>(1); + d = L->recast<Handle>(2); r = Handle::ucl_decompress(s, d); @@ -638,7 +638,7 @@ int sLuaHandle::seek(lua_State * __L) { L->error("Incorrect arguments to method `Handle::seek'"); } - h = (Handle *) LuaObject::getme(L); + h = L->recast<Handle>(); off = L->tonumber(2); @@ -667,7 +667,7 @@ int sLuaHandle::bindex(lua_State * __L) { return 1; } - b = (Buffer *) LuaObject::getme(L); + b = L->recast<Buffer>(); L->push((lua_Number) (*b)[L->tonumber()]); @@ -692,7 +692,7 @@ int sLuaHandle::bnewindex(lua_State * __L) { L->error("Can't write a non-number to buffer"); } - b = (Buffer *) LuaObject::getme(L); + b = L->recast<Buffer>(); (*b)[L->tonumber(2)] = L->tonumber(3); @@ -708,7 +708,7 @@ int sLuaHandle::btell(lua_State * __L) { L->error("Incorrect arguments to method `Buffer::wtell'"); } - h = (Buffer *) LuaObject::getme(L); + h = L->recast<Buffer>(); L->push((lua_Number) h->wtell()); return 1; } @@ -724,7 +724,7 @@ int sLuaHandle::bseek(lua_State * __L) { L->error("Incorrect arguments to method `Buffer::wseek'"); } - h = (Buffer *) LuaObject::getme(L); + h = L->recast<Buffer>(); off = L->tonumber(2); @@ -748,7 +748,7 @@ int sLuaHandle::breset(lua_State * __L) { L->error("Incorrect arguments to method `Buffer::reset'"); } - h = (Buffer *) LuaObject::getme(L); + h = L->recast<Buffer>(); h->reset(); return 0; } diff --git a/lib/LuaHttp.cc b/lib/LuaHttp.cc index b9ad988..80f843b 100644 --- a/lib/LuaHttp.cc +++ b/lib/LuaHttp.cc @@ -305,7 +305,7 @@ int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller) dec_t = dec.to_charp(); L->push(Base64::encode(dec_t, dec.strlen())); } else { - Handle * hdata = (Handle *) LuaObject::getme(L, 1); + Handle * hdata = L->recast<Handle>(1) int size = hdata->GetSize(); char * data = (char *) malloc(size); hdata->read(data, size); @@ -338,7 +338,7 @@ int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller) h.Update(dec); L->push(h.Finish()); } else { - Handle * hdata = (Handle *) LuaObject::getme(L, 1); + Handle * hdata = L->recast<Handle>(1) MD5 h; h.Update(hdata); L->push(h.Finish()); @@ -352,7 +352,7 @@ int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller) h.Update(dec); L->push(h.Finish()); } else { - Handle * hdata = (Handle *) LuaObject::getme(L, 1); + Handle * hdata = L->recast<Handle>(1) SHA1 h; h.Update(hdata); L->push(h.Finish()); @@ -366,7 +366,7 @@ int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller) h.Update(dec); L->push(h.Finish()); } else { - Handle * hdata = (Handle *) LuaObject::getme(L, 1); + Handle * hdata = L->recast<Handle>(1) SHA256 h; h.Update(hdata); L->push(h.Finish()); diff --git a/lib/LuaXML.cc b/lib/LuaXML.cc index a8dfbc0..73eac42 100644 --- a/lib/LuaXML.cc +++ b/lib/LuaXML.cc @@ -101,7 +101,7 @@ static int ParseString(lua_State *L) { static int ParseHandle(lua_State *__L) { Lua * L = Lua::find(__L); - Handle * h = (Handle *) LuaObject::getme(L, 1); + Handle * h = L->recast<Handle>(1); TiXmlDocument doc; Buffer b(true); |