Cleaned up some StrongRef-related code.

This commit is contained in:
Alex Szpakowski
2016-03-01 18:57:17 -04:00
parent 8d7481c3b2
commit f51bbb8462
13 changed files with 52 additions and 53 deletions
+21 -12
View File
@@ -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;
}