diff --git a/src/common/Object.h b/src/common/Object.h index 3340c609f..e916c9fd8 100644 --- a/src/common/Object.h +++ b/src/common/Object.h @@ -72,93 +72,6 @@ public: **/ virtual void release(); - /** - * Meant to be used as a temporary object. Facilitates safer and cleaner - * code to release objects by doing so when the AutoRelease object is - * destroyed (e.g. goes out of scope.) - **/ - class AutoRelease - { - public: - - AutoRelease(Object *obj) - : object(obj) - { - } - - ~AutoRelease() - { - if (object) - object->release(); - } - - private: - - AutoRelease() {} - Object *object; - - }; // AutoRelease - - /** - * Partial re-implementation + specialization of std::shared_ptr. We can't - * use C++11's stdlib yet... - **/ - template - class StrongRef - { - public: - - StrongRef() - : object(nullptr) - { - } - - StrongRef(T *obj) - : object(obj) - { - if (object) object->retain(); - } - - StrongRef(const StrongRef &other) - : object(other.get()) - { - if (object) object->retain(); - } - - ~StrongRef() - { - if (object) object->release(); - } - - StrongRef &operator = (const StrongRef &other) - { - set(other.get()); - return *this; - } - - T *operator->() const - { - return object; - } - - void set(T *obj) - { - if (obj) obj->retain(); - if (object) object->release(); - object = obj; - } - - T *get() const - { - return object; - } - - private: - - T *object; - - }; // StrongRef - private: // The reference count. @@ -166,6 +79,66 @@ private: }; // Object +/** + * Partial re-implementation + specialization of std::shared_ptr. We can't + * use C++11's stdlib yet... + **/ +template +class StrongRef +{ +public: + + StrongRef() + : object(nullptr) + { + } + + StrongRef(T *obj) + : object(obj) + { + if (object) object->retain(); + } + + StrongRef(const StrongRef &other) + : object(other.get()) + { + if (object) object->retain(); + } + + ~StrongRef() + { + if (object) object->release(); + } + + StrongRef &operator = (const StrongRef &other) + { + set(other.get()); + return *this; + } + + T *operator->() const + { + return object; + } + + void set(T *obj) + { + if (obj) obj->retain(); + if (object) object->release(); + object = obj; + } + + T *get() const + { + return object; + } + +private: + + T *object; + +}; // StrongRef + } // love #endif // LOVE_OBJECT_H diff --git a/src/modules/audio/openal/Source.h b/src/modules/audio/openal/Source.h index 24824cf29..b6dfd4ff4 100644 --- a/src/modules/audio/openal/Source.h +++ b/src/modules/audio/openal/Source.h @@ -148,7 +148,7 @@ private: static const unsigned int MAX_BUFFERS = 8; ALuint streamBuffers[MAX_BUFFERS]; - Object::StrongRef staticBuffer; + StrongRef staticBuffer; float pitch; float volume; @@ -183,7 +183,7 @@ private: int sampleRate; int channels; - Object::StrongRef decoder; + StrongRef decoder; unsigned int toLoop; diff --git a/src/modules/font/ImageRasterizer.h b/src/modules/font/ImageRasterizer.h index 20d006157..a3c2ef0b8 100644 --- a/src/modules/font/ImageRasterizer.h +++ b/src/modules/font/ImageRasterizer.h @@ -53,7 +53,7 @@ private: void load(); // The image data - Object::StrongRef imageData; + StrongRef imageData; // The glyphs in the font uint32 *glyphs; diff --git a/src/modules/font/freetype/TrueTypeRasterizer.h b/src/modules/font/freetype/TrueTypeRasterizer.h index 8f3caf527..be545702b 100644 --- a/src/modules/font/freetype/TrueTypeRasterizer.h +++ b/src/modules/font/freetype/TrueTypeRasterizer.h @@ -58,7 +58,7 @@ private: FT_Face face; // File data - Object::StrongRef data; + StrongRef data; }; // FreetypeRasterizer } // freetype diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index 0529072a2..a7ac30b6f 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/src/modules/graphics/opengl/Font.h @@ -185,7 +185,7 @@ private: Glyph *addGlyph(uint32 glyph); Glyph *findGlyph(uint32 glyph); - Object::StrongRef rasterizer; + StrongRef rasterizer; int height; float lineHeight; diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index 4c82aa63d..e259f33aa 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -173,7 +173,7 @@ void Graphics::setViewportSize(int width, int height) // We want to affect the main screen, not any Canvas that's currently active // (not that any *should* be active when this is called.) - std::vector> canvases = states.back().canvases; + std::vector> canvases = states.back().canvases; setCanvas(); // Set the viewport to top-left corner. @@ -662,7 +662,7 @@ void Graphics::setCanvas(Canvas *canvas) canvas->startGrab(); - std::vector> canvasref; + std::vector> canvasref; canvasref.push_back(canvas); std::swap(state.canvases, canvasref); @@ -680,7 +680,7 @@ void Graphics::setCanvas(const std::vector &canvases) auto attachments = std::vector(canvases.begin() + 1, canvases.end()); canvases[0]->startGrab(attachments); - std::vector> canvasrefs; + std::vector> canvasrefs; canvasrefs.reserve(canvases.size()); for (Canvas *c : canvases) @@ -689,12 +689,12 @@ void Graphics::setCanvas(const std::vector &canvases) std::swap(state.canvases, canvasrefs); } -void Graphics::setCanvas(const std::vector> &canvases) +void Graphics::setCanvas(const std::vector> &canvases) { std::vector canvaslist; canvaslist.reserve(canvases.size()); - for (const Object::StrongRef &c : canvases) + for (const StrongRef &c : canvases) canvaslist.push_back(c.get()); return setCanvas(canvaslist); @@ -715,7 +715,7 @@ std::vector Graphics::getCanvas() const std::vector canvases; canvases.reserve(states.back().canvases.size()); - for (const Object::StrongRef &c : states.back().canvases) + for (const StrongRef &c : states.back().canvases) canvases.push_back(c.get()); return canvases; @@ -1091,7 +1091,7 @@ love::image::ImageData *Graphics::newScreenshot(love::image::Image *image, bool { // Temporarily unbind the currently active canvas (glReadPixels reads the // active framebuffer, not the main one.) - std::vector> canvases = states.back().canvases; + std::vector> canvases = states.back().canvases; setCanvas(); int w = getWidth(); diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index ee238bf2d..35ac1ed88 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -207,7 +207,7 @@ public: void setCanvas(Canvas *canvas); void setCanvas(const std::vector &canvases); - void setCanvas(const std::vector> &canvases); + void setCanvas(const std::vector> &canvases); void setCanvas(); std::vector getCanvas() const; @@ -458,10 +458,10 @@ private: bool scissor; OpenGL::Viewport scissorBox; - Object::StrongRef font; - Object::StrongRef shader; + StrongRef font; + StrongRef shader; - std::vector> canvases; + std::vector> canvases; ColorMask colorMask; diff --git a/src/modules/graphics/opengl/Image.h b/src/modules/graphics/opengl/Image.h index 55d2ef5f5..0e96032ed 100644 --- a/src/modules/graphics/opengl/Image.h +++ b/src/modules/graphics/opengl/Image.h @@ -158,11 +158,11 @@ private: // The ImageData from which the texture is created. May be null if // Compressed image data was used to create the texture. - Object::StrongRef data; + StrongRef data; // Or the Compressed Image Data from which the texture is created. May be // null if raw ImageData was used to create the texture. - Object::StrongRef cdata; + StrongRef cdata; // Real dimensions of the texture, if it was auto-padded to POT size. int paddedWidth, paddedHeight; diff --git a/src/modules/graphics/opengl/Mesh.h b/src/modules/graphics/opengl/Mesh.h index fab7daca4..7791f6c11 100644 --- a/src/modules/graphics/opengl/Mesh.h +++ b/src/modules/graphics/opengl/Mesh.h @@ -178,7 +178,7 @@ private: int range_min; int range_max; - Object::StrongRef texture; + StrongRef texture; // Whether the per-vertex colors are used when drawing. bool colors_enabled; diff --git a/src/modules/graphics/opengl/ParticleSystem.cpp b/src/modules/graphics/opengl/ParticleSystem.cpp index cfc328230..546b7a8c8 100644 --- a/src/modules/graphics/opengl/ParticleSystem.cpp +++ b/src/modules/graphics/opengl/ParticleSystem.cpp @@ -739,7 +739,7 @@ std::vector ParticleSystem::getQuads() const std::vector quadlist; quadlist.reserve(quads.size()); - for (const Object::StrongRef &q : quads) + for (const StrongRef &q : quads) quadlist.push_back(q.get()); return quadlist; diff --git a/src/modules/graphics/opengl/ParticleSystem.h b/src/modules/graphics/opengl/ParticleSystem.h index 67a945a7e..d92c8c54a 100644 --- a/src/modules/graphics/opengl/ParticleSystem.h +++ b/src/modules/graphics/opengl/ParticleSystem.h @@ -576,7 +576,7 @@ protected: Vertex *particleVerts; // The texture to be drawn. - Object::StrongRef texture; + StrongRef texture; // Whether the particle emitter is active. bool active; @@ -656,7 +656,7 @@ protected: std::vector colors; // Quads. - std::vector> quads; + std::vector> quads; bool relativeRotation; diff --git a/src/modules/graphics/opengl/SpriteBatch.h b/src/modules/graphics/opengl/SpriteBatch.h index 89e7f75c3..9f9188f47 100644 --- a/src/modules/graphics/opengl/SpriteBatch.h +++ b/src/modules/graphics/opengl/SpriteBatch.h @@ -124,7 +124,7 @@ private: */ void setColorv(Vertex *v, const Color &color); - Object::StrongRef texture; + StrongRef texture; // Max number of sprites in the batch. int size; diff --git a/src/modules/mouse/sdl/Mouse.h b/src/modules/mouse/sdl/Mouse.h index 3ec2cfab0..9623cdbea 100644 --- a/src/modules/mouse/sdl/Mouse.h +++ b/src/modules/mouse/sdl/Mouse.h @@ -69,7 +69,7 @@ public: private: - Object::StrongRef curCursor; + StrongRef curCursor; std::map systemCursors; diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index a27c1e9f7..e399cbc92 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -441,7 +441,7 @@ private: // // This ensures that a World only can be destroyed // once all bodies have been destroyed too. - Object::StrongRef world; + StrongRef world; bodyudata *udata; diff --git a/src/modules/sound/lullaby/Decoder.h b/src/modules/sound/lullaby/Decoder.h index 70a34303f..576fd52e1 100644 --- a/src/modules/sound/lullaby/Decoder.h +++ b/src/modules/sound/lullaby/Decoder.h @@ -53,7 +53,7 @@ protected: // The encoded data. This should be replaced with buffered file // reads in the future. - Object::StrongRef data; + StrongRef data; // File extension. std::string ext; diff --git a/src/modules/thread/LuaThread.h b/src/modules/thread/LuaThread.h index 375f9c5c3..ef78a6aba 100644 --- a/src/modules/thread/LuaThread.h +++ b/src/modules/thread/LuaThread.h @@ -50,7 +50,7 @@ private: void onError(); - Object::StrongRef code; + StrongRef code; std::string name; std::string error; diff --git a/src/modules/window/sdl/Window.h b/src/modules/window/sdl/Window.h index a36be3d87..51a1ad016 100644 --- a/src/modules/window/sdl/Window.h +++ b/src/modules/window/sdl/Window.h @@ -123,7 +123,7 @@ private: int width; int height; WindowSettings settings; - Object::StrongRef icon; + StrongRef icon; } curMode;