diff dwt/browser/FilePickerFactory.d @ 286:44258e0b6687

More fixes for xpcom
author John Reimer<terminal.node@gmail.com>
date Tue, 05 Aug 2008 10:11:58 -0700
parents 93409d9838c5
children eec6ddb07873
line wrap: on
line diff
--- a/dwt/browser/FilePickerFactory.d	Thu Jul 31 21:56:03 2008 -0700
+++ b/dwt/browser/FilePickerFactory.d	Tue Aug 05 10:11:58 2008 -0700
@@ -7,121 +7,100 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
- * Port to the D programming language:
- *     John Reimer <terminal.node@gmail.com>
  *******************************************************************************/
-
 module dwt.browser.FilePickerFactory;
 
 import dwt.dwthelper.utils;
 
+import dwt.internal.C;
+import dwt.internal.mozilla.XPCOM;
+import dwt.internal.mozilla.XPCOMObject;
 import dwt.internal.mozilla.nsID;
-import dwt.internal.mozilla.
 import dwt.internal.mozilla.nsIFactory;
 import dwt.internal.mozilla.nsISupports;
 
-class FilePickerFactory : nsISupports, nsIFactory 
-{
-
-    /**************************************************************************
-
-    **************************************************************************/
-    
-    int _refCount = 0;
-    
-    /**************************************************************************
-
-    **************************************************************************/
+class FilePickerFactory {
+    XPCOMObject supports;
+    XPCOMObject factory;
+    int refCount = 0;
 
-    this () 
-    {
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+FilePickerFactory () {
+    createCOMInterfaces ();
+}
 
-    nsrefcnt AddRef () 
-    {
-        _refCount++;
-        return _refCount;
-    }
-
-    /**************************************************************************
-
-    **************************************************************************/
+int AddRef () {
+    refCount++;
+    return refCount;
+}
 
-    int QueryInterface ( ref nsID riid, void** ppvObject)
-    {
-        if (riid is null || ppvObject is null) 
-            return NS_ERROR_NO_INTERFACE;
-        
-        if (guid == nsISupports.IID)) 
-        {
-            *ppvObject = cast(void*)cast(nsISupports) this; 
-            AddRef ();
-            return NS_OK;
-        }
+void createCOMInterfaces () {
+    /* Create each of the interfaces that this object implements */
+    supports = new XPCOMObject (new int[] {2, 0, 0}) {
+        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
+        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
+        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
+    };
     
-        if (guid == nsIFactory.IID) 
-        {
-            *ppvObject = cast(void*)cast(nsIFactory) this;
-            AddRef ();
-            return NS_OK;
-        }
-    
-        *ppvObject = null;
-        return NS_ERROR_NO_INTERFACE;
-    }
+    factory = new XPCOMObject (new int[] {2, 0, 0, 3, 1}) {
+        public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);}
+        public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
+        public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
+        public int /*long*/ method3 (int /*long*/[] args) {return CreateInstance (args[0], args[1], args[2]);}
+        public int /*long*/ method4 (int /*long*/[] args) {return LockFactory ((int)/*64*/args[0]);}
+    };
+}
 
-    /**************************************************************************
+void disposeCOMInterfaces () {
+    if (supports !is null) {
+        supports.dispose ();
+        supports = null;
+    }   
+    if (factory !is null) {
+        factory.dispose ();
+        factory = null; 
+    }
+}
 
-    **************************************************************************/
+int /*long*/ getAddress () {
+    return factory.getAddress ();
+}
 
-    nsrefcnt Release () 
-    {
-        _refCount--;
-        if (_refCount is 0) 
-            return 0;
-        return _refCount;
+int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
+    if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
+    nsID guid = new nsID ();
+    XPCOM.memmove (guid, riid, nsID.sizeof);
+    
+    if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
+        XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
+        AddRef ();
+        return XPCOM.NS_OK;
+    }
+    if (guid.Equals (nsIFactory.NS_IFACTORY_IID)) {
+        XPCOM.memmove (ppvObject, new int /*long*/[] {factory.getAddress ()}, C.PTR_SIZEOF);
+        AddRef ();
+        return XPCOM.NS_OK;
     }
     
-    /**************************************************************************
-
-    **************************************************************************/
-
-    /* nsIFactory */
+    XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF);
+    return XPCOM.NS_ERROR_NO_INTERFACE;
+}
 
-    nsresult CreateInstance (nsISupports aOuter, nsIID* iid, void** result) 
-    {
-        if (result is null)
-            return NS_ERROR_INVALID_ARG;
-
-        FilePicker picker = new FilePicker ();
-        picker.AddRef ();
+int Release () {
+    refCount--;
+    if (refCount is 0) disposeCOMInterfaces ();
+    return refCount;
+}
+    
+/* nsIFactory */
 
-        nsresult rv = picker.QueryInterface( iid, result );
-        
-        // TODO: If the query fails, the error should be checked and the 
-        //       newly created object deleted. In C++, this is done
-        //       like so:
-        //
-        //      if (NS_FAILED(rv)) {
-        //          *result = nsnull;
-        //          delete picker;
-        //      }
-        //
-        // Deletion of the object really isn't critical here for D, but
-        // it may be considered "good habit" to do anyway for XPCOM objects.
-        // For now, just return the rv value.  NS_FAILED needs to be
-        // ported first. -JJR
+int CreateInstance (int /*long*/ aOuter, int /*long*/ iid, int /*long*/ result) {
+    FilePicker picker = new FilePicker ();
+    picker.AddRef ();
+    XPCOM.memmove (result, new int /*long*/[] {picker.getAddress ()}, C.PTR_SIZEOF);
+    return XPCOM.NS_OK;
+}
 
-        return rv;
-    }
-
-    nsresult LockFactory (int lock) 
-    {
-        return NS_OK;
-    }
-
-}
\ No newline at end of file
+int LockFactory (int lock) {
+    return XPCOM.NS_OK;
+}
+}