mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Cleaned up some StrongRef-related code.
This commit is contained in:
+21
-12
@@ -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 <typename T>
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -43,6 +43,7 @@ public:
|
||||
Variant(love::Type udatatype, void *userdata);
|
||||
Variant(std::vector<std::pair<Variant, Variant>> *table);
|
||||
Variant(const Variant &v);
|
||||
Variant(Variant &&v);
|
||||
~Variant();
|
||||
|
||||
Variant &operator = (const Variant &v);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -74,16 +74,14 @@ int w_wait(lua_State *L)
|
||||
|
||||
int w_push(lua_State *L)
|
||||
{
|
||||
Message *m = Message::fromLua(L, 1);
|
||||
StrongRef<Message> 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<Message> m(new Message("quit", args), Acquire::NORETAIN);
|
||||
instance()->push(m);
|
||||
m->release();
|
||||
|
||||
luax_pushboolean(L, true);
|
||||
return 1;
|
||||
|
||||
@@ -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<Data> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -223,17 +223,16 @@ int w_newFileData(lua_State *L)
|
||||
{
|
||||
File *file = luax_checkfile(L, 1);
|
||||
|
||||
FileData *data = 0;
|
||||
StrongRef<FileData> 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
|
||||
|
||||
@@ -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<FileData> data = filesystem->read(filename.c_str());
|
||||
data->release();
|
||||
// read() returns a retained ref already.
|
||||
StrongRef<FileData> 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")
|
||||
|
||||
@@ -43,9 +43,7 @@ public:
|
||||
|
||||
Rasterizer *Font::newTrueTypeRasterizer(int size, TrueTypeRasterizer::Hinting hinting)
|
||||
{
|
||||
StrongRef<DefaultFontData> data(new DefaultFontData);
|
||||
data->release();
|
||||
|
||||
StrongRef<DefaultFontData> data(new DefaultFontData, Acquire::NORETAIN);
|
||||
return newTrueTypeRasterizer(data.get(), size, hinting);
|
||||
}
|
||||
|
||||
|
||||
@@ -203,11 +203,10 @@ void Graphics::checkSetDefaultFont()
|
||||
if (!fontmodule)
|
||||
throw love::Exception("Font module has not been loaded.");
|
||||
|
||||
StrongRef<font::Rasterizer> r(fontmodule->newTrueTypeRasterizer(12, font::TrueTypeRasterizer::HINTING_NORMAL));
|
||||
r->release();
|
||||
auto hinting = font::TrueTypeRasterizer::HINTING_NORMAL;
|
||||
StrongRef<font::Rasterizer> 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());
|
||||
|
||||
@@ -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> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,9 +116,8 @@ void LuaThread::onError()
|
||||
Variant(error.c_str(), error.length())
|
||||
};
|
||||
|
||||
event::Message *msg = new event::Message("threaderror", vargs);
|
||||
StrongRef<event::Message> msg(new event::Message("threaderror", vargs), Acquire::NORETAIN);
|
||||
eventmodule->push(msg);
|
||||
msg->release();
|
||||
}
|
||||
|
||||
} // thread
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user