1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *      John Reimer <terminal.node@gmail.com>
12  *******************************************************************************/
13 module org.eclipse.swt.browser.SimpleEnumerator;
14 
15 //import java.lang.all;
16 
17 //import org.eclipse.swt.internal.C;
18 import XPCOM = org.eclipse.swt.internal.mozilla.XPCOM;
19 
20 import org.eclipse.swt.internal.mozilla.Common;
21 import org.eclipse.swt.internal.mozilla.nsID;
22 import org.eclipse.swt.internal.mozilla.nsISimpleEnumerator;
23 import org.eclipse.swt.internal.mozilla.nsISupports;
24 
25 class SimpleEnumerator : nsISimpleEnumerator{
26     int refCount = 0;
27     nsISupports[] values;
28     int index = 0;
29 
30 this (nsISupports[] values) {
31     this.values = values;
32     for (int i = 0; i < values.length; i++) {
33         values[i].AddRef ();
34     }
35 }
36 
37 extern(System)
38 nsrefcnt AddRef () {
39     refCount++;
40     return refCount;
41 }
42 
43 extern(System)
44 nsresult QueryInterface (in nsID* riid, void** ppvObject) {
45     if (riid is null || ppvObject is null) return XPCOM.NS_ERROR_NO_INTERFACE;
46     //nsID guid = new nsID ();
47     //XPCOM.memmove (guid, riid, nsID.sizeof);
48 
49     if (*riid == nsISupports.IID) {
50         *ppvObject = cast(void*)cast(nsISupports)this;
51         AddRef ();
52         return XPCOM.NS_OK;
53     }
54     if (*riid == nsISimpleEnumerator.IID) {
55         *ppvObject = cast(void*)cast(nsISimpleEnumerator)this;
56         AddRef ();
57         return XPCOM.NS_OK;
58     }
59 
60     *ppvObject = null;
61     return XPCOM.NS_ERROR_NO_INTERFACE;
62 }
63 
64 extern(System)
65 nsresult Release () {
66     refCount--;
67     //if (refCount is 0) disposeCOMInterfaces ();
68     return refCount;
69 }
70 
71 extern(System)
72 nsresult HasMoreElements (PRBool* _retval) {
73     bool more = values !is null && index < values.length;
74     *_retval = more ? 1 : 0; /*PRBool */
75     return XPCOM.NS_OK;
76 }
77 
78 extern(System)
79 nsresult GetNext (nsISupports* _retval) {
80     if (values is null || index is values.length) return XPCOM.NS_ERROR_UNEXPECTED;
81     nsISupports value = values[index++];
82     value.AddRef ();
83     *_retval = value;
84     return XPCOM.NS_OK;
85 }       
86 }
87