From e62f3074cb024e3601cef3072e290635de6e9542 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 5 Jul 2018 21:12:57 +0200 Subject: [PATCH] [DEV] remove __release() and __addRef() --- rabbit/Object.cpp | 26 ++++++++++++++++++ rabbit/Object.hpp | 16 +++-------- rabbit/ObjectPtr.cpp | 65 ++++++++++++++++---------------------------- rabbit/ObjectPtr.hpp | 1 + rabbit/sqapi.cpp | 4 +-- 5 files changed, 57 insertions(+), 55 deletions(-) diff --git a/rabbit/Object.cpp b/rabbit/Object.cpp index 15a1f07..ce36a9f 100644 --- a/rabbit/Object.cpp +++ b/rabbit/Object.cpp @@ -31,3 +31,29 @@ rabbit::Object rabbit::Object::getRealObject() const { rabbit::UserPointer rabbit::Object::getUserDataValue() const { return (rabbit::UserPointer)sq_aligning(_unVal.pUserData + 1); } + +void rabbit::Object::addRef() { + if ((_type&SQOBJECT_REF_COUNTED) != 0) { + _unVal.pRefCounted->refCountIncrement(); + } +} + +void rabbit::Object::releaseRef() { + if ( (_type&SQOBJECT_REF_COUNTED) != 0 + && (_unVal.pRefCounted->refCountDecrement()==0)) { + _unVal.pRefCounted->release(); + } + _type = rabbit::OT_NULL; + _unVal.raw = 0; +} + +void rabbit::Object::swap(rabbit::Object& _obj) { + rabbit::ObjectType tOldType = _type; + rabbit::ObjectValue unOldVal = _unVal; + _type = _obj._type; + _unVal = _obj._unVal; + _obj._type = tOldType; + _obj._unVal = unOldVal; +} + + diff --git a/rabbit/Object.hpp b/rabbit/Object.hpp index 59f486e..185e4b2 100644 --- a/rabbit/Object.hpp +++ b/rabbit/Object.hpp @@ -223,20 +223,12 @@ namespace rabbit { } rabbit::Object getRealObject() const; rabbit::UserPointer getUserDataValue() const; + + void addRef(); + void releaseRef(); + void swap(rabbit::Object& _obj); }; - #define ISREFCOUNTED(t) (t&SQOBJECT_REF_COUNTED) - - #define __addRef(type,unval) if(ISREFCOUNTED(type)) \ - { \ - unval.pRefCounted->refCountIncrement(); \ - } - - #define __release(type,unval) if(ISREFCOUNTED(type) && (unval.pRefCounted->refCountDecrement()==0)) \ - { \ - unval.pRefCounted->release(); \ - } - inline void _Swap(rabbit::Object &a,rabbit::Object &b) { rabbit::ObjectType tOldType = a._type; diff --git a/rabbit/ObjectPtr.cpp b/rabbit/ObjectPtr.cpp index 99aebae..2e6181c 100644 --- a/rabbit/ObjectPtr.cpp +++ b/rabbit/ObjectPtr.cpp @@ -9,26 +9,19 @@ #include #include -#define RABBIT_OBJ_REF_TYPE_INSTANCIATE(type,_class,sym) \ +#define RABBIT_OBJ_REF_TYPE_INSTANCIATE(type, _class, sym) \ rabbit::ObjectPtr::ObjectPtr(_class * x) \ { \ _unVal.raw = 0; \ _type=type; \ _unVal.sym = x; \ assert(_unVal.pTable); \ - _unVal.pRefCounted->refCountIncrement(); \ + addRef(); \ } \ rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(_class *x) \ { \ - rabbit::ObjectType tOldType; \ - rabbit::ObjectValue unOldVal; \ - tOldType=_type; \ - unOldVal=_unVal; \ - _type = type; \ - _unVal.raw = 0; \ - _unVal.sym = x; \ - _unVal.pRefCounted->refCountIncrement(); \ - __release(tOldType,unOldVal); \ + rabbit::ObjectPtr tmp{x}; \ + swap(tmp); \ return *this; \ } @@ -41,10 +34,8 @@ } \ rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(_class x) \ { \ - __release(_type,_unVal); \ - _type = type; \ - _unVal.raw = 0; \ - _unVal.sym = x; \ + rabbit::ObjectPtr tmp{x}; \ + swap(tmp); \ return *this; \ } @@ -78,13 +69,13 @@ rabbit::ObjectPtr::ObjectPtr() { rabbit::ObjectPtr::ObjectPtr(const rabbit::ObjectPtr& _obj) { _type = _obj._type; _unVal = _obj._unVal; - __addRef(_type,_unVal); + addRef(); } rabbit::ObjectPtr::ObjectPtr(const rabbit::Object& _obj) { _type = _obj._type; _unVal = _obj._unVal; - __addRef(_type,_unVal); + addRef(); } rabbit::ObjectPtr::ObjectPtr(bool _value) { @@ -94,51 +85,43 @@ rabbit::ObjectPtr::ObjectPtr(bool _value) { } rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(bool _value) { - __release(_type,_unVal); + releaseRef(); _unVal.raw = 0; _type = rabbit::OT_BOOL; _unVal.nInteger = _value?1:0; return *this; } +void rabbit::ObjectPtr::swap(rabbit::ObjectPtr& _obj) { + rabbit::ObjectType tOldType = _type; + rabbit::ObjectValue unOldVal = _unVal; + _type = _obj._type; + _unVal = _obj._unVal; + _obj._type = tOldType; + _obj._unVal = unOldVal; +} + rabbit::ObjectPtr::~ObjectPtr() { - __release(_type,_unVal); + releaseRef(); } rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(const rabbit::ObjectPtr& _obj) { - rabbit::ObjectType tOldType; - rabbit::ObjectValue unOldVal; - tOldType=_type; - unOldVal=_unVal; - _unVal = _obj._unVal; - _type = _obj._type; - __addRef(_type,_unVal); - __release(tOldType,unOldVal); + rabbit::ObjectPtr tmp{_obj}; + swap(tmp); return *this; } rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(const rabbit::Object& _obj) { - rabbit::ObjectType tOldType; - rabbit::ObjectValue unOldVal; - tOldType=_type; - unOldVal=_unVal; - _unVal = _obj._unVal; - _type = _obj._type; - __addRef(_type,_unVal); - __release(tOldType,unOldVal); + rabbit::ObjectPtr tmp{_obj}; + swap(tmp); return *this; } void rabbit::ObjectPtr::Null() { - rabbit::ObjectType tOldType = _type; - rabbit::ObjectValue unOldVal = _unVal; - _type = rabbit::OT_NULL; - _unVal.raw = 0; - __release(tOldType ,unOldVal); + releaseRef(); } - uint64_t rabbit::translateIndex(const rabbit::ObjectPtr &idx) { switch(idx.getType()){ case rabbit::OT_NULL: diff --git a/rabbit/ObjectPtr.hpp b/rabbit/ObjectPtr.hpp index b9d48fe..339013f 100644 --- a/rabbit/ObjectPtr.hpp +++ b/rabbit/ObjectPtr.hpp @@ -52,6 +52,7 @@ namespace rabbit { ObjectPtr& operator=(const ObjectPtr& _obj); ObjectPtr& operator=(const Object& _obj); void Null(); + void swap(rabbit::ObjectPtr& _obj); private: ObjectPtr(const char * _obj){} //safety }; diff --git a/rabbit/sqapi.cpp b/rabbit/sqapi.cpp index 93d6252..ab7c40c 100644 --- a/rabbit/sqapi.cpp +++ b/rabbit/sqapi.cpp @@ -177,7 +177,7 @@ void rabbit::sq_addref(rabbit::VirtualMachine* v,rabbit::Object *po) if(po->isRefCounted() == false) { return; } - __addRef(po->_type,po->_unVal); + po->addRef(); } uint64_t rabbit::sq_getrefcount(rabbit::VirtualMachine* v,rabbit::Object *po) @@ -194,7 +194,7 @@ rabbit::Bool rabbit::sq_release(rabbit::VirtualMachine* v,rabbit::Object *po) return SQTrue; } bool ret = (po->_unVal.pRefCounted->refCountget() <= 1) ? SQTrue : SQFalse; - __release(po->_type,po->_unVal); + po->releaseRef(); return ret; //the ret val doesn't work(and cannot be fixed) }