mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Added a way for love's reference-counted objects to be released more safely in some situations (e.g. setters)
This commit is contained in:
@@ -65,6 +65,33 @@ 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
|
||||
|
||||
private:
|
||||
|
||||
// The reference count.
|
||||
|
||||
@@ -543,8 +543,7 @@ Color Graphics::getBackgroundColor() const
|
||||
|
||||
void Graphics::setFont(Font *font)
|
||||
{
|
||||
if (currentFont != 0)
|
||||
currentFont->release();
|
||||
Object::AutoRelease fontrelease(currentFont);
|
||||
|
||||
currentFont = font;
|
||||
|
||||
|
||||
@@ -193,8 +193,7 @@ void ParticleSystem::remove(particle *p)
|
||||
|
||||
void ParticleSystem::setImage(Image *image)
|
||||
{
|
||||
if (this->image != 0)
|
||||
this->image->release();
|
||||
Object::AutoRelease imagerelease(this->image);
|
||||
|
||||
this->image = image;
|
||||
this->image->retain();
|
||||
|
||||
@@ -176,8 +176,9 @@ void SpriteBatch::unlock()
|
||||
|
||||
void SpriteBatch::setImage(Image *newimage)
|
||||
{
|
||||
Object::AutoRelease imagerelease(image);
|
||||
|
||||
newimage->retain();
|
||||
image->release();
|
||||
image = newimage;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user