comparison dwt/browser/SimpleEnumerator.d @ 125:5583f8eeee6c

Synced mozilla with dwt-linux
author Jacob Carlborg <doob@me.com>
date Fri, 16 Jan 2009 12:49:08 +0100
parents d8635bb48c7c
children
comparison
equal deleted inserted replaced
124:540fa4e9974a 125:5583f8eeee6c
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2003, 2007 IBM Corporation and others. 2 * Copyright (c) 2003, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * John Reimer <terminal.node@gmail.com>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.browser.SimpleEnumerator; 13 module dwt.browser.SimpleEnumerator;
12 14
13 import dwt.dwthelper.utils; 15 //import dwt.dwthelper.utils;
14 16
15 import dwt.internal.C; 17 //import dwt.internal.C;
16 import dwt.internal.mozilla.XPCOM; 18 import XPCOM = dwt.internal.mozilla.XPCOM;
17 import dwt.internal.mozilla.XPCOMObject; 19
20 import dwt.internal.mozilla.Common;
18 import dwt.internal.mozilla.nsID; 21 import dwt.internal.mozilla.nsID;
19 import dwt.internal.mozilla.nsISimpleEnumerator; 22 import dwt.internal.mozilla.nsISimpleEnumerator;
20 import dwt.internal.mozilla.nsISupports; 23 import dwt.internal.mozilla.nsISupports;
21 24
22 class SimpleEnumerator { 25 class SimpleEnumerator : nsISimpleEnumerator{
23 XPCOMObject supports;
24 XPCOMObject simpleEnumerator;
25 int refCount = 0; 26 int refCount = 0;
26 nsISupports[] values; 27 nsISupports[] values;
27 int index = 0; 28 int index = 0;
28 29
29 this (nsISupports[] values) { 30 this (nsISupports[] values) {
30 this.values = values; 31 this.values = values;
31 for (int i = 0; i < values.length; i++) { 32 for (int i = 0; i < values.length; i++) {
32 values[i].AddRef (); 33 values[i].AddRef ();
33 } 34 }
34 createCOMInterfaces ();
35 } 35 }
36 36
37 int AddRef () { 37 extern(System)
38 nsrefcnt AddRef () {
38 refCount++; 39 refCount++;
39 return refCount; 40 return refCount;
40 } 41 }
41 42
42 void createCOMInterfaces () { 43 extern(System)
43 /* Create each of the interfaces that this object implements */ 44 nsresult QueryInterface (nsID* riid, void** ppvObject) {
44 supports = new XPCOMObject (new int[] {2, 0, 0}) { 45 if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
45 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} 46 //nsID guid = new nsID ();
46 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();} 47 //XPCOM.memmove (guid, riid, nsID.sizeof);
47 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
48 };
49 48
50 simpleEnumerator = new XPCOMObject (new int[] {2, 0, 0, 1, 1}) { 49 if (*riid == nsISupports.IID) {
51 public int /*long*/ method0 (int /*long*/[] args) {return QueryInterface (args[0], args[1]);} 50 *ppvObject = cast(void*)cast(nsISupports)this;
52 public int /*long*/ method1 (int /*long*/[] args) {return AddRef ();}
53 public int /*long*/ method2 (int /*long*/[] args) {return Release ();}
54 public int /*long*/ method3 (int /*long*/[] args) {return HasMoreElements (args[0]);}
55 public int /*long*/ method4 (int /*long*/[] args) {return GetNext (args[0]);}
56 };
57 }
58
59 void disposeCOMInterfaces () {
60 if (supports !is null) {
61 supports.dispose ();
62 supports = null;
63 }
64 if (simpleEnumerator !is null) {
65 simpleEnumerator.dispose ();
66 simpleEnumerator = null;
67 }
68 if (values !is null) {
69 for (int i = 0; i < values.length; i++) {
70 values[i].Release ();
71 }
72 values = null;
73 }
74 }
75
76 int /*long*/ getAddress () {
77 return simpleEnumerator.getAddress ();
78 }
79
80 int QueryInterface (int /*long*/ riid, int /*long*/ ppvObject) {
81 if (riid is 0 || ppvObject is 0) return XPCOM.NS_ERROR_NO_INTERFACE;
82 nsID guid = new nsID ();
83 XPCOM.memmove (guid, riid, nsID.sizeof);
84
85 if (guid.Equals (nsISupports.NS_ISUPPORTS_IID)) {
86 XPCOM.memmove (ppvObject, new int /*long*/[] {supports.getAddress ()}, C.PTR_SIZEOF);
87 AddRef (); 51 AddRef ();
88 return XPCOM.NS_OK; 52 return XPCOM.NS_OK;
89 } 53 }
90 if (guid.Equals (nsISimpleEnumerator.NS_ISIMPLEENUMERATOR_IID)) { 54 if (*riid == nsISimpleEnumerator.IID) {
91 XPCOM.memmove (ppvObject, new int /*long*/[] {simpleEnumerator.getAddress ()}, C.PTR_SIZEOF); 55 *ppvObject = cast(void*)cast(nsISimpleEnumerator)this;
92 AddRef (); 56 AddRef ();
93 return XPCOM.NS_OK; 57 return XPCOM.NS_OK;
94 } 58 }
95 59
96 XPCOM.memmove (ppvObject, new int /*long*/[] {0}, C.PTR_SIZEOF); 60 *ppvObject = null;
97 return XPCOM.NS_ERROR_NO_INTERFACE; 61 return XPCOM.NS_ERROR_NO_INTERFACE;
98 } 62 }
99 63
100 int Release () { 64 extern(System)
65 nsresult Release () {
101 refCount--; 66 refCount--;
102 if (refCount is 0) disposeCOMInterfaces (); 67 //if (refCount is 0) disposeCOMInterfaces ();
103 return refCount; 68 return refCount;
104 } 69 }
105 70
106 int HasMoreElements (int /*long*/ _retval) { 71 extern(System)
72 nsresult HasMoreElements (PRBool* _retval) {
107 bool more = values !is null && index < values.length; 73 bool more = values !is null && index < values.length;
108 XPCOM.memmove (_retval, new int[] {more ? 1 : 0}, 4); /*PRBool */ 74 *_retval = more ? 1 : 0; /*PRBool */
109 return XPCOM.NS_OK; 75 return XPCOM.NS_OK;
110 } 76 }
111 77
112 int GetNext (int /*long*/ _retval) { 78 extern(System)
79 nsresult GetNext (nsISupports* _retval) {
113 if (values is null || index is values.length) return XPCOM.NS_ERROR_UNEXPECTED; 80 if (values is null || index is values.length) return XPCOM.NS_ERROR_UNEXPECTED;
114 nsISupports value = values[index++]; 81 nsISupports value = values[index++];
115 value.AddRef (); 82 value.AddRef ();
116 XPCOM.memmove (_retval, new int /*long*/[] {value.getAddress ()}, C.PTR_SIZEOF); 83 *_retval = value;
117 return XPCOM.NS_OK; 84 return XPCOM.NS_OK;
118 } 85 }
119 } 86 }
120 87