fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / squirrel / squirrel / sqobject.h
index 9f5823d..ee655b0 100644 (file)
@@ -26,7 +26,10 @@ enum SQMetaMethod{
        MT_CLONED=12,
        MT_NEWSLOT=13,
        MT_DELSLOT=14,
-       MT_LAST = 15,
+       MT_TOSTRING=15,
+       MT_NEWMEMBER=16,
+       MT_INHERITED=17,
+       MT_LAST = 18
 };
 
 #define MM_ADD         _SC("_add")
@@ -44,21 +47,36 @@ enum SQMetaMethod{
 #define MM_CLONED      _SC("_cloned")
 #define MM_NEWSLOT     _SC("_newslot")
 #define MM_DELSLOT     _SC("_delslot")
+#define MM_TOSTRING    _SC("_tostring")
+#define MM_NEWMEMBER _SC("_newmember")
+#define MM_INHERITED _SC("_inherited")
 
 #define MINPOWER2 4
 
 struct SQRefCounted
 {
-       unsigned int _uiRef;
+       SQRefCounted() { _uiRef = 0; _weakref = NULL; }
+       virtual ~SQRefCounted();
+       SQWeakRef *GetWeakRef(SQObjectType type);
+       SQUnsignedInteger _uiRef;
+       struct SQWeakRef *_weakref;
        virtual void Release()=0;
 };
 
+struct SQWeakRef : SQRefCounted
+{
+       void Release();
+       SQObject _obj;
+};
+
+#define _realval(o) (type((o)) != OT_WEAKREF?(SQObject)o:_weakref(o)->_obj)
+
 struct SQObjectPtr;
 
 #define __AddRef(type,unval) if(ISREFCOUNTED(type))    \
                { \
                        unval.pRefCounted->_uiRef++; \
-               }  
+               }
 
 #define __Release(type,unval) if(ISREFCOUNTED(type) && ((--unval.pRefCounted->_uiRef)<=0))     \
                {       \
@@ -97,6 +115,8 @@ struct SQObjectPtr;
 #define _class(obj) ((obj)._unVal.pClass)
 #define _instance(obj) ((obj)._unVal.pInstance)
 #define _delegable(obj) ((SQDelegable *)(obj)._unVal.pDelegable)
+#define _weakref(obj) ((obj)._unVal.pWeakRef)
+#define _refcounted(obj) ((obj)._unVal.pRefCounted)
 #define _rawval(obj) ((obj)._unVal.pRefCounted)
 
 #define _stringval(obj) (obj)._unVal.pString->_val
@@ -195,6 +215,13 @@ struct SQObjectPtr : public SQObject
                assert(_unVal.pThread);
                __AddRef(_type,_unVal);
        }
+       SQObjectPtr(SQWeakRef *pWeakRef)
+       {
+               _type=OT_WEAKREF;
+               _unVal.pWeakRef=pWeakRef;
+               assert(_unVal.pWeakRef);
+               __AddRef(_type,_unVal);
+       }
        SQObjectPtr(SQFunctionProto *pFunctionProto)
        {
                _type=OT_FUNCPROTO;
@@ -204,16 +231,19 @@ struct SQObjectPtr : public SQObject
        }
        SQObjectPtr(SQInteger nInteger)
        {
+               _unVal.pUserPointer=NULL;
                _type=OT_INTEGER;
                _unVal.nInteger=nInteger;
        }
        SQObjectPtr(SQFloat fFloat)
        {
+               _unVal.pUserPointer=NULL;
                _type=OT_FLOAT;
                _unVal.fFloat=fFloat;
        }
        SQObjectPtr(bool bBool)
        {
+               _unVal.pUserPointer=NULL;
                _type = OT_BOOL;
                _unVal.nInteger = bBool?1:0;
        }
@@ -233,7 +263,7 @@ struct SQObjectPtr : public SQObject
                _unVal.pUserPointer=NULL;
        }
        inline SQObjectPtr& operator=(const SQObjectPtr& obj)
-       { 
+       {
                SQObjectType tOldType;
                SQObjectValue unOldVal;
                tOldType=_type;
@@ -245,7 +275,7 @@ struct SQObjectPtr : public SQObject
                return *this;
        }
        inline SQObjectPtr& operator=(const SQObject& obj)
-       { 
+       {
                SQObjectType tOldType;
                SQObjectValue unOldVal;
                tOldType=_type;
@@ -288,12 +318,14 @@ struct SQCollectable : public SQRefCounted {
 #endif
 
 struct SQDelegable : public CHAINABLE_OBJ {
-       virtual bool GetMetaMethod(SQMetaMethod mm,SQObjectPtr &res);
+       bool SetDelegate(SQTable *m);
+       virtual bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
        SQTable *_delegate;
 };
 
-unsigned int TranslateIndex(const SQObjectPtr &idx);
+SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx);
 typedef sqvector<SQObjectPtr> SQObjectPtrVec;
-typedef sqvector<int> SQIntVec;
+typedef sqvector<SQInteger> SQIntVec;
+
 
 #endif //_SQOBJECT_H_