From f51bbb8462530f65fcbdfc5e4e7036d5e28160b4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 1 Mar 2016 18:57:17 -0400 Subject: [PATCH] Cleaned up some StrongRef-related code. --- src/common/Object.h | 33 ++++++++++++++-------- src/common/Variant.cpp | 8 ++++++ src/common/Variant.h | 1 + src/modules/audio/openal/Source.cpp | 11 ++------ src/modules/event/wrap_Event.cpp | 11 +++----- src/modules/filesystem/wrap_File.cpp | 5 ++-- src/modules/filesystem/wrap_Filesystem.cpp | 5 ++-- src/modules/font/BMFontRasterizer.cpp | 9 +++--- src/modules/font/Font.cpp | 4 +-- src/modules/graphics/opengl/Graphics.cpp | 7 ++--- src/modules/physics/box2d/wrap_Fixture.cpp | 5 ++-- src/modules/thread/LuaThread.cpp | 3 +- src/modules/video/theora/VideoStream.cpp | 3 +- 13 files changed, 52 insertions(+), 53 deletions(-) diff --git a/src/common/Object.h b/src/common/Object.h index 5cc04c349..705623291 100644 --- a/src/common/Object.h +++ b/src/common/Object.h @@ -75,32 +75,41 @@ private: }; // Object -/** - * Partial re-implementation + specialization of std::shared_ptr. We can't - * use C++11's stdlib yet... - **/ + +enum class Acquire +{ + RETAIN, + NORETAIN, +}; + template class StrongRef { public: StrongRef() - : object(nullptr) + : object(nullptr) { } - StrongRef(T *obj) - : object(obj) + StrongRef(T *obj, Acquire acquire = Acquire::RETAIN) + : object(obj) { - if (object) object->retain(); + if (object && acquire == Acquire::RETAIN) object->retain(); } StrongRef(const StrongRef &other) - : object(other.get()) + : object(other.get()) { if (object) object->retain(); } + StrongRef(StrongRef &&other) + : object(other.object) + { + other.object = nullptr; + } + ~StrongRef() { if (object) object->release(); @@ -117,7 +126,7 @@ public: return object; } - operator bool() const + explicit operator bool() const { return object != nullptr; } @@ -127,9 +136,9 @@ public: return object; } - void set(T *obj) + void set(T *obj, Acquire acquire = Acquire::RETAIN) { - if (obj) obj->retain(); + if (obj && acquire == Acquire::RETAIN) obj->retain(); if (object) object->release(); object = obj; } diff --git a/src/common/Variant.cpp b/src/common/Variant.cpp index b26a368ef..7b33ee80b 100644 --- a/src/common/Variant.cpp +++ b/src/common/Variant.cpp @@ -111,6 +111,14 @@ Variant::Variant(const Variant &v) data.table->retain(); } +Variant::Variant(Variant &&v) + : type(std::move(v.type)) + , udatatype(std::move(v.udatatype)) + , data(std::move(v.data)) +{ + v.type = NIL; +} + Variant::~Variant() { switch (type) diff --git a/src/common/Variant.h b/src/common/Variant.h index 9a860a73c..3c4b9c5a3 100644 --- a/src/common/Variant.h +++ b/src/common/Variant.h @@ -43,6 +43,7 @@ public: Variant(love::Type udatatype, void *userdata); Variant(std::vector> *table); Variant(const Variant &v); + Variant(Variant &&v); ~Variant(); Variant &operator = (const Variant &v); diff --git a/src/modules/audio/openal/Source.cpp b/src/modules/audio/openal/Source.cpp index b9fb49a36..a58c1fe79 100644 --- a/src/modules/audio/openal/Source.cpp +++ b/src/modules/audio/openal/Source.cpp @@ -104,10 +104,7 @@ Source::Source(Pool *pool, love::sound::SoundData *soundData) if (fmt == 0) throw InvalidFormatException(soundData->getChannels(), soundData->getBitDepth()); - staticBuffer.set(new StaticDataBuffer(fmt, soundData->getData(), (ALsizei) soundData->getSize(), soundData->getSampleRate())); - - // The buffer has a +2 retain count right now, but we want it to have +1. - staticBuffer->release(); + staticBuffer.set(new StaticDataBuffer(fmt, soundData->getData(), (ALsizei) soundData->getSize(), sampleRate), Acquire::NORETAIN); float z[3] = {0, 0, 0}; @@ -179,11 +176,7 @@ Source::Source(const Source &s) if (type == TYPE_STREAM) { if (s.decoder.get()) - { - love::sound::Decoder *dec = s.decoder->clone(); - decoder.set(dec); - dec->release(); - } + decoder.set(s.decoder->clone(), Acquire::NORETAIN); alGenBuffers(MAX_BUFFERS, streamBuffers); } diff --git a/src/modules/event/wrap_Event.cpp b/src/modules/event/wrap_Event.cpp index efe8191df..de486bf61 100644 --- a/src/modules/event/wrap_Event.cpp +++ b/src/modules/event/wrap_Event.cpp @@ -74,16 +74,14 @@ int w_wait(lua_State *L) int w_push(lua_State *L) { - Message *m = Message::fromLua(L, 1); + StrongRef m(Message::fromLua(L, 1), Acquire::NORETAIN); - luax_pushboolean(L, m != nullptr); + luax_pushboolean(L, m.get() != nullptr); - if (m == nullptr) + if (m.get() == nullptr) return 1; instance()->push(m); - m->release(); - return 1; } @@ -101,9 +99,8 @@ int w_quit(lua_State *L) if (Variant::fromLua(L, 1, &v)) args.push_back(v); - Message *m = new Message("quit", args); + StrongRef m(new Message("quit", args), Acquire::NORETAIN); instance()->push(m); - m->release(); luax_pushboolean(L, true); return 1; diff --git a/src/modules/filesystem/wrap_File.cpp b/src/modules/filesystem/wrap_File.cpp index b0209dff4..90e96c01e 100644 --- a/src/modules/filesystem/wrap_File.cpp +++ b/src/modules/filesystem/wrap_File.cpp @@ -108,13 +108,13 @@ int w_File_isOpen(lua_State *L) int w_File_read(lua_State *L) { File *file = luax_checkfile(L, 1); - Data *d = 0; + StrongRef d = nullptr; int64 size = (int64) luaL_optnumber(L, 2, (lua_Number) File::ALL); try { - d = file->read(size); + d.set(file->read(size), Acquire::NORETAIN); } catch (love::Exception &e) { @@ -123,7 +123,6 @@ int w_File_read(lua_State *L) lua_pushlstring(L, (const char *) d->getData(), d->getSize()); lua_pushnumber(L, d->getSize()); - d->release(); return 2; } diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index e01b3840e..73bf874a1 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -223,17 +223,16 @@ int w_newFileData(lua_State *L) { File *file = luax_checkfile(L, 1); - FileData *data = 0; + StrongRef data; try { - data = file->read(); + data.set(file->read(), Acquire::NORETAIN); } catch (love::Exception &e) { return luax_ioError(L, "%s", e.what()); } luax_pushtype(L, FILESYSTEM_FILE_DATA_ID, data); - data->release(); return 1; } else diff --git a/src/modules/font/BMFontRasterizer.cpp b/src/modules/font/BMFontRasterizer.cpp index bda538d34..dd02634d7 100644 --- a/src/modules/font/BMFontRasterizer.cpp +++ b/src/modules/font/BMFontRasterizer.cpp @@ -198,12 +198,11 @@ void BMFontRasterizer::parseConfig(const std::string &configtext) if (!imagemodule) throw love::Exception("Image module not loaded!"); - // Release these variables right away since StrongRef retains. - StrongRef data = filesystem->read(filename.c_str()); - data->release(); + // read() returns a retained ref already. + StrongRef data(filesystem->read(filename.c_str()), Acquire::NORETAIN); - images[pageindex].set(imagemodule->newImageData(data.get())); - images[pageindex]->release(); + // Same with newImageData. + images[pageindex].set(imagemodule->newImageData(data.get()), Acquire::NORETAIN); } } else if (tag == "char") diff --git a/src/modules/font/Font.cpp b/src/modules/font/Font.cpp index c3a67dbcd..1aeb0eb29 100644 --- a/src/modules/font/Font.cpp +++ b/src/modules/font/Font.cpp @@ -43,9 +43,7 @@ public: Rasterizer *Font::newTrueTypeRasterizer(int size, TrueTypeRasterizer::Hinting hinting) { - StrongRef data(new DefaultFontData); - data->release(); - + StrongRef data(new DefaultFontData, Acquire::NORETAIN); return newTrueTypeRasterizer(data.get(), size, hinting); } diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 3ca3db0ec..32e0e0041 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -203,11 +203,10 @@ void Graphics::checkSetDefaultFont() if (!fontmodule) throw love::Exception("Font module has not been loaded."); - StrongRef r(fontmodule->newTrueTypeRasterizer(12, font::TrueTypeRasterizer::HINTING_NORMAL)); - r->release(); + auto hinting = font::TrueTypeRasterizer::HINTING_NORMAL; + StrongRef r(fontmodule->newTrueTypeRasterizer(12, hinting), Acquire::NORETAIN); - defaultFont.set(newFont(r.get())); - defaultFont->release(); + defaultFont.set(newFont(r.get()), Acquire::NORETAIN); } states.back().font.set(defaultFont.get()); diff --git a/src/modules/physics/box2d/wrap_Fixture.cpp b/src/modules/physics/box2d/wrap_Fixture.cpp index da045c547..6074c256d 100644 --- a/src/modules/physics/box2d/wrap_Fixture.cpp +++ b/src/modules/physics/box2d/wrap_Fixture.cpp @@ -118,8 +118,8 @@ int w_Fixture_getBody(lua_State *L) int w_Fixture_getShape(lua_State *L) { Fixture *t = luax_checkfixture(L, 1); - Shape *shape = t->getShape(); - if (shape == 0) + StrongRef shape(t->getShape(), Acquire::NORETAIN); + if (shape.get() == nullptr) return 0; switch (shape->getType()) { @@ -139,7 +139,6 @@ int w_Fixture_getShape(lua_State *L) luax_pushtype(L, PHYSICS_SHAPE_ID, shape); break; } - shape->release(); return 1; } diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 0c5b096ae..4c463ecfe 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/src/modules/thread/LuaThread.cpp @@ -116,9 +116,8 @@ void LuaThread::onError() Variant(error.c_str(), error.length()) }; - event::Message *msg = new event::Message("threaderror", vargs); + StrongRef msg(new event::Message("threaderror", vargs), Acquire::NORETAIN); eventmodule->push(msg); - msg->release(); } } // thread diff --git a/src/modules/video/theora/VideoStream.cpp b/src/modules/video/theora/VideoStream.cpp index 0b6e91e3f..12dbca8bc 100644 --- a/src/modules/video/theora/VideoStream.cpp +++ b/src/modules/video/theora/VideoStream.cpp @@ -64,8 +64,7 @@ VideoStream::VideoStream(love::filesystem::File *file) throw ex; } - frameSync = new DeltaSync(); - frameSync->release(); + frameSync.set(new DeltaSync(), Acquire::NORETAIN); } VideoStream::~VideoStream()