From 33d20620b51e2c85a4fcce7c3b8c408ec1a31e68 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 21 Feb 2015 20:52:29 -0400 Subject: [PATCH] The Proxy struct now stores an actual love::Object pointer (rather than void*), which makes code using Proxies a bit cleaner. --HG-- branch : minor --- src/common/Variant.cpp | 4 ++-- src/common/runtime.cpp | 11 ++++------- src/common/runtime.h | 12 +++++------ src/modules/event/sdl/Event.cpp | 34 ++++++++++++++++---------------- src/modules/thread/LuaThread.cpp | 2 +- 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/common/Variant.cpp b/src/common/Variant.cpp index 8a55274ff..d3b157313 100644 --- a/src/common/Variant.cpp +++ b/src/common/Variant.cpp @@ -102,8 +102,8 @@ Variant::Variant(love::Type udatatype, void *userdata) { Proxy *p = (Proxy *) userdata; flags = p->flags; - data.userdata = p->data; - ((love::Object *) data.userdata)->retain(); + data.userdata = p->object; + p->object->retain(); } else data.userdata = userdata; diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index aca046b76..206e39c39 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -42,10 +42,7 @@ namespace love static int w__gc(lua_State *L) { Proxy *p = (Proxy *) lua_touserdata(L, 1); - Object *object = (Object *) p->data; - - object->release(); - + p->object->release(); return 0; } @@ -67,7 +64,7 @@ static int w__eq(lua_State *L) { Proxy *p1 = (Proxy *)lua_touserdata(L, 1); Proxy *p2 = (Proxy *)lua_touserdata(L, 2); - luax_pushboolean(L, p1->data == p2->data); + luax_pushboolean(L, p1->object == p2->object); return 1; } @@ -209,7 +206,7 @@ int luax_register_module(lua_State *L, const WrappedModule &m) luax_insistregistry(L, REGISTRY_MODULES); Proxy *p = (Proxy *)lua_newuserdata(L, sizeof(Proxy)); - p->data = m.module; + p->object = m.module; p->flags = m.flags; luaL_newmetatable(L, m.module->getName()); @@ -383,7 +380,7 @@ void luax_rawnewtype(lua_State *L, const char *name, bits flags, love::Object *o object->retain(); - u->data = (void *) object; + u->object = object; u->flags = flags; luaL_newmetatable(L, name); diff --git a/src/common/runtime.h b/src/common/runtime.h index b4da0b677..693712c6e 100644 --- a/src/common/runtime.h +++ b/src/common/runtime.h @@ -65,8 +65,8 @@ struct Proxy // Holds type information (see types.h). bits flags; - // The light userdata (pointer to the love::Object). - void *data; + // Pointer to the actual object. + Object *object; }; /** @@ -409,7 +409,7 @@ T *luax_checktype(lua_State *L, int idx, const char *name, love::bits type) if ((u->flags & type) != type) luax_typerror(L, idx, name); - return (T *)u->data; + return (T *)u->object; } template @@ -428,7 +428,7 @@ T *luax_getmodule(lua_State *L, const char *k, love::bits type) lua_pop(L, 2); - return (T *)u->data; + return (T *)u->object; } template @@ -450,7 +450,7 @@ T *luax_optmodule(lua_State *L, const char *k, love::bits type) lua_pop(L, 2); - return (T *) u->data; + return (T *) u->object; } /** @@ -465,7 +465,7 @@ T *luax_optmodule(lua_State *L, const char *k, love::bits type) template T *luax_totype(lua_State *L, int idx, const char * /* name */, love::bits /* type */) { - return (T *)(((Proxy *)lua_touserdata(L, idx))->data); + return (T *)(((Proxy *)lua_touserdata(L, idx))->object); } Type luax_type(lua_State *L, int idx); diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index d71fc5cc2..595aa270c 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -352,11 +352,11 @@ Message *Event::convert(const SDL_Event &e) const else { Proxy proxy; - proxy.data = new love::filesystem::DroppedFile(e.drop.file); + proxy.object = new love::filesystem::DroppedFile(e.drop.file); proxy.flags = FILESYSTEM_DROPPED_FILE_T; vargs.push_back(new Variant(FILESYSTEM_DROPPED_FILE_ID, &proxy)); msg = new Message("filedropped", vargs); - ((Object *) proxy.data)->release(); + proxy.object->release(); } } SDL_free(e.drop.file); @@ -401,8 +401,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: proxy.flags = JOYSTICK_JOYSTICK_T; - proxy.data = joymodule->getJoystickFromID(e.jbutton.which); - if (!proxy.data) + proxy.object = joymodule->getJoystickFromID(e.jbutton.which); + if (!proxy.object) break; vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); @@ -414,8 +414,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const case SDL_JOYAXISMOTION: { proxy.flags = JOYSTICK_JOYSTICK_T; - proxy.data = joymodule->getJoystickFromID(e.jaxis.which); - if (!proxy.data) + proxy.object = joymodule->getJoystickFromID(e.jaxis.which); + if (!proxy.object) break; vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); @@ -430,8 +430,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; proxy.flags = JOYSTICK_JOYSTICK_T; - proxy.data = joymodule->getJoystickFromID(e.jhat.which); - if (!proxy.data) + proxy.object = joymodule->getJoystickFromID(e.jhat.which); + if (!proxy.object) break; vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); @@ -448,8 +448,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; proxy.flags = JOYSTICK_JOYSTICK_T; - proxy.data = joymodule->getJoystickFromID(e.cbutton.which); - if (!proxy.data) + proxy.object = joymodule->getJoystickFromID(e.cbutton.which); + if (!proxy.object) break; vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); @@ -464,8 +464,8 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; proxy.flags = JOYSTICK_JOYSTICK_T; - proxy.data = joymodule->getJoystickFromID(e.caxis.which); - if (!proxy.data) + proxy.object = joymodule->getJoystickFromID(e.caxis.which); + if (!proxy.object) break; vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); @@ -478,9 +478,9 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; case SDL_JOYDEVICEADDED: // jdevice.which is the joystick device index. - proxy.data = joymodule->addJoystick(e.jdevice.which); + proxy.object = joymodule->addJoystick(e.jdevice.which); proxy.flags = JOYSTICK_JOYSTICK_T; - if (proxy.data) + if (proxy.object) { vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); msg = new Message("joystickadded", vargs); @@ -488,11 +488,11 @@ Message *Event::convertJoystickEvent(const SDL_Event &e) const break; case SDL_JOYDEVICEREMOVED: // jdevice.which is the joystick instance ID now. - proxy.data = joymodule->getJoystickFromID(e.jdevice.which); + proxy.object = joymodule->getJoystickFromID(e.jdevice.which); proxy.flags = JOYSTICK_JOYSTICK_T; - if (proxy.data) + if (proxy.object) { - joymodule->removeJoystick((joystick::Joystick *) proxy.data); + joymodule->removeJoystick((joystick::Joystick *) proxy.object); vargs.push_back(new Variant(JOYSTICK_JOYSTICK_ID, (void *) &proxy)); msg = new Message("joystickremoved", vargs); } diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 21707b257..83870b510 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/src/modules/thread/LuaThread.cpp @@ -110,7 +110,7 @@ void LuaThread::onError() Proxy p; p.flags = THREAD_THREAD_T; - p.data = this; + p.object = this; std::vector> vargs = { new Variant(THREAD_THREAD_ID, &p),