Forráskód Böngészése

Refactoring to remove duplicated code.

mingodad 8 éve
szülő
commit
616849fc7b
1 módosított fájl, 49 hozzáadás és 54 törlés
  1. 49 54
      SquiLu/squirrel/sqobject.h

+ 49 - 54
SquiLu/squirrel/sqobject.h

@@ -113,8 +113,8 @@ struct SQObjectPtr;
 		{	\
 			unval.pRefCounted->Release();	\
 		}
-
-#define __Release(type,unval) if(ISREFCOUNTED(type)) __ReleaseRefCounted(unval)
+
+#define __Release(type,unval) if(ISREFCOUNTED(type)) __ReleaseRefCounted(unval)
 
 #define __ObjRelease(obj) { \
 	if((obj)) {	\
@@ -177,8 +177,8 @@ struct SQObjectPtr;
 	} \
 	inline SQObjectPtr& operator=(_class *x) \
 	{  \
-		SQObjectValue old_unVal; \
-		bool isRefCounted = ISREFCOUNTED(_type);\
+		SQObjectValue old_unVal; \
+		bool isRefCounted = ISREFCOUNTED(_type);\
 		if(isRefCounted) old_unVal = _unVal;\
 		_type = type; \
 		SQ_REFOBJECT_INIT() \
@@ -186,11 +186,11 @@ struct SQObjectPtr;
 		_unVal.pRefCounted->_uiRef++; \
 		if(isRefCounted) __ReleaseRefCounted(old_unVal); \
 		return *this; \
-	} \
-	inline _class *to##sym()\
-	{ \
-        assert(_type == type); \
-        return _unVal.p##sym;\
+	} \
+	inline _class *to##sym()\
+	{ \
+        assert(_type == type); \
+        return _unVal.p##sym;\
 	}
 
 #define _SCALAR_TYPE_DECL(type,_class,prefix,sym) \
@@ -201,19 +201,19 @@ struct SQObjectPtr;
 		_unVal.prefix##sym = x; \
 	} \
 	inline SQObjectPtr& operator=(_class x) \
-	{  \
+	{  \
 		if(_type != type){\
 			__Release(_type,_unVal); \
 			_type = type; \
-			SQ_OBJECT_RAWINIT() \
+			SQ_OBJECT_RAWINIT() \
 		}\
 		_unVal.prefix##sym = x; \
 		return *this; \
 	} \
-	inline _class to##sym()\
-	{ \
-        assert(_type == type); \
-        return _unVal.prefix##sym;\
+	inline _class to##sym()\
+	{ \
+        assert(_type == type); \
+        return _unVal.prefix##sym;\
 	}
 struct SQObjectPtr : public SQObject
 {
@@ -223,18 +223,16 @@ struct SQObjectPtr : public SQObject
 		_type=OT_NULL;
 		_unVal.pUserPointer=NULL;
 	}
+	#define _constructWith(o){_type = o._type; _unVal = o._unVal; __AddRef(_type,_unVal);}
 	SQObjectPtr(const SQObjectPtr &o)
 	{
-		_type = o._type;
-		_unVal = o._unVal;
-		__AddRef(_type,_unVal);
+	    _constructWith(o);
 	}
 	SQObjectPtr(const SQObject &o)
 	{
-		_type = o._type;
-		_unVal = o._unVal;
-		__AddRef(_type,_unVal);
+	    _constructWith(o);
 	}
+	#undef _constructWith
 	_REF_TYPE_DECL(OT_TABLE,SQTable,Table)
 	_REF_TYPE_DECL(OT_CLASS,SQClass,Class)
 	_REF_TYPE_DECL(OT_INSTANCE,SQInstance,Instance)
@@ -260,11 +258,11 @@ struct SQObjectPtr : public SQObject
 		_unVal.nInteger = bBool?1:0;
 	}
 	inline SQObjectPtr& operator=(bool b)
-	{
-		if(_type != OT_BOOL){
+	{
+		if(_type != OT_BOOL){
 			__Release(_type,_unVal);
 			SQ_OBJECT_RAWINIT()
-			_type = OT_BOOL;
+			_type = OT_BOOL;
 		}
 		_unVal.nInteger = b?1:0;
 		return *this;
@@ -275,46 +273,43 @@ struct SQObjectPtr : public SQObject
 		__Release(_type,_unVal);
 	}
 
-	inline SQObjectPtr& operator=(const SQObjectPtr& obj)
-	{
-		//saving temporarily the old value for cases
-		//where we are assigning a inner value to the old value
-		//local tbl = {a=2, b=4}; tbl = tbl.a;
-		SQObjectValue old_unVal;
-		bool isRefCounted = ISREFCOUNTED(_type);
+	inline SQObjectPtr& _assignThis(const SQObject& obj)
+	{
+		//saving temporarily the old value for cases
+		//where we are assigning a inner value to the old value
+		//local tbl = {a=2, b=4}; tbl = tbl.a;
+		SQObjectValue old_unVal;
+		bool isRefCounted = ISREFCOUNTED(_type);
 		if(isRefCounted) old_unVal = _unVal;
 		_unVal = obj._unVal;
 		_type = obj._type;
 		__AddRef(_type,_unVal);
-		if(isRefCounted) __ReleaseRefCounted(old_unVal);
+		if(isRefCounted) __ReleaseRefCounted(old_unVal);
 		return *this;
 	}
+	inline SQObjectPtr& operator=(const SQObjectPtr& obj)
+	{
+	    return _assignThis(obj);
+	}
 	inline SQObjectPtr& operator=(const SQObject& obj)
-	{
-		SQObjectValue old_unVal;
-		bool isRefCounted = ISREFCOUNTED(_type);
-		if(isRefCounted) old_unVal = _unVal;
-		_unVal = obj._unVal;
-		_type = obj._type;
-		__AddRef(_type,_unVal);
-		if(isRefCounted) __ReleaseRefCounted(old_unVal);
-		return *this;
+	{
+	    return _assignThis(obj);
 	}
-
+
 	inline void Null()
-	{
-		if(_type != OT_NULL){
-			__Release(_type,_unVal);
+	{
+		if(_type != OT_NULL){
+			__Release(_type,_unVal);
 			_type = OT_NULL;
 			_unVal.raw = (SQRawObjectVal)NULL;
 		}
 	}
 	inline bool isNull()
-	{
-		return _type == OT_NULL;
+	{
+		return _type == OT_NULL;
 	}
-	SQObjectPtr operator[](SQInteger nidx);
-	SQObjectPtr operator[](const SQChar *key);
+	SQObjectPtr operator[](SQInteger nidx);
+	SQObjectPtr operator[](const SQChar *key);
 	private:
 		SQObjectPtr(const SQChar *){} //safety
 };
@@ -371,11 +366,11 @@ typedef sqvector<SQInteger> SQIntVec;
 const SQChar *GetTypeName(const SQObjectPtr &obj1);
 const SQChar *IdType2Name(SQObjectType type);
 
-const SQChar *SQGetOpName(int op_code);
-const SQChar *SQGetArithOpName(int it);
-const SQChar *SQGetNewObjTypeName(int it);
-const SQChar *SQGetArrayAppendTypeName(int it);
-const SQChar *SQGetCmpOpName(int it);
+const SQChar *SQGetOpName(int op_code);
+const SQChar *SQGetArithOpName(int it);
+const SQChar *SQGetNewObjTypeName(int it);
+const SQChar *SQGetArrayAppendTypeName(int it);
+const SQChar *SQGetCmpOpName(int it);
 const SQChar *SQGetBitwiseOpName(int it);
 
 #endif //_SQOBJECT_H_