changeset 261:8f7bb7fc3123 lifetime

Preparing for a better world
author maxter
date Mon, 21 Sep 2009 13:11:04 +0000
parents b5773ccab07d
children 90131f64c9c9
files qt/d1/qt/QtdObject.d
diffstat 1 files changed, 104 insertions(+), 73 deletions(-) [+]
line wrap: on
line diff
--- a/qt/d1/qt/QtdObject.d	Fri Sep 18 18:52:03 2009 +0000
+++ b/qt/d1/qt/QtdObject.d	Mon Sep 21 13:11:04 2009 +0000
@@ -22,14 +22,15 @@
 {
     none,
     // The native object will not be deleted when the wrapper is deleted
-    skipNativeDelete          = 0b0001,
-    skipDDelete               = 0b0010,
-    hasDId                    = 0b0100,
-    stackAllocated            = 0b1000
-    /+
-    // The will be deleted when is goes out of scope. Implies skipNativeDelete
-    scoped                    = 0x08
-    +/
+    skipNativeDelete          = 0b0_0001,
+    // The wrapper will not be deleted when the native object is deleted
+    skipDDelete               = 0b0_0010,
+    // D object reference is stored in the shell
+    hasDId                    = 0b0_0100,
+    // The wrapper is allocated on thread-local stack and destroyed at the end of the scope
+    stackAllocated            = 0b0_1000
+    // It is a QObject
+    isQObject                 = 0b1_0000
 }
 
 class MetaObject
@@ -39,7 +40,7 @@
     private
     {
         MetaObject _base;
-        ClassInfo _classInfo;         
+        ClassInfo _classInfo;
     }
     
     //COMPILER BUG: not accessible from QMetaObject
@@ -108,16 +109,14 @@
         super(base);
     }
     
-    void construct(T : QtdObject, Concrete = T)()
+    void construct(T : QtdObject)()
     {
         super.construct!(T);
         _createWrapper = &T.__createWrapper;
     }
 }
 
-/++
-    Meta-object for polymorphic Qt classes.
-+/
+
 final class QtdMetaObject : QtdMetaObjectBase
 {
     alias typeof(this) This;
@@ -152,13 +151,39 @@
     }
 }
 
+class IdMappings
+{
+    private void* _data;
+    
+    this()
+    {
+    }
+
+    void add(void* nativeId, void* dId)
+    {
+    }
+    
+    void remove(void* dId)
+    {
+    }
+    
+    void* opIndex[void* nativeId]
+    {
+    }
+    
+    ~this()
+    {
+        free(_data);
+    }
+}
+
 abstract class QtdObjectBase
-{
+{    
     alias typeof(this) This;
     
     void* __nativeId;
     QtdObjectFlags __flags;
-    
+        
     new (size_t size, QtdObjectFlags flags = QtdObjectFlags.none)
     {
         return flags & QtdObjectFlags.stackAllocated ? __stackAlloc.alloc(size) :
@@ -177,6 +202,7 @@
     {
         __nativeId = nativeId;
         __flags = flags;
+                
         debug(QtdVerbose) __print("D wrapper constructed");
     }
     
@@ -201,10 +227,11 @@
         
         if (!(__flags & QtdObjectFlags.skipNativeDelete))
         {
+            // Avoid deleting the wrapper twice
             __flags |= QtdObjectFlags.skipDDelete;
             debug(QtdVerbose) __print("About to call native delete");
             __deleteNative;
-        }     
+        }
     }
 }
 
@@ -214,81 +241,73 @@
     private
     {
         typeof(this) __next, __prev;
+        ubyte __nativeRef_;
         static typeof(this) __root;
     }
-    
-    /++
-        Use this method instead of 'is' operator to check if two D objects
-        wrap the same native object.
-    +/
-    bool isSame(QtdObject other)
-    {
-        return __nativeId == other.__nativeId;
-    }
        
     mixin SignalHandlerOps;
 
     this(void* nativeId, QtdObjectFlags flags)
     {
-        super (nativeId, flags);       
-        if (!(flags & QtdObjectFlags.skipNativeDelete))
-            __pin;
+        super (nativeId, flags);
+        
+        if (!(flags & QtdObjectFlags.isQObject) && !(flags & QtdObjectFlags.hasDId))
+            __addIdMapping;
     }
     
-    final void __pin()
-    {
-        debug(QtdVerbose) __print("Pinning");
-        
-        __next = __root;
-        __root = this;
-        if (__next)
-            __next.__prev = this;        
-    }
+    void __addIdMapping() {}
+    void __removeIdMapping() {}
     
-    final void __unpin()
+    final void __nativeRef()
     {
-        debug(QtdVerbose) __print("Unpinning");
-                
-        if (__prev)
-            __prev.__next = __next;
-        else
-            __root = __next;
+        assert (__nativeRef_ < 255);
         
-        if (__next)      
-            __next.__prev = __prev;
+        if (!__nativeRef_)
+        {
+            __next = __root;
+            __root = this;
+            if (__next)
+                __next.__prev = this;        
+        }
+        __nativeRef_++;
+        
+        debug(QtdVerbose) __print("Native ref incremented");
     }
     
-    void __nativeOwnership(bool value)
-    {       
-        if (value)
+    final void __nativeDeref()
+    {
+        assert (__nativeRef > 0);
+        __nativeRef_--;
+               
+        if (!__nativeRef_)
         {
-            assert (!(__flags & QtdObjectFlags.skipNativeDelete));            
-            __flags |= QtdObjectFlags.skipNativeDelete;
-            __unpin;
+            if (__prev)
+                __prev.__next = __next;
+            else
+                __root = __next;
+            
+            if (__next)      
+                __next.__prev = __prev;
         }
-        else
+        
+        debug(QtdVerbose) __print("Native ref decremented");
+    }
+    
+    ~this()
+    {
+        if (!(__flags & QtdObjectFlags.isQObject) && !(__flags & QtdObjectFlags.hasDId))
+            __removeMapping;
+        
+        if (__nativeRef_)
         {
-            assert (__flags & QtdObjectFlags.skipNativeDelete);            
-            __flags = __flags &= ~QtdObjectFlags.skipNativeDelete;
-            __pin;
+            if (__nativeRef_ > 1)
+            {
+                debug(QtdVerbose) __print("Native ref is greater then 1 when deleting the object");
+                __nativeRef_ = 1;
+            }
+            __nativeDeref;
         }
     }
-      
-    ~this()
-    {
-        debug(QtdVerbose) __print("In QtdObject destructor");
-        
-        if (__prev || __root is this)
-            __unpin;
-    }    
-}
-
-/++
-+/
-void dispose(QtdObjectBase obj)
-{
-    obj.__flags &= ~QtdObjectFlags.skipNativeDelete;
-    delete obj;
 }
 
 // Called from shell destructors
@@ -303,4 +322,16 @@
         obj.__flags |= QtdObjectFlags.skipNativeDelete;
         delete obj;
     }
-}
\ No newline at end of file
+}
+
+extern(C) void qtd_native_ref(void* dId)
+{
+    (cast(QtdObject)dId).__nativeRef;
+}
+
+extern(C) void qtd_native_deref(void* dId)
+{
+    (cast(QtdObject)dId).__nativeDeref;
+}
+
+