1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-04-02 18:37:13 +02:00

Minor changes to move semantics in sqrat library.

This commit is contained in:
Sandu Liviu Catalin 2020-04-13 13:45:24 +03:00
parent 48c72e3cfb
commit 49aa85102c
5 changed files with 168 additions and 39 deletions

View File

@ -72,7 +72,7 @@ public:
/// \param obj An Object that should already represent a Squirrel array
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ArrayBase(Object&& obj) : Object(std::move(obj)) {
ArrayBase(Object&& obj) noexcept : Object(std::forward< Object >(obj)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -90,7 +90,7 @@ public:
/// \param sa ArrayBase to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ArrayBase(ArrayBase&& sa) : Object(std::move(sa)) {
ArrayBase(ArrayBase&& sa) noexcept : Object(std::forward< ArrayBase >(sa)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -124,8 +124,8 @@ public:
/// \return The ArrayBase itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ArrayBase& operator=(ArrayBase&& sa) {
Object::operator = (std::move(sa));
ArrayBase& operator=(ArrayBase&& sa) noexcept {
Object::operator = (std::forward< ArrayBase >(sa));
return *this;
}
@ -595,7 +595,7 @@ public:
/// \param obj An Object that should already represent a Squirrel array
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Array(Object&& obj) : ArrayBase(std::move(obj)) {
Array(Object&& obj) noexcept : ArrayBase(std::forward< Object >(obj)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -623,7 +623,7 @@ public:
/// \param sa Array to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Array(Array&& sa) : ArrayBase(std::move(sa)) {
Array(Array&& sa) noexcept : ArrayBase(std::forward< Array >(sa)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -647,8 +647,8 @@ public:
/// \return The Array itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Array& operator=(Array&& sa) {
ArrayBase::operator = (std::move(sa));
Array& operator=(Array&& sa) noexcept {
ArrayBase::operator = (std::forward< Array >(sa));
return *this;
}

View File

@ -60,7 +60,7 @@ struct Function {
sq_addref(DefaultVM::Get_(), &mObj);
}
// Move constructor
Function(Function&& sf) : mEnv(sf.mEnv), mObj(sf.mObj) {
Function(Function&& sf) noexcept : mEnv(sf.mEnv), mObj(sf.mObj) {
sq_resetobject(&sf.GetEnv());
sq_resetobject(&sf.GetFunc());
}
@ -121,7 +121,7 @@ struct Function {
return *this;
}
// Move Assignment operator
Function& operator=(Function&& sf) {
Function& operator=(Function&& sf) noexcept {
Release();
mEnv = sf.mEnv;
mObj = sf.mObj;

View File

@ -1,3 +1,6 @@
#ifndef HEADER_1593521F164F3DCC
#define HEADER_1593521F164F3DCC
//
// SqratObject: Referenced Squirrel Object Wrapper
//
@ -43,6 +46,8 @@
namespace Sqrat {
struct LightObj;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// The base class for classes that represent Squirrel objects
///
@ -89,11 +94,27 @@ public:
/// \param so Object to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object(Object&& so) : vm(so.vm), obj(so.obj), release(so.release) {
sq_resetobject(&so.GetObject());
Object(Object&& so) noexcept : vm(so.vm), obj(so.obj), release(so.release) {
so.ResetObj();
so.release = false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Light object copy constructor
///
/// \param so Object to copy
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object(const LightObj& so);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Light object copy constructor
///
/// \param so Object to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object(LightObj&& so) noexcept;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs an Object from a Squirrel object
///
@ -101,7 +122,7 @@ public:
/// \param v VM that the object will exist in
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object(HSQOBJECT o, HSQUIRRELVM v = DefaultVM::Get()) : vm(v), obj(o), release(true) {
Object(HSQOBJECT o, HSQUIRRELVM v = DefaultVM::Get()) : vm(v), obj(o), release(!sq_isnull(o)) {
sq_addref(vm, &obj);
}
@ -115,6 +136,7 @@ public:
Object(SQInteger i, HSQUIRRELVM v = DefaultVM::Get()) : vm(v), release(true) {
if (SQ_FAILED(sq_getstackobj(vm, i, &obj))) {
sq_resetobject(&obj);
release = false;
} else {
sq_addref(vm, &obj);
}
@ -213,18 +235,55 @@ public:
/// \return The Object itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object& operator=(Object&& so) {
Object& operator=(Object&& so) noexcept {
if(release) {
Release();
}
vm = so.vm;
obj = so.obj;
release = so.release;
sq_resetobject(&so.GetObject());
so.release = false;
so.Reset();
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param so LightObj to copy
///
/// \return The Object itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object& operator=(const LightObj& so);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Assignment operator
///
/// \param so LightObj to move
///
/// \return The Object itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Object& operator=(LightObj&& so) noexcept;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Discards obj member value without releasing the reference.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ResetObj() {
sq_resetobject(&GetObject());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Discards any member values without releasing any references.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Reset() {
vm = nullptr;
sq_resetobject(&GetObject());
release = false;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the Squirrel VM for this Object (reference)
///
@ -954,6 +1013,22 @@ struct LightObj {
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Discards obj member value without releasing the reference. Only present for compatibility with Object type.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ResetObj() {
sq_resetobject(&mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Discards any member values without releasing any references. Only present for compatibility with Object type.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Reset() {
sq_resetobject(&mObj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the type of the LightObj as defined by the Squirrel API
///
@ -1081,6 +1156,40 @@ struct LightObj {
}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Object::Object(const LightObj& so) : vm(SqVM()), obj(so.mObj), release(!so.IsNull()) {
sq_addref(vm, &obj);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Object::Object(LightObj&& so) noexcept : vm(SqVM()), obj(so.mObj), release(!so.IsNull()) {
so.Reset();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Object& Object::operator=(const LightObj& so) {
if(release) {
Release();
}
vm = SqVM();
obj = so.mObj;
release = !so.IsNull();
sq_addref(vm, &GetObject());
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline Object& Object::operator=(LightObj&& so) noexcept {
if(release) {
Release();
}
vm = SqVM();
obj = so.mObj;
release = !so.IsNull();
so.Reset();
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Used to get and push LightObj instances to and from the stack as references (LightObj is always a reference)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1207,3 +1316,5 @@ struct Var<const HSQOBJECT&> : Var<HSQOBJECT> {Var(HSQUIRRELVM vm, SQInteger idx
}
#endif
#endif // header guard

View File

@ -72,7 +72,25 @@ public:
/// \param obj An Object that should already represent a Squirrel table
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TableBase(Object&& obj) : Object(std::move(obj)) {
TableBase(Object&& obj) noexcept : Object(std::forward< Object >(obj)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Construct the TableBase from an Object that already exists
///
/// \param obj An Object that should already represent a Squirrel table
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TableBase(const LightObj& obj) : Object(obj) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Construct the TableBase from an Object that already exists
///
/// \param obj An Object that should already represent a Squirrel table
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TableBase(LightObj&& obj) : Object(std::forward< LightObj >(obj)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -100,7 +118,7 @@ public:
/// \param st TableBase to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TableBase(TableBase&& st) : Object(std::move(st)) {
TableBase(TableBase&& st) noexcept : Object(std::forward< TableBase >(st)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -124,8 +142,8 @@ public:
/// \return The TableBase itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TableBase& operator=(TableBase&& st) {
Object::operator = (std::move(st));
TableBase& operator=(TableBase&& st) noexcept {
Object::operator = (std::forward< TableBase >(st));
return *this;
}
@ -509,7 +527,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Table : public TableBase {
public:
using TableBase::TableBase;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Default constructor (null)
///
@ -548,7 +566,7 @@ public:
/// \param obj An Object that should already represent a Squirrel table
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Table(Object&& obj) : TableBase(std::move(obj)) {
Table(Object&& obj) noexcept : TableBase(std::forward< Table >(obj)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -590,7 +608,7 @@ public:
/// \param st Table to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Table(Table&& st) : TableBase(std::move(st)) {
Table(Table&& st) noexcept : TableBase(std::forward< Table >(st)) {
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -614,8 +632,8 @@ public:
/// \return The Table itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Table& operator=(Table&& st) {
TableBase::operator = (std::move(st));
Table& operator=(Table&& st) noexcept {
TableBase::operator = (std::forward< Table >(st));
return *this;
}
@ -627,7 +645,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class RootTable : public TableBase {
public:
using TableBase::TableBase;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a RootTable object to represent the given VM's root table
///
@ -648,7 +666,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class RegistryTable : public TableBase {
public:
using TableBase::TableBase;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs a RegistryTable object to represent the given VM's registry table
///

View File

@ -423,7 +423,7 @@ public:
/// \param msg A nice error message
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit Exception(string && msg) : message(msg) {}
explicit Exception(string && msg) noexcept : message(std::forward< string >(msg)) {}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Constructs an exception
@ -698,7 +698,7 @@ public:
/// \param other SharedPtr to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SharedPtr(SharedPtr<T>&& other)
SharedPtr(SharedPtr<T>&& other) noexcept
: m_Ptr(other.m_Ptr)
, m_Ref(other.m_Ref)
{
@ -715,7 +715,7 @@ public:
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class U>
SharedPtr(SharedPtr<U>&& other)
SharedPtr(SharedPtr<U>&& other) noexcept
: m_Ptr(static_cast<T*>(other.m_Ptr))
, m_Ref(other.m_Ref)
{
@ -800,7 +800,7 @@ public:
/// \return The SharedPtr itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SharedPtr<T>& operator=(SharedPtr<T>&& other)
SharedPtr<T>& operator=(SharedPtr<T>&& other) noexcept
{
if (m_Ptr != other.m_Ptr)
{
@ -827,7 +827,7 @@ public:
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class U>
SharedPtr<T>& operator=(SharedPtr<U>&& other)
SharedPtr<T>& operator=(SharedPtr<U>&& other) noexcept
{
if (m_Ptr != static_cast<T*>(other.m_Ptr))
{
@ -1193,7 +1193,7 @@ public:
/// \param other WeakPtr to move
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WeakPtr(WeakPtr<T>&& other)
WeakPtr(WeakPtr<T>&& other) noexcept
: m_Ptr(other.m_Ptr)
, m_Ref(other.m_Ref)
{
@ -1210,7 +1210,7 @@ public:
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class U>
WeakPtr(WeakPtr<U>&& other)
WeakPtr(WeakPtr<U>&& other) noexcept
: m_Ptr(static_cast<T*>(other.m_Ptr))
, m_Ref(other.m_Ref)
{
@ -1293,7 +1293,7 @@ public:
/// \return The WeakPtr itself
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WeakPtr<T>& operator=(WeakPtr<T>&& other)
WeakPtr<T>& operator=(WeakPtr<T>&& other) noexcept
{
if (m_Ptr != other.m_Ptr)
{
@ -1320,7 +1320,7 @@ public:
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <class U>
WeakPtr<T>& operator=(WeakPtr<U>&& other)
WeakPtr<T>& operator=(WeakPtr<U>&& other) noexcept
{
if (m_Ptr != static_cast<T*>(other.m_Ptr))
{
@ -1683,7 +1683,7 @@ struct StackStrF
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Move constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackStrF(StackStrF && o)
StackStrF(StackStrF && o) noexcept
: mPtr(o.mPtr)
, mLen(o.mLen)
, mRes(o.mRes)
@ -1718,7 +1718,7 @@ struct StackStrF
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Move assignment operator.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
StackStrF & operator = (StackStrF && o)
StackStrF & operator = (StackStrF && o) noexcept
{
if (this != &o)
{
@ -1966,7 +1966,7 @@ public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Move constructor.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
DeleteGuard(DeleteGuard && o)
DeleteGuard(DeleteGuard && o) noexcept
: m_Ptr(o.m_Ptr)
{
o.m_Ptr = nullptr;