Change: [Script] Automate the ScriptObject reference counting
This commit is contained in:
@@ -21,6 +21,8 @@
|
||||
#include "../script_suspend.hpp"
|
||||
#include "../squirrel.hpp"
|
||||
|
||||
#include <utility>
|
||||
|
||||
/**
|
||||
* The callback function for Mode-classes.
|
||||
*/
|
||||
@@ -367,4 +369,68 @@ bool ScriptObject::ScriptDoCommandHelper<Tcmd, Tret(*)(DoCommandFlag, Targs...)>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internally used class to automate the ScriptObject reference counting.
|
||||
* @api -all
|
||||
*/
|
||||
template <typename T>
|
||||
class ScriptObjectRef {
|
||||
private:
|
||||
T *data; ///< The reference counted object.
|
||||
public:
|
||||
/**
|
||||
* Create the reference counter for the given ScriptObject instance.
|
||||
* @param data The underlying object.
|
||||
*/
|
||||
ScriptObjectRef(T *data) : data(data)
|
||||
{
|
||||
this->data->AddRef();
|
||||
}
|
||||
|
||||
/* No copy constructor. */
|
||||
ScriptObjectRef(const ScriptObjectRef<T> &ref) = delete;
|
||||
|
||||
/* Move constructor. */
|
||||
ScriptObjectRef(ScriptObjectRef<T> &&ref) noexcept : data(std::exchange(ref.data, nullptr))
|
||||
{
|
||||
}
|
||||
|
||||
/* No copy assignment. */
|
||||
ScriptObjectRef& operator=(const ScriptObjectRef<T> &other) = delete;
|
||||
|
||||
/* Move assignment. */
|
||||
ScriptObjectRef& operator=(ScriptObjectRef<T> &&other) noexcept
|
||||
{
|
||||
std::swap(this->data, other.data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the reference counted object.
|
||||
*/
|
||||
~ScriptObjectRef()
|
||||
{
|
||||
if (this->data != nullptr) this->data->Release();
|
||||
}
|
||||
|
||||
/**
|
||||
* Dereferencing this reference returns a reference to the reference
|
||||
* counted object
|
||||
* @return Reference to the underlying object.
|
||||
*/
|
||||
T &operator*()
|
||||
{
|
||||
return *this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* The arrow operator on this reference returns the reference counted object.
|
||||
* @return Pointer to the underlying object.
|
||||
*/
|
||||
T *operator->()
|
||||
{
|
||||
return this->data;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* SCRIPT_OBJECT_HPP */
|
||||
|
Reference in New Issue
Block a user